Project

General

Profile

Download (13.2 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.ArrayList;
14
import java.util.HashSet;
15
import java.util.List;
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.io.specimen.abcd206.in.Abcd206ImportConfigurator;
24
import eu.etaxonomy.cdm.io.specimen.excel.in.SpecimenSynthesysExcelImportConfigurator;
25
import eu.etaxonomy.cdm.io.taxonx2013.TaxonXImportConfigurator;
26
import eu.etaxonomy.cdm.model.agent.Person;
27
import eu.etaxonomy.cdm.model.agent.Team;
28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.common.LanguageString;
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33
import eu.etaxonomy.cdm.model.location.Point;
34
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
35
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
36

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

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

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

    
84
    /*
85
     * Constructor
86
     * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
87
     * @param app: the CDM Application Controller
88
     * @param locality
89
     * @param languageIso
90
     * @param longitude
91
     * @param latitude
92
     * @param collectorNames
93
     */
94
    public UnitsGatheringEvent(ITermService termService, String locality, String languageIso, Double longitude,
95
            Double latitude, String elevationText, String elevationMin, String elevationMax, String elevationUnit, String date, List<String> collectorName, List<String> team, Abcd206ImportConfigurator config){
96
        this.setLocality(termService, locality, languageIso);
97
        this.setCoordinates(longitude, latitude);
98
        this.setDate(date);
99
        this.setElevation(elevationText, elevationMin, elevationMax, elevationUnit);
100
        if (!collectorName.isEmpty()) {
101
            List<String> tmp =  new ArrayList<String>(new HashSet<String>(collectorName));
102
            this.setCollector(tmp.get(0), config);
103
        }
104
        if (!team.isEmpty()) {
105
            List<String> tmpTeam = new ArrayList<String>(new HashSet<String>(team));
106
            this.setTeam(StringUtils.join(tmpTeam," & "), config);
107
        }
108
    }
109

    
110
    public GatheringEvent getGatheringEvent(){
111
        return this.gatheringEvent;
112
    }
113

    
114
    /**
115
     * Set the locality for the current GatheringEvent
116
     * @param locality
117
     * @param langageIso
118
     */
119
    public void setLocality(ITermService termService, String locality, String languageIso){
120
        LanguageString loc = null;
121
        List<LanguageString> languages = termService.getAllLanguageStrings(0, 0);
122
        boolean locFound=false;
123
        if ((languageIso == null) || (termService.getLanguageByIso(languageIso) == null)){
124
            //            if (languageIso != null && termService.getLanguageByIso(languageIso) == null ){
125
            //                logger.info("unknown iso used for the locality: "+languageIso);
126
            //            }
127
            for (LanguageString ls:languages){
128
                if (ls.getText().equalsIgnoreCase(locality)){
129
                    loc=ls;
130
                    locFound=true;
131
                    //                    System.out.println("REUSE LOCALITY");
132
                }
133
            }
134
            if (!locFound){
135
                loc = LanguageString.NewInstance(locality, Language.DEFAULT());
136
                termService.saveLanguageData(loc);
137
                languages.add(loc);
138
            }
139
        }else{
140
            for (LanguageString ls:languages){
141
                if (ls.getText().equalsIgnoreCase(locality) && ls.getLanguage().equals(termService.getLanguageByIso(languageIso))){
142
                    loc=ls;
143
                    locFound=true;
144
                    //                    System.out.println("REUSE LOCALITY");
145
                }
146
            }
147
            if (!locFound) {
148
                loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
149
                termService.saveLanguageData(loc);
150
                languages.add(loc);
151
            }
152
        }
153
        if (loc == null){logger.warn("PROBLEM LOCALITY");}
154
        this.gatheringEvent.setLocality(loc);
155
    }
156

    
157
    /*
158
     * return the locality associated to the GatheringEvent
159
     */
160
    public LanguageString getLocality(){
161
        return this.gatheringEvent.getLocality();
162
    }
163

    
164
    /*
165
     * Set the coordinates for the current GatheringEvent
166
     * @param: longitude
167
     * @param: latitude
168
     */
169
    public void setCoordinates(Double longitude, Double latitude){
170
        //create coordinates point
171
        if((longitude == null) || (latitude == null)){
172
            return;
173
        }
174
        Point coordinates = Point.NewInstance();
175
        //add coordinates
176
        if (longitude != 0.0) {
177
            coordinates.setLongitude(longitude);
178
        }
179
        if (latitude != 0.0) {
180
            coordinates.setLatitude(latitude);
181
        }
182
        //for proiBiosphere Quentin's data
183
        coordinates.setReferenceSystem(ReferenceSystem.WGS84());
184
        this.gatheringEvent.setExactLocation(coordinates);
185

    
186
    }
187

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

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

    
220
    /*
221
     * Add a NamedArea to the GatheringEvent
222
     * @param area: the NamedArea to add
223
     */
224

    
225
    public void addArea(DefinedTermBase area){
226
        if (area.isInstanceOf(NamedArea.class)) {
227
            this.gatheringEvent.addCollectingArea((NamedArea) area);
228
        } else {
229
            logger.info("OUPPPPSS :"+area.getClass());
230
        }
231
    }
232

    
233

    
234

    
235
    /*
236
     * Create a new collector or collector's team
237
     * @param: collectorNames: the list of names to add as collector/collectorTeam
238
     * USED - create each time a new Collector
239
     */
240
    public void setCollector(String collectorName, SpecimenSynthesysExcelImportConfigurator config){
241
        //        System.out.println("collectors : "+collectorNames.toString());
242
        Person collector;
243
        collector = Person.NewInstance();
244
        collector.setTitleCache(collectorName, true);
245
        if (DEBUG) {
246
            System.out.println("getcoll:"+config.getPersons().get(collector.getTitleCache()));
247
        }
248
        this.gatheringEvent.setCollector(config.getPersons().get(collector.getTitleCache()));
249

    
250
    }
251

    
252
    /*
253
     * Create a new collector or collector's team
254
     * @param: collectorNames: the list of names to add as collector/collectorTeam
255
     * USED - create each time a new Collector
256
     */
257
    public void setCollector(String collectorName, TaxonXImportConfigurator config, IAgentService agentService){
258
        //        System.out.println("collectors : "+collectorNames.toString());
259
        Person collector;
260
        collector = Person.NewInstance();
261
        collector.setTitleCache(collectorName, true);
262
        Person collector_db = config.getPersons().get(collector.getTitleCache());
263
        if (collector_db == null) {
264
            UUID uuid = agentService.saveOrUpdate(collector);
265
            collector_db=(Person) agentService.find(uuid);
266
        }
267
        this.gatheringEvent.setCollector(collector_db);
268

    
269
    }
270

    
271

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

    
288
    /**
289
     * @param tp
290
     */
291
    public void setGatheringDate(TimePeriod tp) {
292
        this.gatheringEvent.setTimeperiod(tp);
293
    }
294

    
295
    /**
296
     * @param gatheringTeam
297
     */
298
    public void setTeam(String gatheringTeam, SpecimenSynthesysExcelImportConfigurator config) {
299
        Team t = new Team();
300
        if ((gatheringTeam != null) && !gatheringTeam.isEmpty()) {
301
            if ((gatheringTeam.indexOf("et al.") != -1) || (gatheringTeam.indexOf("& al.") != -1) || (gatheringTeam.indexOf(" al.") != -1)){
302
                t.setTitleCache(gatheringTeam);
303
            } else{
304
                String[] tmp1 = gatheringTeam.split(" et ");
305
                for (String elt:tmp1){
306
                    String tmp2[] = elt.split("&");
307
                    for (String elt2:tmp2) {
308
                        if (!elt2.trim().isEmpty()) {
309
                            Person p = Person.NewInstance();
310
                            p.setTitleCache(elt2);
311
                            t.addTeamMember(p);
312
                        }
313
                    }
314
                }
315
            }
316
        }
317
        if (DEBUG) {
318
            System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
319
        }
320
        this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
321
    }
322

    
323
    /**
324
     * @param gatheringTeam
325
     */
326
    public void setTeam(String gatheringTeam, Abcd206ImportConfigurator config) {
327
        Team t = new Team();
328
        if ((gatheringTeam != null) && !gatheringTeam.isEmpty()) {
329
            if ((gatheringTeam.indexOf("et al.") != -1) || (gatheringTeam.indexOf("& al.") != -1) || (gatheringTeam.indexOf(" al.") != -1)){
330
                t.setTitleCache(gatheringTeam);
331
            } else{
332
                String[] tmp1 = gatheringTeam.split(" et ");
333
                for (String elt:tmp1){
334
                    String tmp2[] = elt.split("&");
335
                    for (String elt2:tmp2) {
336
                        if (!elt2.trim().isEmpty()) {
337
                            Person p = Person.NewInstance();
338
                            p.setTitleCache(elt2);
339
                            t.addTeamMember(p);
340
                        }
341
                    }
342
                }
343
            }
344
        }
345
        if (DEBUG) {
346
            System.out.println("getteam:"+config.getTeams().get(t.getTitleCache()));
347
        }
348
        this.gatheringEvent.setCollector(config.getTeams().get(t.getTitleCache()));
349
    }
350

    
351

    
352
}
(4-4/4)