2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.specimen
;
11 import java
.util
.HashMap
;
13 import java
.util
.UUID
;
15 import org
.apache
.commons
.lang3
.StringUtils
;
16 import org
.apache
.logging
.log4j
.LogManager
;
17 import org
.apache
.logging
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
21 import eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
.Abcd206ImportConfigurator
;
22 import eu
.etaxonomy
.cdm
.io
.specimen
.excel
.in
.SpecimenSynthesysExcelImportConfigurator
;
23 import eu
.etaxonomy
.cdm
.io
.taxonx2013
.TaxonXImportConfigurator
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
31 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
32 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
33 import eu
.etaxonomy
.cdm
.model
.location
.ReferenceSystem
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
35 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
36 import eu
.etaxonomy
.cdm
.strategy
.parser
.TimePeriodParser
;
42 public class UnitsGatheringEvent
{
44 private static final Logger logger
= LogManager
.getLogger();
46 private static final boolean DEBUG
= false;
47 private final GatheringEvent gatheringEvent
= GatheringEvent
.NewInstance();
51 * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
52 * @param app: the CDM Application Controller
57 * @param collectorNames
59 public UnitsGatheringEvent(ITermService termService
, String locality
, String languageIso
, Double longitude
,
60 Double latitude
, String collectorName
, String team
, SpecimenSynthesysExcelImportConfigurator config
){
61 this.setLocality(termService
, locality
, languageIso
);
62 this.setCoordinates(longitude
, latitude
);
63 if (!collectorName
.isEmpty()) {
64 this.setCollector(collectorName
, config
);
66 if (!team
.isEmpty()) {
67 this.setTeam(team
, config
);
71 public UnitsGatheringEvent(ITermService termService
, String locality
, String collectorName
, Double longitude
,
72 Double latitude
, TaxonXImportConfigurator config
,IAgentService agentService
){
73 if (!StringUtils
.isEmpty(locality
)) {
74 this.setLocality(termService
, locality
, null);
76 this.setCoordinates(longitude
, latitude
);
77 if (!StringUtils
.isEmpty(collectorName
)) {
78 this.setCollector(collectorName
, config
, agentService
);
80 // if (!team.isEmpty()) {
81 // this.setTeam(team, config);
87 * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
88 * @param app: the CDM Application Controller
93 * @param collectorNames
95 public UnitsGatheringEvent(ITermService termService
, String locality
, String languageIso
, Double longitude
,
96 Double latitude
, String errorRadius
, String elevationText
, String elevationMin
, String elevationMax
, String elevationUnit
,
97 String date
, String gatheringNotes
, String gatheringMethod
, ReferenceSystem referenceSystem
,
98 Abcd206ImportConfigurator config
) {
99 this.setLocality(termService
, locality
, languageIso
);
100 Integer errorRadiusInt
= null;
101 if (StringUtils
.isNotBlank(errorRadius
)){
102 errorRadiusInt
= Integer
.getInteger(errorRadius
);
105 this.setCoordinates(longitude
, latitude
, referenceSystem
, errorRadiusInt
);
107 this.setNotes(gatheringNotes
);
108 this.setElevation(elevationText
, elevationMin
, elevationMax
, elevationUnit
);
109 this.setGatheringMethod(gatheringMethod
);
118 * @param gatheringImages
120 public void setGatheringImages(HashMap
<String
, Map
<String
, String
>> gatheringImages
) {
124 public GatheringEvent
getGatheringEvent(){
125 return this.gatheringEvent
;
129 * Set the locality for the current GatheringEvent
133 public void setLocality(ITermService termService
, String locality
, String languageIso
){
135 LanguageString loc
= null;
136 if (languageIso
== null){
137 loc
= LanguageString
.NewInstance(locality
, Language
.DEFAULT());
139 loc
= LanguageString
.NewInstance(locality
, termService
.getLanguageByIso(languageIso
));
143 if (loc
== null){logger
.warn("PROBLEM LOCALITY");}
144 this.gatheringEvent
.setLocality(loc
);
148 * return the locality associated to the GatheringEvent
150 public LanguageString
getLocality(){
151 return this.gatheringEvent
.getLocality();
155 * Set the coordinates for the current GatheringEvent
159 public void setCoordinates(Double longitude
, Double latitude
){
160 setCoordinates(longitude
, latitude
, null, null);
163 public void setCoordinates(Double longitude
, Double latitude
, ReferenceSystem referenceSystem
, Integer errorRadius
){
164 //create coordinates point
165 if((longitude
== null) || (latitude
== null)){
168 Point coordinates
= Point
.NewInstance();
170 if (longitude
!= 0.0) {
171 coordinates
.setLongitude(longitude
);
173 if (latitude
!= 0.0) {
174 coordinates
.setLatitude(latitude
);
176 if (errorRadius
!= null && errorRadius
!= 0) {
177 coordinates
.setErrorRadius(errorRadius
);
179 coordinates
.setReferenceSystem(referenceSystem
);
180 this.gatheringEvent
.setExactLocation(coordinates
);
184 public void setNotes(String gatheringNotes
){
185 this.gatheringEvent
.addAnnotation(Annotation
.NewDefaultLanguageInstance(gatheringNotes
));
188 public void setDate(String date
){
189 this.gatheringEvent
.setTimeperiod(TimePeriodParser
.parseString(date
));
193 public void setElevation(String elevationText
, String elevationMin
, String elevationMax
, String elevationUnit
){
194 if(elevationText
!=null){
195 this.gatheringEvent
.setAbsoluteElevationText(elevationText
);
198 //TODO check for unit at string end
199 String pattern
= "\\D";// regex for non-digits
200 if(StringUtils
.isNotBlank(elevationMin
)){
201 Integer min
= Integer
.parseInt(elevationMin
.replaceAll(pattern
, ""));
202 this.gatheringEvent
.setAbsoluteElevation(min
);
204 if(StringUtils
.isNotBlank(elevationMax
)){
205 Integer max
= Integer
.parseInt(elevationMax
.replaceAll(pattern
, ""));
206 this.gatheringEvent
.setAbsoluteElevationMax(max
);
208 if(StringUtils
.isNotBlank(elevationUnit
)){
209 if(!elevationUnit
.equals("m")){
210 //TODO convert if necessary
216 public void setHeight(String heightText
, String heightMin
, String heightMax
, String heightUnit
){
217 if(heightText
!=null){
218 this.gatheringEvent
.setAbsoluteElevationText(heightText
);
221 //TODO check for unit at string end
222 String pattern
= "\\D";// regex for non-digits
223 if(StringUtils
.isNotBlank(heightMin
)){
224 Double min
= Double
.parseDouble(heightMin
.replaceAll(pattern
, ""));
225 this.gatheringEvent
.setDistanceToGround(min
);
227 if(StringUtils
.isNotBlank(heightMax
)){
228 Double max
= Double
.parseDouble(heightMax
.replaceAll(pattern
, ""));
229 this.gatheringEvent
.setDistanceToGroundMax(max
);
231 if(StringUtils
.isNotBlank(heightUnit
)){
232 if (!heightUnit
.equals("m")){
233 logger
.debug("The unit " + heightUnit
+ " of the distance to ground is not meter.");
239 public void setGatheringDepth(String depthText
, Double depthMin
, Double depthMax
, String depthUnit
){
241 this.gatheringEvent
.setDistanceToWaterSurfaceText(depthText
);
244 if (StringUtils
.isNotBlank(depthUnit
) && depthUnit
.equals("cm")){
245 if (depthMin
!= null) {
246 depthMin
= depthMin
/100;
248 if (depthMax
!= null) {
249 depthMax
= depthMax
/100;
253 this.gatheringEvent
.setDistanceToWaterSurface(depthMin
);
256 this.gatheringEvent
.setDistanceToWaterSurfaceMax(depthMax
);
258 if(StringUtils
.isNotBlank(depthUnit
)){
260 if (!depthUnit
.equals("m")){
261 logger
.debug("The unit " + depthUnit
+ " of the distance to ground is not meter.");
268 * Add a NamedArea to the GatheringEvent
269 * @param area: the NamedArea to add
272 public void addArea(DefinedTermBase area
){
273 if (area
.isInstanceOf(NamedArea
.class)) {
274 this.gatheringEvent
.addCollectingArea((NamedArea
) area
);
276 logger
.info("OUPPPPSS :"+area
.getClass());
283 * Create a new collector or collector's team
284 * @param: collectorNames: the list of names to add as collector/collectorTeam
285 * USED - create each time a new Collector
287 public void setCollector(String collectorName
, SpecimenSynthesysExcelImportConfigurator config
){
288 // System.out.println("collectors : "+collectorNames.toString());
290 collector
= Person
.NewInstance();
291 collector
.setTitleCache(collectorName
, true);
293 System
.out
.println("getcoll:"+config
.getPersons().get(collector
.getTitleCache()));
295 this.gatheringEvent
.setCollector(config
.getPersons().get(collector
.getTitleCache()));
300 * Create a new collector or collector's team
301 * @param: collectorNames: the list of names to add as collector/collectorTeam
302 * USED - create each time a new Collector
304 public void setCollector(String collectorName
, TaxonXImportConfigurator config
, IAgentService agentService
){
305 // System.out.println("collectors : "+collectorNames.toString());
307 collector
= Person
.NewInstance();
308 collector
.setTitleCache(collectorName
, true);
309 Person collector_db
= config
.getPersons().get(collector
.getTitleCache());
310 if (collector_db
== null) {
311 UUID uuid
= agentService
.saveOrUpdate(collector
);
312 collector_db
=(Person
) agentService
.find(uuid
);
314 this.gatheringEvent
.setCollector(collector_db
);
320 * Create a new collector or collector's team
321 * @param: collectorNames: the list of names to add as collector/collectorTeam
322 * USED - create each time a new Collector
324 public void setCollector(TeamOrPersonBase collector
, Abcd206ImportConfigurator config
){
325 // System.out.println("collectors : "+collectorNames.toString());
328 System
.out
.println("getcoll:"+config
.getPersons().get(collector
.getTitleCache()));
330 this.gatheringEvent
.setCollector(collector
);
336 public void setGatheringDate(TimePeriod tp
) {
337 this.gatheringEvent
.setTimeperiod(tp
);
343 public void setGatheringMethod(String gatheringMethod
) {
344 this.gatheringEvent
.setCollectingMethod(gatheringMethod
);
347 public String
getGatheringMethod(){
348 return this.gatheringEvent
.getCollectingMethod();
351 public void setTeam(String gatheringTeam
, SpecimenSynthesysExcelImportConfigurator config
) {
353 if ((gatheringTeam
!= null) && !gatheringTeam
.isEmpty()) {
354 if ((gatheringTeam
.indexOf("et al.") != -1) || (gatheringTeam
.indexOf("& al.") != -1) || (gatheringTeam
.indexOf(" al.") != -1)){
355 t
.setTitleCache(gatheringTeam
);
357 String
[] tmp1
= gatheringTeam
.split(" et ");
358 for (String elt
:tmp1
){
359 String tmp2
[] = elt
.split("&");
360 for (String elt2
:tmp2
) {
361 if (!elt2
.trim().isEmpty()) {
362 Person p
= Person
.NewInstance();
363 p
.setTitleCache(elt2
);
371 System
.out
.println("getteam:"+config
.getTeams().get(t
.getTitleCache()));
373 this.gatheringEvent
.setCollector(config
.getTeams().get(t
.getTitleCache()));
377 * @param gatheringTeam
379 public void setTeam(String gatheringTeam
, Abcd206ImportConfigurator config
) {
381 if ((gatheringTeam
!= null) && !gatheringTeam
.isEmpty()) {
382 if ((gatheringTeam
.indexOf("et al.") != -1) || (gatheringTeam
.indexOf("& al.") != -1) || (gatheringTeam
.indexOf(" al.") != -1)){
383 t
.setTitleCache(gatheringTeam
);
385 String
[] tmp1
= gatheringTeam
.split(" et ");
386 for (String elt
:tmp1
){
387 String tmp2
[] = elt
.split("&");
388 for (String elt2
:tmp2
) {
389 if (!elt2
.trim().isEmpty()) {
390 Person p
= Person
.NewInstance();
391 p
.setTitleCache(elt2
);
399 System
.out
.println("getteam:"+config
.getTeams().get(t
.getTitleCache()));
401 //this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));