c2310e44d564f66af4d2a0e6aa560ef13de94a90
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / excel / common / ExcelRowBase.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
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.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.reference.Reference;
28
29 /**
30 * Base class for data holder classes for excel or similar imports.
31 *
32 * @author a.mueller
33 * @date 13.07.2011
34 */
35 public abstract class ExcelRowBase {
36 @SuppressWarnings("unused")
37 private static final Logger logger = Logger.getLogger(ExcelRowBase.class);
38
39 private UUID cdmUuid = null;
40
41 private String ecology;
42 private String plantDescription;
43
44
45 // private String family;
46 // private String genus;
47 // private String specificEpithet;
48
49
50 private TreeMap<Integer, IdentifiableSource> sources = new TreeMap<Integer, IdentifiableSource>();
51 private TreeMap<Integer, SpecimenTypeDesignation> types = new TreeMap<Integer, SpecimenTypeDesignation>();
52 private List<PostfixTerm> extensions = new ArrayList<PostfixTerm>();
53
54 //features
55 private Map<UUID, TreeMap<Integer, String>> featureTexts = new HashMap<UUID, TreeMap<Integer, String>>();
56 private Map<UUID, TreeMap<Integer, String>> featureLanguages = new HashMap<UUID, TreeMap<Integer, String>>();
57 //feature sources
58 private Map<UUID, TreeMap<Integer, SourceDataHolder>> textSources = new HashMap<UUID, TreeMap<Integer, SourceDataHolder>>();
59
60
61 public ExcelRowBase() {
62 }
63
64 public class PostfixTerm{
65 public PostfixTerm(){}
66 public String term;
67 public String postfix;
68 }
69
70
71 public class SourceDataHolder{
72 private TreeMap<Integer, Map<SourceType, String>> sources = new TreeMap<Integer, Map<SourceType, String>>();
73
74 public void putSource(int index, SourceType type, String value){
75 Map<SourceType, String> map = sources.get(index);
76 if (map == null){
77 map = new HashMap<SourceType, String>();
78 sources.put(index, map);
79 }
80 map.put(type, value);
81 }
82
83 public List<Map<SourceType, String>> getSources() {
84 return getOrdered(sources);
85 }
86 }
87
88
89
90 // **************************** GETTER / SETTER *********************************/
91
92
93 public void setCdmUuid(UUID cdmUuid) {
94 this.cdmUuid = cdmUuid;
95 }
96
97
98 public UUID getCdmUuid() {
99 return cdmUuid;
100 }
101
102 //
103 // /**
104 // * @return the author
105 // */
106 // public String getAuthor() {
107 // return author;
108 // }
109 //
110 //
111 // /**
112 // * @param author the author to set
113 // */
114 // public void setAuthor(String author) {
115 // this.author = author;
116 // }
117
118
119
120 /**
121 * @return the ecology
122 */
123 public String getEcology() {
124 return ecology;
125 }
126
127
128 /**
129 * @param ecology the ecology to set
130 */
131 public void setEcology(String ecology) {
132 this.ecology = ecology;
133 }
134
135
136 /**
137 * @return the plantDescription
138 */
139 public String getPlantDescription() {
140 return plantDescription;
141 }
142
143
144 /**
145 * @param plantDescription the plantDescription to set
146 */
147 public void setPlantDescription(String plantDescription) {
148 this.plantDescription = plantDescription;
149 }
150
151 public void putIdInSource(int key, String id){
152 IdentifiableSource source = getOrMakeSource(key);
153 source.setIdInSource(id);
154 }
155 public void putSourceReference(int key, Reference<?> reference){
156 IdentifiableSource source = getOrMakeSource(key);
157 source.setCitation(reference);
158 }
159
160 public List<IdentifiableSource> getSources() {
161 return getOrdered(sources);
162 }
163
164
165 /**
166 * @param key
167 * @return
168 */
169 private IdentifiableSource getOrMakeSource(int key) {
170 IdentifiableSource source = sources.get(key);
171 if (source == null){
172 source = IdentifiableSource.NewInstance();
173 sources.put(key, source);
174 }
175 return source;
176 }
177
178
179 public void putTypeCategory(int key, SpecimenTypeDesignationStatus status){
180 SpecimenTypeDesignation designation = getOrMakeTypeDesignation(key);
181 designation.setTypeStatus(status);
182 }
183 public void putTypifiedName(int key, TaxonNameBase<?,?> name){
184 if (name != null){
185 SpecimenTypeDesignation designation = getOrMakeTypeDesignation(key);
186 name.addTypeDesignation(designation, false);
187 }
188 }
189
190 public List<SpecimenTypeDesignation> getTypeDesignations() {
191 return getOrdered(types);
192 }
193
194
195 private SpecimenTypeDesignation getOrMakeTypeDesignation(int key) {
196 SpecimenTypeDesignation designation = types.get(key);
197 if (designation == null){
198 designation = SpecimenTypeDesignation.NewInstance();
199 types.put(key, designation);
200 }
201 return designation;
202 }
203
204 private<T extends Object> List<T> getOrdered(TreeMap<?, T> tree) {
205 List<T> result = new ArrayList<T>();
206 for (T value : tree.values()){
207 result.add(value);
208 }
209 return result;
210 }
211
212 public void addExtension(String levelPostfix, String value) {
213 PostfixTerm term = new PostfixTerm();
214 term.term = value;
215 term.postfix = levelPostfix;
216 this.extensions.add(term);
217 }
218
219 public List<PostfixTerm> getExtensions(){
220 return extensions;
221 }
222
223 //***************** FEATURES ***************************************************/
224
225 public void putFeature(UUID featureUuid, int index, String value) {
226 TreeMap<Integer, String> featureMap = featureTexts.get(featureUuid);
227 if (featureMap == null){
228 featureMap = new TreeMap<Integer, String>();
229 featureTexts.put(featureUuid, featureMap);
230 }
231 featureMap.put(index, value);
232 }
233
234 public void putFeatureLanguage(UUID featureUuid, int index, String value) {
235 TreeMap<Integer, String> featureLanguageMap = featureLanguages.get(featureUuid);
236 if (featureLanguageMap == null){
237 featureLanguageMap = new TreeMap<Integer, String>();
238 featureLanguages.put(featureUuid, featureLanguageMap);
239 }
240 featureLanguageMap.put(index, value);
241 }
242
243 public Set<UUID> getFeatures() {
244 return featureTexts.keySet();
245 }
246
247 public List<String> getFeatureTexts(UUID featureUuid) {
248 TreeMap<Integer, String> map = featureTexts.get(featureUuid);
249 if (map != null){
250 return getOrdered(map);
251 }else{
252 return null;
253 }
254 }
255
256 public List<String> getFeatureLanguages(UUID featureUuid) {
257 TreeMap<Integer, String> map = featureLanguages.get(featureUuid);
258 if (map != null){
259 return getOrdered(map);
260 }else{
261 return null;
262 }
263 }
264
265
266 public void putFeatureSource(UUID featureUuid, int featureIndex, SourceType refType, String value, int refIndex) {
267 //feature Map
268 TreeMap<Integer, SourceDataHolder> featureMap = textSources.get(featureUuid);
269 if (featureMap == null){
270 featureMap = new TreeMap<Integer, SourceDataHolder>();
271 textSources.put(featureUuid, featureMap);
272 }
273 //sourcedText
274 SourceDataHolder sourceDataHolder = featureMap.get(featureIndex);
275 if (sourceDataHolder == null){
276 sourceDataHolder = new SourceDataHolder();
277 featureMap.put(featureIndex, sourceDataHolder);
278 }
279 //
280 sourceDataHolder.putSource(refIndex, refType, value);
281 }
282
283
284 public SourceDataHolder getFeatureTextReferences(UUID featureUuid, int index) {
285 TreeMap<Integer, SourceDataHolder> textMap = textSources.get(featureUuid);
286 if (textMap == null){
287 return new SourceDataHolder();
288 }else{
289 SourceDataHolder sourceMap = textMap.get(index);
290 return sourceMap;
291 }
292
293 }
294
295
296
297 }