bugfixes for abcd import
[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
10 package eu.etaxonomy.cdm.io.specimen;
11
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.ListIterator;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
20 import eu.etaxonomy.cdm.api.service.ITermService;
21 import eu.etaxonomy.cdm.model.agent.AgentBase;
22 import eu.etaxonomy.cdm.model.agent.Person;
23 import eu.etaxonomy.cdm.model.agent.Team;
24 import eu.etaxonomy.cdm.model.common.Language;
25 import eu.etaxonomy.cdm.model.common.LanguageString;
26 import eu.etaxonomy.cdm.model.location.NamedArea;
27 import eu.etaxonomy.cdm.model.location.Point;
28 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
29
30 /**
31 * @author p.kelbert
32 * @created 20.10.2008
33 * @version 1.0
34 */
35 public class UnitsGatheringEvent {
36
37 private static final Logger logger = Logger.getLogger(UnitsGatheringEvent.class);
38 private GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
39
40 /*
41 * Constructor
42 * Fill in the locality, coordinates and the collector(s) for the current GatheringEvent
43 * @param app: the CDM Application Controller
44 * @param locality
45 * @param languageIso
46 * @param longitude
47 * @param latitude
48 * @param collectorNames
49 */
50 public UnitsGatheringEvent(ITermService termService, String locality, String languageIso, Double longitude, Double latitude, ArrayList<String> collectorNames){
51 this.setLocality(termService, locality, languageIso);
52 this.setCoordinates(longitude, latitude);
53 this.setCollector(collectorNames);
54 }
55
56 public GatheringEvent getGatheringEvent(){
57 return this.gatheringEvent;
58 }
59
60 /*
61 * Set the locality for the current GatheringEvent
62 * @param locality
63 * @param langageIso
64 */
65 public void setLocality(ITermService termService, String locality, String languageIso){
66 LanguageString loc;
67 if (languageIso == null || termService.getLanguageByIso(languageIso) == null){
68 if (languageIso != null && termService.getLanguageByIso(languageIso) == null ){
69 logger.info("unknown iso used for the locality: "+languageIso);
70 }
71 //FIXME should be UNKNOWN
72 loc = LanguageString.NewInstance(locality, Language.DEFAULT());
73 }else{
74 loc = LanguageString.NewInstance(locality, termService.getLanguageByIso(languageIso));
75 }
76 this.gatheringEvent.setLocality(loc);
77 }
78
79 /*
80 * return the locality associated to the GatheringEvent
81 */
82 public LanguageString getLocality(){
83 return this.gatheringEvent.getLocality();
84 }
85
86 /*
87 * Set the coordinates for the current GatheringEvent
88 * @param: longitude
89 * @param: latitude
90 */
91 public void setCoordinates(Double longitude, Double latitude){
92 //create coordinates point
93 Point coordinates = Point.NewInstance();
94 //add coordinates
95 coordinates.setLongitude(longitude);
96 coordinates.setLatitude(latitude);
97 this.gatheringEvent.setExactLocation(coordinates);
98 }
99
100 public void setElevation(Integer elevation){
101 this.gatheringEvent.setAbsoluteElevation(elevation);
102 }
103
104 /*
105 * Add a NamedArea to the GatheringEvent
106 * @param area: the NamedArea to add
107 */
108
109 public void addArea(NamedArea area){
110 this.gatheringEvent.addCollectingArea(area);
111 }
112
113 /*
114 * If the collector already exists, then use it
115 * if not, create a new collector
116 * NOT USED
117 */
118 public void setCollector(ICdmApplicationConfiguration config, ArrayList<String> collectorNames,boolean getExisting){
119 //create collector
120 AgentBase collector;
121 ListIterator<String> collectors = collectorNames.listIterator();
122 //add the collectors
123 String collName;
124 while (collectors.hasNext()){
125 collName = collectors.next();
126 /*check if the collector does already exist*/
127 try{
128 List<AgentBase> col = config.getAgentService().findByTitle(null,collName,null,null,null,null,null, null).getRecords();
129 collector=col.get(0);
130 }catch (Exception e) {
131 collector = Person.NewInstance();
132 collector.setTitleCache(collName, true);
133 }
134 this.gatheringEvent.setCollector(collector);
135 }
136 }
137
138 /*
139 * Create a new collector or collector's team
140 * @param: collectorNames: the list of names to add as collector/collectorTeam
141 * USED - create each time a new Collector
142 */
143 public void setCollector(ArrayList<String> collectorNames){
144 Person collector;
145 String collName;
146
147 if (collectorNames.size()>1){
148 Team team = new Team();
149 for (String strCollectorName : collectorNames){
150 collector = Person.NewInstance();
151 collector.setTitleCache(strCollectorName, true);
152 team.addTeamMember(collector);
153 this.gatheringEvent.setCollector(team);
154 }
155 }else if (collectorNames.size() == 1) {
156 collName = collectorNames.get(0);
157 collector = Person.NewInstance();
158 collector.setTitleCache(collName, true);
159 this.gatheringEvent.setCollector(collector);
160 }
161
162 }
163
164
165 }