cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ImportStateBase.java
1 /**
2 * Copyright (C) 2007 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.io.common;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.UUID;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.api.service.IService;
18 import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
19 import eu.etaxonomy.cdm.model.common.AnnotationType;
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21 import eu.etaxonomy.cdm.model.common.ExtensionType;
22 import eu.etaxonomy.cdm.model.common.Language;
23 import eu.etaxonomy.cdm.model.common.MarkerType;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
26 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
27 import eu.etaxonomy.cdm.model.description.State;
28 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
29 import eu.etaxonomy.cdm.model.location.NamedArea;
30 import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
31 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
33 import eu.etaxonomy.cdm.model.name.Rank;
34 import eu.etaxonomy.cdm.model.reference.Reference;
35 import eu.etaxonomy.cdm.model.taxon.Classification;
36 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
37 import eu.etaxonomy.cdm.model.term.DefinedTerm;
38 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
39 import eu.etaxonomy.cdm.model.term.TermVocabulary;
40
41 /**
42 * @author a.mueller
43 * @since 11.05.2009
44 */
45 public abstract class ImportStateBase<CONFIG extends ImportConfiguratorBase, IO extends CdmImportBase>
46 extends IoStateBase<CONFIG, IO, ImportResult> {
47
48 @SuppressWarnings("unused")
49 private static final Logger logger = Logger.getLogger(ImportStateBase.class);
50
51
52 private boolean success = true;
53
54 //States
55 private boolean isCheck;
56
57 private Map<Object,Classification> treeMap = new HashMap<>();
58
59 private Map<Reference,UUID> treeUuidMap = new HashMap<>();
60
61 private Map<String,UUID> classificationKeyUuidMap = new HashMap<>();
62
63 private Map<String, Object> anyStatusItemMap = new HashMap<>();
64
65 private IInputTransformer inputTransformer;
66
67 private Map<UUID, ExtensionType> extensionTypeMap = new HashMap<>();
68 private Map<UUID, MarkerType> markerTypeMap = new HashMap<>();
69 private Map<UUID, AnnotationType> annotationTypeMap = new HashMap<>();
70 private Map<UUID, DefinedTerm> identifierTypeMap = new HashMap<>();
71
72 private Map<UUID, NamedArea> namedAreaMap = new HashMap<>();
73 private Map<UUID, NamedAreaLevel> namedAreaLevelMap = new HashMap<>();
74 private Map<UUID, Feature> featureMap = new HashMap<>();
75 private Map<UUID, State> stateTermMap = new HashMap<>();
76 private Map<UUID, MeasurementUnit> measurementUnitMap = new HashMap<>();
77
78 private Map<UUID, StatisticalMeasure> statisticalMeasureMap = new HashMap<>();
79 private Map<UUID, DefinedTerm> modifierMap = new HashMap<>();
80
81 private Map<UUID, PresenceAbsenceTerm> presenceTermMap = new HashMap<>();
82 private Map<UUID, Language> languageMap = new HashMap<>();
83 private Map<UUID, TaxonRelationshipType> taxonRelationshipTypeMap = new HashMap<>();
84
85 private Map<UUID, ReferenceSystem> referenceSystemMap = new HashMap<>();
86 private Map<UUID, Rank> rankMap = new HashMap<>();
87 private Map<UUID, DefinedTerm> kindOfUnitMap = new HashMap<>();
88
89 private Map<UUID, TermVocabulary<?>> termedVocabularyMap = new HashMap<>();
90
91 private Map<UUID, NomenclaturalStatusType> nomenclaturalStatusTypeMap = new HashMap<>();
92
93 protected IService<CdmBase> service = null;
94
95 protected ImportStateBase(CONFIG config){
96 this.config = config;
97 stores.put(ICdmIO.TEAM_STORE, new MapWrapper<>(service));
98 stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<>(service));
99 stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<>(service));
100 stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<>(service));
101 stores.put(ICdmIO.TAXON_STORE, new MapWrapper<>(service));
102 stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<>(service));
103
104 if (getTransformer() == null){
105 IInputTransformer newTransformer = config.getTransformer();
106 // if (newTransformer == null){
107 // newTransformer = new DefaultTransf();
108 // }
109 setTransformer(newTransformer);
110 }
111
112 }
113
114 /**
115 * Resets (empties) all maps which map a uuid to a {@link DefinedTermBase term}.
116 * This is usually needed when a a new transaction is opened and user defined terms are reused.
117 */
118 public void resetUuidTermMaps(){
119 extensionTypeMap = new HashMap<>();
120 markerTypeMap = new HashMap<>();
121 annotationTypeMap = new HashMap<>();
122
123 namedAreaMap = new HashMap<>();
124 namedAreaLevelMap = new HashMap<>();
125 featureMap = new HashMap<>();
126 stateTermMap = new HashMap<>();
127 measurementUnitMap = new HashMap<>();
128 statisticalMeasureMap = new HashMap<>();
129 modifierMap = new HashMap<>();
130
131 presenceTermMap = new HashMap<>();
132 languageMap = new HashMap<>();
133 taxonRelationshipTypeMap = new HashMap<>();
134
135 referenceSystemMap = new HashMap<>();
136 rankMap = new HashMap<>();
137 nomenclaturalStatusTypeMap = new HashMap<>();
138 }
139
140 //different type of stores that are used by the known imports
141 protected Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>();
142
143 public Map<String, MapWrapper<? extends CdmBase>> getStores() {
144 return stores;
145 }
146 public void setStores(Map<String, MapWrapper<? extends CdmBase>> stores) {
147 this.stores = stores;
148 }
149 public MapWrapper<? extends CdmBase> getStore(String storeLabel){
150 return stores.get(storeLabel);
151 }
152
153 public Classification getTree(Object ref) {
154 return treeMap.get(ref);
155 }
156 public void putTree(Object ref, Classification tree) {
157 if (tree != null){
158 this.treeMap.put(ref, tree);
159 }
160 }
161
162 public int countTrees(){
163 return treeUuidMap.size();
164 }
165
166 public UUID getTreeUuid(Reference ref) {
167 return treeUuidMap.get(ref);
168 }
169
170 public void putTreeUuid(Reference ref, Classification tree) {
171 if (tree != null && tree.getUuid() != null){
172 this.treeUuidMap.put(ref, tree.getUuid());
173 }
174 }
175
176 public int countTreeUuids(){
177 return treeUuidMap.size();
178 }
179
180
181 /**
182 * Adds a classification uuid to the classification uuid map,
183 * which maps a key for the classification to its UUID in the CDM
184 * @param classificationKeyId
185 * @param classification
186 */
187 public void putClassificationUuidInt(int classificationKeyId, Classification classification) {
188 putClassificationUuid(String.valueOf(classificationKeyId), classification);
189 }
190 public void putClassificationUuid(String treeKey, Classification tree) {
191 if (tree != null && tree.getUuid() != null){
192 this.classificationKeyUuidMap.put(treeKey, tree.getUuid());
193 }
194 }
195
196 public UUID getTreeUuidByIntTreeKey(int treeKey) {
197 return classificationKeyUuidMap.get(String.valueOf(treeKey));
198 }
199 public UUID getTreeUuidByTreeKey(String treeKey) {
200 return classificationKeyUuidMap.get(treeKey);
201 }
202
203 public DefinedTerm getIdentifierType(UUID uuid){
204 return identifierTypeMap.get(uuid);
205 }
206 public void putIdentifierType(DefinedTerm identifierType){
207 identifierTypeMap.put(identifierType.getUuid(), identifierType);
208 }
209
210 public ExtensionType getExtensionType(UUID uuid){
211 return extensionTypeMap.get(uuid);
212 }
213 public void putExtensionType(ExtensionType extensionType){
214 extensionTypeMap.put(extensionType.getUuid(), extensionType);
215 }
216
217 public MarkerType getMarkerType(UUID uuid){
218 return markerTypeMap.get(uuid);
219 }
220 public void putMarkerType(MarkerType markerType){
221 markerTypeMap.put(markerType.getUuid(), markerType);
222 }
223
224 public AnnotationType getAnnotationType(UUID uuid){
225 return annotationTypeMap.get(uuid);
226 }
227 public void putAnnotationType(AnnotationType annotationType){
228 annotationTypeMap.put(annotationType.getUuid(), annotationType);
229 }
230
231 public NamedArea getNamedArea(UUID uuid){
232 return namedAreaMap.get(uuid);
233 }
234 public void putNamedArea(NamedArea namedArea){
235 namedAreaMap.put(namedArea.getUuid(), namedArea);
236 }
237
238 public NamedAreaLevel getNamedAreaLevel(UUID uuid){
239 return namedAreaLevelMap.get(uuid);
240 }
241 public void putNamedAreaLevel(NamedAreaLevel namedAreaLevel){
242 namedAreaLevelMap.put(namedAreaLevel.getUuid(), namedAreaLevel);
243 }
244
245 public Rank getRank(UUID uuid){
246 return rankMap.get(uuid);
247 }
248 public void putRank(Rank rank){
249 rankMap.put(rank.getUuid(), rank);
250 }
251
252 public State getStateTerm(UUID uuid){
253 return stateTermMap.get(uuid);
254 }
255 public void putStateTerm(State stateTerm){
256 stateTermMap.put(stateTerm.getUuid(), stateTerm);
257 }
258
259 public Feature getFeature(UUID uuid){
260 return featureMap.get(uuid);
261 }
262 public void putFeature(Feature feature){
263 featureMap.put(feature.getUuid(), feature);
264 }
265
266 public NomenclaturalStatusType getNomenclaturalStatusType(UUID uuid){
267 return nomenclaturalStatusTypeMap.get(uuid);
268 }
269 public void putNomenclaturalStatusType(NomenclaturalStatusType feature){
270 nomenclaturalStatusTypeMap.put(feature.getUuid(), feature);
271 }
272
273 public DefinedTerm getKindOfUnit(UUID uuid){
274 return kindOfUnitMap.get(uuid);
275 }
276 public void putKindOfUnit(DefinedTerm unit){
277 kindOfUnitMap.put(unit.getUuid(), unit);
278 }
279
280 public MeasurementUnit getMeasurementUnit(UUID uuid){
281 return measurementUnitMap.get(uuid);
282 }
283
284 public void putMeasurementUnit(MeasurementUnit unit){
285 measurementUnitMap.put(unit.getUuid(), unit);
286 }
287
288 public void putStatisticalMeasure(StatisticalMeasure unit){
289 statisticalMeasureMap.put(unit.getUuid(), unit);
290 }
291
292 public StatisticalMeasure getStatisticalMeasure(UUID uuid){
293 return statisticalMeasureMap.get(uuid);
294 }
295
296
297 public DefinedTerm getModifier(UUID uuid){
298 return modifierMap.get(uuid);
299 }
300
301 public void putModifier(DefinedTerm unit){
302 modifierMap.put(unit.getUuid(), unit);
303 }
304
305 public TaxonRelationshipType getTaxonRelationshipType(UUID uuid){
306 return taxonRelationshipTypeMap.get(uuid);
307 }
308
309 public void putTaxonRelationshipType(TaxonRelationshipType relType){
310 taxonRelationshipTypeMap.put(relType.getUuid(), relType);
311 }
312
313
314 public PresenceAbsenceTerm getPresenceAbsenceTerm(UUID uuid){
315 return presenceTermMap.get(uuid);
316 }
317
318 public void putPresenceAbsenceTerm(PresenceAbsenceTerm presenceTerm){
319 presenceTermMap.put(presenceTerm.getUuid(), presenceTerm);
320 }
321
322 public Language getLanguage(UUID uuid){
323 return languageMap.get(uuid);
324 }
325
326 public void putLanguage(Language language){
327 languageMap.put(language.getUuid(), language);
328 }
329
330
331 public ReferenceSystem getReferenceSystem(UUID uuid){
332 return referenceSystemMap.get(uuid);
333 }
334
335 public void putReferenceSystem(ReferenceSystem referenceSystem){
336 referenceSystemMap.put(referenceSystem.getUuid(), referenceSystem);
337 }
338
339
340 public TermVocabulary<?> getTermedVocabulary(UUID uuid) {
341 return termedVocabularyMap.get(uuid);
342 }
343
344 public void putTermedVocabularyMap(TermVocabulary<?> termedVocabulary) {
345 this.termedVocabularyMap.put(termedVocabulary.getUuid(), termedVocabulary);
346 }
347
348
349 public Object getStatusItem(String key){
350 return anyStatusItemMap.get(key);
351 }
352
353 public void putStatusItem(String key, Object statusItem){
354 anyStatusItemMap.put(key, statusItem);
355 }
356
357
358
359
360 //TODO make this abstract or find another way to force that the
361 //transformer exists
362 public IInputTransformer getTransformer(){
363 return inputTransformer;
364 }
365
366 public void setTransformer(IInputTransformer transformer){
367 this.inputTransformer = transformer;
368 }
369
370 /**
371 * Returns true, if this import is in validation state. False otherwise
372 * @return
373 */
374 public boolean isCheck() {
375 return isCheck;
376 }
377
378 /**
379 * @see #isCheck
380 * @param isCheck
381 */
382 public void setCheck(boolean isCheck) {
383 this.isCheck = isCheck;
384 }
385
386
387 public void setSuccess(boolean success) {
388 this.success = success;
389 }
390
391 public void setUnsuccessfull(){
392 this.success = false;
393 }
394
395 public boolean isSuccess() {
396 return success;
397 }
398 /**
399 * Returns the import report as a byte array
400 * @return
401 */
402 public byte[] getReportAsByteArray() {
403 return null;
404 }
405
406 }