1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.api.facade;
|
11
|
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
|
18
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
19
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
20
|
import eu.etaxonomy.cdm.model.agent.Institution;
|
21
|
import eu.etaxonomy.cdm.model.agent.Person;
|
22
|
import eu.etaxonomy.cdm.model.agent.Team;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.Language;
|
25
|
import eu.etaxonomy.cdm.model.common.Representation;
|
26
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
27
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
28
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
29
|
import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
|
30
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
31
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
32
|
|
33
|
/**
|
34
|
* @author a.mueller
|
35
|
* @date 03.06.2010
|
36
|
*
|
37
|
*/
|
38
|
public class DerivedUnitFacadeFieldObservationCacheStrategy extends StrategyBase implements IIdentifiableEntityCacheStrategy<FieldObservation> {
|
39
|
private static final long serialVersionUID = 1578628591216605619L;
|
40
|
private static final Logger logger = Logger.getLogger(DerivedUnitFacadeFieldObservationCacheStrategy.class);
|
41
|
|
42
|
private static final UUID uuid = UUID.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
|
43
|
|
44
|
/* (non-Javadoc)
|
45
|
* @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
|
46
|
*/
|
47
|
@Override
|
48
|
protected UUID getUuid() {
|
49
|
return uuid;
|
50
|
}
|
51
|
|
52
|
private boolean includeEmptySeconds = false;
|
53
|
private boolean includeReferenceSystem = true;
|
54
|
|
55
|
|
56
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
|
58
|
*/
|
59
|
@Override
|
60
|
public String getTitleCache(FieldObservation fieldObservation) {
|
61
|
DerivedUnitFacade facade;
|
62
|
String result = "";
|
63
|
DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
|
64
|
config.setFirePropertyChangeEvents(false);
|
65
|
facade = DerivedUnitFacade.NewInstance(DerivedUnitType.FieldObservation, fieldObservation, config);
|
66
|
result = getFieldData(facade);
|
67
|
result = addPlantDescription(result, facade);
|
68
|
facade.close();
|
69
|
return result;
|
70
|
}
|
71
|
|
72
|
|
73
|
protected String getFieldData(DerivedUnitFacade facade) {
|
74
|
String ALTITUDE_PREFIX = "alt. ";
|
75
|
String ALTITUDE_POSTFIX = " m";
|
76
|
|
77
|
String result = "";
|
78
|
|
79
|
|
80
|
//country
|
81
|
String strCountry = null;
|
82
|
NamedArea country = facade.getCountry();
|
83
|
Representation repCountry = country == null ? null : country.getRepresentation(Language.DEFAULT());
|
84
|
//TODO currently the label is the 3 digit representation of the country and text is the full text.
|
85
|
//this is against the common way of handling text, label and labelabbrev in defined terms
|
86
|
strCountry = repCountry == null ? null: repCountry.getText();
|
87
|
result = CdmUtils.concat(", ", result, strCountry);
|
88
|
|
89
|
//locality
|
90
|
result = CdmUtils.concat(", ", result, facade.getLocalityText());
|
91
|
|
92
|
//elevation
|
93
|
if (facade.getAbsoluteElevation() != null){
|
94
|
result = CdmUtils.concat(", " , result, ALTITUDE_PREFIX);
|
95
|
result += facade.getAbsoluteElevation() + ALTITUDE_POSTFIX;
|
96
|
}
|
97
|
|
98
|
//exact locality
|
99
|
if (facade.getExactLocation() != null){
|
100
|
String exactLocation = facade.getExactLocation().toSexagesimalString(this.includeEmptySeconds, this.includeReferenceSystem);
|
101
|
result = CdmUtils.concat(", ", result, exactLocation);
|
102
|
}
|
103
|
|
104
|
//ecology
|
105
|
result = CdmUtils.concat(", ", result, facade.getEcology());
|
106
|
|
107
|
//gathering period
|
108
|
//TODO period.toString ??
|
109
|
TimePeriod gatheringPeriod = facade.getGatheringPeriod();
|
110
|
result = CdmUtils.concat(", ", result, (gatheringPeriod == null? null : gatheringPeriod.toString()));
|
111
|
|
112
|
//collector (team) and field number
|
113
|
String collectorAndFieldNumber = getCollectorAndFieldNumber(facade);
|
114
|
result = CdmUtils.concat(", ", result, collectorAndFieldNumber);
|
115
|
|
116
|
|
117
|
return result;
|
118
|
}
|
119
|
|
120
|
|
121
|
protected String addPlantDescription(String result, DerivedUnitFacade facade) {
|
122
|
|
123
|
//plant description
|
124
|
result = CdmUtils.concat("; ", result, facade.getPlantDescription());
|
125
|
if (CdmUtils.isNotEmpty(result)){
|
126
|
result += ".";
|
127
|
}
|
128
|
|
129
|
return result;
|
130
|
}
|
131
|
|
132
|
|
133
|
|
134
|
private String getCollectorAndFieldNumber(DerivedUnitFacade facade) {
|
135
|
String result = "";
|
136
|
AgentBase<?> collector = facade.getCollector();
|
137
|
String fieldNumber = facade.getFieldNumber();
|
138
|
Person primaryCollector = facade.getPrimaryCollector();
|
139
|
|
140
|
if (collector == null){
|
141
|
return fieldNumber;
|
142
|
}else if(collector.isProtectedTitleCache()){
|
143
|
return CdmUtils.concat(" ", collector.getTitleCache(), fieldNumber);
|
144
|
}else{
|
145
|
result = "";
|
146
|
Team collectorTeam;
|
147
|
if (collector.isInstanceOf(Person.class)){
|
148
|
collectorTeam = Team.NewInstance();
|
149
|
if (primaryCollector == null){
|
150
|
primaryCollector = CdmBase.deproxy(collector, Person.class);
|
151
|
}
|
152
|
collectorTeam.addTeamMember(primaryCollector);
|
153
|
} else if (collector.isInstanceOf(Team.class) && ! collector.isProtectedTitleCache() ){
|
154
|
collectorTeam = CdmBase.deproxy(collector, Team.class);
|
155
|
}else{
|
156
|
return CdmUtils.concat(" ", collector.getTitleCache(), fieldNumber);
|
157
|
}
|
158
|
|
159
|
int counter = 0;
|
160
|
int teamSize = collectorTeam.getTeamMembers().size();
|
161
|
boolean fieldNumberAdded = false;
|
162
|
for (Person member : collectorTeam.getTeamMembers()){
|
163
|
counter++;
|
164
|
String concatString = (counter >= teamSize)? " & " : ", ";
|
165
|
result = CdmUtils.concat(concatString, result, getMemberString(member) );
|
166
|
if (member.equals(primaryCollector)){
|
167
|
result = addFieldNumber(result, fieldNumber);
|
168
|
fieldNumberAdded = true;
|
169
|
}
|
170
|
}
|
171
|
if (! fieldNumberAdded){
|
172
|
result = addFieldNumber(result, fieldNumber);
|
173
|
}
|
174
|
return result;
|
175
|
}
|
176
|
|
177
|
}
|
178
|
|
179
|
|
180
|
|
181
|
private String addFieldNumber(String result, String fieldNumber) {
|
182
|
result = CdmUtils.concat(" ", result, fieldNumber);
|
183
|
return result;
|
184
|
}
|
185
|
|
186
|
|
187
|
|
188
|
/**
|
189
|
* Strategy to format a collector team member name
|
190
|
* @param member
|
191
|
* @return
|
192
|
*/
|
193
|
private String getMemberString(Person member) {
|
194
|
if (StringUtils.isNotBlank(member.getLastname()) && ! member.isProtectedTitleCache() ){
|
195
|
String result = member.getLastname();
|
196
|
if (StringUtils.isNotBlank(member.getFirstname())){
|
197
|
result = member.getFirstname().substring(0,1) + ". " + result;
|
198
|
}
|
199
|
return result;
|
200
|
}else{
|
201
|
return member.getTitleCache();
|
202
|
}
|
203
|
}
|
204
|
|
205
|
|
206
|
|
207
|
private boolean testPrimaryCollectorInCollectorTeam(AgentBase collector, Person primaryCollector) {
|
208
|
if (collector.isInstanceOf(Person.class)){
|
209
|
return collector.equals(primaryCollector);
|
210
|
}else if (collector.isInstanceOf(Team.class)){
|
211
|
Team collectorTeam = CdmBase.deproxy(collector, Team.class);
|
212
|
return collectorTeam.getTeamMembers().contains(primaryCollector);
|
213
|
}else{
|
214
|
logger.warn("Collector is not of type person or team");
|
215
|
return false;
|
216
|
}
|
217
|
}
|
218
|
|
219
|
|
220
|
|
221
|
/**
|
222
|
* @param facade
|
223
|
*/
|
224
|
private String getCode(DerivedUnitFacade facade) {
|
225
|
String code = "";
|
226
|
if(facade.getCollection() != null){
|
227
|
code = facade.getCollection().getCode();
|
228
|
if (CdmUtils.isEmpty(code)){
|
229
|
Institution institution = facade.getCollection().getInstitute();
|
230
|
if (institution != null){
|
231
|
code = institution.getCode();
|
232
|
}
|
233
|
if (CdmUtils.isEmpty(code)){
|
234
|
Collection superCollection = facade.getCollection().getSuperCollection();
|
235
|
if (superCollection != null){
|
236
|
code = superCollection.getCode();
|
237
|
}
|
238
|
}
|
239
|
}
|
240
|
}
|
241
|
return code;
|
242
|
}
|
243
|
|
244
|
// ************************** GETTER / SETTER ******************************************************
|
245
|
|
246
|
public boolean isIncludeSeconds() {
|
247
|
return includeEmptySeconds;
|
248
|
}
|
249
|
|
250
|
|
251
|
|
252
|
public void setIncludeSeconds(boolean includeSeconds) {
|
253
|
this.includeEmptySeconds = includeSeconds;
|
254
|
}
|
255
|
|
256
|
|
257
|
|
258
|
public void setIncludeReferenceSystem(boolean includeReferenceSystem) {
|
259
|
this.includeReferenceSystem = includeReferenceSystem;
|
260
|
}
|
261
|
|
262
|
|
263
|
|
264
|
public boolean isIncludeReferenceSystem() {
|
265
|
return includeReferenceSystem;
|
266
|
}
|
267
|
|
268
|
|
269
|
}
|