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
.berlinModel
;
12 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.T_STATUS_ACCEPTED
;
13 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.T_STATUS_PARTIAL_SYN
;
14 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.T_STATUS_PRO_PARTE_SYN
;
15 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.T_STATUS_SYNONYM
;
17 import java
.sql
.ResultSet
;
18 import java
.sql
.SQLException
;
20 import java
.util
.UUID
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.springframework
.stereotype
.Component
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
28 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
29 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
30 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
45 public class BerlinModelTaxonImport
extends BerlinModelImportBase
{
46 private static final Logger logger
= Logger
.getLogger(BerlinModelTaxonImport
.class);
48 private int modCount
= 10000;
50 public BerlinModelTaxonImport(){
55 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
58 protected boolean doCheck(IImportConfigurator config
){
59 boolean result
= true;
60 BerlinModelImportConfigurator bmiConfig
= (BerlinModelImportConfigurator
)config
;
61 logger
.warn("Checking for Taxa not yet fully implemented");
62 result
&= checkTaxonStatus(bmiConfig
);
63 result
&= checkInactivated(bmiConfig
);
68 private boolean checkTaxonStatus(BerlinModelImportConfigurator bmiConfig
){
70 boolean result
= true;
71 Source source
= bmiConfig
.getSource();
72 String strSQL
= " SELECT RelPTaxon.RelQualifierFk, RelPTaxon.relPTaxonId, PTaxon.PTNameFk, PTaxon.PTRefFk, PTaxon_1.PTNameFk AS Expr1, PTaxon.RIdentifier, PTaxon_1.RIdentifier AS Expr3, Name.FullNameCache " +
74 " INNER JOIN PTaxon ON RelPTaxon.PTNameFk1 = PTaxon.PTNameFk AND RelPTaxon.PTRefFk1 = PTaxon.PTRefFk " +
75 " INNER JOIN PTaxon AS PTaxon_1 ON RelPTaxon.PTNameFk2 = PTaxon_1.PTNameFk AND RelPTaxon.PTRefFk2 = PTaxon_1.PTRefFk " +
76 " INNER JOIN Name ON PTaxon.PTNameFk = Name.NameId " +
77 " WHERE (dbo.PTaxon.StatusFk = 1) AND ((RelPTaxon.RelQualifierFk = 7) OR (RelPTaxon.RelQualifierFk = 6) OR (RelPTaxon.RelQualifierFk = 2)) ";
78 ResultSet rs
= source
.getResultSet(strSQL
);
79 boolean firstRow
= true;
84 System
.out
.println("========================================================");
85 logger
.warn("There are taxa that have a 'is synonym of' - relationship but having taxon status 'accepted'!");
86 System
.out
.println("========================================================");
88 int rIdentifier
= rs
.getInt("RIdentifier");
89 int nameFk
= rs
.getInt("PTNameFk");
90 int refFk
= rs
.getInt("PTRefFk");
91 int relPTaxonId
= rs
.getInt("relPTaxonId");
92 String taxonName
= rs
.getString("FullNameCache");
94 System
.out
.println("RIdentifier:" + rIdentifier
+ "\n name: " + nameFk
+
95 "\n taxonName: " + taxonName
+ "\n refId: " + refFk
+ "\n RelPTaxonId: " + relPTaxonId
);
96 result
= firstRow
= false;
99 System
.out
.println(" ");
103 } catch (SQLException e
) {
109 private boolean checkInactivated(BerlinModelImportConfigurator bmiConfig
){
111 boolean result
= true;
112 Source source
= bmiConfig
.getSource();
113 String strSQL
= " SELECT * " +
115 " INNER JOIN Name ON PTaxon.PTNameFk = Name.NameId " +
116 " WHERE (PTaxon.DoubtfulFlag = 'i') ";
117 ResultSet rs
= source
.getResultSet(strSQL
);
118 boolean firstRow
= true;
123 System
.out
.println("========================================================");
124 logger
.warn("There are taxa that have a doubtful flag 'i'(inactivated). Inactivated is not supported by CDM!");
125 System
.out
.println("========================================================");
127 int rIdentifier
= rs
.getInt("RIdentifier");
128 int nameFk
= rs
.getInt("PTNameFk");
129 int refFk
= rs
.getInt("PTRefFk");
130 String taxonName
= rs
.getString("FullNameCache");
132 System
.out
.println("RIdentifier:" + rIdentifier
+ "\n nameId: " + nameFk
+
133 "\n taxonName: " + taxonName
+ "\n refId: " + refFk
);
134 result
= firstRow
= false;
137 System
.out
.println(" ");
141 } catch (SQLException e
) {
148 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
151 protected boolean doInvoke(IImportConfigurator config
,
152 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
){
154 //make not needed maps empty
155 String teamStore
= ICdmIO
.TEAM_STORE
;
156 MapWrapper
<?
extends CdmBase
> store
= stores
.get(teamStore
);
157 MapWrapper
<TeamOrPersonBase
> teamMap
= (MapWrapper
<TeamOrPersonBase
>)store
;
161 MapWrapper
<TaxonNameBase
<?
,?
>> taxonNameMap
= (MapWrapper
<TaxonNameBase
<?
,?
>>)stores
.get(ICdmIO
.TAXONNAME_STORE
);
162 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.REFERENCE_STORE
);
163 MapWrapper
<ReferenceBase
> nomRefMap
= (MapWrapper
<ReferenceBase
>)stores
.get(ICdmIO
.NOMREF_STORE
);
164 MapWrapper
<TaxonBase
> taxonMap
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
166 BerlinModelImportConfigurator bmiConfig
= (BerlinModelImportConfigurator
)config
;
167 Source source
= bmiConfig
.getSource();
169 logger
.info("start makeTaxa ...");
171 String namespace
= "PTaxon";
174 //get data from database
180 ResultSet rs
= source
.getResultSet(strQuery
) ;
186 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info("PTaxa handled: " + (i
-1));}
188 //create TaxonName element
189 int taxonId
= rs
.getInt("RIdentifier");
190 int statusFk
= rs
.getInt("statusFk");
192 int nameFk
= rs
.getInt("PTNameFk");
193 int refFk
= rs
.getInt("PTRefFk");
194 String doubtful
= rs
.getString("DoubtfulFlag");
196 if (resultSetHasColumn(rs
,"UUID")){
197 uuid
= rs
.getString("UUID");
200 TaxonNameBase
<?
,?
> taxonName
= null;
201 if (taxonNameMap
!= null){
202 taxonName
= taxonNameMap
.get(nameFk
);
205 ReferenceBase reference
= null;
206 if (referenceMap
!= null){
207 reference
= referenceMap
.get(refFk
);
208 if (reference
== null){
209 reference
= nomRefMap
.get(refFk
);
213 if(! config
.isIgnoreNull()){
214 if (taxonName
== null ){
215 logger
.warn("TaxonName belonging to taxon (RIdentifier = " + taxonId
+ ") could not be found in store. Taxon will not be transported");
216 continue; //next taxon
217 }else if (reference
== null ){
218 logger
.warn("Reference belonging to taxon could not be found in store. Taxon will not be imported");
219 continue; //next taxon
226 logger
.debug(statusFk
);
227 if (statusFk
== T_STATUS_ACCEPTED
){
228 taxon
= Taxon
.NewInstance(taxonName
, reference
);
230 }else if (statusFk
== T_STATUS_SYNONYM
|| statusFk
== T_STATUS_PRO_PARTE_SYN
|| statusFk
== T_STATUS_PARTIAL_SYN
){
231 synonym
= Synonym
.NewInstance(taxonName
, reference
);
233 if (statusFk
== T_STATUS_PRO_PARTE_SYN
){
234 bmiConfig
.addProParteSynonym(synonym
);
236 if (statusFk
== T_STATUS_PARTIAL_SYN
){
237 bmiConfig
.addPartialSynonym(synonym
);
240 logger
.warn("TaxonStatus " + statusFk
+ " not yet implemented. Taxon (RIdentifier = " + taxonId
+ ") left out.");
244 taxonBase
.setUuid(UUID
.fromString(uuid
));
248 // dbAttrName = "Detail";
249 // cdmAttrName = "Micro";
250 // ImportHelper.addStringValue(rs, taxonBase, dbAttrName, cdmAttrName);
252 if (doubtful
.equals("a")){
253 taxonBase
.setDoubtful(false);
254 }else if(doubtful
.equals("d")){
255 taxonBase
.setDoubtful(true);
256 }else if(doubtful
.equals("i")){
258 logger
.warn("Doubtful = i (inactivated) not yet implemented. Doubtful set to false");
262 ImportHelper
.setOriginalSource(taxonBase
, bmiConfig
.getSourceReference(), taxonId
, namespace
);
271 taxonMap
.put(taxonId
, taxonBase
);
272 } catch (Exception e
) {
273 logger
.warn("An exception occurred when creating taxon with id " + taxonId
+ ". Taxon could not be saved.");
276 //invokeRelations(source, cdmApp, deleteAll, taxonMap, referenceMap);
277 logger
.info("saving taxa ...");
278 getTaxonService().saveTaxonAll(taxonMap
.objects());
280 logger
.info("end makeTaxa ...");
283 } catch (SQLException e
) {
284 logger
.error("SQLException:" + e
);
293 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
295 protected boolean isIgnore(IImportConfigurator config
){
296 return ! config
.isDoTaxa();