3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.facade
;
12 import java
.util
.UUID
;
14 import org
.apache
.commons
.lang
.StringUtils
;
15 import org
.apache
.log4j
.Logger
;
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
;
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);
42 private static final UUID uuid
= UUID
.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
45 * @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
48 protected UUID
getUuid() {
52 private boolean includeEmptySeconds
= false;
53 private boolean includeReferenceSystem
= true;
57 * @see eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.common.IdentifiableEntity)
60 public String
getTitleCache(FieldObservation fieldObservation
) {
61 DerivedUnitFacade facade
;
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
);
73 protected String
getFieldData(DerivedUnitFacade facade
) {
74 String ALTITUDE_PREFIX
= "alt. ";
75 String ALTITUDE_POSTFIX
= " m";
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
);
90 result
= CdmUtils
.concat(", ", result
, facade
.getLocalityText());
93 if (facade
.getAbsoluteElevation() != null){
94 result
= CdmUtils
.concat(", " , result
, ALTITUDE_PREFIX
);
95 result
+= facade
.getAbsoluteElevation() + ALTITUDE_POSTFIX
;
99 if (facade
.getExactLocation() != null){
100 String exactLocation
= facade
.getExactLocation().toSexagesimalString(this.includeEmptySeconds
, this.includeReferenceSystem
);
101 result
= CdmUtils
.concat(", ", result
, exactLocation
);
105 result
= CdmUtils
.concat(", ", result
, facade
.getEcology());
108 //TODO period.toString ??
109 TimePeriod gatheringPeriod
= facade
.getGatheringPeriod();
110 result
= CdmUtils
.concat(", ", result
, (gatheringPeriod
== null?
null : gatheringPeriod
.toString()));
112 //collector (team) and field number
113 String collectorAndFieldNumber
= getCollectorAndFieldNumber(facade
);
114 result
= CdmUtils
.concat(", ", result
, collectorAndFieldNumber
);
121 protected String
addPlantDescription(String result
, DerivedUnitFacade facade
) {
124 result
= CdmUtils
.concat("; ", result
, facade
.getPlantDescription());
125 if (CdmUtils
.isNotEmpty(result
)){
134 private String
getCollectorAndFieldNumber(DerivedUnitFacade facade
) {
136 AgentBase
<?
> collector
= facade
.getCollector();
137 String fieldNumber
= facade
.getFieldNumber();
138 Person primaryCollector
= facade
.getPrimaryCollector();
140 if (collector
== null){
142 }else if(collector
.isProtectedTitleCache()){
143 return CdmUtils
.concat(" ", collector
.getTitleCache(), fieldNumber
);
147 if (collector
.isInstanceOf(Person
.class)){
148 collectorTeam
= Team
.NewInstance();
149 if (primaryCollector
== null){
150 primaryCollector
= CdmBase
.deproxy(collector
, Person
.class);
152 collectorTeam
.addTeamMember(primaryCollector
);
153 } else if (collector
.isInstanceOf(Team
.class) && ! collector
.isProtectedTitleCache() ){
154 collectorTeam
= CdmBase
.deproxy(collector
, Team
.class);
156 return CdmUtils
.concat(" ", collector
.getTitleCache(), fieldNumber
);
160 int teamSize
= collectorTeam
.getTeamMembers().size();
161 boolean fieldNumberAdded
= false;
162 for (Person member
: collectorTeam
.getTeamMembers()){
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;
171 if (! fieldNumberAdded
){
172 result
= addFieldNumber(result
, fieldNumber
);
181 private String
addFieldNumber(String result
, String fieldNumber
) {
182 result
= CdmUtils
.concat(" ", result
, fieldNumber
);
189 * Strategy to format a collector team member name
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
;
201 return member
.getTitleCache();
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
);
214 logger
.warn("Collector is not of type person or team");
224 private String
getCode(DerivedUnitFacade facade
) {
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();
233 if (CdmUtils
.isEmpty(code
)){
234 Collection superCollection
= facade
.getCollection().getSuperCollection();
235 if (superCollection
!= null){
236 code
= superCollection
.getCode();
244 // ************************** GETTER / SETTER ******************************************************
246 public boolean isIncludeSeconds() {
247 return includeEmptySeconds
;
252 public void setIncludeSeconds(boolean includeSeconds
) {
253 this.includeEmptySeconds
= includeSeconds
;
258 public void setIncludeReferenceSystem(boolean includeReferenceSystem
) {
259 this.includeReferenceSystem
= includeReferenceSystem
;
264 public boolean isIncludeReferenceSystem() {
265 return includeReferenceSystem
;