cleanup
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / facade / DerivedUnitFacadeCacheStrategy.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.facade;
10
11 import java.util.UUID;
12 import java.util.stream.Collectors;
13
14 import org.apache.commons.lang3.StringUtils;
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17
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;
25
26 /**
27 * @author a.mueller
28 * @since 03.06.2010
29 *
30 * @deprecated with #9678 a similar cache strategy (DerivedUnitDefaultCacheStrategy)
31 * was implemented in cdmlib-model. This class may be removed in future.
32 */
33 @Deprecated
34 public class DerivedUnitFacadeCacheStrategy
35 extends StrategyBase
36 implements IIdentifiableEntityCacheStrategy<DerivedUnit> {
37
38 private static final long serialVersionUID = 1578628591216605619L;
39 @SuppressWarnings("unused")
40 private static final Logger logger = LogManager.getLogger();
41
42 private static final UUID uuid = UUID.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
43
44 @Override
45 protected UUID getUuid() {return uuid;}
46
47 private boolean includeEmptySeconds = false;
48 private boolean includeReferenceSystem = true;
49
50 private String collectionAccessionSeperator = ": ";
51
52 private boolean addTrailingDot = false;
53
54 public static DerivedUnitFacadeCacheStrategy NewInstance(){
55 return new DerivedUnitFacadeCacheStrategy();
56 }
57
58 public static DerivedUnitFacadeCacheStrategy NewInstance(boolean addTrailingDot){
59 return new DerivedUnitFacadeCacheStrategy(addTrailingDot, null);
60 }
61
62 public static DerivedUnitFacadeCacheStrategy NewInstance( boolean addTrailingDot, String collectionAccessionSeperator){
63 return new DerivedUnitFacadeCacheStrategy(addTrailingDot, collectionAccessionSeperator);
64 }
65
66 //******************************* CONSTRUCTOR *******************************************/
67
68 //default value constructor
69 private DerivedUnitFacadeCacheStrategy() {}
70
71 private DerivedUnitFacadeCacheStrategy(boolean addTrailingDot, String collectionAccessionSeperator) {
72 this.addTrailingDot = addTrailingDot;
73 if (collectionAccessionSeperator != null){
74 this.collectionAccessionSeperator = collectionAccessionSeperator;
75 }
76 }
77
78 @Override
79 public String getTitleCache(DerivedUnit derivedUnit) {
80 return getTitleCache(derivedUnit, false);
81 }
82
83 public String getTitleCache(DerivedUnit derivedUnit, boolean skipFieldUnit) {
84
85 String result = "";
86 DerivedUnitFacadeFieldUnitCacheStrategy fieldStrategy = new DerivedUnitFacadeFieldUnitCacheStrategy();
87
88 DerivedUnitFacade facade;
89 // NOTE: regarding the string representations of MediaTypes, see https://dev.e-taxonomy.eu/redmine/issues/7608
90 try {
91 DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
92 config.setFirePropertyChangeEvents(false);
93 facade = DerivedUnitFacade.NewInstance(derivedUnit, config);
94
95 if(!skipFieldUnit){
96 result += fieldStrategy.getFieldData(facade);
97 }
98
99 //exsiccatum
100 String exsiccatum = null;
101 try {
102 exsiccatum = facade.getExsiccatum();
103 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
104 //NO exsiccatum if this facade doe not represent a specimen
105 }
106 result = CdmUtils.concat("; ", result, exsiccatum);
107
108 //Herbarium & identifier
109 String barcode = getSpecimenLabel(facade);
110 if (StringUtils.isNotBlank(barcode)) {
111 result = (result + " (" + barcode + ")").trim();
112 }
113 //result
114 result = fieldStrategy.addPlantDescription(result, facade);
115
116 if (addTrailingDot){
117 result = CdmUtils.addTrailingDotIfNotExists(result);
118 }
119
120 } catch (DerivedUnitFacadeNotSupportedException e) {
121 e.printStackTrace();
122 }
123
124 return result;
125 }
126
127 /**
128 * Produces the collection barcode which is the combination of the collection code and
129 * accession number.
130 *
131 * @param result
132 * @param facade
133 * @return
134 */
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;
142 }
143
144
145 /**
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
150 */
151 private String getUnitNumber(DerivedUnitFacade facade) {
152 String result;
153
154 if (isNotBlank(facade.getAccessionNumber())){
155 result = facade.getAccessionNumber();
156 }else if (isNotBlank(facade.getBarcode())){
157 result = facade.getBarcode();
158 }else{
159 result = facade.getCatalogNumber();
160 }
161 if(result != null){
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", "");
167 // }
168 }
169 return result;
170 }
171
172 private String getCode(DerivedUnitFacade facade) {
173 String code = "";
174 if(facade.getCollection() != null){
175 code = facade.getCollection().getCode();
176 if (StringUtils.isBlank(code)){
177 code = facade.getCollection().getName();
178 }
179 if (StringUtils.isBlank(code)){
180 Institution institution = facade.getCollection().getInstitute();
181 if (institution != null){
182 code = institution.getCode();
183 }
184 if (StringUtils.isBlank(code)){
185 Collection superCollection = facade.getCollection().getSuperCollection();
186 if (superCollection != null){
187 code = superCollection.getCode();
188 }
189 }
190 }
191 }
192 return code;
193 }
194 //copied from DerivedUnitDefaultCacheStrategy
195 private String getSpecimenStatusStr(DerivedUnit specimen) {
196 String result = null;
197 if (!specimen.getStatus().isEmpty()){
198 result = specimen.getStatus()
199 .stream()
200 .map(s->s.getType())
201 .filter(t->t != null)
202 .map(t->t.getPreferredRepresentation(Language.DEFAULT()).getLabel())
203 .sorted((s1,s2)->s1.compareTo(s2))
204 .collect(Collectors.joining(", "));
205 }
206 return result;
207 }
208
209 // ************************** GETTER / SETTER ******************************************************
210
211 public boolean isIncludeSeconds() {
212 return includeEmptySeconds;
213 }
214
215 public void setIncludeSeconds(boolean includeSeconds) {
216 this.includeEmptySeconds = includeSeconds;
217 }
218
219 public void setIncludeReferenceSystem(boolean includeReferenceSystem) {
220 this.includeReferenceSystem = includeReferenceSystem;
221 }
222
223 public boolean isIncludeReferenceSystem() {
224 return includeReferenceSystem;
225 }
226
227 public boolean isAddTrailingDot() {
228 return addTrailingDot;
229 }
230
231 public void setAddTrailingDot(boolean addTrailingDot) {
232 this.addTrailingDot = addTrailingDot;
233 }
234
235 }