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