View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.connector.store.jpa.model.common;
25  
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.GeneratedValue;
29  import javax.persistence.GenerationType;
30  import javax.persistence.Id;
31  import javax.persistence.Lob;
32  import javax.persistence.NamedQueries;
33  import javax.persistence.NamedQuery;
34  import javax.persistence.Table;
35  import org.hibernate.annotations.Index;
36  import org.modeshape.graph.property.DateTime;
37  import org.modeshape.graph.property.basic.JodaDateTime;
38  
39  /**
40   * Represents a record of the changes that have been made to the repository. The actual change events are serialized and stored in
41   * a binary (and compressed) format.
42   */
43  @Entity
44  @Table( name = "DNA_CHANGELOG" )
45  @org.hibernate.annotations.Table( appliesTo = "DNA_CHANGELOG", indexes = @Index( name = "NS_CHANGE_TS_INX", columnNames = {"UTC_TIMESTAMP"} ) )
46  @NamedQueries( {
47      @NamedQuery( name = "ChangeLogEntity.findBetween", query = "select entry from ChangeLogEntity as entry where entry.timestampInUtc >= :start and entry.timestampInUtc <= :end" ),
48      @NamedQuery( name = "ChangeLogEntity.deleteBefore", query = "delete ChangeLogEntity entry where entry.timestampInUtc < :timestamp" )} )
49  public class ChangeLogEntity {
50  
51      @Id
52      @GeneratedValue( strategy = GenerationType.AUTO )
53      @Column( name = "ID", updatable = false )
54      private Long id;
55  
56      @Column( name = "USERNAME", updatable = false, nullable = false, length = 64, unique = false )
57      private String username;
58  
59      @Column( name = "UTC_TIMESTAMP", updatable = false, nullable = false, unique = false )
60      private long timestampInUtc;
61  
62      @Column( name = "CHANGE_COUNT", updatable = false, nullable = false, unique = false )
63      private int numChanges;
64  
65      @Lob
66      @Column( name = "CHANGES", updatable = false, nullable = false, unique = false )
67      private byte[] changes;
68  
69      public ChangeLogEntity( String username,
70                              DateTime timestamp,
71                              int numChanges,
72                              byte[] changes ) {
73          this.username = username;
74          this.timestampInUtc = timestamp.toUtcTimeZone().getMilliseconds();
75          this.numChanges = numChanges;
76          this.changes = changes;
77      }
78  
79      /**
80       * @return id
81       */
82      public Long getId() {
83          return id;
84      }
85  
86      /**
87       * @return username
88       */
89      public String getUsername() {
90          return username;
91      }
92  
93      /**
94       * @return timestampInUtc
95       */
96      public long getTimestampInUtc() {
97          return timestampInUtc;
98      }
99  
100     /**
101      * @return changes
102      */
103     public byte[] getChanges() {
104         return changes;
105     }
106 
107     /**
108      * @return numChanges
109      */
110     public int getNumChanges() {
111         return numChanges;
112     }
113 
114     /**
115      * {@inheritDoc}
116      * 
117      * @see java.lang.Object#hashCode()
118      */
119     @Override
120     public int hashCode() {
121         return id.hashCode();
122     }
123 
124     /**
125      * {@inheritDoc}
126      * 
127      * @see java.lang.Object#equals(java.lang.Object)
128      */
129     @Override
130     public boolean equals( Object obj ) {
131         if (obj == this) return true;
132         if (obj instanceof ChangeLogEntity) {
133             ChangeLogEntity that = (ChangeLogEntity)obj;
134             return id.equals(that.id);
135         }
136         return false;
137     }
138 
139     /**
140      * {@inheritDoc}
141      * 
142      * @see java.lang.Object#toString()
143      */
144     @Override
145     public String toString() {
146         return "" + numChanges + " changes by " + username + " at " + new JodaDateTime(timestampInUtc);
147     }
148 }