area matching in specimen 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 if(longitude == null || latitude == null){
94 return;
95 }
96 Point coordinates = Point.NewInstance();
97 //add coordinates
98 coordinates.setLongitude(longitude);
99 coordinates.setLatitude(latitude);
100 this.gatheringEvent.setExactLocation(coordinates);
101
102 }
103
104 public void setElevation(Integer elevation){
105 this.gatheringEvent.setAbsoluteElevation(elevation);
106 }
107
108 /*
109 * Add a NamedArea to the GatheringEvent
110 * @param area: the NamedArea to add
111 */
112
113 public void addArea(NamedArea area){
114 this.gatheringEvent.addCollectingArea(area);
115 }
116
117 /*
118 * If the collector already exists, then use it
119 * if not, create a new collector
120 * NOT USED
121 */
122 public void setCollector(ICdmApplicationConfiguration config, ArrayList<String> collectorNames,boolean getExisting){
123 //create collector
124 AgentBase collector;
125 ListIterator<String> collectors = collectorNames.listIterator();
126 //add the collectors
127 String collName;
128 while (collectors.hasNext()){
129 collName = collectors.next();
130 /*check if the collector does already exist*/
131 try{
132 List<AgentBase> col = config.getAgentService().findByTitle(null,collName,null,null,null,null,null, null).getRecords();
133 collector=col.get(0);
134 }catch (Exception e) {
135 collector = Person.NewInstance();
136 collector.setTitleCache(collName, true);
137 }
138 this.gatheringEvent.setCollector(collector);
139 }
140 }
141
142 /*
143 * Create a new collector or collector's team
144 * @param: collectorNames: the list of names to add as collector/collectorTeam
145 * USED - create each time a new Collector
146 */
147 public void setCollector(ArrayList<String> collectorNames){
148 Person collector;
149 String collName;
150
151 if (collectorNames.size()>1){
152 Team team = new Team();
153 for (String strCollectorName : collectorNames){
154 collector = Person.NewInstance();
155 collector.setTitleCache(strCollectorName, true);
156 team.addTeamMember(collector);
157 this.gatheringEvent.setCollector(team);
158 }
159 }else if (collectorNames.size() == 1) {
160 collName = collectorNames.get(0);
161 collector = Person.NewInstance();
162 collector.setTitleCache(collName, true);
163 this.gatheringEvent.setCollector(collector);
164 }
165
166 }
167
168
169 }