cleanup
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraImportBase.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.mexico;
10
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13 import java.util.Map;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.common.CdmUtils;
18 import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState;
19 import eu.etaxonomy.cdm.io.common.DbImportBase;
20 import eu.etaxonomy.cdm.io.common.ICdmIO;
21 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
22 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
23 import eu.etaxonomy.cdm.model.reference.ISourceable;
24 import eu.etaxonomy.cdm.model.reference.Reference;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27
28 /**
29 * @author a.mueller
30 * @since 20.03.2008
31 */
32 public abstract class MexicoEfloraImportBase
33 extends DbImportBase<MexicoEfloraImportState, MexicoEfloraImportConfigurator>
34 implements ICdmIO<MexicoEfloraImportState>, IPartitionedIO<MexicoEfloraImportState> {
35
36 private static final long serialVersionUID = -5229728676004248450L;
37 private static final Logger logger = Logger.getLogger(MexicoEfloraImportBase.class);
38
39 public MexicoEfloraImportBase(String tableName, String pluralString ) {
40 super(tableName, pluralString);
41 }
42
43 @Override
44 protected String getIdQuery(MexicoEfloraImportState state){
45 String result = " SELECT " + getTableName() + "id FROM " + getTableName();
46 return result;
47 }
48
49 //can be overriden
50 protected String getIdInSource(MexicoEfloraImportState state, ResultSet rs) throws SQLException {
51 return null;
52 }
53
54
55 protected Taxon getTaxon(BerlinModelImportState state, int taxonId, Map<String, TaxonBase> taxonMap, int factId) {
56 TaxonBase<?> taxonBase = taxonMap.get(String.valueOf(taxonId));
57
58 //TODO for testing
59 // if (taxonBase == null && ! state.getConfig().isDoTaxa()){
60 // taxonBase = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()), null);
61 // }
62
63 Taxon taxon;
64 if ( taxonBase instanceof Taxon ) {
65 taxon = (Taxon) taxonBase;
66 } else if (taxonBase != null) {
67 logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
68 return null;
69 } else {
70 logger.warn("TaxonBase (" + taxonId + ") for Fact(Specimen) with factId " + factId + " is null.");
71 return null;
72 }
73 return taxon;
74 }
75
76
77 /**
78 * Searches first in the detail maps then in the ref maps for a reference.
79 * Returns the reference as soon as it finds it in one of the map, according
80 * to the order of the map.
81 * If nomRefDetailFk is <code>null</code> no search on detail maps is performed.
82 * If one of the maps is <code>null</code> no search on the according map is
83 * performed. <BR>
84 * You may define the order of search by the order you pass the maps but
85 * make sure to always pass the detail maps first.
86 * @param firstDetailMap
87 * @param secondDetailMap
88 * @param firstRefMap
89 * @param secondRefMap
90 * @param nomRefDetailFk
91 * @param nomRefFk
92 * @return
93 */
94 protected Reference getReferenceFromMaps(
95 Map<String, Reference> detailMap,
96 Map<String, Reference> refMap,
97 String nomRefDetailFk,
98 String nomRefFk) {
99 Reference ref = null;
100 if (detailMap != null){
101 ref = detailMap.get(nomRefDetailFk);
102 }
103 if (ref == null){
104 ref = refMap.get(nomRefFk);
105 }
106 return ref;
107 }
108
109 protected Reference getSourceReference(Reference sourceReference) {
110 Reference persistentSourceReference = getReferenceService().find(sourceReference.getUuid()); //just to be sure
111 if (persistentSourceReference != null){
112 sourceReference = persistentSourceReference;
113 }
114 return sourceReference;
115 }
116
117 protected static <T extends IdentifiableSource> boolean importSourceExists(ISourceable<T> sourceable, String idInSource,
118 String namespace, Reference ref) {
119 for (T source : sourceable.getSources()){
120 if (CdmUtils.nullSafeEqual(namespace, source.getIdNamespace()) &&
121 CdmUtils.nullSafeEqual(idInSource, source.getIdInSource()) &&
122 CdmUtils.nullSafeEqual(ref, source.getCitation())){
123 return true;
124 }
125 }
126 return false;
127 }
128 }