ref #7458 implementation of the note field for the TypeSpecimenEditor
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / model / registration / SpecimenTypeDesignationWorkingSetDTO.java
1 /**
2 * Copyright (C) 2017 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 package eu.etaxonomy.cdm.vaadin.model.registration;
10
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.joda.time.Partial;
17
18 import eu.etaxonomy.cdm.model.agent.AgentBase;
19 import eu.etaxonomy.cdm.model.common.Annotation;
20 import eu.etaxonomy.cdm.model.common.Language;
21 import eu.etaxonomy.cdm.model.common.LanguageString;
22 import eu.etaxonomy.cdm.model.common.VersionableEntity;
23 import eu.etaxonomy.cdm.model.location.NamedArea;
24 import eu.etaxonomy.cdm.model.location.Point;
25 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
26 import eu.etaxonomy.cdm.model.name.TaxonName;
27 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
28 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
29 import eu.etaxonomy.cdm.model.reference.Reference;
30
31 /**
32 * @author a.kohlbecker
33 * @since Jun 16, 2017
34 *
35 */
36 public class SpecimenTypeDesignationWorkingSetDTO<OWNER extends VersionableEntity> {
37
38 FieldUnit fieldUnit;
39
40 VersionableEntity baseEntity;
41
42 /**
43 * List of all SpecimenTypeDesignation that have been loaded into the
44 * DTO. By comparing this list with <code>specimenTypeDesignations</code>
45 * it is possible to find those that have been deleted.
46 */
47 List<SpecimenTypeDesignation> specimenTypeDesignationsLoaded = new ArrayList<>();
48
49 List<SpecimenTypeDesignationDTO> specimenTypeDesignationsDTOs = new ArrayList<>();
50
51 OWNER owner;
52
53 private Reference citation;
54
55 private TaxonName typifiedName;
56
57 /**
58 *
59 * @param owner
60 * @param baseEntity
61 * @param specimenTypeDesignations can be <code>null</code>
62 */
63 public SpecimenTypeDesignationWorkingSetDTO(OWNER owner, VersionableEntity baseEntity, List<SpecimenTypeDesignation> specimenTypeDesignations, Reference citation, TaxonName typifiedName) {
64 super();
65 this.owner = owner;
66 this.baseEntity = baseEntity;
67 if(citation == null){
68 throw new NullPointerException("citation must not be null");
69 }
70 if(typifiedName == null){
71 throw new NullPointerException("typifiedName must not be null");
72 }
73 this.citation = citation;
74 this.typifiedName = typifiedName;
75 if(baseEntity instanceof FieldUnit){
76 this.fieldUnit = (FieldUnit) baseEntity;
77 if(fieldUnit.getGatheringEvent() == null){
78 fieldUnit.setGatheringEvent(GatheringEvent.NewInstance());
79 }
80 }
81 if(specimenTypeDesignations != null){
82 specimenTypeDesignationsLoaded = specimenTypeDesignations;
83 specimenTypeDesignations.forEach(std -> specimenTypeDesignationsDTOs.add(new SpecimenTypeDesignationDTO(std)));
84 }
85 }
86
87 /**
88 * @param reg
89 * @param newfieldUnit
90 * @param citationEntityID
91 * @param typifiedNameEntityID
92 */
93 public SpecimenTypeDesignationWorkingSetDTO(OWNER reg, FieldUnit newfieldUnit, Reference citation, TaxonName typifiedName) {
94 this(reg, newfieldUnit, null, citation, typifiedName);
95 }
96
97 /**
98 * @return the fieldUnit
99 * <code>null</code> if the base baseEntity is a not a fieldUnit
100 */
101 public FieldUnit getFieldUnit() {
102 return fieldUnit;
103 }
104
105
106 /**
107 *
108 * @return the baseEntity
109 * <code>null</code> if the base baseEntity is a fieldUnit
110 */
111 public VersionableEntity getBaseEntity() {
112 return baseEntity;
113 }
114
115 /**
116 * @return the typeDesignation entities managed in this workingset
117 */
118 protected List<SpecimenTypeDesignation> getSpecimenTypeDesignations() {
119 List<SpecimenTypeDesignation> specimenTypeDesignations = new ArrayList(specimenTypeDesignationsDTOs.size());
120 for(SpecimenTypeDesignationDTO dto : specimenTypeDesignationsDTOs){
121 specimenTypeDesignations.add(dto.asSpecimenTypeDesignation());
122 }
123 return specimenTypeDesignations;
124 }
125
126 public List<SpecimenTypeDesignationDTO> getSpecimenTypeDesignationDTOs(){
127 return specimenTypeDesignationsDTOs;
128 }
129
130 /**
131 * The {@link VersionableEntity} which contains the DerivedUnit in this working set.
132 * This can be for example a {@link Registration} entity
133 *
134 * @return
135 */
136 public OWNER getOwner() {
137 return owner;
138 }
139
140 // ====== FieldUnit Wrapper methods ====== //
141
142
143 public String getFieldNumber() {
144 return fieldUnit.getFieldNumber();
145 }
146
147 public void setFieldNumber(String fieldNumber) {
148 this.fieldUnit.setFieldNumber(fieldNumber);
149 }
150
151 public String getFieldNotes() {
152 return fieldUnit.getFieldNotes();
153 }
154
155 // ====== GateringEvent Wrapper methods ====== //
156
157 public String getLocality(){
158 if(fieldUnit.getGatheringEvent().getLocality() != null){
159 return fieldUnit.getGatheringEvent().getLocality().getText();
160 }
161 return null;
162 }
163
164 public void setLocality(String locality){
165 fieldUnit.getGatheringEvent().setLocality(
166 LanguageString.NewInstance(locality, Language.DEFAULT())
167 );
168 }
169 public NamedArea getCountry() {
170 return fieldUnit.getGatheringEvent().getCountry();
171 }
172
173 public void setCountry(NamedArea country) {
174 fieldUnit.getGatheringEvent().setCountry(country);
175 }
176
177 public Point getExactLocation() {
178 return fieldUnit.getGatheringEvent().getExactLocation();
179 }
180
181 public void setExactLocation(Point exactLocation) {
182 fieldUnit.getGatheringEvent().setExactLocation(exactLocation);
183 }
184
185 public Integer getAbsoluteElevation() {
186 return fieldUnit.getGatheringEvent().getAbsoluteElevation();
187 }
188
189 public void setAbsoluteElevation(Integer absoluteElevation) {
190 fieldUnit.getGatheringEvent().setAbsoluteElevation(absoluteElevation);
191 }
192
193 public Integer getAbsoluteElevationMax() {
194 return fieldUnit.getGatheringEvent().getAbsoluteElevationMax();
195 }
196
197 public void setAbsoluteElevationMax(Integer absoluteElevationMax) {
198 fieldUnit.getGatheringEvent().setAbsoluteElevationMax(absoluteElevationMax);
199 }
200
201 public String getAbsoluteElevationText() {
202 return fieldUnit.getGatheringEvent().getAbsoluteElevationText();
203 }
204
205 public void setAbsoluteElevationText(String absoluteElevationText) {
206 fieldUnit.getGatheringEvent().setAbsoluteElevationText(absoluteElevationText);
207 }
208
209 public Double getDistanceToWaterSurface() {
210 return fieldUnit.getGatheringEvent().getDistanceToWaterSurface();
211 }
212
213 public void setDistanceToWaterSurface(Double distanceToWaterSurface) {
214 fieldUnit.getGatheringEvent().setDistanceToWaterSurface(distanceToWaterSurface);
215 }
216
217 public Double getDistanceToWaterSurfaceMax() {
218 return fieldUnit.getGatheringEvent().getDistanceToWaterSurfaceMax();
219 }
220
221 public void setDistanceToWaterSurfaceMax(Double distanceToWaterSurfaceMax) {
222 fieldUnit.getGatheringEvent().setDistanceToWaterSurfaceMax(distanceToWaterSurfaceMax);
223 }
224
225 public String getDistanceToWaterSurfaceText() {
226 return fieldUnit.getGatheringEvent().getDistanceToWaterSurfaceText();
227 }
228
229 public void setDistanceToWaterSurfaceText(String distanceToWaterSurfaceText) {
230 fieldUnit.getGatheringEvent().setDistanceToWaterSurfaceText(distanceToWaterSurfaceText);
231 }
232
233
234 public Double getDistanceToGround() {
235 return fieldUnit.getGatheringEvent().getDistanceToGround();
236 }
237
238 public void setDistanceToGround(Double distanceToGround) {
239 fieldUnit.getGatheringEvent().setDistanceToGround(distanceToGround);
240 }
241
242 public Double getDistanceToGroundMax() {
243 return fieldUnit.getGatheringEvent().getDistanceToGroundMax();
244 }
245
246 public void setDistanceToGroundMax(Double distanceToGroundMax) {
247 fieldUnit.getGatheringEvent().setDistanceToGroundMax(distanceToGroundMax);
248 }
249
250 public String getDistanceToGroundText() {
251 return fieldUnit.getGatheringEvent().getDistanceToGroundText();
252 }
253
254 public AgentBase getCollector(){
255 return fieldUnit.getGatheringEvent().getActor();
256 }
257
258 public void setCollector(AgentBase collector){
259 fieldUnit.getGatheringEvent().setActor(collector);
260 }
261
262 public void setDistanceToGroundText(String distanceToGroundText) {
263 fieldUnit.getGatheringEvent().setDistanceToWaterSurfaceText(distanceToGroundText);
264 }
265
266 /**
267 * WARNING: This method returns only one of the possibly multiple areas which can
268 * be hold by the GatheringEvent.
269 *
270 * @return
271 */
272 public NamedArea getCollectingArea() {
273 try {
274 return fieldUnit.getGatheringEvent().getCollectingAreas().iterator().next();
275 } catch (Exception e){
276 return null;
277 }
278 }
279
280 public void setCollectingArea(NamedArea collectingArea) throws Exception {
281 if(fieldUnit.getGatheringEvent().getCollectingAreas().size() > 1){
282 throw new Exception("The GatheringEvent has multiple collectingAreas, use addCollectingArea() instead");
283 }
284 fieldUnit.getGatheringEvent().getCollectingAreas().clear();
285 fieldUnit.getGatheringEvent().addCollectingArea(collectingArea);
286
287 }
288
289 public Set<NamedArea> getCollectingAreas() {
290 return fieldUnit.getGatheringEvent().getCollectingAreas();
291 }
292
293 public Partial getGatheringDate(){
294 return fieldUnit.getGatheringEvent().getGatheringDate();
295 }
296
297 public void setGatheringDate(Partial gatheringDate){
298 fieldUnit.getGatheringEvent().setGatheringDate(gatheringDate);
299 }
300
301 /**
302 * @return the citation
303 */
304 public Reference getCitation() {
305 return citation;
306 }
307
308 /**
309 * @param citation the citation to set
310 */
311 public void setCitation(Reference citation) {
312 this.citation = citation;
313 }
314
315 /**
316 * @return the typifiedName
317 */
318 public TaxonName getTypifiedName() {
319 return typifiedName;
320 }
321
322 /**
323 * @param typifiedName the typifiedName to set
324 */
325 public void setTypifiedName(TaxonName typifiedName) {
326 this.typifiedName = typifiedName;
327 }
328
329 /**
330 *
331 * @return the set of SpecimenTypeDesignation that haven been deleted from the <code>SpecimenTypeDesignationWorkingSetDTO</code>.
332 */
333 public Set<SpecimenTypeDesignation> deletedSpecimenTypeDesignations() {
334 Set<SpecimenTypeDesignation> deletedEntities = new HashSet<>(specimenTypeDesignationsLoaded);
335 deletedEntities.removeAll(getSpecimenTypeDesignations());
336 return deletedEntities;
337 }
338
339 public Set<Annotation> getAnnotations() {
340 if(fieldUnit != null){
341 return fieldUnit.getAnnotations();
342 } else {
343 return null;
344 }
345 }
346
347 public void setAnnotations(Set<Annotation> annotations) {
348
349 if(fieldUnit != null){
350 List<Annotation> currentAnnotations = new ArrayList<>(fieldUnit.getAnnotations());
351 List<Annotation> annotationsSeen = new ArrayList<>();
352 for(Annotation a : annotations){
353 if(a == null){
354 continue;
355 }
356 if(!currentAnnotations.contains(a)){
357 fieldUnit.addAnnotation(a);
358 }
359 annotationsSeen.add(a);
360 }
361 for(Annotation a : currentAnnotations){
362 if(!annotationsSeen.contains(a)){
363 fieldUnit.removeAnnotation(a);
364 }
365 }
366 }
367 }
368
369 }