Project

General

Profile

Download (13.9 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

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

    
45
    private static final Logger logger = Logger.getLogger(UnitsGatheringEvent.class);
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 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
        this.setCoordinates(longitude, latitude, referenceSystem);
101
        this.setDate(date);
102
        this.setNotes(gatheringNotes);
103
        this.setElevation(elevationText, elevationMin, elevationMax, elevationUnit);
104
        this.setGatheringMethod(gatheringMethod);
105

    
106

    
107

    
108

    
109

    
110
    }
111

    
112
    /**
113
     * @param gatheringImages
114
     */
115
    public void setGatheringImages(HashMap<String, Map<String, String>> gatheringImages) {
116

    
117

    
118
    }
119
    public GatheringEvent getGatheringEvent(){
120
        return this.gatheringEvent;
121
    }
122

    
123
    /**
124
     * Set the locality for the current GatheringEvent
125
     * @param locality
126
     * @param langageIso
127
     */
128
    public void setLocality(ITermService termService, String locality, String languageIso){
129

    
130
        LanguageString loc = null;
131
        if (languageIso == null){
132
            loc = LanguageString.NewInstance(locality, Language.DEFAULT());
133
        }else{
134
            loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
135
        }
136

    
137

    
138
        if (loc == null){logger.warn("PROBLEM LOCALITY");}
139
        this.gatheringEvent.setLocality(loc);
140
    }
141

    
142
    /*
143
     * return the locality associated to the GatheringEvent
144
     */
145
    public LanguageString getLocality(){
146
        return this.gatheringEvent.getLocality();
147
    }
148

    
149
    /*
150
     * Set the coordinates for the current GatheringEvent
151
     * @param: longitude
152
     * @param: latitude
153
     */
154
    public void setCoordinates(Double longitude, Double latitude){
155
        setCoordinates(longitude, latitude, null);
156
    }
157

    
158
    public void setCoordinates(Double longitude, Double latitude, ReferenceSystem referenceSystem){
159
        //create coordinates point
160
        if((longitude == null) || (latitude == null)){
161
            return;
162
        }
163
        Point coordinates = Point.NewInstance();
164
        //add coordinates
165
        if (longitude != 0.0) {
166
            coordinates.setLongitude(longitude);
167
        }
168
        if (latitude != 0.0) {
169
            coordinates.setLatitude(latitude);
170
        }
171
        coordinates.setReferenceSystem(referenceSystem);
172
        this.gatheringEvent.setExactLocation(coordinates);
173

    
174
    }
175

    
176
    public void setNotes(String gatheringNotes){
177
        this.gatheringEvent.addAnnotation(Annotation.NewDefaultLanguageInstance(gatheringNotes));
178
    }
179

    
180
    public void setDate(String date){
181
        TimePeriod timeperiod = this.gatheringEvent.getTimeperiod();
182
        if(timeperiod==null){
183
            timeperiod = TimePeriod.NewInstance();
184
            this.gatheringEvent.setTimeperiod(timeperiod);
185
        }
186
        timeperiod.setFreeText(date);
187
    }
188

    
189
    public void setElevation(String elevationText, String elevationMin, String elevationMax, String elevationUnit){
190
        if(elevationText!=null){
191
            this.gatheringEvent.setAbsoluteElevationText(elevationText);
192
        }
193
        else{
194
            //TODO check for unit at string end
195
            String pattern = "\\D";// regex for non-digits
196
            if(elevationMin!=null){
197
                Integer min = Integer.parseInt(elevationMin.replaceAll(pattern, ""));
198
                this.gatheringEvent.setAbsoluteElevation(min);
199
            }
200
            if(elevationMax!=null){
201
                Integer max = Integer.parseInt(elevationMax.replaceAll(pattern, ""));
202
                this.gatheringEvent.setAbsoluteElevationMax(max);
203
            }
204
            if(elevationUnit!=null){
205
                if(!elevationUnit.equals("m")){
206
                    //TODO convert if necessary
207
                }
208
            }
209
        }
210
    }
211

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

    
235
    public void setGatheringDepth(String depthText, Double depthMin, Double depthMax, String depthUnit){
236
        if(depthText!=null){
237
            this.gatheringEvent.setDistanceToWaterSurfaceText(depthText);
238
        }
239
        else{
240

    
241
            if(depthMin!=null){
242
                this.gatheringEvent.setDistanceToWaterSurface(depthMin);
243
            }
244
            if(depthMax!=null){
245
                this.gatheringEvent.setDistanceToWaterSurfaceMax(depthMax);
246
            }
247
            if(depthUnit!=null){
248
                if (!depthUnit.equals("m")){
249
                    logger.debug("The unit " + depthUnit + " of the distance to ground is not meter.");
250
                }
251
            }
252
        }
253
    }
254

    
255
    /*
256
     * Add a NamedArea to the GatheringEvent
257
     * @param area: the NamedArea to add
258
     */
259

    
260
    public void addArea(DefinedTermBase area){
261
        if (area.isInstanceOf(NamedArea.class)) {
262
            this.gatheringEvent.addCollectingArea((NamedArea) area);
263
        } else {
264
            logger.info("OUPPPPSS :"+area.getClass());
265
        }
266
    }
267

    
268

    
269

    
270
    /*
271
     * Create a new collector or collector's team
272
     * @param: collectorNames: the list of names to add as collector/collectorTeam
273
     * USED - create each time a new Collector
274
     */
275
    public void setCollector(String collectorName, SpecimenSynthesysExcelImportConfigurator config){
276
        //        System.out.println("collectors : "+collectorNames.toString());
277
        Person collector;
278
        collector = Person.NewInstance();
279
        collector.setTitleCache(collectorName, true);
280
        if (DEBUG) {
281
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
282
        }
283
        this.gatheringEvent.setCollector(config.getPersons().get(collector.getTitleCache()));
284

    
285
    }
286

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

    
304
    }
305

    
306

    
307
    /*
308
     * Create a new collector or collector's team
309
     * @param: collectorNames: the list of names to add as collector/collectorTeam
310
     * USED - create each time a new Collector
311
     */
312
    public void setCollector(TeamOrPersonBase collector, Abcd206ImportConfigurator config){
313
        //        System.out.println("collectors : "+collectorNames.toString());
314

    
315
        if (DEBUG) {
316
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
317
        }
318
        this.gatheringEvent.setCollector(collector);
319
    }
320

    
321
    /**
322
     * @param tp
323
     */
324
    public void setGatheringDate(TimePeriod tp) {
325
        this.gatheringEvent.setTimeperiod(tp);
326
    }
327

    
328
    /**
329
     * @param tp
330
     */
331
    public void setGatheringMethod(String gatheringMethod) {
332
        this.gatheringEvent.setCollectingMethod(gatheringMethod);
333
    }
334

    
335
    public String getGatheringMethod(){
336
        return this.gatheringEvent.getCollectingMethod();
337
    }
338

    
339
    /**
340
     * @param gatheringTeam
341
     */
342
    public void setTeam(String gatheringTeam, SpecimenSynthesysExcelImportConfigurator config) {
343
        Team t = new Team();
344
        if ((gatheringTeam != null) && !gatheringTeam.isEmpty()) {
345
            if ((gatheringTeam.indexOf("et al.") != -1) || (gatheringTeam.indexOf("& al.") != -1) || (gatheringTeam.indexOf(" al.") != -1)){
346
                t.setTitleCache(gatheringTeam);
347
            } else{
348
                String[] tmp1 = gatheringTeam.split(" et ");
349
                for (String elt:tmp1){
350
                    String tmp2[] = elt.split("&");
351
                    for (String elt2:tmp2) {
352
                        if (!elt2.trim().isEmpty()) {
353
                            Person p = Person.NewInstance();
354
                            p.setTitleCache(elt2);
355
                            t.addTeamMember(p);
356
                        }
357
                    }
358
                }
359
            }
360
        }
361
        if (DEBUG) {
362
            System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
363
        }
364
        this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
365
    }
366

    
367
    /**
368
     * @param gatheringTeam
369
     */
370
    public void setTeam(String gatheringTeam, Abcd206ImportConfigurator config) {
371
        Team t = new Team();
372
        if ((gatheringTeam != null) && !gatheringTeam.isEmpty()) {
373
            if ((gatheringTeam.indexOf("et al.") != -1) || (gatheringTeam.indexOf("& al.") != -1) || (gatheringTeam.indexOf(" al.") != -1)){
374
                t.setTitleCache(gatheringTeam);
375
            } else{
376
                String[] tmp1 = gatheringTeam.split(" et ");
377
                for (String elt:tmp1){
378
                    String tmp2[] = elt.split("&");
379
                    for (String elt2:tmp2) {
380
                        if (!elt2.trim().isEmpty()) {
381
                            Person p = Person.NewInstance();
382
                            p.setTitleCache(elt2);
383
                            t.addTeamMember(p);
384
                        }
385
                    }
386
                }
387
            }
388
        }
389
        if (DEBUG) {
390
            System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
391
        }
392
        //this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
393
    }
394

    
395

    
396
}
(7-7/7)