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