001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.connector.store.jpa.model.common;
025    
026    import javax.persistence.Column;
027    import javax.persistence.Entity;
028    import javax.persistence.GeneratedValue;
029    import javax.persistence.GenerationType;
030    import javax.persistence.Id;
031    import javax.persistence.Lob;
032    import javax.persistence.NamedQueries;
033    import javax.persistence.NamedQuery;
034    import javax.persistence.Table;
035    import org.hibernate.annotations.Index;
036    import org.jboss.dna.graph.property.DateTime;
037    import org.jboss.dna.graph.property.basic.JodaDateTime;
038    
039    /**
040     * Represents a record of the changes that have been made to the repository. The actual change events are serialized and stored in
041     * a binary (and compressed) format.
042     * 
043     * @author Randall Hauch
044     */
045    @Entity
046    @Table( name = "DNA_CHANGELOG" )
047    @org.hibernate.annotations.Table( appliesTo = "DNA_CHANGELOG", indexes = @Index( name = "NS_CHANGE_TS_INX", columnNames = {"UTC_TIMESTAMP"} ) )
048    @NamedQueries( {
049        @NamedQuery( name = "ChangeLogEntity.findBetween", query = "select entry from ChangeLogEntity as entry where entry.timestampInUtc >= :start and entry.timestampInUtc <= :end" ),
050        @NamedQuery( name = "ChangeLogEntity.deleteBefore", query = "delete ChangeLogEntity entry where entry.timestampInUtc < :timestamp" )} )
051    public class ChangeLogEntity {
052    
053        @Id
054        @GeneratedValue( strategy = GenerationType.AUTO )
055        @Column( name = "ID", updatable = false )
056        private Long id;
057    
058        @Column( name = "USERNAME", updatable = false, nullable = false, length = 64, unique = false )
059        private String username;
060    
061        @Column( name = "UTC_TIMESTAMP", updatable = false, nullable = false, unique = false )
062        private long timestampInUtc;
063    
064        @Column( name = "CHANGE_COUNT", updatable = false, nullable = false, unique = false )
065        private int numChanges;
066    
067        @Lob
068        @Column( name = "CHANGES", updatable = false, nullable = false, unique = false )
069        private byte[] changes;
070    
071        public ChangeLogEntity( String username,
072                                DateTime timestamp,
073                                int numChanges,
074                                byte[] changes ) {
075            this.username = username;
076            this.timestampInUtc = timestamp.toUtcTimeZone().getMilliseconds();
077            this.numChanges = numChanges;
078            this.changes = changes;
079        }
080    
081        /**
082         * @return id
083         */
084        public Long getId() {
085            return id;
086        }
087    
088        /**
089         * @return username
090         */
091        public String getUsername() {
092            return username;
093        }
094    
095        /**
096         * @return timestampInUtc
097         */
098        public long getTimestampInUtc() {
099            return timestampInUtc;
100        }
101    
102        /**
103         * @return changes
104         */
105        public byte[] getChanges() {
106            return changes;
107        }
108    
109        /**
110         * @return numChanges
111         */
112        public int getNumChanges() {
113            return numChanges;
114        }
115    
116        /**
117         * {@inheritDoc}
118         * 
119         * @see java.lang.Object#hashCode()
120         */
121        @Override
122        public int hashCode() {
123            return id.hashCode();
124        }
125    
126        /**
127         * {@inheritDoc}
128         * 
129         * @see java.lang.Object#equals(java.lang.Object)
130         */
131        @Override
132        public boolean equals( Object obj ) {
133            if (obj == this) return true;
134            if (obj instanceof ChangeLogEntity) {
135                ChangeLogEntity that = (ChangeLogEntity)obj;
136                return id.equals(that.id);
137            }
138            return false;
139        }
140    
141        /**
142         * {@inheritDoc}
143         * 
144         * @see java.lang.Object#toString()
145         */
146        @Override
147        public String toString() {
148            return "" + numChanges + " changes by " + username + " at " + new JodaDateTime(timestampInUtc);
149        }
150    }