Project

General

Profile

Download (7.72 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

    
10
package eu.etaxonomy.cdm.io.excel.common;
11

    
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.TreeMap;
18
import java.util.UUID;
19

    
20
import org.apache.log4j.Logger;
21

    
22
import eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase.SourceType;
23
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
24
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
25
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29

    
30
/**
31
 * Base class for data holder classes for excel or similar imports.
32
 *
33
 * @author a.mueller
34
 * @since 13.07.2011
35
 */
36
public abstract class ExcelRowBase {
37
	@SuppressWarnings("unused")
38
	private static final Logger logger = Logger.getLogger(ExcelRowBase.class);
39

    
40
	private UUID cdmUuid = null;
41

    
42
	private String ecology;
43
	private String plantDescription;
44

    
45

    
46
//	private String family;
47
//	private String genus;
48
//	private String specificEpithet;
49

    
50

    
51
	private TreeMap<Integer, IdentifiableSource> sources = new TreeMap<Integer, IdentifiableSource>();
52
	private TreeMap<Integer, SpecimenTypeDesignation> types = new TreeMap<Integer, SpecimenTypeDesignation>();
53
	private List<PostfixTerm> extensions  = new ArrayList<PostfixTerm>();
54

    
55
	//features
56
	private Map<UUID, TreeMap<Integer, String>> featureTexts = new HashMap<UUID, TreeMap<Integer, String>>();
57
	private Map<UUID, TreeMap<Integer, String>> featureLanguages = new HashMap<UUID, TreeMap<Integer, String>>();
58
	//feature sources
59
	private Map<UUID, TreeMap<Integer, SourceDataHolder>> textSources = new HashMap<UUID, TreeMap<Integer, SourceDataHolder>>();
60

    
61

    
62
	public ExcelRowBase() {
63
	}
64

    
65
	public class PostfixTerm{
66
		public PostfixTerm(){}
67
		public String term;
68
		public String postfix;
69
	}
70

    
71

    
72
	public class SourceDataHolder{
73
		private TreeMap<Integer, Map<SourceType, String>> sources = new TreeMap<Integer, Map<SourceType, String>>();
74

    
75
		public void putSource(int index, SourceType type, String value){
76
			Map<SourceType, String> map = sources.get(index);
77
			if (map == null){
78
				map = new HashMap<SourceType, String>();
79
				sources.put(index, map);
80
			}
81
			map.put(type, value);
82
		}
83

    
84
		public List<Map<SourceType, String>> getSources() {
85
			return getOrdered(sources);
86
		}
87
	}
88

    
89

    
90

    
91
// **************************** GETTER / SETTER *********************************/
92

    
93

    
94
	public void setCdmUuid(UUID cdmUuid) {
95
		this.cdmUuid = cdmUuid;
96
	}
97

    
98

    
99
	public UUID getCdmUuid() {
100
		return cdmUuid;
101
	}
102

    
103
//
104
//	/**
105
//	 * @return the author
106
//	 */
107
//	public String getAuthor() {
108
//		return author;
109
//	}
110
//
111
//
112
//	/**
113
//	 * @param author the author to set
114
//	 */
115
//	public void setAuthor(String author) {
116
//		this.author = author;
117
//	}
118

    
119

    
120

    
121
	/**
122
	 * @return the ecology
123
	 */
124
	public String getEcology() {
125
		return ecology;
126
	}
127

    
128

    
129
	/**
130
	 * @param ecology the ecology to set
131
	 */
132
	public void setEcology(String ecology) {
133
		this.ecology = ecology;
134
	}
135

    
136

    
137
	/**
138
	 * @return the plantDescription
139
	 */
140
	public String getPlantDescription() {
141
		return plantDescription;
142
	}
143

    
144

    
145
	/**
146
	 * @param plantDescription the plantDescription to set
147
	 */
148
	public void setPlantDescription(String plantDescription) {
149
		this.plantDescription = plantDescription;
150
	}
151

    
152
	public void putIdInSource(int key, String id){
153
		IdentifiableSource source = getOrMakeSource(key);
154
		source.setIdInSource(id);
155
	}
156
	public void putSourceReference(int key, Reference reference){
157
		IdentifiableSource source = getOrMakeSource(key);
158
		source.setCitation(reference);
159
	}
160

    
161
	public List<IdentifiableSource> getSources() {
162
		return getOrdered(sources);
163
	}
164

    
165

    
166
	/**
167
	 * @param key
168
	 * @return
169
	 */
170
	private IdentifiableSource getOrMakeSource(int key) {
171
		IdentifiableSource  source = sources.get(key);
172
		if (source == null){
173
			source = IdentifiableSource.NewInstance(OriginalSourceType.Unknown);
174
			sources.put(key, source);
175
		}
176
		return source;
177
	}
178

    
179

    
180
	public void putTypeCategory(int key, SpecimenTypeDesignationStatus status){
181
		SpecimenTypeDesignation designation = getOrMakeTypeDesignation(key);
182
		designation.setTypeStatus(status);
183
	}
184
	public void putTypifiedName(int key, TaxonName name){
185
		if (name != null){
186
			SpecimenTypeDesignation designation = getOrMakeTypeDesignation(key);
187
			name.addTypeDesignation(designation, false);
188
		}
189
	}
190

    
191
	public List<SpecimenTypeDesignation> getTypeDesignations() {
192
		return getOrdered(types);
193
	}
194

    
195

    
196
	private SpecimenTypeDesignation getOrMakeTypeDesignation(int key) {
197
		SpecimenTypeDesignation designation = types.get(key);
198
		if (designation == null){
199
			designation = SpecimenTypeDesignation.NewInstance();
200
			types.put(key, designation);
201
		}
202
		return designation;
203
	}
204

    
205
	private<T extends Object> List<T> getOrdered(TreeMap<?, T> tree) {
206
		List<T> result = new ArrayList<T>();
207
		for (T value : tree.values()){
208
			result.add(value);
209
		}
210
		return result;
211
	}
212

    
213
	public void addExtension(String levelPostfix, String value) {
214
		PostfixTerm term = new PostfixTerm();
215
		term.term = value;
216
		term.postfix = levelPostfix;
217
		this.extensions.add(term);
218
	}
219

    
220
	public List<PostfixTerm> getExtensions(){
221
		return extensions;
222
	}
223

    
224
//***************** FEATURES ***************************************************/
225

    
226
	public void putFeature(UUID featureUuid, int index, String value) {
227
		TreeMap<Integer, String> featureMap = featureTexts.get(featureUuid);
228
		if (featureMap == null){
229
			featureMap = new TreeMap<Integer, String>();
230
			featureTexts.put(featureUuid, featureMap);
231
		}
232
		featureMap.put(index, value);
233
	}
234

    
235
	public void putFeatureLanguage(UUID featureUuid, int index, String value) {
236
		TreeMap<Integer, String> featureLanguageMap = featureLanguages.get(featureUuid);
237
		if (featureLanguageMap == null){
238
			featureLanguageMap = new TreeMap<Integer, String>();
239
			featureLanguages.put(featureUuid, featureLanguageMap);
240
		}
241
		featureLanguageMap.put(index, value);
242
	}
243

    
244
	public Set<UUID> getFeatures() {
245
		return featureTexts.keySet();
246
	}
247

    
248
	public List<String> getFeatureTexts(UUID featureUuid) {
249
		TreeMap<Integer, String> map = featureTexts.get(featureUuid);
250
		if (map != null){
251
			return getOrdered(map);
252
		}else{
253
			return null;
254
		}
255
	}
256

    
257
	public List<String> getFeatureLanguages(UUID featureUuid) {
258
		TreeMap<Integer, String> map = featureLanguages.get(featureUuid);
259
		if (map != null){
260
			return getOrdered(map);
261
		}else{
262
			return null;
263
		}
264
	}
265

    
266

    
267
	public void putFeatureSource(UUID featureUuid,	int featureIndex, SourceType refType, String value, int refIndex) {
268
		//feature Map
269
		TreeMap<Integer, SourceDataHolder> featureMap = textSources.get(featureUuid);
270
		if (featureMap == null){
271
			featureMap = new TreeMap<Integer, SourceDataHolder>();
272
			textSources.put(featureUuid, featureMap);
273
		}
274
		//sourcedText
275
		SourceDataHolder sourceDataHolder = featureMap.get(featureIndex);
276
		if (sourceDataHolder == null){
277
			sourceDataHolder = new SourceDataHolder();
278
			featureMap.put(featureIndex, sourceDataHolder);
279
		}
280
		//
281
		sourceDataHolder.putSource(refIndex, refType, value);
282
	}
283

    
284

    
285
	public SourceDataHolder getFeatureTextReferences(UUID featureUuid, int index) {
286
		TreeMap<Integer, SourceDataHolder> textMap = textSources.get(featureUuid);
287
		if (textMap == null){
288
			return new SourceDataHolder();
289
		}else{
290
			SourceDataHolder sourceMap = textMap.get(index);
291
			return sourceMap;
292
		}
293

    
294
	}
295

    
296

    
297

    
298
}
(4-4/5)