(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / VersionableEntity.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.model.common;
11
12 import eu.etaxonomy.cdm.model.agent.Person;
13 import org.apache.log4j.Logger;
14 import org.hibernate.annotations.Cascade;
15 import org.hibernate.annotations.CascadeType;
16
17 import java.util.*;
18 import javax.persistence.*;
19
20 /**
21 * The class keeps track of versions via a full linked list to different version objects, or a simple updated/updatedBy property in the same object.
22 *
23 * Full versioning allows concrete subclasses to keep track of previous or later versions of an object.
24 * A different version is another (persistent) java object, but with the same UUID.
25 * The version history is established as a linked list of the version objects in time.
26 * If versioning via the linked list is used, updated/updatedBy is the same as created/createdBy (better NULL?).
27 *
28 * Versioning can be turned off and in this case this class provides updated/updatedBy to keep track of the latest change event.
29 *
30 * @author m.doering
31 * @created 08-Nov-2007 13:07:01
32 *
33 * @param <T>
34 */
35 @MappedSuperclass
36 public abstract class VersionableEntity<T extends VersionableEntity> extends CdmBase {
37 private static final Logger logger = Logger.getLogger(VersionableEntity.class);
38
39 //time of last update for this object
40 private Calendar updated;
41 private Person updatedBy;
42 private T nextVersion;
43 private T previousVersion;
44
45
46 /**
47 * Returns the succeeding version of this object with the same UUID
48 * @return next, i.e. succeeding version of this object
49 */
50 //@OneToOne(mappedBy="previousVersion")
51 @Transient
52 public T getNextVersion(){
53 return this.nextVersion;
54 }
55 public void setNextVersion(T nextVersion){
56 this.nextVersion = nextVersion;
57 }
58
59 //@OneToOne
60 @Transient
61 public T getPreviousVersion(){
62 return this.previousVersion;
63 }
64 public void setPreviousVersion(T previousVersion){
65 this.previousVersion = previousVersion;
66 }
67
68
69 @ManyToOne(fetch=FetchType.LAZY)
70 @Cascade({CascadeType.SAVE_UPDATE})
71 public Person getUpdatedBy(){
72 return this.updatedBy;
73 }
74
75 /**
76 *
77 * @param updatedBy updatedBy
78 */
79 public void setUpdatedBy(Person updatedBy){
80 this.updatedBy = updatedBy;
81 }
82
83 /**
84 *
85 * @return
86 */
87 @Temporal(TemporalType.TIMESTAMP)
88 @Version
89 @Basic(fetch = FetchType.LAZY)
90 public Calendar getUpdated(){
91 return this.updated;
92 }
93
94 /**
95 *
96 * @param updated updated
97 */
98 public void setUpdated(Calendar updated){
99 this.updated = updated;
100 }
101
102 /**
103 * based on created
104 */
105 @Transient
106 public Calendar getValidFrom(){
107 return null;
108 }
109
110 /**
111 * based on updated
112 */
113 @Transient
114 public Calendar getValidTo(){
115 return null;
116 }
117
118 /**
119 * Is true if UUID and created timestamp are the same for the passed Object and this one.
120 * @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
121 * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
122 * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
123 * for more information about equals and hashcode.
124 */
125 @Override
126 public boolean equals(Object obj) {
127 if (obj == this){
128 return true;
129 }
130 if (obj == null){
131 return false;
132 }
133 if (!CdmBase.class.isAssignableFrom(obj.getClass())){
134 return false;
135 }
136 ICdmBase cdmObj = (ICdmBase)obj;
137 boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
138 boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
139 if (! uuidEqual || !createdEqual){
140 return false;
141 }
142 return true;
143 }
144
145
146 /** Overrides {@link eu.etaxonomy.cdm.model.common.CdmBase#hashCode()}
147 * See {@link http://www.hibernate.org/109.html}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html}
148 * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html}
149 * for more information about equals and hashcode.
150 */
151 @Override
152 public int hashCode() {
153 int hashCode = 7;
154 hashCode = 29 * hashCode + this.getUuid().hashCode();
155 //hashCode = 29 * hashCode + this.getCreated().hashCode();
156 return hashCode;
157 }
158
159 }