Project

General

Profile

Download (6.61 KB) Statistics
| Branch: | Tag: | Revision:
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.Column;
17
import javax.persistence.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.ManyToOne;
20
import javax.validation.Valid;
21
import javax.xml.bind.annotation.XmlAccessType;
22
import javax.xml.bind.annotation.XmlAccessorType;
23
import javax.xml.bind.annotation.XmlElement;
24
import javax.xml.bind.annotation.XmlIDREF;
25
import javax.xml.bind.annotation.XmlRootElement;
26
import javax.xml.bind.annotation.XmlSchemaType;
27
import javax.xml.bind.annotation.XmlType;
28

    
29
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
30
import org.hibernate.annotations.Cascade;
31
import org.hibernate.annotations.CascadeType;
32
import org.hibernate.envers.Audited;
33
import org.hibernate.search.annotations.Field;
34
import org.hibernate.search.annotations.Indexed;
35
import org.hibernate.search.annotations.IndexedEmbedded;
36
import org.springframework.beans.factory.annotation.Configurable;
37

    
38
import eu.etaxonomy.cdm.model.agent.Person;
39
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
40
import eu.etaxonomy.cdm.strategy.cache.occurrence.FieldUnitDefaultCacheStrategy;
41

    
42
/**
43
 *
44
 * In situ observation of a taxon in the field. If a specimen exists,
45
 * in most cases a parallel field unit object should be instantiated and the specimen then
46
 * is "derived" from the field unit via derivation type "accessioning" or any other.
47
 *
48
 * @author m.doering
49
 * @since 08-Nov-2007 13:06:40
50
 */
51
@XmlAccessorType(XmlAccessType.FIELD)
52
@XmlType(name = "FieldUnit", propOrder = {
53
    "fieldNumber",
54
    "primaryCollector",
55
    "fieldNotes",
56
    "gatheringEvent"
57
})
58
@XmlRootElement(name = "FieldUnit")
59
@Entity
60
@Indexed(index = "eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase")
61
@Audited
62
@Configurable
63
public class FieldUnit extends SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> implements Cloneable{
64

    
65
    private static final long serialVersionUID = -7586670941559035171L;
66
	private static final Logger logger = LogManager.getLogger(FieldUnit.class);
67

    
68
	@XmlElement(name = "FieldNumber")
69
	@Field
70
    //TODO Val #3379
71
//	@NullOrNotEmpty
72
	@Column(length=255)
73
	private String fieldNumber;
74

    
75
	@XmlElement(name = "PrimaryCollector")
76
	@XmlIDREF
77
	@XmlSchemaType(name = "IDREF")
78
	@ManyToOne(fetch = FetchType.LAZY)
79
    @Cascade( { CascadeType.SAVE_UPDATE,CascadeType.MERGE })
80
    @IndexedEmbedded(depth = 2)
81
    @Valid
82
	private Person primaryCollector;
83

    
84
	@XmlElement(name = "FieldNotes")
85
	@Field
86
    //TODO Val #3379
87
//	@NullOrNotEmpty
88
	@Column(length=255)
89
	private String fieldNotes;
90

    
91
	@XmlElement(name = "GatheringEvent")
92
	@XmlIDREF
93
	@XmlSchemaType(name = "IDREF")
94
	@ManyToOne(fetch = FetchType.LAZY)
95
    @Cascade( { CascadeType.SAVE_UPDATE,CascadeType.MERGE })
96
    @IndexedEmbedded(depth = 2)
97
    @Valid
98
	private GatheringEvent gatheringEvent;
99

    
100
// *************** FACTORY METHOD *************************/
101

    
102
	/**
103
	 * Factory method.
104
	 */
105
	public static FieldUnit NewInstance(){
106
		return new FieldUnit();
107
	}
108

    
109
//****************************** CONSTRUCTOR **************************************/
110

    
111
	/**
112
	 * Constructor
113
	 */
114
	protected FieldUnit(){
115
		super(SpecimenOrObservationType.FieldUnit);
116
        initDefaultCacheStrategy();
117
	}
118

    
119
//****************************** CACHE STRATEGY **************************************/
120

    
121
    /**
122
     * Sets the default cache strategy
123
     */
124
    @Override
125
    protected void initDefaultCacheStrategy() {
126
        this.cacheStrategy = FieldUnitDefaultCacheStrategy.NewInstance();
127
    }
128

    
129
// ************************ GETTER / SETTER *******************************************
130

    
131
	@Override
132
    public String getTitleCache() {
133
	    //most changes in fieldUnits take place in gathering events, therefore we compute titleCache each time from scratch
134
        if (!this.protectedTitleCache){
135
            this.titleCache = null;
136
        }
137
        return super.getTitleCache();
138
    }
139

    
140
    public GatheringEvent getGatheringEvent() {
141
    	return gatheringEvent;
142
	}
143

    
144
	public void setGatheringEvent(GatheringEvent gatheringEvent) {
145
		this.gatheringEvent = gatheringEvent;
146
		addGatheringEventPropertyChangeListener();
147
	}
148

    
149

    
150
	private void addGatheringEventPropertyChangeListener() {
151
		if (gatheringEvent != null){
152
			gatheringEvent.addPropertyChangeListener(getNewGatheringEventPropChangeListener());
153
		}
154
	}
155

    
156
	/**
157
	 * The collectors field number. If the collector is a team the field number
158
	 * is taken from the field book of the primary collector.
159
	 * @see #primaryCollector
160
	 * @return
161
	 */
162
	public String getFieldNumber() {
163
		return fieldNumber;
164
	}
165

    
166
	public void setFieldNumber(String fieldNumber) {
167
		this.fieldNumber = isBlank(fieldNumber)? null : fieldNumber;
168
	}
169

    
170

    
171
	/**
172
	 * The primary collector is the person who the field books belongs to.
173
	 * So the field number is also taken from him (his field book).
174
	 * @see #fieldNumber
175
	 * @param primaryCollector
176
	 */
177
	public void setPrimaryCollector(Person primaryCollector) {
178
		this.primaryCollector = primaryCollector;
179
	}
180

    
181
	public Person getPrimaryCollector() {
182
		return primaryCollector;
183
	}
184

    
185
	public String getFieldNotes() {
186
		return fieldNotes;
187
	}
188

    
189
	public void setFieldNotes(String fieldNotes) {
190
		this.fieldNotes = isBlank(fieldNotes)? null : fieldNotes;
191
	}
192

    
193
	// *********** Listener *****************************/
194

    
195
	private PropertyChangeListener getNewGatheringEventPropChangeListener() {
196
		PropertyChangeListener listener = new PropertyChangeListener(){
197

    
198
			@Override
199
            public void propertyChange(PropertyChangeEvent event) {
200
				firePropertyChange(event);
201
			}
202

    
203
		};
204
		return listener;
205
	}
206

    
207
	//*********** CLONE **********************************/
208

    
209
	/**
210
	 * Clones <i>this</i> field unit. This is a shortcut that enables to
211
	 * create a new instance that differs only slightly from <i>this</i> field unit
212
	 * by modifying only some of the attributes.<BR>
213
	 * This method overrides the clone method from {@link SpecimenOrObservationBase SpecimenOrObservationBase}.
214
	 *
215
	 * @see SpecimenOrObservationBase#clone()
216
	 * @see eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity#clone()
217
	 * @see java.lang.Object#clone()
218
	 */
219
	@Override
220
	public FieldUnit clone(){
221
		try{
222
			FieldUnit result = (FieldUnit)super.clone();
223
			//no changes to: fieldNotes, fieldNumber
224
			return result;
225
		} catch (CloneNotSupportedException e) {
226
			logger.warn("Object does not implement cloneable");
227
			e.printStackTrace();
228
			return null;
229
		}
230

    
231
	}
232

    
233
}
(6-6/15)