3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.edaphobase
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 public class EdaphobaseSynonymyImport
extends EdaphobaseImportBase
{
38 private static final long serialVersionUID
= 6641343927320994726L;
40 private static final Logger logger
= Logger
.getLogger(EdaphobaseSynonymyImport
.class);
42 private static final String tableName
= "tax_synonym";
44 private static final String pluralString
= "synonym relationships";
51 public EdaphobaseSynonymyImport() {
52 super(tableName
, pluralString
);
56 protected String
getIdQuery(EdaphobaseImportState state
) {
57 return "SELECT tax_synonym_id "
58 + " FROM tax_synonym "
59 + " ORDER BY tax_synonym.a_taxon_fk_taxon_id ";
63 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
64 String result
= "SELECT sr.* "
65 + " FROM tax_synonym sr"
66 + " WHERE tax_synonym_id IN (@IDSET)";
67 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
73 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
74 ResultSet rs
= partitioner
.getResultSet();
75 Map
<String
, TaxonBase
> map
= partitioner
.getObjectMap(TAXON_NAMESPACE
);
76 Reference
<?
> sourceReference
= state
.getTransactionalSourceReference();
78 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
81 int id
= rs
.getInt("tax_synonym_id");
83 // boolean isValid = rs.getBoolean("valid");
84 Integer synId
= nullSafeInt(rs
, "a_taxon_fk_taxon_id");
85 Integer accId
= nullSafeInt(rs
, "b_taxon_fk_taxon_id");
88 if (synId
== null || accId
== null){
89 logger
.warn("Either a_taxon or b_taxon is NULL for tax_synonym " + id
);
91 TaxonBase
<?
> synonymCandidate
= state
.getRelatedObject(TAXON_NAMESPACE
, String
.valueOf(synId
), TaxonBase
.class);
92 if (synonymCandidate
== null){
93 logger
.warn("Synonym " + synId
+ " not found for synonymRelations (tax_synonym): " + id
);
94 }else if (synonymCandidate
.isInstanceOf(Taxon
.class)){
95 String message
= "Synonym ("+synId
+") is not synonym but accepted (valid). Can't add synonym relationship for tax_synonym: "+id
;
98 Synonym synonym
= CdmBase
.deproxy(synonymCandidate
, Synonym
.class);
99 TaxonBase
<?
> accepted
= state
.getRelatedObject(TAXON_NAMESPACE
, String
.valueOf(accId
), TaxonBase
.class);
100 if (accepted
== null){
101 logger
.warn("Accepted(parent) taxon " + accId
+ " not found for tax_synonym " + id
);
102 }else if(accepted
.isInstanceOf(Synonym
.class)){
103 String message
= "Taxon ("+accId
+") is not accepted but synonym. Can't add synonym relationship for tax_synonym: "+id
;
104 logger
.warn(message
);
106 Taxon taxon
= CdmBase
.deproxy(accepted
, Taxon
.class);
107 taxon
.addSynonym(synonym
, SynonymRelationshipType
.SYNONYM_OF());
108 taxaToSave
.add(synonym
);
109 taxaToSave
.add(taxon
);
115 // String nameSpace = "tax_taxon";
116 // ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
117 // ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
121 } catch (SQLException e
) {
122 // TODO Auto-generated catch block
126 getTaxonService().saveOrUpdate(taxaToSave
);
131 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
132 EdaphobaseImportState state
) {
136 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
139 Set
<String
> taxonIdSet
= new HashSet
<String
>();
141 handleForeignKey(rs
, taxonIdSet
, "a_taxon_fk_taxon_id");
142 handleForeignKey(rs
, taxonIdSet
, "b_taxon_fk_taxon_id");
146 nameSpace
= TAXON_NAMESPACE
;
147 cdmClass
= TaxonBase
.class;
149 Map
<String
, TaxonBase
> taxonMap
= (Map
<String
, TaxonBase
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
150 result
.put(nameSpace
, taxonMap
);
152 } catch (SQLException e
) {
153 throw new RuntimeException(e
);
161 protected boolean doCheck(EdaphobaseImportState state
) {
166 protected boolean isIgnore(EdaphobaseImportState state
) {
167 return ! state
.getConfig().isDoSynonyms();