Merge branch 'release/5.43.0'
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / UnitsGatheringEvent.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 package eu.etaxonomy.cdm.io.specimen;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.UUID;
14
15 import org.apache.commons.lang3.StringUtils;
16 import org.apache.logging.log4j.LogManager;
17 import org.apache.logging.log4j.Logger;
18
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;
37
38 /**
39 * @author p.kelbert
40 * @since 20.10.2008
41 */
42 public class UnitsGatheringEvent {
43
44 private static final Logger logger = LogManager.getLogger();
45
46 private static final boolean DEBUG = false;
47 private final GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
48
49 /*
50 * Constructor
51 * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
52 * @param app: the CDM Application Controller
53 * @param locality
54 * @param languageIso
55 * @param longitude
56 * @param latitude
57 * @param collectorNames
58 */
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);
65 }
66 if (!team.isEmpty()) {
67 this.setTeam(team, config);
68 }
69 }
70 //
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);
75 }
76 this.setCoordinates(longitude, latitude);
77 if (!StringUtils.isEmpty(collectorName)) {
78 this.setCollector(collectorName, config, agentService);
79 }
80 // if (!team.isEmpty()) {
81 // this.setTeam(team, config);
82 // }
83 }
84
85 /*
86 * Constructor
87 * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
88 * @param app: the CDM Application Controller
89 * @param locality
90 * @param languageIso
91 * @param longitude
92 * @param latitude
93 * @param collectorNames
94 */
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);
103 }
104
105 this.setCoordinates(longitude, latitude, referenceSystem, errorRadiusInt);
106 this.setDate(date);
107 this.setNotes(gatheringNotes);
108 this.setElevation(elevationText, elevationMin, elevationMax, elevationUnit);
109 this.setGatheringMethod(gatheringMethod);
110
111
112
113
114
115 }
116
117 /**
118 * @param gatheringImages
119 */
120 public void setGatheringImages(HashMap<String, Map<String, String>> gatheringImages) {
121
122
123 }
124 public GatheringEvent getGatheringEvent(){
125 return this.gatheringEvent;
126 }
127
128 /**
129 * Set the locality for the current GatheringEvent
130 * @param locality
131 * @param langageIso
132 */
133 public void setLocality(ITermService termService, String locality, String languageIso){
134
135 LanguageString loc = null;
136 if (languageIso == null){
137 loc = LanguageString.NewInstance(locality, Language.DEFAULT());
138 }else{
139 loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
140 }
141
142
143 if (loc == null){logger.warn("PROBLEM LOCALITY");}
144 this.gatheringEvent.setLocality(loc);
145 }
146
147 /*
148 * return the locality associated to the GatheringEvent
149 */
150 public LanguageString getLocality(){
151 return this.gatheringEvent.getLocality();
152 }
153
154 /*
155 * Set the coordinates for the current GatheringEvent
156 * @param: longitude
157 * @param: latitude
158 */
159 public void setCoordinates(Double longitude, Double latitude){
160 setCoordinates(longitude, latitude, null, null);
161 }
162
163 public void setCoordinates(Double longitude, Double latitude, ReferenceSystem referenceSystem, Integer errorRadius){
164 //create coordinates point
165 if((longitude == null) || (latitude == null)){
166 return;
167 }
168 Point coordinates = Point.NewInstance();
169 //add coordinates
170 if (longitude != 0.0) {
171 coordinates.setLongitude(longitude);
172 }
173 if (latitude != 0.0) {
174 coordinates.setLatitude(latitude);
175 }
176 if (errorRadius != null && errorRadius != 0) {
177 coordinates.setErrorRadius(errorRadius);
178 }
179 coordinates.setReferenceSystem(referenceSystem);
180 this.gatheringEvent.setExactLocation(coordinates);
181
182 }
183
184 public void setNotes(String gatheringNotes){
185 this.gatheringEvent.addAnnotation(Annotation.NewDefaultLanguageInstance(gatheringNotes));
186 }
187
188 public void setDate(String date){
189 this.gatheringEvent.setTimeperiod(TimePeriodParser.parseString(date));
190
191 }
192
193 public void setElevation(String elevationText, String elevationMin, String elevationMax, String elevationUnit){
194 if(elevationText!=null){
195 this.gatheringEvent.setAbsoluteElevationText(elevationText);
196 }
197 else{
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);
203 }
204 if(StringUtils.isNotBlank(elevationMax)){
205 Integer max = Integer.parseInt(elevationMax.replaceAll(pattern, ""));
206 this.gatheringEvent.setAbsoluteElevationMax(max);
207 }
208 if(StringUtils.isNotBlank(elevationUnit)){
209 if(!elevationUnit.equals("m")){
210 //TODO convert if necessary
211 }
212 }
213 }
214 }
215
216 public void setHeight(String heightText, String heightMin, String heightMax, String heightUnit){
217 if(heightText!=null){
218 this.gatheringEvent.setAbsoluteElevationText(heightText);
219 }
220 else{
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);
226 }
227 if(StringUtils.isNotBlank(heightMax)){
228 Double max = Double.parseDouble(heightMax.replaceAll(pattern, ""));
229 this.gatheringEvent.setDistanceToGroundMax(max);
230 }
231 if(StringUtils.isNotBlank(heightUnit)){
232 if (!heightUnit.equals("m")){
233 logger.debug("The unit " + heightUnit + " of the distance to ground is not meter.");
234 }
235 }
236 }
237 }
238
239 public void setGatheringDepth(String depthText, Double depthMin, Double depthMax, String depthUnit){
240 if(depthText!=null){
241 this.gatheringEvent.setDistanceToWaterSurfaceText(depthText);
242 }
243 else{
244 if (StringUtils.isNotBlank(depthUnit) && depthUnit.equals("cm")){
245 if (depthMin != null) {
246 depthMin = depthMin/100;
247 }
248 if (depthMax != null) {
249 depthMax = depthMax/100;
250 }
251 }
252 if(depthMin!=null){
253 this.gatheringEvent.setDistanceToWaterSurface(depthMin);
254 }
255 if(depthMax!=null){
256 this.gatheringEvent.setDistanceToWaterSurfaceMax(depthMax);
257 }
258 if(StringUtils.isNotBlank(depthUnit)){
259
260 if (!depthUnit.equals("m")){
261 logger.debug("The unit " + depthUnit + " of the distance to ground is not meter.");
262 }
263 }
264 }
265 }
266
267 /*
268 * Add a NamedArea to the GatheringEvent
269 * @param area: the NamedArea to add
270 */
271
272 public void addArea(DefinedTermBase area){
273 if (area.isInstanceOf(NamedArea.class)) {
274 this.gatheringEvent.addCollectingArea((NamedArea) area);
275 } else {
276 logger.info("OUPPPPSS :"+area.getClass());
277 }
278 }
279
280
281
282 /*
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
286 */
287 public void setCollector(String collectorName, SpecimenSynthesysExcelImportConfigurator config){
288 // System.out.println("collectors : "+collectorNames.toString());
289 Person collector;
290 collector = Person.NewInstance();
291 collector.setTitleCache(collectorName, true);
292 if (DEBUG) {
293 System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
294 }
295 this.gatheringEvent.setCollector(config.getPersons().get(collector.getTitleCache()));
296
297 }
298
299 /*
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
303 */
304 public void setCollector(String collectorName, TaxonXImportConfigurator config, IAgentService agentService){
305 // System.out.println("collectors : "+collectorNames.toString());
306 Person collector;
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);
313 }
314 this.gatheringEvent.setCollector(collector_db);
315
316 }
317
318
319 /*
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
323 */
324 public void setCollector(TeamOrPersonBase collector, Abcd206ImportConfigurator config){
325 // System.out.println("collectors : "+collectorNames.toString());
326
327 if (DEBUG) {
328 System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
329 }
330 this.gatheringEvent.setCollector(collector);
331 }
332
333 /**
334 * @param tp
335 */
336 public void setGatheringDate(TimePeriod tp) {
337 this.gatheringEvent.setTimeperiod(tp);
338 }
339
340 /**
341 * @param tp
342 */
343 public void setGatheringMethod(String gatheringMethod) {
344 this.gatheringEvent.setCollectingMethod(gatheringMethod);
345 }
346
347 public String getGatheringMethod(){
348 return this.gatheringEvent.getCollectingMethod();
349 }
350
351 public void setTeam(String gatheringTeam, SpecimenSynthesysExcelImportConfigurator config) {
352 Team t = new Team();
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);
356 } else{
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);
364 t.addTeamMember(p);
365 }
366 }
367 }
368 }
369 }
370 if (DEBUG) {
371 System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
372 }
373 this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
374 }
375
376 /**
377 * @param gatheringTeam
378 */
379 public void setTeam(String gatheringTeam, Abcd206ImportConfigurator config) {
380 Team t = new Team();
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);
384 } else{
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);
392 t.addTeamMember(p);
393 }
394 }
395 }
396 }
397 }
398 if (DEBUG) {
399 System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
400 }
401 //this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
402 }
403
404
405 }