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
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.stereotype
.Component
;
22 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
23 import eu
.etaxonomy
.cdm
.io
.common
.DbImportBase
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
32 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
46 @SuppressWarnings("serial")
47 public class RedListGefaesspflanzenImportClassification
extends DbImportBase
<RedListGefaesspflanzenImportState
, RedListGefaesspflanzenImportConfigurator
> {
49 private static final Logger logger
= Logger
.getLogger(RedListGefaesspflanzenImportClassification
.class);
51 private static final String tableName
= "Rote Liste Gefäßpflanzen";
53 private static final String pluralString
= "classifications";
55 public RedListGefaesspflanzenImportClassification() {
56 super(tableName
, pluralString
);
60 protected String
getIdQuery(RedListGefaesspflanzenImportState state
) {
61 return "SELECT NAMNR "
62 + "FROM V_TAXATLAS_D20_EXPORT t "
67 protected String
getRecordQuery(RedListGefaesspflanzenImportConfigurator config
) {
68 String result
= " SELECT * "
69 + " FROM V_TAXATLAS_D20_EXPORT t "
70 + " WHERE t.NAMNR IN (@IDSET)";
71 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
76 protected void doInvoke(RedListGefaesspflanzenImportState state
) {
77 makeClassification(state
);
78 super.doInvoke(state
);
83 public boolean doPartition(ResultSetPartitioner partitioner
, RedListGefaesspflanzenImportState state
) {
84 ResultSet rs
= partitioner
.getResultSet();
85 Classification gesamtListeClassification
= getClassificationService().load(state
.getConfig().getClassificationUuid());
86 Classification checklistClassification
= getClassificationService().load(RedListUtil
.checkListClassificationUuid
);
89 makeSingleTaxonNode(state
, rs
, gesamtListeClassification
, checklistClassification
);
92 } catch (SQLException e
) {
96 logger
.info("Update classification (1000 nodes)");
97 getClassificationService().saveOrUpdate(gesamtListeClassification
);
98 getClassificationService().saveOrUpdate(checklistClassification
);
102 private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state
, ResultSet rs
, Classification gesamtListeClassification
, Classification checklistClassification
)
103 throws SQLException
{
104 long id
= rs
.getLong(RedListUtil
.NAMNR
);
105 String parentId
= String
.valueOf(rs
.getLong(RedListUtil
.LOWER
));
106 String gueltString
= rs
.getString(RedListUtil
.GUELT
);
107 String taxZusatzString
= rs
.getString(RedListUtil
.TAX_ZUSATZ
);
110 TaxonBase taxonBaseGL
= state
.getRelatedObject(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
111 Taxon parentGL
= (Taxon
) state
.getRelatedObject(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, parentId
, TaxonBase
.class);
112 createParentChildNodes(gesamtListeClassification
, id
, gueltString
, taxZusatzString
, taxonBaseGL
, parentGL
);
115 TaxonBase taxonBaseCL
= state
.getRelatedObject(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, String
.valueOf(id
), TaxonBase
.class);
116 Taxon parentCL
= (Taxon
) state
.getRelatedObject(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, parentId
, TaxonBase
.class);
117 if(taxonBaseCL
!=null){//null check necessary because not all taxa exist in the checklist
118 createParentChildNodes(checklistClassification
, id
, gueltString
, taxZusatzString
, taxonBaseCL
, parentCL
);
122 private void createParentChildNodes(Classification classification
, long id
, String gueltString
,
123 String taxZusatzString
, TaxonBase taxonBase
, Taxon parent
) {
125 RedListUtil
.logMessage(id
, "parent taxon of "+taxonBase
+" is null." , logger
);
129 RedListUtil
.logMessage(id
, "child taxon/synonym of "+parent
+" is null." , logger
);
133 if(taxonBase
.isInstanceOf(Taxon
.class)){
135 String appendedPhrase
= taxonBase
.getAppendedPhrase();
136 if(appendedPhrase
!=null && appendedPhrase
.equals(RedListUtil
.AUCT
)){
137 parent
.addMisappliedName((Taxon
) taxonBase
, null, null);
140 classification
.addParentChild(parent
, (Taxon
)taxonBase
, null, null);
143 if(CdmUtils
.isNotBlank(taxZusatzString
)){
144 if(taxZusatzString
.trim().equals("p. p.")){
145 RedListUtil
.logMessage(id
, "pro parte for accepted taxon "+taxonBase
, logger
);
150 else if(taxonBase
.isInstanceOf(Synonym
.class)){
152 if(gueltString
.equals(RedListUtil
.GUELT_BASIONYM
)){
153 parent
.addHomotypicSynonym((Synonym
) taxonBase
, null, null);
154 parent
.getName().addBasionym(taxonBase
.getName());
158 SynonymRelationship synonymRelationship
= parent
.addSynonym((Synonym
) taxonBase
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF(), null, null);
161 if(CdmUtils
.isNotBlank(taxZusatzString
)){
162 if(taxZusatzString
.trim().equals("p. p.")){
163 synonymRelationship
.setProParte(true);
165 else if(taxZusatzString
.trim().equals("s. l. p. p.")){
166 synonymRelationship
.setProParte(true);
167 taxonBase
.setAppendedPhrase("s. l.");
169 else if(taxZusatzString
.trim().equals("s. str. p. p.")){
170 synonymRelationship
.setProParte(true);
171 taxonBase
.setAppendedPhrase("s. str.");
173 else if(taxZusatzString
.trim().equals("s. l.")
174 || taxZusatzString
.trim().equals("s. str.")){
175 taxonBase
.setAppendedPhrase(taxZusatzString
);
178 RedListUtil
.logMessage(id
, "unknown value "+taxZusatzString
+" for column "+RedListUtil
.TAX_ZUSATZ
, logger
);
184 taxonBase
.setSec(classification
.getReference());
188 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
189 RedListGefaesspflanzenImportState state
) {
190 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
192 Set
<String
> idSet
= new HashSet
<String
>();
195 idSet
.add(String
.valueOf(rs
.getLong(RedListUtil
.NAMNR
)));
196 idSet
.add(String
.valueOf(rs
.getLong(RedListUtil
.LOWER
)));
198 } catch (SQLException e
) {
201 Map
<String
, TaxonBase
> taxonMapGesamtListe
= (Map
<String
, TaxonBase
>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase
.class, idSet
, RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
);
202 result
.put(RedListUtil
.TAXON_GESAMTLISTE_NAMESPACE
, taxonMapGesamtListe
);
203 Map
<String
, TaxonBase
> taxonMapCheckliste
= (Map
<String
, TaxonBase
>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase
.class, idSet
, RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
);
204 result
.put(RedListUtil
.TAXON_CHECKLISTE_NAMESPACE
, taxonMapCheckliste
);
208 private void makeClassification(RedListGefaesspflanzenImportState state
) {
210 Classification classification
= Classification
.NewInstance(state
.getConfig().getClassificationName());
211 classification
.setName(LanguageString
.NewInstance("Gesamtliste", Language
.DEFAULT()));
212 classification
.setUuid(state
.getConfig().getClassificationUuid());
213 Reference gesamtListeReference
= ReferenceFactory
.newGeneric();
214 gesamtListeReference
.setUuid(RedListUtil
.gesamtListeReferenceUuid
);
215 gesamtListeReference
.setTitle("Gesamtliste");
216 classification
.setReference(gesamtListeReference
);
217 getClassificationService().save(classification
);
219 Classification checklistClassification
= Classification
.NewInstance("Checkliste");
220 checklistClassification
.setUuid(RedListUtil
.checkListClassificationUuid
);
221 Reference checklistReference
= ReferenceFactory
.newGeneric();
222 checklistReference
.setUuid(RedListUtil
.checkListClassificationUuid
);
223 checklistReference
.setTitle("Gesamtliste");
224 checklistClassification
.setReference(checklistReference
);
225 getClassificationService().save(checklistClassification
);
229 protected boolean doCheck(RedListGefaesspflanzenImportState state
) {
234 protected boolean isIgnore(RedListGefaesspflanzenImportState state
) {