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
.facade
;
11 import java
.util
.UUID
;
12 import java
.util
.stream
.Collectors
;
14 import org
.apache
.commons
.lang3
.StringUtils
;
15 import org
.apache
.logging
.log4j
.LogManager
;
16 import org
.apache
.logging
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
19 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
20 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
21 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
22 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
23 import eu
.etaxonomy
.cdm
.strategy
.StrategyBase
;
24 import eu
.etaxonomy
.cdm
.strategy
.cache
.common
.IIdentifiableEntityCacheStrategy
;
30 * @deprecated with #9678 a similar cache strategy (DerivedUnitDefaultCacheStrategy)
31 * was implemented in cdmlib-model. This class may be removed in future.
34 public class DerivedUnitFacadeCacheStrategy
36 implements IIdentifiableEntityCacheStrategy
<DerivedUnit
> {
38 private static final long serialVersionUID
= 1578628591216605619L;
39 @SuppressWarnings("unused")
40 private static final Logger logger
= LogManager
.getLogger();
42 private static final UUID uuid
= UUID
.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
45 protected UUID
getUuid() {return uuid
;}
47 private boolean includeEmptySeconds
= false;
48 private boolean includeReferenceSystem
= true;
50 private String collectionAccessionSeperator
= ": ";
52 private boolean addTrailingDot
= false;
54 public static DerivedUnitFacadeCacheStrategy
NewInstance(){
55 return new DerivedUnitFacadeCacheStrategy();
58 public static DerivedUnitFacadeCacheStrategy
NewInstance(boolean addTrailingDot
){
59 return new DerivedUnitFacadeCacheStrategy(addTrailingDot
, null);
62 public static DerivedUnitFacadeCacheStrategy
NewInstance( boolean addTrailingDot
, String collectionAccessionSeperator
){
63 return new DerivedUnitFacadeCacheStrategy(addTrailingDot
, collectionAccessionSeperator
);
66 //******************************* CONSTRUCTOR *******************************************/
68 //default value constructor
69 private DerivedUnitFacadeCacheStrategy() {}
71 private DerivedUnitFacadeCacheStrategy(boolean addTrailingDot
, String collectionAccessionSeperator
) {
72 this.addTrailingDot
= addTrailingDot
;
73 if (collectionAccessionSeperator
!= null){
74 this.collectionAccessionSeperator
= collectionAccessionSeperator
;
79 public String
getTitleCache(DerivedUnit derivedUnit
) {
80 return getTitleCache(derivedUnit
, false);
83 public String
getTitleCache(DerivedUnit derivedUnit
, boolean skipFieldUnit
) {
86 DerivedUnitFacadeFieldUnitCacheStrategy fieldStrategy
= new DerivedUnitFacadeFieldUnitCacheStrategy();
88 DerivedUnitFacade facade
;
89 // NOTE: regarding the string representations of MediaTypes, see https://dev.e-taxonomy.eu/redmine/issues/7608
91 DerivedUnitFacadeConfigurator config
= DerivedUnitFacadeConfigurator
.NewInstance();
92 config
.setFirePropertyChangeEvents(false);
93 facade
= DerivedUnitFacade
.NewInstance(derivedUnit
, config
);
96 result
+= fieldStrategy
.getFieldData(facade
);
100 String exsiccatum
= null;
102 exsiccatum
= facade
.getExsiccatum();
103 } catch (MethodNotSupportedByDerivedUnitTypeException e
) {
104 //NO exsiccatum if this facade doe not represent a specimen
106 result
= CdmUtils
.concat("; ", result
, exsiccatum
);
108 //Herbarium & identifier
109 String barcode
= getSpecimenLabel(facade
);
110 if (StringUtils
.isNotBlank(barcode
)) {
111 result
= (result
+ " (" + barcode
+ ")").trim();
114 result
= fieldStrategy
.addPlantDescription(result
, facade
);
117 result
= CdmUtils
.addTrailingDotIfNotExists(result
);
120 } catch (DerivedUnitFacadeNotSupportedException e
) {
128 * Produces the collection barcode which is the combination of the collection code and
135 public String
getSpecimenLabel(DerivedUnitFacade facade
) {
136 String code
= getCode(facade
);
137 String identifier
= getUnitNumber(facade
/*, code*/);
138 String collectionData
= CdmUtils
.concat(collectionAccessionSeperator
, code
, identifier
);
139 String specimenStatusStr
= getSpecimenStatusStr(facade
.innerDerivedUnit());
140 collectionData
= CdmUtils
.concat(", " , collectionData
, specimenStatusStr
);
141 return collectionData
;
146 * Computes the unit number which might be an accession number, barcode, catalogue number, ...
147 * In future if the unit number starts with the same string as the barcode
148 * it might be replaced.
149 * @param facade the derived unit facade
151 private String
getUnitNumber(DerivedUnitFacade facade
) {
154 if (isNotBlank(facade
.getAccessionNumber())){
155 result
= facade
.getAccessionNumber();
156 }else if (isNotBlank(facade
.getBarcode())){
157 result
= facade
.getBarcode();
159 result
= facade
.getCatalogNumber();
162 result
= result
.trim();
163 //#10379 do not deduplicate anymore
164 // String code = getCode(facade);
165 // if(isNotBlank(code) && result.startsWith(code + " ")){
166 // result = result.replaceAll("^" + code + "\\s", "");
172 private String
getCode(DerivedUnitFacade facade
) {
174 if(facade
.getCollection() != null){
175 code
= facade
.getCollection().getCode();
176 if (StringUtils
.isBlank(code
)){
177 code
= facade
.getCollection().getName();
179 if (StringUtils
.isBlank(code
)){
180 Institution institution
= facade
.getCollection().getInstitute();
181 if (institution
!= null){
182 code
= institution
.getCode();
184 if (StringUtils
.isBlank(code
)){
185 Collection superCollection
= facade
.getCollection().getSuperCollection();
186 if (superCollection
!= null){
187 code
= superCollection
.getCode();
194 //copied from DerivedUnitDefaultCacheStrategy
195 private String
getSpecimenStatusStr(DerivedUnit specimen
) {
196 String result
= null;
197 if (!specimen
.getStatus().isEmpty()){
198 result
= specimen
.getStatus()
201 .filter(t
->t
!= null)
202 .map(t
->t
.getPreferredRepresentation(Language
.DEFAULT()).getLabel())
203 .sorted((s1
,s2
)->s1
.compareTo(s2
))
204 .collect(Collectors
.joining(", "));
209 // ************************** GETTER / SETTER ******************************************************
211 public boolean isIncludeSeconds() {
212 return includeEmptySeconds
;
215 public void setIncludeSeconds(boolean includeSeconds
) {
216 this.includeEmptySeconds
= includeSeconds
;
219 public void setIncludeReferenceSystem(boolean includeReferenceSystem
) {
220 this.includeReferenceSystem
= includeReferenceSystem
;
223 public boolean isIncludeReferenceSystem() {
224 return includeReferenceSystem
;
227 public boolean isAddTrailingDot() {
228 return addTrailingDot
;
231 public void setAddTrailingDot(boolean addTrailingDot
) {
232 this.addTrailingDot
= addTrailingDot
;