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
;
18 import java
.util
.UUID
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 public class EdaphobaseClassificationImport
extends EdaphobaseImportBase
{
40 private static final long serialVersionUID
= -9138378836474086070L;
42 private static final Logger logger
= Logger
.getLogger(EdaphobaseClassificationImport
.class);
44 private static final String tableName
= "tax_taxon";
46 private static final String pluralString
= "taxon relationships";
53 public EdaphobaseClassificationImport() {
54 super(tableName
, pluralString
);
58 protected String
getIdQuery(EdaphobaseImportState state
) {
59 return "SELECT taxon_id FROM "
60 + " (SELECT DISTINCT taxon_id, length(path_to_root) FROM tax_taxon t "
61 + " ORDER BY length(path_to_root), taxon_id) as drvTbl ";
65 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
66 String result
= "SELECT DISTINCT t.* "
68 + " WHERE taxon_id IN (@IDSET)";
69 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
74 protected void doInvoke(EdaphobaseImportState state
) {
75 makeClassification(state
);
76 super.doInvoke(state
);
81 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
82 ResultSet rs
= partitioner
.getResultSet();
83 Map
<String
, Classification
> map
= partitioner
.getObjectMap(CLASSIFICATION_NAMESPACE
);
84 Classification classification
= map
.get(state
.getConfig().getClassificationUuid().toString());
85 Reference
<?
> sourceReference
= state
.getTransactionalSourceReference();
87 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
90 int id
= rs
.getInt("taxon_id");
92 boolean isValid
= rs
.getBoolean("valid");
93 // boolean idDeleted = rs.getBoolean("deleted");
94 // String treeIndex = rs.getString("path_to_root");
95 // Integer rankFk = rs.getInt("tax_rank_fk");
96 // String officialRemark = rs.getString("official_remark");
97 // boolean isGroup = rs.getBoolean("taxonomic_group");
98 Integer parentTaxonFk
= nullSafeInt(rs
, "parent_taxon_fk");
100 if (parentTaxonFk
!= null){
101 TaxonBase
<?
> parent
= state
.getRelatedObject(TAXON_NAMESPACE
, parentTaxonFk
.toString(), TaxonBase
.class);
103 logger
.warn("Parent taxon " + parentTaxonFk
+ " not found for taxon " + id
);
106 TaxonNameBase
<?
,?
> parentName
= parent
.getName();
108 TaxonBase
<?
> child
= state
.getRelatedObject(TAXON_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
109 // TaxonNameBase<?,?> childName = child.getName();
111 // handleMissingNameParts(CdmBase.deproxy(childName, NonViralName.class), CdmBase.deproxy(parentName, NonViralName.class));
114 if (parent
.isInstanceOf(Synonym
.class)){
115 logger
.warn("Parent taxon (" + parentTaxonFk
+ " is not valid for valid child " + id
);
117 Taxon accParent
= CdmBase
.deproxy(parent
, Taxon
.class);
118 classification
.addParentChild(accParent
, (Taxon
)child
, sourceReference
, null);
119 taxaToSave
.add(accParent
);
122 // Synonym synonym = CdmBase.deproxy(child, Synonym.class);
123 // if (synonym == null){
124 // logger.warn("Synonym " + id + " not found for taxon ");
126 // if(parent.isInstanceOf(Synonym.class)){
127 // String message = "Taxon ("+parentTaxonFk+") is not accepted but synonym. Can't add synonym ("+id+")";
128 // logger.warn(message);
130 // Taxon accepted = CdmBase.deproxy(parent, Taxon.class);
131 //// accepted.addSynonym(synonym, SynonymRelationshipType.SYNONYM_OF());
132 // taxaToSave.add(accepted);
139 // String nameSpace = "tax_taxon";
140 // ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
141 // ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
145 } catch (SQLException e
) {
149 getTaxonService().saveOrUpdate(taxaToSave
);
154 // * @param childName
155 // * @param parentName
157 // private void handleMissingNameParts(NonViralName<?> childName, NonViralName<?> parentName) {
158 // if (childName.getGenusOrUninomial())
162 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
163 EdaphobaseImportState state
) {
167 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
170 Set
<String
> taxonIdSet
= new HashSet
<String
>();
172 handleForeignKey(rs
, taxonIdSet
, "taxon_id");
173 handleForeignKey(rs
, taxonIdSet
, "parent_taxon_fk");
177 nameSpace
= TAXON_NAMESPACE
;
178 cdmClass
= TaxonBase
.class;
180 Map
<String
, TaxonBase
> taxonMap
= (Map
<String
, TaxonBase
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
181 result
.put(nameSpace
, taxonMap
);
184 Map
<String
, Classification
> classificationMap
= new HashMap
<>();
185 UUID classificationUuid
= state
.getConfig().getClassificationUuid();
186 Classification classification
= getClassificationService().find(state
.getConfig().getClassificationUuid());
187 classificationMap
.put(classificationUuid
.toString(), classification
);
188 result
.put(CLASSIFICATION_NAMESPACE
, classificationMap
);
190 } catch (SQLException e
) {
191 throw new RuntimeException(e
);
201 private void makeClassification(EdaphobaseImportState state
) {
202 Classification classification
= Classification
.NewInstance(state
.getConfig().getClassificationName());
203 classification
.setUuid(state
.getConfig().getClassificationUuid());
204 getClassificationService().save(classification
);
209 protected boolean doCheck(EdaphobaseImportState state
) {
214 protected boolean isIgnore(EdaphobaseImportState state
) {
215 return ! state
.getConfig().isDoTaxa();