Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
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.api.facade;
10

    
11
import java.util.UUID;
12

    
13
import org.apache.commons.lang.StringUtils;
14
import org.apache.log4j.Logger;
15

    
16
import eu.etaxonomy.cdm.common.CdmUtils;
17
import eu.etaxonomy.cdm.model.agent.Institution;
18
import eu.etaxonomy.cdm.model.occurrence.Collection;
19
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
20
import eu.etaxonomy.cdm.strategy.StrategyBase;
21
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 03.06.2010
26
 */
27
public class DerivedUnitFacadeCacheStrategy
28
        extends StrategyBase implements
29
        IIdentifiableEntityCacheStrategy<DerivedUnit> {
30

    
31
    private static final long serialVersionUID = 1578628591216605619L;
32
	@SuppressWarnings("unused")
33
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeCacheStrategy.class);
34

    
35
	private static final UUID uuid = UUID.fromString("df4672c1-ce5c-4724-af6d-91e2b326d4a4");
36

    
37
	@Override
38
	protected UUID getUuid() {return uuid;}
39

    
40
	private boolean includeEmptySeconds = false;
41
	private boolean includeReferenceSystem = true;
42

    
43
	@Override
44
    public String getTitleCache(DerivedUnit derivedUnit) {
45
	    return getTitleCache(derivedUnit, false, true);
46
	}
47

    
48
	public String getTitleCache(DerivedUnit derivedUnit, boolean skipFieldUnit, boolean addTrailingDot) {
49

    
50
	    String result = "";
51
	    DerivedUnitFacadeFieldUnitCacheStrategy fieldStrategy = new DerivedUnitFacadeFieldUnitCacheStrategy();
52

    
53
		DerivedUnitFacade facade;
54
		// NOTE: regarding the string representations of MediaTypes, see https://dev.e-taxonomy.eu/redmine/issues/7608
55
		try {
56
			DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
57
			config.setFirePropertyChangeEvents(false);
58
			facade = DerivedUnitFacade.NewInstance(derivedUnit, config);
59

    
60
	        if(!skipFieldUnit){
61
	            result += fieldStrategy.getFieldData(facade);
62
	        }
63

    
64
			//exsiccatum
65
			String exsiccatum = null;
66
			try {
67
				exsiccatum = facade.getExsiccatum();
68
			} catch (MethodNotSupportedByDerivedUnitTypeException e) {
69
				//NO exsiccatum if this facade doe not represent a specimen
70
			}
71
			result = CdmUtils.concat("; ", result, exsiccatum);
72

    
73
			//Herbarium & identifier
74
			String barcode = getSpecimenLabel(facade);
75
	        if (StringUtils.isNotBlank(barcode)) {
76
	            result = (result + " (" +  barcode + ")").trim();
77
	        }
78
			//result
79
			result = fieldStrategy.addPlantDescription(result, facade);
80

    
81
			if (addTrailingDot){
82
			    result = CdmUtils.addTrailingDotIfNotExists(result);
83
			}
84

    
85
		} catch (DerivedUnitFacadeNotSupportedException e) {
86
			e.printStackTrace();
87
		}
88

    
89
		return result;
90
	}
91

    
92
    /**
93
     * Produces the collection barcode which is the combination of the collection code and
94
     * accession number.
95
     *
96
     * @param result
97
     * @param facade
98
     * @return
99
     */
100
    public String getSpecimenLabel(DerivedUnitFacade facade) {
101
        String code = getCode(facade);
102
        String identifier = getUnitNumber(facade /*, code*/);
103
        String collectionData = CdmUtils.concat(" ", code, identifier);
104
        return collectionData;
105
    }
106

    
107

    
108
    /**
109
     * Computes the unit number which might be an accession number, barcode, catalogue number, ...
110
     * In future if the unit number starts with the same string as the barcode
111
     * it might be replaced.
112
     * @param facade the derived unit facade
113
     */
114
    private String getUnitNumber(DerivedUnitFacade facade) {
115
        String result;
116
        if (isNotBlank(facade.getAccessionNumber())){
117
            result = facade.getAccessionNumber();
118
        }else if (isNotBlank(facade.getBarcode())){
119
            result = facade.getBarcode();
120
        }else{
121
            result = facade.getCatalogNumber();
122
        }
123
        String code = getCode(facade);
124
        if(result != null){
125
            result = result.trim();
126
            if(isNotBlank(code) && result.startsWith(code + " ")){
127
                result = result.replaceAll("^" + code + "\\s", "");
128
            }
129
        }
130
        return result;
131
    }
132

    
133

    
134
	/**
135
	 * @param facade
136
	 */
137
	private String getCode(DerivedUnitFacade facade) {
138
		String code = "";
139
		if(facade.getCollection() != null){
140
			code = facade.getCollection().getCode();
141
			if (StringUtils.isBlank(code)){
142
			    code = facade.getCollection().getName();
143
			}
144
			if (StringUtils.isBlank(code)){
145
				Institution institution = facade.getCollection().getInstitute();
146
				if (institution != null){
147
					code = institution.getCode();
148
				}
149
				if (StringUtils.isBlank(code)){
150
					Collection superCollection = facade.getCollection().getSuperCollection();
151
					if (superCollection != null){
152
						code = superCollection.getCode();
153
					}
154
				}
155
			}
156
		}
157
		return code;
158
	}
159

    
160
// ************************** GETTER / SETTER ******************************************************
161

    
162
	public boolean isIncludeSeconds() {
163
		return includeEmptySeconds;
164
	}
165

    
166
	public void setIncludeSeconds(boolean includeSeconds) {
167
		this.includeEmptySeconds = includeSeconds;
168
	}
169

    
170
	public void setIncludeReferenceSystem(boolean includeReferenceSystem) {
171
		this.includeReferenceSystem = includeReferenceSystem;
172
	}
173

    
174
	public boolean isIncludeReferenceSystem() {
175
		return includeReferenceSystem;
176
	}
177

    
178
}
(2-2/6)