2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.erms
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
25 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
26 import eu
.etaxonomy
.cdm
.io
.pesi
.erms
.validation
.ErmsSourceUsesImportValidator
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
29 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
30 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.OriginalSourceType
;
32 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 public class ErmsSourceUsesImport
extends ErmsImportBase
<CommonTaxonName
> {
42 private static final long serialVersionUID
= -5139234838768878653L;
44 private static final Logger logger
= Logger
.getLogger(ErmsSourceUsesImport
.class);
46 // private DbImportMapping<ErmsImportState, ErmsImportConfigurator> mapping; //not needed
48 private static final String pluralString
= "source uses";
49 private static final String dbTableName
= "tu_sources";
50 private static final Class
<?
> cdmTargetClass
= null;
52 public ErmsSourceUsesImport(){
53 super(pluralString
, dbTableName
, cdmTargetClass
);
57 protected String
getIdQuery() {
58 String strQuery
= " SELECT tu_id, source_id, sourceuse_id " +
60 " ORDER BY tu_id, source_id, sourceuse_id ";
65 protected String
getRecordQuery(ErmsImportConfigurator config
) {
66 String strRecordQuery
=
68 " FROM tu_sources INNER JOIN sourceuses ON tu_sources.sourceuse_id = sourceuses.sourceuse_id" +
69 " WHERE ( tu_sources.tu_id IN (" + ID_LIST_TOKEN
+ ") AND " +
70 " tu_sources.source_id IN (" + ID_LIST_TOKEN
+ ") AND " +
71 " tu_sources.sourceuse_id IN (" + ID_LIST_TOKEN
+ ") )";
72 return strRecordQuery
;
77 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, ErmsImportState state
) {
78 boolean success
= true ;
79 Set
<CdmBase
> objectsToSave
= new HashSet
<>();
82 ResultSet rs
= partitioner
.getResultSet();
86 //read and normalize values
87 // int sourceUseId = rs.getInt("sourceuse_id");
88 int sourceId
= rs
.getInt("source_id");
89 String strSourceUse
= rs
.getString("sourceuse_name");
90 String strSourceId
= String
.valueOf(sourceId
);
91 int taxonId
= rs
.getInt("tu_id");
92 String strTaxonId
= String
.valueOf(taxonId
);
93 String strPageNr
= rs
.getString("pagenr");
94 if (StringUtils
.isBlank(strPageNr
)){
97 Reference ref
= (Reference
)state
.getRelatedObject(ErmsImportBase
.REFERENCE_NAMESPACE
, strSourceId
);
100 IdentifiableEntity
<?
> objectToSave
= null;
101 //invoke methods for each sourceUse type
102 String srcUseStr
= normalizeSourceUse(strSourceUse
);
103 objectToSave
= makeSourceUse(state
, srcUseStr
, ref
, strTaxonId
, strPageNr
, strSourceId
);
105 if(objectToSave
!= null){
106 objectsToSave
.add(objectToSave
);
108 } catch (Exception e
) {
113 } catch (SQLException e
) {
114 logger
.error("SQLException:" + e
);
118 logger
.debug("real partition size: " + i
);
119 partitioner
.startDoSave();
120 getCommonService().save(objectsToSave
);
124 private String
normalizeSourceUse(String sourceUse
) {
128 private IdentifiableEntity
<?
> makeSourceUse(ErmsImportState state
, String srcUseString
,
129 Reference ref
, String strTaxonId
, String strPageNr
, String strSourceId
) {
131 TaxonBase
<?
> taxonBase
= (TaxonBase
<?
>)state
.getRelatedObject(ErmsImportBase
.TAXON_NAMESPACE
, strTaxonId
);
132 if (taxonBase
== null){
133 logger
.warn("TaxonBase " + strTaxonId
+ " could not be found for source ID " + strSourceId
+ " and source use " + srcUseString
);
135 taxonBase
.addSource(OriginalSourceType
.Other
, ref
, strPageNr
, srcUseString
);
141 // private TaxonName makeOriginalDescription(ResultSetPartitioner<?> partitioner,
142 // ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
143 // TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
144 // taxonName.setNomenclaturalReference(ref);
145 // taxonName.setNomenclaturalMicroReference(strPageNr);
150 // private IdentifiableEntity<?> makeAdditionalSource(ResultSetPartitioner<?> partitioner,
151 // ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
153 // Feature citationFeature = Feature.CITATION();
154 // DescriptionElementBase element = TextData.NewInstance(citationFeature);
155 // DescriptionElementSource source = element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, ref, strPageNr);
156 // if (source == null){
157 // logger.warn("Source is null");
160 // TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
163 // //if taxon base is a synonym, add the description to the accepted taxon
164 // if (taxonBase.isInstanceOf(Synonym.class)){
165 // Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
166 // taxon = synonym.getAcceptedTaxon();
167 // if (taxon == null){
168 // String warning = "Synonym "+ strTaxonId + " has no accepted taxon";
169 // logger.warn(warning);
171 // //throw new IllegalStateException(warning);
173 // //add synonym name as name used in source
174 // source.setNameUsedInSource(synonym.getName());
176 // taxon = (Taxon)taxonBase;
179 // //get or create description and add the element
180 // TaxonDescription description;
181 // if (taxon.getDescriptions().size() > 0){
182 // description = taxon.getDescriptions().iterator().next();
184 // description = TaxonDescription.NewInstance(taxon);
186 // description.addElement(element);
190 // private IdentifiableEntity<?> makeSourceOfSynonymy(@SuppressWarnings("unused") ResultSetPartitioner<?> partitioner,
191 // ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
193 // TaxonBase<?> taxonBase = (TaxonBase<?>)state.getRelatedObject(ErmsImportBase.TAXON_NAMESPACE, strTaxonId);
194 // if (taxonBase == null){
195 // String warning = "taxonBase (id = " + strTaxonId + ") could not be found ";
196 // logger.warn(warning);
198 // }else if (taxonBase.isInstanceOf(Taxon.class)){
199 // Taxon taxon =CdmBase.deproxy(taxonBase, Taxon.class);
200 // Feature sourceOfSynonymyFeature = getFeature(state, ErmsTransformer.uuidSourceOfSynonymy, "Source of synonymy", "Source of synonymy", null, null);
201 // TextData element = TextData.NewInstance(sourceOfSynonymyFeature);
202 // getTaxonDescription(taxon, false, true).addElement(element);
203 // element.addPrimaryTaxonomicSource(ref, strPageNr);
204 // }else if (taxonBase.isInstanceOf(Synonym.class)){
205 // Synonym synonym =CdmBase.deproxy(taxonBase, Synonym.class);
206 // if (synonym.getSec()!= null){
207 // logger.warn("Synonym has sec reference already. Source of synonym can not be set: " + strTaxonId);
209 // synonym.setSec(ref);
211 // if (synonym.getSecMicroReference()!= null){
212 // logger.warn("Synonym has sec micro reference already. Source of synonym detail can not be set: " + strTaxonId);
214 // synonym.setSecMicroReference(strPageNr);
217 // throw new RuntimeException("Unsupported taxonbase class");
223 // private boolean isFirstRediscription = true;
224 // @SuppressWarnings("unused")
225 // private IdentifiableEntity<?> makeRedescription(ResultSetPartitioner<?> partitioner, ErmsImportState state,
226 // Reference ref, String strTaxonId, String strPageNr) {
227 // if (isFirstRediscription){
228 // logger.warn("Rediscription not yet implemented");
229 // isFirstRediscription = false;
234 // private IdentifiableEntity<?> makeCombinationReference(ResultSetPartitioner<?> partitioner,
235 // ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
236 // // Kopie von Orig. Comb.
237 // //TODO ist das wirklich der richtige Name, oder muss ein verknüpfter Name verwendet werden
238 // TaxonName taxonName = (TaxonName)state.getRelatedObject(ErmsImportBase.NAME_NAMESPACE, strTaxonId);
239 // taxonName.setNomenclaturalReference(ref);
240 // taxonName.setNomenclaturalMicroReference(strPageNr);
244 // private boolean isFirstStatusSource = true;
245 // @SuppressWarnings("unused")
246 // private IdentifiableEntity<?> makeStatusSource(ResultSetPartitioner<?> partitioner,
247 // ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
248 // if (isFirstStatusSource){
249 // logger.warn("StatusSource not yet implemented");
250 // isFirstStatusSource = false;
255 // private boolean isFirstEmendation = true;
256 // @SuppressWarnings("unused")
257 // private IdentifiableEntity<?> makeEmendation(ResultSetPartitioner<?> partitioner, ErmsImportState state, Reference ref, String strTaxonId, String strPageNr) {
258 // if (isFirstEmendation){
259 // logger.warn("Emmendation not yet implemented");
260 // isFirstEmendation = false;
265 @SuppressWarnings("unused")
266 public CommonTaxonName
createObject(ResultSet rs
, ErmsImportState state
)
267 throws SQLException
{
268 return null; //not needed
272 //************************************ RELATED OBJECTS *************************************************/
275 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
276 ErmsImportState state
) {
280 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
283 Set
<String
> taxonIdSet
= new HashSet
<>();
284 Set
<String
> nameIdSet
= new HashSet
<>();
285 Set
<String
> referenceIdSet
= new HashSet
<>();
287 handleForeignKey(rs
, taxonIdSet
, "tu_id");
288 handleForeignKey(rs
, nameIdSet
, "tu_id");
289 handleForeignKey(rs
, referenceIdSet
, "source_id");
293 nameSpace
= ErmsImportBase
.NAME_NAMESPACE
;
295 Map
<String
, TaxonName
> nameMap
= getCommonService().getSourcedObjectsByIdInSourceC(TaxonName
.class, idSet
, nameSpace
);
296 result
.put(nameSpace
, nameMap
);
299 nameSpace
= ErmsImportBase
.TAXON_NAMESPACE
;
301 @SuppressWarnings("rawtypes")
302 Map
<String
, TaxonBase
> taxonMap
= getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase
.class, idSet
, nameSpace
);
303 result
.put(nameSpace
, taxonMap
);
306 nameSpace
= ErmsImportBase
.REFERENCE_NAMESPACE
;
307 idSet
= referenceIdSet
;
308 Map
<String
, Reference
> referenceMap
= getCommonService().getSourcedObjectsByIdInSourceC(Reference
.class, idSet
, nameSpace
);
309 result
.put(nameSpace
, referenceMap
);
312 } catch (SQLException e
) {
313 throw new RuntimeException(e
);
321 protected DbImportMapping
<?
, ?
> getMapping() {
322 logger
.info("getMapping not implemented for EmrsSourceUsesImport");
323 return null; // not needed because Mapping is not implemented in this class yet
327 protected boolean doCheck(ErmsImportState state
){
328 IOValidator
<ErmsImportState
> validator
= new ErmsSourceUsesImportValidator();
329 return validator
.validate(state
);
333 protected boolean isIgnore(ErmsImportState state
){
334 return ! state
.getConfig().isDoSourceUse();