merge cate-development2 branch with trunk
[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 javax.persistence.Basic;
13 import javax.persistence.FetchType;
14 import javax.persistence.ManyToOne;
15 import javax.persistence.MappedSuperclass;
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlIDREF;
20 import javax.xml.bind.annotation.XmlSchemaType;
21 import javax.xml.bind.annotation.XmlType;
22 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
23
24 import org.apache.log4j.Logger;
25 import org.hibernate.annotations.Type;
26 import org.hibernate.search.annotations.Field;
27 import org.hibernate.search.annotations.FieldBridge;
28 import org.joda.time.DateTime;
29
30 import eu.etaxonomy.cdm.hibernate.DateTimeBridge;
31 import eu.etaxonomy.cdm.jaxb.DateTimeAdapter;
32 import eu.etaxonomy.cdm.strategy.match.Match;
33 import eu.etaxonomy.cdm.strategy.match.MatchMode;
34
35 /**
36 * 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.
37 *
38 * Full versioning allows concrete subclasses to keep track of previous or later versions of an object.
39 * A different version is another (persistent) java object, but with the same UUID.
40 * The version history is established as a linked list of the version objects in time.
41 * If versioning via the linked list is used, updated/updatedBy is the same as created/createdBy (better NULL?).
42 *
43 * Versioning can be turned off and in this case this class provides updated/updatedBy to keep track of the latest change event.
44 *
45 * @author m.doering
46 * @created 08-Nov-2007 13:07:01
47 *
48 * @param <T>
49 */
50 @XmlAccessorType(XmlAccessType.FIELD)
51 @XmlType(name = "VersionableEntity", propOrder = {
52 "updated",
53 "updatedBy"
54 })
55 @XmlJavaTypeAdapter(value=DateTimeAdapter.class,type=DateTime.class)
56 @MappedSuperclass
57 public abstract class VersionableEntity extends CdmBase {
58 private static final long serialVersionUID = 1409299200302758513L;
59 @SuppressWarnings("unused")
60 private static final Logger logger = Logger.getLogger(VersionableEntity.class);
61
62 @XmlElement(name ="Updated", type = String.class)
63 @XmlJavaTypeAdapter(DateTimeAdapter.class)
64 //@XmlElement(name ="Updated")
65 //@XmlElement(name ="Updated")
66 @Type(type="dateTimeUserType")
67 @Basic(fetch = FetchType.LAZY)
68 @Match(MatchMode.IGNORE)
69 @Field(index = org.hibernate.search.annotations.Index.UN_TOKENIZED)
70 @FieldBridge(impl = DateTimeBridge.class)
71 private DateTime updated;
72
73 @XmlElement(name = "UpdatedBy")
74 @XmlIDREF
75 @XmlSchemaType(name = "IDREF")
76 @ManyToOne(fetch=FetchType.LAZY)
77 @Match(MatchMode.IGNORE)
78 private User updatedBy;
79
80 public User getUpdatedBy(){
81 return this.updatedBy;
82 }
83
84 /**
85 *
86 * @param updatedBy updatedBy
87 */
88 public void setUpdatedBy(User updatedBy){
89 this.updatedBy = updatedBy;
90 }
91
92 /**
93 *
94 * @return
95 */
96 public DateTime getUpdated(){
97 return this.updated;
98 }
99
100 /**
101 *
102 * @param updated updated
103 */
104 public void setUpdated(DateTime updated){
105 this.updated = updated;
106 }
107
108 /**
109 * Is true if UUID and created timestamp are the same for the passed Object and this one.
110 * @see eu.etaxonomy.cdm.model.common.CdmBase#equals(java.lang.Object)
111 * See {@link http://www.hibernate.org/109.html hibernate109}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html geocities}
112 * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html ibm}
113 * for more information about equals and hashcode.
114 */
115 @Override
116 public boolean equals(Object obj) {
117 if (obj == this){
118 return true;
119 }
120 if (obj == null){
121 return false;
122 }
123 if (!CdmBase.class.isAssignableFrom(obj.getClass())){
124 return false;
125 }
126 ICdmBase cdmObj = (ICdmBase)obj;
127 boolean uuidEqual = cdmObj.getUuid().equals(this.getUuid());
128 boolean createdEqual = cdmObj.getCreated().equals(this.getCreated());
129 if (! uuidEqual || !createdEqual){
130 return false;
131 }
132 return true;
133 }
134
135
136 /** Overrides {@link eu.etaxonomy.cdm.model.common.CdmBase#hashCode()}
137 * See {@link http://www.hibernate.org/109.html}, {@link http://www.geocities.com/technofundo/tech/java/equalhash.html}
138 * or {@link http://www.ibm.com/developerworks/java/library/j-jtp05273.html}
139 * for more information about equals and hashcode.
140 */
141 @Override
142 public int hashCode() {
143 int hashCode = 7;
144 hashCode = 29 * hashCode + this.getUuid().hashCode();
145 //hashCode = 29 * hashCode + this.getCreated().hashCode();
146 return hashCode;
147 }
148
149 //********************** CLONE *****************************************/
150
151 /**
152 * Clones this versionable entity.
153 * Set fields for nextVersion, previousVersion, updated, updatedBy and createdBy are set to <tt>null</tt>
154 * The id is set to 0.
155 * The uuid is created new.
156 * The createdWhen is set to the current date.
157 * @see java.lang.Object#clone()
158 */
159 @Override
160 public Object clone() throws CloneNotSupportedException{
161 VersionableEntity result = (VersionableEntity)super.clone();
162
163 result.setUpdated(null);
164 result.setUpdatedBy(null);
165
166 //no changes to: -
167 return result;
168 }
169 }