minor refactoring for FieldObservation listeners
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / occurrence / FieldObservation.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.occurrence;
11
12
13 import java.beans.PropertyChangeEvent;
14 import java.beans.PropertyChangeListener;
15
16 import javax.persistence.Entity;
17 import javax.persistence.FetchType;
18 import javax.persistence.ManyToOne;
19 import javax.validation.Valid;
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlIDREF;
24 import javax.xml.bind.annotation.XmlRootElement;
25 import javax.xml.bind.annotation.XmlSchemaType;
26 import javax.xml.bind.annotation.XmlType;
27
28 import org.apache.log4j.Logger;
29 import org.hibernate.annotations.Cascade;
30 import org.hibernate.annotations.CascadeType;
31 import org.hibernate.envers.Audited;
32 import org.hibernate.search.annotations.Field;
33 import org.hibernate.search.annotations.Index;
34 import org.hibernate.search.annotations.Indexed;
35 import org.hibernate.search.annotations.IndexedEmbedded;
36 import org.hibernate.validator.constraints.Length;
37 import org.springframework.beans.factory.annotation.Configurable;
38
39 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
40 import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
41 import eu.etaxonomy.cdm.validation.annotation.NullOrNotEmpty;
42
43 /**
44 * In situ observation of a taxon in the field. If a specimen exists,
45 * in most cases a parallel field observation object should be instantiated and the specimen then is "derived" from the field unit
46 * @author m.doering
47 * @version 1.0
48 * @created 08-Nov-2007 13:06:40
49 */
50 @XmlAccessorType(XmlAccessType.FIELD)
51 @XmlType(name = "FieldObservation", propOrder = {
52 "fieldNumber",
53 "fieldNotes",
54 "gatheringEvent"
55 })
56 @XmlRootElement(name = "FieldObservation")
57 @Entity
58 @Indexed(index = "eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase")
59 @Audited
60 @Configurable
61 public class FieldObservation extends SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldObservation>> implements Cloneable{
62 private static final Logger logger = Logger.getLogger(FieldObservation.class);
63
64 @XmlElement(name = "FieldNumber")
65 @Field(index=Index.TOKENIZED)
66 @NullOrNotEmpty
67 @Length(max = 255)
68 private String fieldNumber;
69
70 @XmlElement(name = "FieldNotes")
71 @Field(index=Index.TOKENIZED)
72 @NullOrNotEmpty
73 @Length(max = 255)
74 private String fieldNotes;
75
76 @XmlElement(name = "GatheringEvent")
77 @XmlIDREF
78 @XmlSchemaType(name = "IDREF")
79 @ManyToOne(fetch = FetchType.LAZY)
80 @Cascade( { CascadeType.SAVE_UPDATE })
81 @IndexedEmbedded(depth = 2)
82 @Valid
83 private GatheringEvent gatheringEvent;
84
85 /**
86 * Factory method
87 * @return
88 */
89 public static FieldObservation NewInstance(){
90 return new FieldObservation();
91 }
92
93 /**
94 * Constructor
95 */
96 protected FieldObservation(){
97 super();
98 this.cacheStrategy = new IdentifiableEntityDefaultCacheStrategy<FieldObservation>();
99 }
100
101 public GatheringEvent getGatheringEvent() {
102 return gatheringEvent;
103 }
104
105 public void setGatheringEvent(GatheringEvent gatheringEvent) {
106 this.gatheringEvent = gatheringEvent;
107 addGatheringEventPropertyChangeListener();
108 }
109
110
111 private void addGatheringEventPropertyChangeListener() {
112 if (gatheringEvent != null){
113 gatheringEvent.addPropertyChangeListener(getNewGatheringEventPropChangeListener());
114 }
115 }
116
117 public String getFieldNumber() {
118 return fieldNumber;
119 }
120
121 public void setFieldNumber(String fieldNumber) {
122 this.fieldNumber = fieldNumber;
123 }
124
125 public String getFieldNotes() {
126 return fieldNotes;
127 }
128
129 public void setFieldNotes(String fieldNotes) {
130 this.fieldNotes = fieldNotes;
131 }
132
133 // *********** Listener *****************************/
134
135 private PropertyChangeListener getNewGatheringEventPropChangeListener() {
136 PropertyChangeListener listener = new PropertyChangeListener(){
137
138 public void propertyChange(PropertyChangeEvent event) {
139 firePropertyChange(event);
140 }
141
142 };
143 return listener;
144 }
145
146 //*********** CLONE **********************************/
147
148 /**
149 * Clones <i>this</i> field observation. This is a shortcut that enables to
150 * create a new instance that differs only slightly from <i>this</i> field observation
151 * by modifying only some of the attributes.<BR>
152 * This method overrides the clone method from {@link SpecimenOrObservationBase SpecimenOrObservationBase}.
153 *
154 * @see SpecimenOrObservationBase#clone()
155 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
156 * @see java.lang.Object#clone()
157 */
158 @Override
159 public FieldObservation clone(){
160 try{
161 FieldObservation result = (FieldObservation)super.clone();
162 //no changes to: fieldNotes, fieldNumber
163 return result;
164 } catch (CloneNotSupportedException e) {
165 logger.warn("Object does not implement cloneable");
166 e.printStackTrace();
167 return null;
168 }
169
170 }
171 }