merge hibernate4 migration branch into trunk
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / Marker.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.Column;
13 import javax.persistence.Entity;
14 import javax.persistence.FetchType;
15 import javax.persistence.JoinColumn;
16 import javax.persistence.ManyToOne;
17 import javax.persistence.Transient;
18 import javax.validation.constraints.NotNull;
19 import javax.xml.bind.annotation.XmlAccessType;
20 import javax.xml.bind.annotation.XmlAccessorType;
21 import javax.xml.bind.annotation.XmlElement;
22 import javax.xml.bind.annotation.XmlIDREF;
23 import javax.xml.bind.annotation.XmlSchemaType;
24 import javax.xml.bind.annotation.XmlType;
25
26 import org.apache.log4j.Logger;
27 import org.hibernate.annotations.Any;
28 import org.hibernate.annotations.Cascade;
29 import org.hibernate.annotations.CascadeType;
30 import org.hibernate.envers.Audited;
31 import org.hibernate.envers.NotAudited;
32
33 /**
34 * This class aims to make available some "flags" for identifiable entities in a
35 * flexible way. Application developers (and even users) can define their own
36 * "flags" as a MarkerType.
37 * @author m.doering
38 * @version 1.0
39 * @created 08-Nov-2007 13:06:33
40 */
41
42 @XmlAccessorType(XmlAccessType.FIELD)
43 @XmlType(name = "Marker")
44 @Entity
45 @Audited
46 public class Marker extends VersionableEntity implements Cloneable{
47 private static final long serialVersionUID = -7474489691871404610L;
48 @SuppressWarnings("unused")
49 private static final Logger logger = Logger.getLogger(Marker.class);
50
51 @XmlElement(name = "Flag")
52 private boolean flag;
53
54 @XmlElement(name = "MarkerType")
55 @XmlIDREF
56 @XmlSchemaType(name = "IDREF")
57 @ManyToOne(fetch = FetchType.LAZY)
58 @NotNull
59 private MarkerType markerType;
60
61 @XmlElement(name = "MarkedObject")
62 @XmlIDREF
63 @XmlSchemaType(name = "IDREF")
64 @Any(metaDef = "CdmBase",
65 fetch=FetchType.LAZY,
66 metaColumn = @Column(name="markedObj_type"),
67 optional = false)
68 @JoinColumn(name = "markedObj_id")
69 @NotAudited
70 private AnnotatableEntity markedObj;
71
72 /**
73 * Factory method
74 * @return
75 */
76 public static Marker NewInstance(){
77 return new Marker();
78 }
79
80 /**
81 * Factory method
82 * @param markerType The type of the marker
83 * @param flag The value of the marker
84 * @return
85 */
86 public static Marker NewInstance(MarkerType markerType, boolean flag){
87 return new Marker(markerType, flag);
88 }
89
90 public static Marker NewInstance(AnnotatableEntity annotatedObject, boolean flag, MarkerType markerType){
91 Marker marker = new Marker();
92 marker.setFlag(flag);
93 marker.setMarkerType(markerType);
94 annotatedObject.addMarker(marker);
95 return marker;
96 }
97
98 /**
99 * Default Constructor
100 */
101 private Marker() {
102 }
103
104 /**
105 * Constructor
106 * @param flag
107 */
108 protected Marker(MarkerType markerType, boolean flag){
109 this.markerType = markerType;
110 this.flag = flag;
111 }
112
113 /**
114 * @return
115 */
116 public AnnotatableEntity getMarkedObj() {
117 return markedObj;
118 }
119 public void setMarkedObj(AnnotatableEntity newMarkedObject) {
120 this.markedObj = newMarkedObject;
121 }
122
123 /**
124 * @return
125 */
126 public MarkerType getMarkerType(){
127 return this.markerType;
128 }
129 public void setMarkerType(MarkerType type){
130 this.markerType = type;
131 }
132
133 /**
134 * The flag value.
135 * @return
136 */
137 public boolean getFlag(){
138 return this.flag;
139 }
140 public void setFlag(boolean flag){
141 this.flag = flag;
142 }
143
144 /**
145 * @see getFlag()
146 * @return
147 */
148 @Transient
149 public boolean getValue(){
150 return getFlag();
151 }
152
153
154 //****************** CLONE ************************************************/
155
156
157 /* (non-Javadoc)
158 * @see java.lang.Object#clone()
159 */
160 @Override
161 public Object clone() throws CloneNotSupportedException{
162 Marker result = (Marker)super.clone();
163 result.setFlag(this.flag);
164 result.setMarkerType(this.markerType);
165 return result;
166 }
167
168 /**
169 * Clones this marker and sets the clones marked object to 'markedObject'
170 * @see java.lang.Object#clone()
171 */
172 public Marker clone(AnnotatableEntity markedObject) throws CloneNotSupportedException{
173 Marker result = (Marker)clone();
174 result.setMarkedObj(markedObject);
175 return result;
176 }
177
178 }