2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.facade
;
11 import java
.util
.List
;
12 import java
.util
.UUID
;
14 import org
.apache
.commons
.lang
.StringUtils
;
15 import org
.apache
.log4j
.Logger
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
19 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
20 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
21 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
25 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
26 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
27 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
28 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
29 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
30 import eu
.etaxonomy
.cdm
.strategy
.StrategyBase
;
31 import eu
.etaxonomy
.cdm
.strategy
.cache
.agent
.TeamDefaultCacheStrategy
;
32 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
39 public class DerivedUnitFacadeFieldUnitCacheStrategy
extends StrategyBase
implements IIdentifiableEntityCacheStrategy
<FieldUnit
> {
40 private static final long serialVersionUID
= 1578628591216605619L;
41 private static final Logger logger
= Logger
.getLogger(DerivedUnitFacadeFieldUnitCacheStrategy
.class);
43 private static final UUID uuid
= UUID
.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
46 protected UUID
getUuid() {
50 private boolean includeEmptySeconds
= false;
51 private boolean includeReferenceSystem
= true;
54 public String
getTitleCache(FieldUnit fieldUnit
) {
55 DerivedUnitFacade facade
;
57 DerivedUnitFacadeConfigurator config
= DerivedUnitFacadeConfigurator
.NewInstance();
58 config
.setFirePropertyChangeEvents(false);
59 facade
= DerivedUnitFacade
.NewInstance(SpecimenOrObservationType
.FieldUnit
, fieldUnit
, config
);
60 result
= getFieldData(facade
);
61 result
= addPlantDescription(result
, facade
);
67 protected String
getFieldData(DerivedUnitFacade facade
) {
68 String ALTITUDE_PREFIX
= "alt. ";
69 // String ALTITUDE_POSTFIX = " m";
75 String strCountry
= null;
76 NamedArea country
= facade
.getCountry();
77 Representation repCountry
= country
== null ?
null : country
.getRepresentation(Language
.DEFAULT());
78 //TODO currently the label is the 3 digit representation of the country and text is the full text.
79 //this is against the common way of handling text, label and labelabbrev in defined terms
80 strCountry
= repCountry
== null ?
null: repCountry
.getText();
81 result
= CdmUtils
.concat(", ", result
, strCountry
);
84 result
= CdmUtils
.concat(", ", result
, facade
.getLocalityText());
87 if (StringUtils
.isNotBlank(facade
.absoluteElevationToString())){
88 result
= CdmUtils
.concat(", " , result
, ALTITUDE_PREFIX
);
89 result
+= facade
.absoluteElevationToString();
93 if (facade
.getExactLocation() != null){
94 String exactLocation
= facade
.getExactLocation().toSexagesimalString(this.includeEmptySeconds
, this.includeReferenceSystem
);
95 result
= CdmUtils
.concat(", ", result
, exactLocation
);
99 result
= CdmUtils
.concat(", ", result
, facade
.getEcology());
102 //TODO period.toString ??
103 TimePeriod gatheringPeriod
= facade
.getGatheringPeriod();
104 result
= CdmUtils
.concat(", ", result
, (gatheringPeriod
== null?
null : gatheringPeriod
.toString()));
106 //collector (team) and field number
107 String collectorAndFieldNumber
= getCollectorAndFieldNumber(facade
);
108 result
= CdmUtils
.concat(", ", result
, collectorAndFieldNumber
);
115 protected String
addPlantDescription(String result
, DerivedUnitFacade facade
) {
118 result
= CdmUtils
.concat("; ", result
, facade
.getPlantDescription());
119 if (CdmUtils
.isNotEmpty(result
) && !result
.endsWith(".")){
128 private String
getCollectorAndFieldNumber(DerivedUnitFacade facade
) {
130 AgentBase
<?
> collector
= facade
.getCollector();
131 String fieldNumber
= facade
.getFieldNumber();
132 Person primaryCollector
= facade
.getPrimaryCollector();
134 if (collector
== null){
136 }else if(collector
.isProtectedTitleCache()){
137 return CdmUtils
.concat(" ", collector
.getTitleCache(), fieldNumber
);
141 if (collector
.isInstanceOf(Person
.class)){
142 collectorTeam
= Team
.NewInstance();
143 if (primaryCollector
== null){
144 primaryCollector
= CdmBase
.deproxy(collector
, Person
.class);
146 collectorTeam
.addTeamMember(primaryCollector
);
147 } else if (collector
.isInstanceOf(Team
.class) && ! collector
.isProtectedTitleCache() ){
148 collectorTeam
= CdmBase
.deproxy(collector
, Team
.class);
150 return CdmUtils
.concat(" ", collector
.getTitleCache(), fieldNumber
);
154 // int teamSize = collectorTeam.getTeamMembers().size();
155 boolean fieldNumberAdded
= false;
156 List
<Person
> teamMembers
= collectorTeam
.getTeamMembers();
157 for (Person member
: teamMembers
){
159 String concatString
= TeamDefaultCacheStrategy
.concatString(collectorTeam
, teamMembers
, counter
);
160 //(counter >= teamSize)? " & " : ", ";
161 result
= CdmUtils
.concat(concatString
, result
, getMemberString(member
) );
162 if (member
.equals(primaryCollector
)){
163 result
= addFieldNumber(result
, fieldNumber
);
164 fieldNumberAdded
= true;
167 if (collectorTeam
.isHasMoreMembers()){
168 result
= TeamDefaultCacheStrategy
.addHasMoreMembers(result
);
170 if (! fieldNumberAdded
){
171 result
= addFieldNumber(result
, fieldNumber
);
178 private String
addFieldNumber(String str
, String fieldNumber
) {
179 String result
= CdmUtils
.concat(" ", str
, fieldNumber
);
186 * Strategy to format a collector team member name
190 private String
getMemberString(Person member
) {
191 if (StringUtils
.isNotBlank(member
.getLastname()) && ! member
.isProtectedTitleCache() ){
192 String result
= member
.getLastname();
193 if (StringUtils
.isNotBlank(member
.getFirstname())){
194 result
= member
.getFirstname().substring(0,1) + ". " + result
;
198 return member
.getTitleCache();
204 private boolean testPrimaryCollectorInCollectorTeam(AgentBase collector
, Person primaryCollector
) {
205 if (collector
.isInstanceOf(Person
.class)){
206 return collector
.equals(primaryCollector
);
207 }else if (collector
.isInstanceOf(Team
.class)){
208 Team collectorTeam
= CdmBase
.deproxy(collector
, Team
.class);
209 return collectorTeam
.getTeamMembers().contains(primaryCollector
);
211 logger
.warn("Collector is not of type person or team");
221 private String
getCode(DerivedUnitFacade facade
) {
223 if(facade
.getCollection() != null){
224 code
= facade
.getCollection().getCode();
225 if (CdmUtils
.isEmpty(code
)){
226 Institution institution
= facade
.getCollection().getInstitute();
227 if (institution
!= null){
228 code
= institution
.getCode();
230 if (CdmUtils
.isEmpty(code
)){
231 Collection superCollection
= facade
.getCollection().getSuperCollection();
232 if (superCollection
!= null){
233 code
= superCollection
.getCode();
241 // ************************** GETTER / SETTER ******************************************************
243 public boolean isIncludeSeconds() {
244 return includeEmptySeconds
;
249 public void setIncludeSeconds(boolean includeSeconds
) {
250 this.includeEmptySeconds
= includeSeconds
;
255 public void setIncludeReferenceSystem(boolean includeReferenceSystem
) {
256 this.includeReferenceSystem
= includeReferenceSystem
;
261 public boolean isIncludeReferenceSystem() {
262 return includeReferenceSystem
;