Project

General

Profile

Download (15.5 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.List;
15
import java.util.Map;
16
import java.util.UUID;
17

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

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

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

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

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

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

    
108

    
109

    
110

    
111

    
112
    }
113

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

    
119

    
120
    }
121
    public GatheringEvent getGatheringEvent(){
122
        return this.gatheringEvent;
123
    }
124

    
125
    /**
126
     * Set the locality for the current GatheringEvent
127
     * @param locality
128
     * @param langageIso
129
     */
130
    public void setLocality(ITermService termService, String locality, String languageIso){
131
        LanguageString loc = null;
132
        List<LanguageString> languages = termService.getAllLanguageStrings(0, 0);
133
        boolean locFound=false;
134
        if ((languageIso == null) || (termService.getLanguageByIso(languageIso) == null)){
135
            //            if (languageIso != null && termService.getLanguageByIso(languageIso) == null ){
136
            //                logger.info("unknown iso used for the locality: "+languageIso);
137
            //            }
138
            for (LanguageString ls:languages){
139
                if (ls == null) {
140
                    continue;
141
                }
142
                ls = HibernateProxyHelper.deproxy(ls, LanguageString.class);
143
                if (ls.getText() == null){
144
                    continue;
145
                }
146
                if (ls.getText().equalsIgnoreCase(locality)){
147
                    loc=ls;
148
                    locFound=true;
149
                    //                    System.out.println("REUSE LOCALITY");
150
                }
151
            }
152
            if (!locFound){
153
                loc = LanguageString.NewInstance(locality, Language.DEFAULT());
154
                termService.saveLanguageData(loc);
155
                languages.add(loc);
156
            }
157
        }else{
158
            for (LanguageString ls:languages){
159
                if (ls.getText().equalsIgnoreCase(locality) && ls.getLanguage().equals(termService.getLanguageByIso(languageIso))){
160
                    loc=ls;
161
                    locFound=true;
162
                    //                    System.out.println("REUSE LOCALITY");
163
                }
164
            }
165
            if (!locFound) {
166
                loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
167
                termService.saveLanguageData(loc);
168
                languages.add(loc);
169
            }
170
        }
171
        if (loc == null){logger.warn("PROBLEM LOCALITY");}
172
        this.gatheringEvent.setLocality(loc);
173
    }
174

    
175
    /*
176
     * return the locality associated to the GatheringEvent
177
     */
178
    public LanguageString getLocality(){
179
        return this.gatheringEvent.getLocality();
180
    }
181

    
182
    /*
183
     * Set the coordinates for the current GatheringEvent
184
     * @param: longitude
185
     * @param: latitude
186
     */
187
    public void setCoordinates(Double longitude, Double latitude){
188
        setCoordinates(longitude, latitude, null);
189
    }
190

    
191
    public void setCoordinates(Double longitude, Double latitude, ReferenceSystem referenceSystem){
192
        //create coordinates point
193
        if((longitude == null) || (latitude == null)){
194
            return;
195
        }
196
        Point coordinates = Point.NewInstance();
197
        //add coordinates
198
        if (longitude != 0.0) {
199
            coordinates.setLongitude(longitude);
200
        }
201
        if (latitude != 0.0) {
202
            coordinates.setLatitude(latitude);
203
        }
204
        coordinates.setReferenceSystem(referenceSystem);
205
        this.gatheringEvent.setExactLocation(coordinates);
206

    
207
    }
208

    
209
    public void setNotes(String gatheringNotes){
210
        this.gatheringEvent.addAnnotation(Annotation.NewDefaultLanguageInstance(gatheringNotes));
211
    }
212

    
213
    public void setDate(String date){
214
        TimePeriod timeperiod = this.gatheringEvent.getTimeperiod();
215
        if(timeperiod==null){
216
            timeperiod = TimePeriod.NewInstance();
217
            this.gatheringEvent.setTimeperiod(timeperiod);
218
        }
219
        timeperiod.setFreeText(date);
220
    }
221

    
222
    public void setElevation(String elevationText, String elevationMin, String elevationMax, String elevationUnit){
223
        if(elevationText!=null){
224
            this.gatheringEvent.setAbsoluteElevationText(elevationText);
225
        }
226
        else{
227
            //TODO check for unit at string end
228
            String pattern = "\\D";// regex for non-digits
229
            if(elevationMin!=null){
230
                Integer min = Integer.parseInt(elevationMin.replaceAll(pattern, ""));
231
                this.gatheringEvent.setAbsoluteElevation(min);
232
            }
233
            if(elevationMax!=null){
234
                Integer max = Integer.parseInt(elevationMax.replaceAll(pattern, ""));
235
                this.gatheringEvent.setAbsoluteElevationMax(max);
236
            }
237
            if(elevationUnit!=null){
238
                if(!elevationUnit.equals("m")){
239
                    //TODO convert if necessary
240
                }
241
            }
242
        }
243
    }
244

    
245
    public void setHeight(String heightText, String heightMin, String heightMax, String heightUnit){
246
        if(heightText!=null){
247
            this.gatheringEvent.setAbsoluteElevationText(heightText);
248
        }
249
        else{
250
            //TODO check for unit at string end
251
            String pattern = "\\D";// regex for non-digits
252
            if(heightMin!=null){
253
                Double min = Double.parseDouble(heightMin.replaceAll(pattern, ""));
254
                this.gatheringEvent.setDistanceToGround(min);
255
            }
256
            if(heightMax!=null){
257
                Double max = Double.parseDouble(heightMax.replaceAll(pattern, ""));
258
                this.gatheringEvent.setDistanceToGroundMax(max);
259
            }
260
            if(heightUnit!=null){
261
                if (!heightUnit.equals("m")){
262
                    logger.debug("The unit " + heightUnit + " of the distance to ground is not meter.");
263
                }
264
            }
265
        }
266
    }
267

    
268
    public void setGatheringDepth(String depthText, Double depthMin, Double depthMax, String depthUnit){
269
        if(depthText!=null){
270
            this.gatheringEvent.setDistanceToWaterSurfaceText(depthText);
271
        }
272
        else{
273

    
274
            if(depthMin!=null){
275
                this.gatheringEvent.setDistanceToWaterSurface(depthMin);
276
            }
277
            if(depthMax!=null){
278
                this.gatheringEvent.setDistanceToWaterSurfaceMax(depthMax);
279
            }
280
            if(depthUnit!=null){
281
                if (!depthUnit.equals("m")){
282
                    logger.debug("The unit " + depthUnit + " of the distance to ground is not meter.");
283
                }
284
            }
285
        }
286
    }
287

    
288
    /*
289
     * Add a NamedArea to the GatheringEvent
290
     * @param area: the NamedArea to add
291
     */
292

    
293
    public void addArea(DefinedTermBase area){
294
        if (area.isInstanceOf(NamedArea.class)) {
295
            this.gatheringEvent.addCollectingArea((NamedArea) area);
296
        } else {
297
            logger.info("OUPPPPSS :"+area.getClass());
298
        }
299
    }
300

    
301

    
302

    
303
    /*
304
     * Create a new collector or collector's team
305
     * @param: collectorNames: the list of names to add as collector/collectorTeam
306
     * USED - create each time a new Collector
307
     */
308
    public void setCollector(String collectorName, SpecimenSynthesysExcelImportConfigurator config){
309
        //        System.out.println("collectors : "+collectorNames.toString());
310
        Person collector;
311
        collector = Person.NewInstance();
312
        collector.setTitleCache(collectorName, true);
313
        if (DEBUG) {
314
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
315
        }
316
        this.gatheringEvent.setCollector(config.getPersons().get(collector.getTitleCache()));
317

    
318
    }
319

    
320
    /*
321
     * Create a new collector or collector's team
322
     * @param: collectorNames: the list of names to add as collector/collectorTeam
323
     * USED - create each time a new Collector
324
     */
325
    public void setCollector(String collectorName, TaxonXImportConfigurator config, IAgentService agentService){
326
        //        System.out.println("collectors : "+collectorNames.toString());
327
        Person collector;
328
        collector = Person.NewInstance();
329
        collector.setTitleCache(collectorName, true);
330
        Person collector_db = config.getPersons().get(collector.getTitleCache());
331
        if (collector_db == null) {
332
            UUID uuid = agentService.saveOrUpdate(collector);
333
            collector_db=(Person) agentService.find(uuid);
334
        }
335
        this.gatheringEvent.setCollector(collector_db);
336

    
337
    }
338

    
339

    
340
    /*
341
     * Create a new collector or collector's team
342
     * @param: collectorNames: the list of names to add as collector/collectorTeam
343
     * USED - create each time a new Collector
344
     */
345
    public void setCollector(TeamOrPersonBase collector, Abcd206ImportConfigurator config){
346
        //        System.out.println("collectors : "+collectorNames.toString());
347

    
348
        if (DEBUG) {
349
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
350
        }
351
        this.gatheringEvent.setCollector(collector);
352
    }
353

    
354
    /**
355
     * @param tp
356
     */
357
    public void setGatheringDate(TimePeriod tp) {
358
        this.gatheringEvent.setTimeperiod(tp);
359
    }
360

    
361
    /**
362
     * @param tp
363
     */
364
    public void setGatheringMethod(String gatheringMethod) {
365
        this.gatheringEvent.setCollectingMethod(gatheringMethod);
366
    }
367

    
368
    public String getGatheringMethod(){
369
        return this.gatheringEvent.getCollectingMethod();
370
    }
371

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

    
400
    /**
401
     * @param gatheringTeam
402
     */
403
    public void setTeam(String gatheringTeam, Abcd206ImportConfigurator config) {
404
        Team t = new Team();
405
        if ((gatheringTeam != null) && !gatheringTeam.isEmpty()) {
406
            if ((gatheringTeam.indexOf("et al.") != -1) || (gatheringTeam.indexOf("& al.") != -1) || (gatheringTeam.indexOf(" al.") != -1)){
407
                t.setTitleCache(gatheringTeam);
408
            } else{
409
                String[] tmp1 = gatheringTeam.split(" et ");
410
                for (String elt:tmp1){
411
                    String tmp2[] = elt.split("&");
412
                    for (String elt2:tmp2) {
413
                        if (!elt2.trim().isEmpty()) {
414
                            Person p = Person.NewInstance();
415
                            p.setTitleCache(elt2);
416
                            t.addTeamMember(p);
417
                        }
418
                    }
419
                }
420
            }
421
        }
422
        if (DEBUG) {
423
            System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
424
        }
425
        //this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
426
    }
427

    
428

    
429
}
(7-7/7)