2 * Copyright (C) 2015 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.
9 package eu
.etaxonomy
.cdm
.io
.edaphobase
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
17 import java
.util
.UUID
;
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
.Classification
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 public class EdaphobaseClassificationImport
extends EdaphobaseImportBase
{
38 private static final long serialVersionUID
= -9138378836474086070L;
40 private static final Logger logger
= Logger
.getLogger(EdaphobaseClassificationImport
.class);
42 private static final String tableName
= "tax_taxon";
44 private static final String pluralString
= "taxon relationships";
51 public EdaphobaseClassificationImport() {
52 super(tableName
, pluralString
);
56 protected String
getIdQuery(EdaphobaseImportState state
) {
57 return "SELECT taxon_id FROM "
58 + " (SELECT DISTINCT taxon_id, length(path_to_root) FROM tax_taxon t "
59 + " ORDER BY length(path_to_root), taxon_id) as drvTbl ";
63 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
64 String result
= "SELECT DISTINCT t.* "
66 + " WHERE taxon_id IN (@IDSET)";
67 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
72 protected void doInvoke(EdaphobaseImportState state
) {
73 makeClassification(state
);
74 super.doInvoke(state
);
79 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
80 ResultSet rs
= partitioner
.getResultSet();
81 @SuppressWarnings("unchecked")
82 Map
<String
, Classification
> map
= partitioner
.getObjectMap(CLASSIFICATION_NAMESPACE
);
83 Classification classification
= map
.get(state
.getConfig().getClassificationUuid().toString());
85 @SuppressWarnings("rawtypes")
86 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
89 handleSingleRecord(state
, rs
, classification
, taxaToSave
);
91 } catch (SQLException e
) {
95 getTaxonService().saveOrUpdate(taxaToSave
);
99 private void handleSingleRecord(EdaphobaseImportState state
, ResultSet rs
, Classification classification
,
100 @SuppressWarnings("rawtypes") Set
<TaxonBase
> taxaToSave
) throws SQLException
{
101 Reference sourceReference
= state
.getTransactionalSourceReference();
103 int id
= rs
.getInt("taxon_id");
104 boolean isDeleted
= rs
.getBoolean("deleted");
106 logger
.warn("Deleted not handled according to mail Stephan 2018-03-07. ID: " + id
);
111 boolean isValid
= rs
.getBoolean("valid");
112 // boolean idDeleted = rs.getBoolean("deleted");
113 // String treeIndex = rs.getString("path_to_root");
114 // Integer rankFk = rs.getInt("tax_rank_fk");
115 // String officialRemark = rs.getString("official_remark");
116 // boolean isGroup = rs.getBoolean("taxonomic_group");
117 Integer parentTaxonFk
= nullSafeInt(rs
, "parent_taxon_fk");
119 if (parentTaxonFk
!= null){
120 TaxonBase
<?
> parent
= state
.getRelatedObject(TAXON_NAMESPACE
, parentTaxonFk
.toString(), TaxonBase
.class);
122 logger
.warn("Parent taxon " + parentTaxonFk
+ " not found for taxon " + id
);
125 TaxonBase
<?
> child
= state
.getRelatedObject(TAXON_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
128 if (parent
.isInstanceOf(Synonym
.class)){
129 logger
.warn("Parent taxon (" + parentTaxonFk
+ " is not valid for valid child " + id
+ ")");
131 Taxon accParent
= CdmBase
.deproxy(parent
, Taxon
.class);
133 logger
.warn("Child not found. ID= " + id
);
135 classification
.addParentChild(accParent
, (Taxon
)child
, sourceReference
, null);
136 taxaToSave
.add(accParent
);
139 // Synonym synonym = CdmBase.deproxy(child, Synonym.class);
140 // if (synonym == null){
141 // logger.warn("Synonym " + id + " not found for taxon ");
143 // if(parent.isInstanceOf(Synonym.class)){
144 // String message = "Taxon ("+parentTaxonFk+") is not accepted but synonym. Can't add synonym ("+id+")";
145 // logger.warn(message);
147 // Taxon accepted = CdmBase.deproxy(parent, Taxon.class);
148 //// accepted.addSynonym(synonym, SynonymType.SYNONYM_OF());
149 // taxaToSave.add(accepted);
156 // String nameSpace = "tax_taxon";
157 // ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
158 // ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
165 // * @param childName
166 // * @param parentName
168 // private void handleMissingNameParts(INonViralName childName, INonViralName parentName) {
169 // if (childName.getGenusOrUninomial())
173 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
174 EdaphobaseImportState state
) {
178 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
181 Set
<String
> taxonIdSet
= new HashSet
<>();
183 handleForeignKey(rs
, taxonIdSet
, "taxon_id");
184 handleForeignKey(rs
, taxonIdSet
, "parent_taxon_fk");
188 nameSpace
= TAXON_NAMESPACE
;
190 @SuppressWarnings("rawtypes")
191 Map
<String
, TaxonBase
> taxonMap
= getCommonService().getSourcedObjectsByIdInSourceC(TaxonBase
.class, idSet
, nameSpace
);
192 result
.put(nameSpace
, taxonMap
);
195 Map
<String
, Classification
> classificationMap
= new HashMap
<>();
196 UUID classificationUuid
= state
.getConfig().getClassificationUuid();
197 Classification classification
= getClassificationService().find(state
.getConfig().getClassificationUuid());
198 classificationMap
.put(classificationUuid
.toString(), classification
);
199 result
.put(CLASSIFICATION_NAMESPACE
, classificationMap
);
201 } catch (SQLException e
) {
202 throw new RuntimeException(e
);
212 private void makeClassification(EdaphobaseImportState state
) {
213 Classification classification
= Classification
.NewInstance(state
.getConfig().getClassificationName());
214 classification
.setUuid(state
.getConfig().getClassificationUuid());
215 getClassificationService().save(classification
);
220 protected boolean doCheck(EdaphobaseImportState state
) {
225 protected boolean isIgnore(EdaphobaseImportState state
) {
226 return ! state
.getConfig().isDoTaxa();