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
.in
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.Collection
;
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
.berlinModel
.BerlinModelTransformer
;
24 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
25 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
26 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
27 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
31 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
33 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
45 public class BerlinModelFactsImport
extends BerlinModelImportBase
{
46 private static final Logger logger
= Logger
.getLogger(BerlinModelFactsImport
.class);
48 private int modCount
= 10000;
50 public BerlinModelFactsImport(){
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 logger
.warn("Checking for Facts not yet implemented");
61 //result &= checkArticlesWithoutJournal(bmiConfig);
62 //result &= checkPartOfJournal(bmiConfig);
67 private TermVocabulary
<Feature
> getFeatureVocabulary(){
69 //TODO work around until service method works
70 TermVocabulary
<Feature
> featureVocabulary
= BerlinModelTransformer
.factCategory2Feature(1).getVocabulary();
71 //TermVocabulary<Feature> vocabulary = getTermService().getVocabulary(vocabularyUuid);
72 return featureVocabulary
;
73 } catch (UnknownCdmTypeException e
) {
74 logger
.error("Feature vocabulary not available. New vocabulary created");
75 return new TermVocabulary
<Feature
>() ;
79 private MapWrapper
<Feature
> invokeFactCategories(BerlinModelImportConfigurator bmiConfig
){
81 // Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
82 MapWrapper
<Feature
> result
= bmiConfig
.getFeatureMap();
84 Source source
= bmiConfig
.getSource();
88 //get data from database
90 " SELECT FactCategory.* " +
91 " FROM FactCategory "+
93 ResultSet rs
= source
.getResultSet(strQuery
) ;
96 TermVocabulary
<Feature
> featureVocabulary
= getFeatureVocabulary();
101 if ((i
++ % modCount
) == 0 && i
!= 1 ){ logger
.info("FactCategories handled: " + (i
-1));}
103 int factCategoryId
= rs
.getInt("factCategoryId");
104 String factCategory
= rs
.getString("factCategory");
109 feature
= BerlinModelTransformer
.factCategory2Feature(factCategoryId
);
110 } catch (UnknownCdmTypeException e
) {
111 logger
.warn("New Feature (FactCategoryId: " + factCategoryId
+ ")");
112 feature
= Feature
.NewInstance(factCategory
, factCategory
, null);
113 feature
.setVocabulary(featureVocabulary
);
114 feature
.setSupportsTextData(true);
116 // MaxFactNumber int Checked
117 // ExtensionTableName varchar(100) Checked
118 // Description nvarchar(1000) Checked
119 // locExtensionFormName nvarchar(80) Checked
120 // RankRestrictionFk int Checked
123 // featureMap.put(factCategoryId, feature);
124 result
.put(factCategoryId
, feature
);
127 Collection
<Feature
> col
= result
.getAllValues();
128 getTermService().saveTermsAll(col
);
130 } catch (SQLException e
) {
131 logger
.error("SQLException:" + e
);
139 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
142 protected boolean doInvoke(BerlinModelImportState
<BerlinModelImportConfigurator
> state
){
143 boolean result
= true;
145 MapWrapper
<TaxonBase
> taxonMap
= (MapWrapper
<TaxonBase
>)state
.getStore(ICdmIO
.TAXON_STORE
);
146 MapWrapper
<ReferenceBase
> referenceMap
= (MapWrapper
<ReferenceBase
>)state
.getStore(ICdmIO
.REFERENCE_STORE
);
147 MapWrapper
<ReferenceBase
> nomRefMap
= (MapWrapper
<ReferenceBase
>)state
.getStore(ICdmIO
.NOMREF_STORE
);
149 Set
<TaxonBase
> taxonStore
= new HashSet
<TaxonBase
>();
151 BerlinModelImportConfigurator config
= state
.getConfig();
152 Source source
= config
.getSource();
154 logger
.info("start makeFacts ...");
156 MapWrapper
<Feature
> featureMap
= invokeFactCategories(config
);
159 //TaxonBase taxonBase = Taxon.NewInstance(BotanicalName.NewInstance(null), null);
163 //get data from database
165 " SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " +
167 " INNER JOIN PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk " +
168 " LEFT OUTER JOIN RefDetail ON Fact.FactRefDetailFk = RefDetail.RefDetailId AND Fact.FactRefFk = RefDetail.RefFk " +
170 ResultSet rs
= source
.getResultSet(strQuery
) ;
176 if ((i
++ % modCount
) == 0){ logger
.info("Facts handled: " + (i
-1));}
178 //Map<String, Object> valueMap = getValueMap(rs);
180 int factId
= rs
.getInt("factId");
183 Object taxonIdObj
= rs
.getObject("taxonId");
184 int taxonId
= rs
.getInt("taxonId");
185 Object factRefFkObj
= rs
.getObject("factRefFk");
186 int factRefFk
= rs
.getInt("factRefFk");
187 Object categoryFkObj
= rs
.getObject("factCategoryFk");
188 Integer categoryFk
= rs
.getInt("factCategoryFk");
190 String details
= rs
.getString("Details");
191 // int ptDesignationRefFk = rs.getInt("PTDesignationRefFk");
192 // String ptDesignation details = rs.getInt("PTDesignationRefDetailFk");
193 String fact
= CdmUtils
.Nz(rs
.getString("Fact"));
194 String notes
= CdmUtils
.Nz(rs
.getString("notes"));
199 if (taxonIdObj
!= null){
200 taxonBase
= taxonMap
.get(taxonId
);
205 if (categoryFkObj
!= null){
206 feature
= featureMap
.get(categoryFk
);
211 if (taxonBase
!= null){
213 if ( taxonBase
instanceof Taxon
) {
214 taxon
= (Taxon
) taxonBase
;
216 logger
.warn("TaxonBase " + (taxonIdObj
==null?
"(null)":taxonIdObj
) + " for Fact " + factId
+ " was not of type Taxon but: " + taxonBase
.getClass().getSimpleName());
220 TaxonDescription taxonDescription
;
221 Set
<TaxonDescription
> descriptionSet
= taxon
.getDescriptions();
222 if (descriptionSet
.size() > 0) {
223 taxonDescription
= descriptionSet
.iterator().next();
225 taxonDescription
= TaxonDescription
.NewInstance();
226 taxon
.addDescription(taxonDescription
);
230 TextData textData
= TextData
.NewInstance();
231 //TODO textData.putText(fact, bmiConfig.getFactLanguage()); //doesn't work because bmiConfig.getFactLanguage() is not not a persistent Language Object
232 //throws in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language
234 //for diptera database
235 if (categoryFk
== 99 && notes
.contains("<OriginalName>")){
236 notes
= notes
.replaceAll("<OriginalName>", "");
237 notes
= notes
.replaceAll("</OriginalName>", "");
238 fact
= notes
+ ": " + fact
;
240 textData
.putText(fact
, Language
.DEFAULT());
241 textData
.setType(feature
);
244 ReferenceBase citation
;
245 if (factRefFkObj
!= null){
246 citation
= referenceMap
.get(factRefFk
);
247 if (citation
== null){
248 citation
= nomRefMap
.get(factRefFk
);
250 if (citation
== null && (factRefFk
!= 0)){
251 logger
.warn("Citation not found in referenceMap: " + factRefFk
);
258 textData
.setCitation(citation
);
259 textData
.setCitationMicroReference(details
);
260 taxonDescription
.addElement(textData
);
263 // if (categoryFkObj == FACT_DESCRIPTION){
265 // }else if (categoryFkObj == FACT_OBSERVATION){
267 // }else if (categoryFkObj == FACT_DISTRIBUTION_EM){
271 // //logger.warn("FactCategory " + categoryFk + " not yet implemented");
279 doCreatedUpdatedNotes(config
, textData
, rs
, "Fact");
282 taxonStore
.add(taxon
);
285 logger
.warn("Taxon for Fact " + factId
+ " does not exist in store");
287 } catch (RuntimeException re
){
288 logger
.error("A runtime exception occurred during the facts import");
294 logger
.info("Facts handled: " + (i
-1));
295 logger
.info("Taxa to save: " + taxonStore
.size());
296 getTaxonService().saveTaxonAll(taxonStore
);
298 logger
.info("end makeFacts ...");
300 } catch (SQLException e
) {
301 logger
.error("SQLException:" + e
);
308 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
310 protected boolean isIgnore(IImportConfigurator config
){
311 return ! config
.isDoFacts();