bugfixes for index fungorum
[cdmlib-apps.git] / cdm-pesi / src / main / java / eu / etaxonomy / cdm / io / pesi / indexFungorum / IndexFungorumSpeciesImport.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.pesi.indexFungorum;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25 import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
26 import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.Extension;
29 import eu.etaxonomy.cdm.model.common.ExtensionType;
30 import eu.etaxonomy.cdm.model.common.MarkerType;
31 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
32 import eu.etaxonomy.cdm.model.name.NonViralName;
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.Taxon;
37 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
39
40
41 /**
42 * @author a.mueller
43 * @created 27.02.2012
44 */
45 @Component
46 public class IndexFungorumSpeciesImport extends IndexFungorumImportBase {
47 private static final Logger logger = Logger.getLogger(IndexFungorumSpeciesImport.class);
48
49 private static final String pluralString = "species";
50 private static final String dbTableName = "[tblPESIfungi-IFdata]";
51
52 public IndexFungorumSpeciesImport(){
53 super(pluralString, dbTableName, null);
54 }
55
56
57
58
59 @Override
60 protected String getIdQuery() {
61 String result = " SELECT RECORD_NUMBER FROM " + getTableName() +
62 " ORDER BY PreferredName ";
63 return result;
64 }
65
66
67
68
69 /* (non-Javadoc)
70 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
71 */
72 @Override
73 protected String getRecordQuery(IndexFungorumImportConfigurator config) {
74 String strRecordQuery =
75 " SELECT DISTINCT distribution.PreferredNameFDCnumber, species.* , cl.PhylumName" +
76 " FROM tblPESIfungi AS distribution RIGHT OUTER JOIN dbo.[tblPESIfungi-IFdata] AS species ON distribution.PreferredNameIFnumber = species.RECORD_NUMBER " +
77 " LEFT OUTER JOIN [tblPESIfungi-Classification] cl ON species.PreferredName = cl.PreferredName " +
78 " WHERE ( species.RECORD_NUMBER IN (" + ID_LIST_TOKEN + ") )" +
79 "";
80 return strRecordQuery;
81 }
82
83
84 @Override
85 public boolean doPartition(ResultSetPartitioner partitioner, IndexFungorumImportState state) {
86 boolean success = true;
87 Reference<?> sourceReference = state.getRelatedObject(NAMESPACE_REFERENCE, SOURCE_REFERENCE, Reference.class);
88 ResultSet rs = partitioner.getResultSet();
89
90 Classification classification = getClassification(state);
91 try {
92 while (rs.next()){
93
94 //DisplayName, NomRefCache -> don't use, created by Marc
95
96 Integer id = (Integer)rs.getObject("RECORD_NUMBER");
97 String phylumName = rs.getString("PhylumName");
98
99 String preferredName = rs.getString("PreferredName");
100 if (StringUtils.isBlank(preferredName)){
101 logger.warn("Preferred name is blank. This case is not yet handled by IF import. RECORD_NUMBER" + CdmUtils.Nz(id));
102 }
103
104 Rank rank = Rank.SPECIES();
105
106 NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
107 NonViralName<?> name = parser.parseSimpleName(preferredName, NomenclaturalCode.ICBN, rank);
108
109 Taxon taxon = Taxon.NewInstance(name, sourceReference);
110 Taxon parent = getParentTaxon(state, rs);
111 classification.addParentChild(parent, taxon, null, null);
112
113 //author + publication
114 makeAuthorAndPublication(state, rs, name);
115 //source
116 makeSource(state, taxon, id, NAMESPACE_SPECIES );
117
118 //fossil
119 if (FOSSIL_FUNGI.equalsIgnoreCase(phylumName)){
120 ExtensionType fossilExtType = getExtensionType(state, ErmsTransformer.uuidFossilStatus, "fossil status", "fossil status", "fos. stat.");
121 Extension.NewInstance(taxon, PesiTransformer.STR_FOSSIL_ONLY, fossilExtType);
122 }
123 //save
124 getTaxonService().saveOrUpdate(taxon);
125 }
126
127
128 } catch (Exception e) {
129 e.printStackTrace();
130 logger.error(e.getMessage());
131 state.setSuccess(false);
132 success = false;
133 }
134 return success;
135 }
136
137
138 private Taxon getParentTaxon(IndexFungorumImportState state, ResultSet rs) throws SQLException {
139 Integer genusId = rs.getInt("PreferredNameFDCnumber");
140
141 Taxon taxon = state.getRelatedObject(NAMESPACE_GENERA, String.valueOf(genusId), Taxon.class);
142 if (taxon == null){
143 logger.warn("Taxon not found for " + genusId);
144 }
145 return taxon;
146 }
147
148 /* (non-Javadoc)
149 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
150 */
151 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
152 String nameSpace;
153 Class cdmClass;
154 Set<String> idSet;
155 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
156
157 try{
158 Set<String> taxonIdSet = new HashSet<String>();
159 while (rs.next()){
160 handleForeignKey(rs, taxonIdSet,"PreferredNameFDCnumber" );
161 }
162
163 //taxon map
164 nameSpace = NAMESPACE_GENERA;
165 cdmClass = TaxonBase.class;
166 idSet = taxonIdSet;
167 Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
168 result.put(nameSpace, taxonMap);
169
170 //sourceReference
171 Reference<?> sourceReference = getReferenceService().find(PesiTransformer.uuidSourceRefIndexFungorum);
172 Map<String, Reference> referenceMap = new HashMap<String, Reference>();
173 referenceMap.put(SOURCE_REFERENCE, sourceReference);
174 result.put(NAMESPACE_REFERENCE, referenceMap);
175
176 } catch (SQLException e) {
177 throw new RuntimeException(e);
178 }
179 return result;
180 }
181
182
183
184 /* (non-Javadoc)
185 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
186 */
187 @Override
188 protected boolean doCheck(IndexFungorumImportState state){
189 return true;
190 }
191
192 /* (non-Javadoc)
193 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
194 */
195 protected boolean isIgnore(IndexFungorumImportState state){
196 return ! state.getConfig().isDoTaxa();
197 }
198
199
200
201
202
203 }