Project

General

Profile

Download (6.98 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.commons.lang.StringUtils;
30
import org.apache.log4j.Logger;
31
import org.hibernate.annotations.Cascade;
32
import org.hibernate.annotations.CascadeType;
33
import org.hibernate.envers.Audited;
34
import org.hibernate.search.annotations.Field;
35
import org.hibernate.search.annotations.Indexed;
36
import org.hibernate.search.annotations.IndexedEmbedded;
37
import org.springframework.beans.factory.annotation.Configurable;
38

    
39
import eu.etaxonomy.cdm.model.agent.Person;
40
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
41
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
42

    
43
/**
44
 *
45
 * In situ observation of a taxon in the field. If a specimen exists,
46
 * in most cases a parallel field unit object should be instantiated and the specimen then
47
 * is "derived" from the field unit via derivation type "accessioning" or any other.
48
 *
49
 * @author m.doering
50
 * @created 08-Nov-2007 13:06:40
51
 */
52
@XmlAccessorType(XmlAccessType.FIELD)
53
@XmlType(name = "FieldUnit", propOrder = {
54
    "fieldNumber",
55
    "primaryCollector",
56
    "fieldNotes",
57
    "gatheringEvent"
58
})
59
@XmlRootElement(name = "FieldUnit")
60
@Entity
61
@Indexed(index = "eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase")
62
@Audited
63
@Configurable
64
public class FieldUnit extends SpecimenOrObservationBase<IIdentifiableEntityCacheStrategy<FieldUnit>> implements Cloneable{
65
	private static final long serialVersionUID = -7586670941559035171L;
66
	private static final Logger logger = Logger.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
	 * @return
105
	 */
106
	public static FieldUnit NewInstance(){
107
		return new FieldUnit();
108
	}
109

    
110
//****************************** CONSTRUCTOR **************************************/
111

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

    
120
//****************************** CACHE STRATEGY **************************************/
121

    
122
    private static final String facadeStrategyClassName =
123
            "eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeFieldUnitCacheStrategy";
124
    /**
125
     * Sets the default cache strategy
126
     */
127
    @Override
128
    protected void initDefaultCacheStrategy() {
129
        try {
130
            String facadeClassName = facadeStrategyClassName;
131
            Class<?> facadeClass = Class.forName(facadeClassName);
132
            try {
133
                this.cacheStrategy = (IIdentifiableEntityCacheStrategy)facadeClass.newInstance();
134
            } catch (InstantiationException e) {
135
                e.printStackTrace();
136
            } catch (IllegalAccessException e) {
137
                e.printStackTrace();
138
            }
139
        } catch (ClassNotFoundException e) {
140
            this.cacheStrategy = new IdentifiableEntityDefaultCacheStrategy<FieldUnit>();
141
        }
142
    }
143

    
144

    
145
// ************************ GETTER / SETTER *******************************************
146

    
147
	public GatheringEvent getGatheringEvent() {
148
    	return gatheringEvent;
149
	}
150

    
151
	public void setGatheringEvent(GatheringEvent gatheringEvent) {
152
		this.gatheringEvent = gatheringEvent;
153
		addGatheringEventPropertyChangeListener();
154
	}
155

    
156

    
157
	private void addGatheringEventPropertyChangeListener() {
158
		if (gatheringEvent != null){
159
			gatheringEvent.addPropertyChangeListener(getNewGatheringEventPropChangeListener());
160
		}
161
	}
162

    
163
	/**
164
	 * The collectors field number. If the collector is a team the field number
165
	 * is taken from the field book of the primary collector.
166
	 * @see #primaryCollector
167
	 * @return
168
	 */
169
	public String getFieldNumber() {
170
		return fieldNumber;
171
	}
172

    
173
	public void setFieldNumber(String fieldNumber) {
174
		this.fieldNumber = StringUtils.isBlank(fieldNumber)? null : fieldNumber;
175
	}
176

    
177

    
178
	/**
179
	 * The primary collector is the person who the field books belongs to.
180
	 * So the field number is also taken from him (his field book).
181
	 * @see #fieldNumber
182
	 * @param primaryCollector
183
	 */
184
	public void setPrimaryCollector(Person primaryCollector) {
185
		this.primaryCollector = primaryCollector;
186
	}
187

    
188
	public Person getPrimaryCollector() {
189
		return primaryCollector;
190
	}
191

    
192
	public String getFieldNotes() {
193
		return fieldNotes;
194
	}
195

    
196
	public void setFieldNotes(String fieldNotes) {
197
		this.fieldNotes = StringUtils.isBlank(fieldNotes)? null : fieldNotes;
198
	}
199

    
200
	// *********** Listener *****************************/
201

    
202
	private PropertyChangeListener getNewGatheringEventPropChangeListener() {
203
		PropertyChangeListener listener = new PropertyChangeListener(){
204

    
205
			@Override
206
            public void propertyChange(PropertyChangeEvent event) {
207
				firePropertyChange(event);
208
			}
209

    
210
		};
211
		return listener;
212
	}
213

    
214
	//*********** CLONE **********************************/
215

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

    
238
	}
239

    
240
}
(6-6/14)