f57c8989c41a5b01c31fe0b5193eece1b74f59de
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / dwca / in / DwcaImportState.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.io.dwca.in;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.log4j.Logger;
20
21 import eu.etaxonomy.cdm.api.service.IIdentifiableEntityService;
22 import eu.etaxonomy.cdm.io.common.ImportStateBase;
23 import eu.etaxonomy.cdm.io.dwca.in.InMemoryMapping.CdmKey;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27
28 /**
29 * @author a.mueller
30 * @created 23.11.2011
31 */
32 public class DwcaImportState extends ImportStateBase<DwcaImportConfigurator, DwcaImport>{
33 @SuppressWarnings("unused")
34 private static final Logger logger = Logger.getLogger(DwcaImportState.class);
35
36 boolean taxaCreated;
37
38 private IImportMapping mapping = new InMemoryMapping();
39
40 public DwcaImportState(DwcaImportConfigurator config) {
41 super(config);
42 }
43
44 /**
45 * True, if taxa have been fully created.
46 * @return
47 */
48 public boolean isTaxaCreated() {
49 return taxaCreated;
50
51 }
52
53 /**
54 * @param taxaCreated the taxaCreated to set
55 */
56 public void setTaxaCreated(boolean taxaCreated) {
57 this.taxaCreated = taxaCreated;
58 }
59
60 public void putMapping(String namespace, Integer sourceKey, IdentifiableEntity<?> destinationObject){
61 mapping.putMapping(namespace, sourceKey, destinationObject);
62 }
63
64 public void putMapping(String namespace, String sourceKey, IdentifiableEntity<?> destinationObject){
65 mapping.putMapping(namespace, sourceKey, destinationObject);
66 }
67
68
69 public List<IdentifiableEntity> get(String namespace, String sourceKey){
70 return get(namespace, sourceKey, null);
71 }
72
73 public <CLASS extends IdentifiableEntity> List<CLASS> get(String namespace, String sourceKey,Class<CLASS> destinationClass){
74 List<CLASS> result = new ArrayList<CLASS>();
75 Set<CdmKey> keySet = mapping.get(namespace, sourceKey);
76 for (CdmKey<CLASS> key: keySet){
77 if (destinationClass == null || destinationClass.isAssignableFrom(key.clazz)){
78 IIdentifiableEntityService<CLASS> service = getCurrentIO().getServiceByClass(key.clazz);
79 CLASS entity = CdmBase.deproxy(service.find(key.id), key.clazz);
80 result.add(entity);
81 }
82 }
83 return result;
84 }
85
86 public boolean exists(String namespace, String sourceKey,Class<?> destinationClass){
87 return mapping.exists(namespace, sourceKey, destinationClass);
88 }
89
90
91
92
93
94
95 }