Project

General

Profile

Download (14.3 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.io.specimen;
11

    
12

    
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.UUID;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.api.service.IAgentService;
21
import eu.etaxonomy.cdm.api.service.ITermService;
22
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
23
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenSynthesysExcelImportConfigurator;
24
import eu.etaxonomy.cdm.io.taxonx2013.TaxonXImportConfigurator;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
28
import eu.etaxonomy.cdm.model.common.Annotation;
29
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.common.LanguageString;
32
import eu.etaxonomy.cdm.model.common.TimePeriod;
33
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.location.Point;
35
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
36
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
37
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
38

    
39
/**
40
 * @author p.kelbert
41
 * @since 20.10.2008
42
 * @version 1.0
43
 */
44
public class UnitsGatheringEvent {
45

    
46
    private static final Logger logger = Logger.getLogger(UnitsGatheringEvent.class);
47
    private static final boolean DEBUG = false;
48
    private final GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
49

    
50
    /*
51
     * Constructor
52
     * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
53
     * @param app: the CDM Application Controller
54
     * @param locality
55
     * @param languageIso
56
     * @param longitude
57
     * @param latitude
58
     * @param collectorNames
59
     */
60
    public UnitsGatheringEvent(ITermService termService, String locality, String languageIso, Double longitude,
61
            Double latitude, String collectorName, String team, SpecimenSynthesysExcelImportConfigurator config){
62
        this.setLocality(termService, locality, languageIso);
63
        this.setCoordinates(longitude, latitude);
64
        if (!collectorName.isEmpty()) {
65
            this.setCollector(collectorName, config);
66
        }
67
        if (!team.isEmpty()) {
68
            this.setTeam(team, config);
69
        }
70
    }
71
    //
72
    public UnitsGatheringEvent(ITermService termService, String locality, String collectorName, Double longitude,
73
            Double latitude, TaxonXImportConfigurator config,IAgentService agentService){
74
        if (!StringUtils.isEmpty(locality)) {
75
            this.setLocality(termService, locality, null);
76
        }
77
        this.setCoordinates(longitude, latitude);
78
        if (!StringUtils.isEmpty(collectorName)) {
79
            this.setCollector(collectorName, config, agentService);
80
        }
81
        //        if (!team.isEmpty()) {
82
        //            this.setTeam(team, config);
83
        //        }
84
    }
85

    
86
    /*
87
     * Constructor
88
     * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
89
     * @param app: the CDM Application Controller
90
     * @param locality
91
     * @param languageIso
92
     * @param longitude
93
     * @param latitude
94
     * @param collectorNames
95
     */
96
    public UnitsGatheringEvent(ITermService termService, String locality, String languageIso, Double longitude,
97
            Double latitude, String errorRadius, String elevationText, String elevationMin, String elevationMax, String elevationUnit,
98
            String date, String gatheringNotes, String gatheringMethod, ReferenceSystem referenceSystem,
99
             Abcd206ImportConfigurator config) {
100
        this.setLocality(termService, locality, languageIso);
101

    
102
        Integer errorRadiusInt = Integer.getInteger(errorRadius);
103

    
104
        this.setCoordinates(longitude, latitude, referenceSystem, errorRadiusInt);
105
        this.setDate(date);
106
        this.setNotes(gatheringNotes);
107
        this.setElevation(elevationText, elevationMin, elevationMax, elevationUnit);
108
        this.setGatheringMethod(gatheringMethod);
109

    
110

    
111

    
112

    
113

    
114
    }
115

    
116
    /**
117
     * @param gatheringImages
118
     */
119
    public void setGatheringImages(HashMap<String, Map<String, String>> gatheringImages) {
120

    
121

    
122
    }
123
    public GatheringEvent getGatheringEvent(){
124
        return this.gatheringEvent;
125
    }
126

    
127
    /**
128
     * Set the locality for the current GatheringEvent
129
     * @param locality
130
     * @param langageIso
131
     */
132
    public void setLocality(ITermService termService, String locality, String languageIso){
133

    
134
        LanguageString loc = null;
135
        if (languageIso == null){
136
            loc = LanguageString.NewInstance(locality, Language.DEFAULT());
137
        }else{
138
            loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
139
        }
140

    
141

    
142
        if (loc == null){logger.warn("PROBLEM LOCALITY");}
143
        this.gatheringEvent.setLocality(loc);
144
    }
145

    
146
    /*
147
     * return the locality associated to the GatheringEvent
148
     */
149
    public LanguageString getLocality(){
150
        return this.gatheringEvent.getLocality();
151
    }
152

    
153
    /*
154
     * Set the coordinates for the current GatheringEvent
155
     * @param: longitude
156
     * @param: latitude
157
     */
158
    public void setCoordinates(Double longitude, Double latitude){
159
        setCoordinates(longitude, latitude, null, null);
160
    }
161

    
162
    public void setCoordinates(Double longitude, Double latitude, ReferenceSystem referenceSystem, Integer errorRadius){
163
        //create coordinates point
164
        if((longitude == null) || (latitude == null)){
165
            return;
166
        }
167
        Point coordinates = Point.NewInstance();
168
        //add coordinates
169
        if (longitude != 0.0) {
170
            coordinates.setLongitude(longitude);
171
        }
172
        if (latitude != 0.0) {
173
            coordinates.setLatitude(latitude);
174
        }
175
        if (errorRadius != 0) {
176
            coordinates.setErrorRadius(errorRadius);
177
        }
178
        coordinates.setReferenceSystem(referenceSystem);
179
        this.gatheringEvent.setExactLocation(coordinates);
180

    
181
    }
182

    
183
    public void setNotes(String gatheringNotes){
184
        this.gatheringEvent.addAnnotation(Annotation.NewDefaultLanguageInstance(gatheringNotes));
185
    }
186

    
187
    public void setDate(String date){
188
        TimePeriod timeperiod = this.gatheringEvent.getTimeperiod();
189
        if(timeperiod==null){
190
            timeperiod = TimePeriod.NewInstance();
191
            this.gatheringEvent.setTimeperiod(timeperiod);
192
        }else{
193
            this.gatheringEvent.setTimeperiod(TimePeriodParser.parseString(date));
194
        }
195

    
196
    }
197

    
198
    public void setElevation(String elevationText, String elevationMin, String elevationMax, String elevationUnit){
199
        if(elevationText!=null){
200
            this.gatheringEvent.setAbsoluteElevationText(elevationText);
201
        }
202
        else{
203
            //TODO check for unit at string end
204
            String pattern = "\\D";// regex for non-digits
205
            if(elevationMin!=null){
206
                Integer min = Integer.parseInt(elevationMin.replaceAll(pattern, ""));
207
                this.gatheringEvent.setAbsoluteElevation(min);
208
            }
209
            if(elevationMax!=null){
210
                Integer max = Integer.parseInt(elevationMax.replaceAll(pattern, ""));
211
                this.gatheringEvent.setAbsoluteElevationMax(max);
212
            }
213
            if(elevationUnit!=null){
214
                if(!elevationUnit.equals("m")){
215
                    //TODO convert if necessary
216
                }
217
            }
218
        }
219
    }
220

    
221
    public void setHeight(String heightText, String heightMin, String heightMax, String heightUnit){
222
        if(heightText!=null){
223
            this.gatheringEvent.setAbsoluteElevationText(heightText);
224
        }
225
        else{
226
            //TODO check for unit at string end
227
            String pattern = "\\D";// regex for non-digits
228
            if(heightMin!=null){
229
                Double min = Double.parseDouble(heightMin.replaceAll(pattern, ""));
230
                this.gatheringEvent.setDistanceToGround(min);
231
            }
232
            if(heightMax!=null){
233
                Double max = Double.parseDouble(heightMax.replaceAll(pattern, ""));
234
                this.gatheringEvent.setDistanceToGroundMax(max);
235
            }
236
            if(heightUnit!=null){
237
                if (!heightUnit.equals("m")){
238
                    logger.debug("The unit " + heightUnit + " of the distance to ground is not meter.");
239
                }
240
            }
241
        }
242
    }
243

    
244
    public void setGatheringDepth(String depthText, Double depthMin, Double depthMax, String depthUnit){
245
        if(depthText!=null){
246
            this.gatheringEvent.setDistanceToWaterSurfaceText(depthText);
247
        }
248
        else{
249

    
250
            if(depthMin!=null){
251
                this.gatheringEvent.setDistanceToWaterSurface(depthMin);
252
            }
253
            if(depthMax!=null){
254
                this.gatheringEvent.setDistanceToWaterSurfaceMax(depthMax);
255
            }
256
            if(depthUnit!=null){
257
                if (!depthUnit.equals("m")){
258
                    logger.debug("The unit " + depthUnit + " of the distance to ground is not meter.");
259
                }
260
            }
261
        }
262
    }
263

    
264
    /*
265
     * Add a NamedArea to the GatheringEvent
266
     * @param area: the NamedArea to add
267
     */
268

    
269
    public void addArea(DefinedTermBase area){
270
        if (area.isInstanceOf(NamedArea.class)) {
271
            this.gatheringEvent.addCollectingArea((NamedArea) area);
272
        } else {
273
            logger.info("OUPPPPSS :"+area.getClass());
274
        }
275
    }
276

    
277

    
278

    
279
    /*
280
     * Create a new collector or collector's team
281
     * @param: collectorNames: the list of names to add as collector/collectorTeam
282
     * USED - create each time a new Collector
283
     */
284
    public void setCollector(String collectorName, SpecimenSynthesysExcelImportConfigurator config){
285
        //        System.out.println("collectors : "+collectorNames.toString());
286
        Person collector;
287
        collector = Person.NewInstance();
288
        collector.setTitleCache(collectorName, true);
289
        if (DEBUG) {
290
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
291
        }
292
        this.gatheringEvent.setCollector(config.getPersons().get(collector.getTitleCache()));
293

    
294
    }
295

    
296
    /*
297
     * Create a new collector or collector's team
298
     * @param: collectorNames: the list of names to add as collector/collectorTeam
299
     * USED - create each time a new Collector
300
     */
301
    public void setCollector(String collectorName, TaxonXImportConfigurator config, IAgentService agentService){
302
        //        System.out.println("collectors : "+collectorNames.toString());
303
        Person collector;
304
        collector = Person.NewInstance();
305
        collector.setTitleCache(collectorName, true);
306
        Person collector_db = config.getPersons().get(collector.getTitleCache());
307
        if (collector_db == null) {
308
            UUID uuid = agentService.saveOrUpdate(collector);
309
            collector_db=(Person) agentService.find(uuid);
310
        }
311
        this.gatheringEvent.setCollector(collector_db);
312

    
313
    }
314

    
315

    
316
    /*
317
     * Create a new collector or collector's team
318
     * @param: collectorNames: the list of names to add as collector/collectorTeam
319
     * USED - create each time a new Collector
320
     */
321
    public void setCollector(TeamOrPersonBase collector, Abcd206ImportConfigurator config){
322
        //        System.out.println("collectors : "+collectorNames.toString());
323

    
324
        if (DEBUG) {
325
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
326
        }
327
        this.gatheringEvent.setCollector(collector);
328
    }
329

    
330
    /**
331
     * @param tp
332
     */
333
    public void setGatheringDate(TimePeriod tp) {
334
        this.gatheringEvent.setTimeperiod(tp);
335
    }
336

    
337
    /**
338
     * @param tp
339
     */
340
    public void setGatheringMethod(String gatheringMethod) {
341
        this.gatheringEvent.setCollectingMethod(gatheringMethod);
342
    }
343

    
344
    public String getGatheringMethod(){
345
        return this.gatheringEvent.getCollectingMethod();
346
    }
347

    
348
    /**
349
     * @param gatheringTeam
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
}
(7-7/7)