fix #6799: add totalCount of ticks as totalwork and start the progress monitor when...
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / markup / MarkupImportState.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
10 package eu.etaxonomy.cdm.io.markup;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21
22 import eu.etaxonomy.cdm.api.application.ICdmRepository;
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.io.common.XmlImportState;
25 import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
26 import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
27 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
28 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.description.Feature;
31 import eu.etaxonomy.cdm.model.description.FeatureNode;
32 import eu.etaxonomy.cdm.model.description.PolytomousKey;
33 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
34 import eu.etaxonomy.cdm.model.location.NamedArea;
35 import eu.etaxonomy.cdm.model.media.Media;
36 import eu.etaxonomy.cdm.model.occurrence.Collection;
37 import eu.etaxonomy.cdm.model.reference.Reference;
38 import eu.etaxonomy.cdm.model.taxon.Taxon;
39
40 /**
41 * @author a.mueller
42 * @created 11.05.2009
43 */
44 public class MarkupImportState extends XmlImportState<MarkupImportConfigurator, MarkupDocumentImport>{
45 @SuppressWarnings("unused")
46 private static final Logger logger = Logger.getLogger(MarkupImportState.class);
47
48 private ImportDeduplicationHelper<MarkupImportState> deduplicationHelper;
49
50
51 private UnmatchedLeads unmatchedLeads;
52 private boolean onlyNumberedTaxaExist; //attribute in <key>
53
54 private Set<FeatureNode> featureNodesToSave = new HashSet<FeatureNode>();
55
56 private Set<PolytomousKeyNode> polytomousKeyNodesToSave = new HashSet<>();
57
58 private PolytomousKey currentKey;
59
60 private TeamOrPersonBase<?> currentCollector;
61
62 private Set<NamedArea> currentAreas = new HashSet<NamedArea>();
63
64 private Language defaultLanguage;
65
66 private Taxon currentTaxon;
67 private String currentTaxonNum;
68
69 private boolean taxonInClassification = true;
70
71 private String latestGenusEpithet = null;
72
73 private TeamOrPersonBase<?> latestAuthorInHomotype = null;
74 private Reference latestReferenceInHomotype = null;
75
76 private boolean isCitation = false;
77 private boolean isNameType = false;
78 private boolean isProParte = false;
79 private boolean currentTaxonExcluded = false;
80
81 private boolean isSpecimenType = false;
82
83 private boolean taxonIsHybrid = false;
84
85
86
87 private String baseMediaUrl = null;
88
89 private String nameStatus;
90
91
92 private Map<String, FootnoteDataHolder> footnoteRegister = new HashMap<>();
93
94 private Map<String, Media> figureRegister = new HashMap<>();
95
96 private Map<String, Set<AnnotatableEntity>> footnoteRefRegister = new HashMap<>();
97 private Map<String, Set<AnnotatableEntity>> figureRefRegister = new HashMap<>();
98
99 private Map<String, UUID> areaMap = new HashMap<String, UUID>();
100
101 private Map<String,UUID> unknownFeaturesUuids = new HashMap<String, UUID>();
102
103 private List<FeatureSorterInfo> currentGeneralFeatureSorterList; //keep in multiple imports
104 private List<FeatureSorterInfo> currentCharFeatureSorterList; //keep in multiple imports
105 private Map<String,List<FeatureSorterInfo>> generalFeatureSorterListMap = new HashMap<>(); //keep in multiple imports
106 private Map<String,List<FeatureSorterInfo>> charFeatureSorterListMap = new HashMap<>(); //keep in multiple imports
107
108 private String collectionAndType = "";
109
110 private boolean firstSpecimenInFacade;
111
112 /**
113 * This method resets all those variables that should not be reused from one import to another.
114 * @see MarkupImportConfigurator#isReuseExistingState()
115 * @see MarkupImportConfigurator#getNewState()
116 */
117 protected void reset(){
118 featureNodesToSave = new HashSet<FeatureNode>();
119 polytomousKeyNodesToSave = new HashSet<PolytomousKeyNode>();
120 currentKey = null;
121 defaultLanguage = null;
122 currentTaxon = null;
123 currentCollector = null;
124 footnoteRegister = new HashMap<String, FootnoteDataHolder>();
125 figureRegister = new HashMap<String, Media>();
126 footnoteRefRegister = new HashMap<String, Set<AnnotatableEntity>>();
127 figureRefRegister = new HashMap<String, Set<AnnotatableEntity>>();
128 currentAreas = new HashSet<NamedArea>();
129
130 this.resetUuidTermMaps();
131 }
132
133
134 //**************************** CONSTRUCTOR ******************************************/
135
136 public MarkupImportState(MarkupImportConfigurator config) {
137 super(config);
138 if (getTransformer() == null){
139 IInputTransformer newTransformer = config.getTransformer();
140 if (newTransformer == null){
141 newTransformer = new MarkupTransformer();
142 }
143 setTransformer(newTransformer);
144 }
145 }
146
147 // ********************************** GETTER / SETTER *************************************/
148
149 public UnmatchedLeads getUnmatchedLeads() {
150 return unmatchedLeads;
151 }
152
153 public void setUnmatchedLeads(UnmatchedLeads unmatchedKeys) {
154 this.unmatchedLeads = unmatchedKeys;
155 }
156
157 public void setFeatureNodesToSave(Set<FeatureNode> featureNodesToSave) {
158 this.featureNodesToSave = featureNodesToSave;
159 }
160
161 public Set<FeatureNode> getFeatureNodesToSave() {
162 return featureNodesToSave;
163 }
164
165 public Set<PolytomousKeyNode> getPolytomousKeyNodesToSave() {
166 return polytomousKeyNodesToSave;
167 }
168
169 public void setPolytomousKeyNodesToSave(Set<PolytomousKeyNode> polytomousKeyNodesToSave) {
170 this.polytomousKeyNodesToSave = polytomousKeyNodesToSave;
171 }
172
173 public Language getDefaultLanguage() {
174 return this.defaultLanguage;
175 }
176
177 public void setDefaultLanguage(Language defaultLanguage){
178 this.defaultLanguage = defaultLanguage;
179 }
180
181
182 public void setCurrentTaxon(Taxon currentTaxon) {
183 this.currentTaxon = currentTaxon;
184 }
185
186 public Taxon getCurrentTaxon() {
187 return currentTaxon;
188 }
189
190 public void setCurrentTaxonNum(String currentTaxonNum) {
191 this.currentTaxonNum = currentTaxonNum;
192 }
193
194 public String getCurrentTaxonNum() {
195 return currentTaxonNum;
196 }
197
198
199
200 /**
201 * Is the import currently handling a citation?
202 * @return
203 */
204 public boolean isCitation() {
205 return isCitation;
206 }
207
208 public void setCitation(boolean isCitation) {
209 this.isCitation = isCitation;
210 }
211
212
213 public boolean isNameType() {
214 return isNameType;
215 }
216
217 public void setNameType(boolean isNameType) {
218 this.isNameType = isNameType;
219 }
220
221 public void setProParte(boolean isProParte) {
222 this.isProParte = isProParte;
223 }
224
225 public boolean isProParte() {
226 return isProParte;
227 }
228
229 public void setBaseMediaUrl(String baseMediaUrl) {
230 this.baseMediaUrl = baseMediaUrl;
231 }
232
233 public String getBaseMediaUrl() {
234 return baseMediaUrl;
235 }
236
237
238
239 public void registerFootnote(FootnoteDataHolder footnote) {
240 footnoteRegister.put(footnote.id, footnote);
241 }
242
243 public FootnoteDataHolder getFootnote(String key) {
244 return footnoteRegister.get(key);
245 }
246
247
248 public void registerFigure(String key, Media figure) {
249 figureRegister.put(key, figure);
250 }
251
252 public Media getFigure(String key) {
253 return figureRegister.get(key);
254 }
255
256 public Set<AnnotatableEntity> getFootnoteDemands(String footnoteId){
257 return footnoteRefRegister.get(footnoteId);
258 }
259
260 public void putFootnoteDemands(String footnoteId, Set<AnnotatableEntity> demands){
261 footnoteRefRegister.put(footnoteId, demands);
262 }
263
264
265 public Set<AnnotatableEntity> getFigureDemands(String figureId){
266 return figureRefRegister.get(figureId);
267 }
268
269 public void putFigureDemands(String figureId, Set<AnnotatableEntity> demands){
270 figureRefRegister.put(figureId, demands);
271 }
272
273 /**
274 * @param key
275 */
276 public void setCurrentKey(PolytomousKey key) {
277 this.currentKey = key;
278 }
279
280 /**
281 * @return the currentKey
282 */
283 public PolytomousKey getCurrentKey() {
284 return currentKey;
285 }
286
287 /**
288 * @param key
289 * @return
290 * @see java.util.Map#get(java.lang.Object)
291 */
292 public UUID getAreaUuid(Object key) {
293 return areaMap.get(key);
294 }
295
296 /**
297 * @param key
298 * @param value
299 * @return
300 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
301 */
302 public UUID putAreaUuid(String key, UUID value) {
303 return areaMap.put(key, value);
304 }
305
306 public void putUnknownFeatureUuid(String featureLabel, UUID featureUuid) {
307 this.unknownFeaturesUuids.put(featureLabel, featureUuid);
308 }
309
310 public boolean isOnlyNumberedTaxaExist() {
311 return onlyNumberedTaxaExist;
312 }
313
314 public void setOnlyNumberedTaxaExist(boolean onlyNumberedTaxaExist) {
315 this.onlyNumberedTaxaExist = onlyNumberedTaxaExist;
316 }
317
318 public Map<String,List<FeatureSorterInfo>> getGeneralFeatureSorterListMap() {
319 return generalFeatureSorterListMap;
320 }
321 public Map<String,List<FeatureSorterInfo>> getCharFeatureSorterListMap() {
322 return charFeatureSorterListMap;
323 }
324
325 public UUID getUnknownFeatureUuid(String featureLabel){
326 return this.unknownFeaturesUuids.get(featureLabel);
327 }
328
329
330 /**
331 * Adds new lists to the feature sorter list maps using the given key.
332 * If at least 1 list already existed for the given key, true is returned. False
333 * @param key Key that identifies the feature sorter list.
334 * @return <code>true</code> if at least 1 list already exited for the given key. <code>false</code> otherwise.
335 */
336 public boolean addNewFeatureSorterLists(String key) {
337 //general feature sorter list
338 List<FeatureSorterInfo> generalList = new ArrayList<FeatureSorterInfo>();
339 List<FeatureSorterInfo> previous1 = this.generalFeatureSorterListMap.put(key, generalList);
340 currentGeneralFeatureSorterList = generalList;
341
342 //character feature sorter list
343 List<FeatureSorterInfo> charList = new ArrayList<FeatureSorterInfo>();
344 List<FeatureSorterInfo> previous2 = this.charFeatureSorterListMap.put(key, charList);
345 currentCharFeatureSorterList = charList;
346
347 return (previous1 != null || previous2 != null);
348 }
349
350 /**
351 *
352 * @param feature
353 */
354 public FeatureSorterInfo putFeatureToCharSorterList(Feature feature) {
355 FeatureSorterInfo featureSorterInfo = new FeatureSorterInfo(feature);
356 currentCharFeatureSorterList.add(featureSorterInfo);
357 return featureSorterInfo;
358 }
359
360 public FeatureSorterInfo getLatestCharFeatureSorterInfo() {
361 return currentCharFeatureSorterList.get(currentCharFeatureSorterList.size() - 1);
362 }
363
364
365 /**
366 *
367 * @param feature
368 */
369 public void putFeatureToGeneralSorterList(Feature feature) {
370 currentGeneralFeatureSorterList.add(new FeatureSorterInfo(feature));
371
372 }
373
374 public String getLatestGenusEpithet() {
375 return latestGenusEpithet;
376 }
377
378 public void setLatestGenusEpithet(String latestGenusEpithet) {
379 this.latestGenusEpithet = latestGenusEpithet;
380 }
381
382
383 public boolean isTaxonInClassification() {
384 return taxonInClassification;
385 }
386
387
388 public void setTaxonInClassification(boolean taxonInClassification) {
389 this.taxonInClassification = taxonInClassification;
390 }
391
392
393 public TeamOrPersonBase<?> getCurrentCollector() {
394 return currentCollector;
395 }
396
397
398 public void setCurrentCollector(TeamOrPersonBase<?> currentCollector) {
399 this.currentCollector = currentCollector;
400 }
401
402
403 public void addCurrentArea(NamedArea area) {
404 currentAreas.add(area);
405 }
406
407
408 public Set<NamedArea> getCurrentAreas() {
409 return currentAreas;
410 }
411
412 public void removeCurrentAreas(){
413 currentAreas.clear();
414 }
415
416
417 public TeamOrPersonBase<?> getLatestAuthorInHomotype() {
418 return latestAuthorInHomotype;
419 }
420
421
422 public void setLatestAuthorInHomotype(TeamOrPersonBase<?> latestAuthorInHomotype) {
423 this.latestAuthorInHomotype = latestAuthorInHomotype;
424 }
425
426
427 public Reference getLatestReferenceInHomotype() {
428 return latestReferenceInHomotype;
429 }
430
431
432 public void setLatestReferenceInHomotype(Reference latestReferenceInHomotype) {
433 this.latestReferenceInHomotype = latestReferenceInHomotype;
434 }
435
436 public void setSpecimenType(boolean isSpecimenType) {
437 this.isSpecimenType = isSpecimenType;
438 }
439
440 public boolean isSpecimenType() {
441 return isSpecimenType;
442 }
443
444
445 //or do we need to make this a uuid?
446 private Map<String, Collection> collectionMap = new HashMap<String, Collection>();
447
448 public Collection getCollectionByCode(String code) {
449 return collectionMap.get(code);
450 }
451
452 public void putCollectionByCode(String code, Collection collection) {
453 collectionMap.put(code, collection);
454 }
455
456
457
458 public void addCollectionAndType(String txt) {
459 collectionAndType = CdmUtils.concat("@", collectionAndType, txt);
460 }
461 public String getCollectionAndType() {
462 return collectionAndType;
463 }
464 public void resetCollectionAndType() {
465 collectionAndType = "";
466 }
467
468
469 public boolean isCurrentTaxonExcluded() {
470 return currentTaxonExcluded;
471 }
472 public void setCurrentTaxonExcluded(boolean currentTaxonExcluded) {
473 this.currentTaxonExcluded = currentTaxonExcluded;
474 }
475
476 public boolean isFirstSpecimenInFacade() {
477 return firstSpecimenInFacade;
478 }
479 public void setFirstSpecimenInFacade(boolean firstSpecimenInFacade) {
480 this.firstSpecimenInFacade = firstSpecimenInFacade;
481 }
482
483 public ImportDeduplicationHelper<MarkupImportState> getDeduplicationHelper(ICdmRepository repository) {
484 if (this.deduplicationHelper == null){
485 this.deduplicationHelper = new ImportDeduplicationHelper<>(repository);
486 }
487 return deduplicationHelper;
488 }
489 public void setDeduplicationHelper(ImportDeduplicationHelper<MarkupImportState> deduplicationHelper) {
490 this.deduplicationHelper = deduplicationHelper;
491 }
492
493 public void setNameStatus(String nameStatus) {
494 this.nameStatus = nameStatus;
495 }
496 public String getNameStatus() {
497 return nameStatus;
498 }
499
500 public boolean isTaxonIsHybrid() {
501 return taxonIsHybrid;
502 }
503 public void setTaxonIsHybrid(boolean taxonIsHybrid) {
504 this.taxonIsHybrid = taxonIsHybrid;
505 }
506
507 }