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