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
.redlist
.gefaesspflanzen
;
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
.common
.CdmUtils
;
24 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
25 import eu
.etaxonomy
.cdm
.io
.common
.DbImportBase
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
47 @SuppressWarnings("serial")
48 public class RedListGefaesspflanzenImportClassification
extends DbImportBase
<RedListGefaesspflanzenImportState
, RedListGefaesspflanzenImportConfigurator
> {
50 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportClassification
.class);
52 private static final String tableName
= "Rote Liste Gefäßpflanzen";
54 private static final String pluralString
= "classifications";
56 public RedListGefaesspflanzenImportClassification() {
57 super(tableName
, pluralString
);
61 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
62 return "SELECT NAMNR "
63 + "FROM V_TAXATLAS_D20_EXPORT t "
68 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
69 String result
= " SELECT * "
70 + " FROM V_TAXATLAS_D20_EXPORT t "
71 + " WHERE t.NAMNR IN (@IDSET)";
72 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
77 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
78 makeClassification("Gesamtliste", state
.getConfig().getClassificationUuid(), "Gesamtliste_ref", RedListUtil
.gesamtListeReferenceUuid
, state
);
79 makeClassification("Checkliste", RedListUtil
.checkListClassificationUuid
, "Checkliste_ref", RedListUtil
.checkListReferenceUuid
, state
);
80 makeClassification("E", RedListUtil
.uuidClassificationE
, "E_ref", RedListUtil
.uuidClassificationReferenceE
, state
);
81 makeClassification("W", RedListUtil
.uuidClassificationW
, "W_ref", RedListUtil
.uuidClassificationReferenceW
, state
);
82 makeClassification("K", RedListUtil
.uuidClassificationK
, "K_ref", RedListUtil
.uuidClassificationReferenceK
, state
);
83 makeClassification("AW", RedListUtil
.uuidClassificationAW
, "AW_ref", RedListUtil
.uuidClassificationReferenceAW
, state
);
84 makeClassification("AO", RedListUtil
.uuidClassificationAO
, "AO_ref", RedListUtil
.uuidClassificationReferenceAO
, state
);
85 makeClassification("R", RedListUtil
.uuidClassificationR
, "R_ref", RedListUtil
.uuidClassificationReferenceR
, state
);
86 makeClassification("O", RedListUtil
.uuidClassificationO
, "O_ref", RedListUtil
.uuidClassificationReferenceO
, state
);
87 makeClassification("S", RedListUtil
.uuidClassificationS
, "S_ref", RedListUtil
.uuidClassificationReferenceS
, state
);
88 super.doInvoke(state
);
93 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
94 ResultSet rs
= partitioner
.getResultSet();
95 Classification gesamtListeClassification
= getClassificationService().load(state
.getConfig().getClassificationUuid());
96 Classification checklistClassification
= getClassificationService().load(RedListUtil
.checkListClassificationUuid
);
97 Classification classificationE
= getClassificationService().load(RedListUtil
.uuidClassificationE
);
98 Classification classificationW
= getClassificationService().load(RedListUtil
.uuidClassificationW
);
99 Classification classificationK
= getClassificationService().load(RedListUtil
.uuidClassificationK
);
100 Classification classificationAW
= getClassificationService().load(RedListUtil
.uuidClassificationAW
);
101 Classification classificationAO
= getClassificationService().load(RedListUtil
.uuidClassificationAO
);
102 Classification classificationR
= getClassificationService().load(RedListUtil
.uuidClassificationR
);
103 Classification classificationO
= getClassificationService().load(RedListUtil
.uuidClassificationO
);
104 Classification classificationS
= getClassificationService().load(RedListUtil
.uuidClassificationS
);
107 makeSingleTaxonNode(state
, rs
, gesamtListeClassification
, checklistClassification
,
108 classificationE
, classificationW
, classificationK
, classificationAW
109 , classificationAO
, classificationR
, classificationO
, classificationS
);
112 } catch (SQLException e
) {
116 logger
.info("Update classification (1000 nodes)");
117 getClassificationService().saveOrUpdate(gesamtListeClassification
);
118 getClassificationService().saveOrUpdate(checklistClassification
);
122 private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state
, ResultSet rs
,
123 Classification gesamtListeClassification
, Classification checklistClassification
,
124 Classification classificationE
, Classification classificationW
, Classification classificationK
,
125 Classification classificationAW
, Classification classificationAO
, Classification classificationR
,
126 Classification classificationO
, Classification classificationS
)
127 throws SQLException
{
128 long id
= rs
.getLong(RedListUtil
.NAMNR
);
129 String parentId
= String
.valueOf(rs
.getLong(RedListUtil
.LOWER
));
130 String gueltString
= rs
.getString(RedListUtil
.GUELT
);
131 String taxZusatzString
= rs
.getString(RedListUtil
.TAX_ZUSATZ
);
134 TaxonBase taxonBaseGL
= state
.getRelatedObject(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
135 Taxon parentGL
= (Taxon
) state
.getRelatedObject(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, parentId
, TaxonBase
.class);
136 createParentChildNodes(gesamtListeClassification
, id
, gueltString
, taxZusatzString
, taxonBaseGL
, parentGL
);
139 TaxonBase taxonBaseCL
= state
.getRelatedObject(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
140 Taxon parentCL
= (Taxon
) state
.getRelatedObject(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, parentId
, TaxonBase
.class);
141 if(taxonBaseCL
!=null){//null check necessary because not all taxa exist in the checklist
142 createParentChildNodes(checklistClassification
, id
, gueltString
, taxZusatzString
, taxonBaseCL
, parentCL
);
145 if(taxonBaseGL
!= null && taxonBaseCL
!=null
146 && taxonBaseGL
.getUuid().equals(taxonBaseCL
.getUuid())){
147 RedListUtil
.logMessage(id
, "Same UUID for "+taxonBaseGL
+ " (Gesamtliste) and "+taxonBaseCL
+" (Checkliste)", logger
);
149 if(parentGL
!=null && parentCL
!=null && parentGL
.getUuid().equals(parentCL
.getUuid())){
150 RedListUtil
.logMessage(id
, "Same UUID for "+parentGL
+ " (Gesamtliste) and "+parentCL
+" (Checkliste)", logger
);
153 //add taxa for concept relationships to E, W, K, AW, AO, R, O, S
154 addTaxonToClassification(classificationE
, RedListUtil
.CLASSIFICATION_NAMESPACE_E
, id
, state
);
155 addTaxonToClassification(classificationW
, RedListUtil
.CLASSIFICATION_NAMESPACE_W
, id
, state
);
156 addTaxonToClassification(classificationK
, RedListUtil
.CLASSIFICATION_NAMESPACE_K
, id
, state
);
157 addTaxonToClassification(classificationAW
, RedListUtil
.CLASSIFICATION_NAMESPACE_AW
, id
, state
);
158 addTaxonToClassification(classificationAO
, RedListUtil
.CLASSIFICATION_NAMESPACE_AO
, id
, state
);
159 addTaxonToClassification(classificationR
, RedListUtil
.CLASSIFICATION_NAMESPACE_R
, id
, state
);
160 addTaxonToClassification(classificationO
, RedListUtil
.CLASSIFICATION_NAMESPACE_O
, id
, state
);
161 addTaxonToClassification(classificationS
, RedListUtil
.CLASSIFICATION_NAMESPACE_S
, id
, state
);
164 private void addTaxonToClassification(Classification classification
, String classificationNamespace
, long id
, RedListGefaesspflanzenImportState state
){
165 Taxon taxon
= HibernateProxyHelper
.deproxy(state
.getRelatedObject(classificationNamespace
, String
.valueOf(id
), TaxonBase
.class), Taxon
.class);
166 classification
.addChildTaxon(taxon
, null, null);
169 private void createParentChildNodes(Classification classification
, long id
, String gueltString
,
170 String taxZusatzString
, TaxonBase taxonBase
, Taxon parent
) {
172 RedListUtil
.logMessage(id
, "child taxon/synonym of "+parent
+" is null. ("+classification
.getTitleCache()+")" , logger
);
176 if(taxonBase
.isInstanceOf(Taxon
.class)){
178 String appendedPhrase
= taxonBase
.getAppendedPhrase();
179 if(appendedPhrase
!=null && appendedPhrase
.equals(RedListUtil
.AUCT
)){
181 RedListUtil
.logMessage(id
, "parent taxon of misapplied name "+taxonBase
+" is null. ("+classification
.getTitleCache()+")" , logger
);
184 parent
.addMisappliedName((Taxon
) taxonBase
, null, null);
187 classification
.addParentChild(parent
, (Taxon
)taxonBase
, null, null);
190 if(CdmUtils
.isNotBlank(taxZusatzString
)){
191 if(taxZusatzString
.trim().equals("p. p.")){
192 RedListUtil
.logMessage(id
, "pro parte for accepted taxon "+taxonBase
, logger
);
197 else if(taxonBase
.isInstanceOf(Synonym
.class)){
199 RedListUtil
.logMessage(id
, "parent taxon of synonym "+taxonBase
+" is null. ("+classification
.getTitleCache()+")" , logger
);
203 if(gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
204 parent
.addHomotypicSynonym((Synonym
) taxonBase
, null, null);
205 parent
.getName().addBasionym(taxonBase
.getName());
209 SynonymRelationship synonymRelationship
= parent
.addSynonym((Synonym
) taxonBase
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF(), null, null);
212 if(CdmUtils
.isNotBlank(taxZusatzString
)){
213 if(taxZusatzString
.trim().equals("p. p.")){
214 synonymRelationship
.setProParte(true);
216 else if(taxZusatzString
.trim().equals("s. l. p. p.")){
217 synonymRelationship
.setProParte(true);
218 taxonBase
.setAppendedPhrase("s. l.");
220 else if(taxZusatzString
.trim().equals("s. str. p. p.")){
221 synonymRelationship
.setProParte(true);
222 taxonBase
.setAppendedPhrase("s. str.");
224 else if(taxZusatzString
.trim().equals("s. l.")
225 || taxZusatzString
.trim().equals("s. str.")){
226 taxonBase
.setAppendedPhrase(taxZusatzString
);
229 RedListUtil
.logMessage(id
, "unknown value "+taxZusatzString
+" for column "+RedListUtil
.TAX_ZUSATZ
, logger
);
235 taxonBase
.setSec(classification
.getReference());
236 taxonBase
.setTitleCache(null, false);//refresh title cache
240 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
241 RedListGefaesspflanzenImportState state
) {
242 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
244 Set
<String
> idSet
= new HashSet
<String
>();
247 idSet
.add(String
.valueOf(rs
.getLong(RedListUtil
.NAMNR
)));
248 idSet
.add(String
.valueOf(rs
.getLong(RedListUtil
.LOWER
)));
250 } catch (SQLException e
) {
253 Map
<String
, TaxonBase
> taxonMapGesamtListe
= (Map
<String
, TaxonBase
>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase
.class, idSet
, RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
);
254 result
.put(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, taxonMapGesamtListe
);
255 Map
<String
, TaxonBase
> taxonMapCheckliste
= (Map
<String
, TaxonBase
>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase
.class, idSet
, RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
);
256 result
.put(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, taxonMapCheckliste
);
260 private void makeClassification(String classificationName
, UUID classificationUuid
, String referenceName
, UUID referenceUuid
, RedListGefaesspflanzenImportState state
) {
261 Classification classification
= Classification
.NewInstance(classificationName
, Language
.DEFAULT());
262 classification
.setUuid(classificationUuid
);
263 Reference gesamtListeReference
= ReferenceFactory
.newGeneric();
264 gesamtListeReference
.setTitle(referenceName
);
265 gesamtListeReference
.setUuid(referenceUuid
);
266 classification
.setReference(gesamtListeReference
);
267 getClassificationService().save(classification
);
271 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
276 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {