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.
9 package eu
.etaxonomy
.cdm
.io
.berlinModel
.out
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.stereotype
.Component
;
19 import org
.springframework
.transaction
.TransactionStatus
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
;
23 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelFactsImport
;
24 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.CreatedAndNotesMapper
;
25 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.DbIntegerAnnotationMapper
;
26 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.DbMarkerMapper
;
27 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.DbObjectMapper
;
28 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IdMapper
;
29 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.MethodMapper
;
30 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.RefDetailMapper
;
31 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
32 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
33 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
36 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
37 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
38 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 public class BerlinModelFactExport
extends BerlinModelExportBase
<TextData
> {
53 private static final Logger logger
= Logger
.getLogger(BerlinModelFactExport
.class);
55 private static int modCount
= 2500;
56 private static final String dbTableName
= "Fact";
57 private static final String pluralString
= "Facts";
58 private static final Class
<?
extends CdmBase
> standardMethodParameter
= TextData
.class;
60 private static Source source
;
62 public BerlinModelFactExport(){
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
70 protected boolean doCheck(IExportConfigurator config
){
71 boolean result
= true;
72 logger
.warn("Checking for " + pluralString
+ " not yet implemented");
73 //result &= checkArticlesWithoutJournal(bmiConfig);
74 //result &= checkPartOfJournal(bmiConfig);
79 private BerlinModelExportMapping
getMapping(){
80 String tableName
= dbTableName
;
81 BerlinModelExportMapping mapping
= new BerlinModelExportMapping(tableName
);
82 mapping
.addMapper(IdMapper
.NewInstance("FactId"));
83 mapping
.addMapper(MethodMapper
.NewInstance("PTNameFk", this.getClass(), "getPTNameFk", TextData
.class, DbExportStateBase
.class));
84 mapping
.addMapper(MethodMapper
.NewInstance("PTRefFk", this.getClass(), "getPTRefFk", TextData
.class, DbExportStateBase
.class));
85 mapping
.addMapper(MethodMapper
.NewInstance("Fact", this));
86 mapping
.addMapper(MethodMapper
.NewInstance("FactCategoryFk", this));
88 mapping
.addMapper(DbObjectMapper
.NewInstance("citation", "FactRefFk"));
89 mapping
.addMapper(RefDetailMapper
.NewInstance("citationMicroReference","citation", "FactRefDetailFk"));
90 mapping
.addMapper(DbObjectMapper
.NewInstance("citation", "PTDesignationRefFk"));
91 mapping
.addMapper(RefDetailMapper
.NewInstance("citationMicroReference","citation", "PTDesignationRefDetailFk"));
92 mapping
.addMapper(DbMarkerMapper
.NewInstance(MarkerType
.IS_DOUBTFUL(), "DoubtfulFlag", false));
93 mapping
.addMapper(DbMarkerMapper
.NewInstance(MarkerType
.IS_DOUBTFUL(), "PublishFlag", true));
94 mapping
.addMapper(DbIntegerAnnotationMapper
.NewInstance(BerlinModelFactsImport
.SEQUENCE_PREFIX
, "Sequence", 999));
95 mapping
.addMapper(CreatedAndNotesMapper
.NewInstance());
102 protected boolean doInvoke(BerlinModelExportState
<BerlinModelExportConfigurator
> state
){
104 logger
.info("start make " + pluralString
+ " ...");
105 boolean success
= true ;
108 TransactionStatus txStatus
= startTransaction(true);
110 List
<DescriptionBase
> list
= getDescriptionService().list(1000000000, 0);
112 BerlinModelExportMapping mapping
= getMapping();
113 mapping
.initialize(state
);
115 this.source
= state
.getConfig().getDestination();
117 for (DescriptionBase
<?
> desc
: list
){
118 for (DescriptionElementBase descEl
: desc
.getElements()){
119 doCount(count
++, modCount
, pluralString
);
120 if (descEl
.isInstanceOf(TextData
.class)){
121 success
&= mapping
.invoke(descEl
);
123 logger
.warn (descEl
.getClass().getSimpleName() + " not yet supported for Fact Export.");
127 commitTransaction(txStatus
);
128 logger
.info("end make " + pluralString
+ " ..." + getSuccessString(success
));
131 }catch(SQLException e
){
133 logger
.error(e
.getMessage());
141 protected boolean doDelete(BerlinModelExportState
<BerlinModelExportConfigurator
> state
){
142 BerlinModelExportConfigurator bmeConfig
= state
.getConfig();
145 Source destination
= bmeConfig
.getDestination();
147 sql
= "DELETE FROM Fact";
148 destination
.setQuery(sql
);
149 destination
.update(sql
);
156 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
158 protected boolean isIgnore(IExportConfigurator config
){
159 return ! ((BerlinModelExportConfigurator
)config
).isDoFacts();
162 //called by MethodMapper
163 @SuppressWarnings("unused")
164 private static Integer
getFactCategoryFk(TextData textData
){
165 Feature feature
= textData
.getFeature();
166 Integer catFk
= BerlinModelTransformer
.textData2FactCategoryFk(feature
);
169 catFk
= findCategory(feature
);
172 //catFk = insertCategory(feature);
178 //called by MethodMapper
179 @SuppressWarnings("unused")
180 private static Integer
getPTNameFk(TextData textData
, DbExportStateBase
<?
> state
){
181 return getObjectFk(textData
, state
, true);
184 //called by MethodMapper
185 @SuppressWarnings("unused")
186 private static Integer
getPTRefFk(TextData textData
, DbExportStateBase
<?
> state
){
187 return getObjectFk(textData
, state
, false);
190 private static Integer
getObjectFk(TextData textData
, DbExportStateBase
<?
> state
, boolean isName
){
191 DescriptionBase
<?
> desc
= textData
.getInDescription();
192 if (desc
.isInstanceOf(TaxonDescription
.class)){
193 TaxonDescription taxonDesc
= (TaxonDescription
)desc
;
194 Taxon taxon
= taxonDesc
.getTaxon();
196 CdmBase cdmBase
= (isName
) ? taxon
.getName(): taxon
.getSec();
197 return state
.getDbId(cdmBase
);
200 logger
.warn("No taxon found for description: " + textData
.toString());
204 //called by MethodMapper
205 @SuppressWarnings("unused")
206 private static String
getFact(TextData textData
){
207 // Map<Language, LanguageString> map = textData.getMultilanguageText();
208 Language preferredLanguage
= null;
210 String result
= textData
.getText(Language
.DEFAULT());
212 Map
<Language
, LanguageString
> map
= textData
.getMultilanguageText();
213 for (Language language
: map
.keySet()){
214 String tmp
= textData
.getText(language
);
215 if (! CdmUtils
.Nz(tmp
).trim().equals("")){
224 private static Map
<Feature
, Integer
> featureMap
= new HashMap
<Feature
, Integer
>();
226 @Deprecated //TODO quick and dirty for Salvador export
227 private static Integer
findCategory(Feature feature
){
228 if (featureMap
.get(feature
) != null){
229 return featureMap
.get(feature
);
231 Integer result
= null;
232 String label
= feature
.getLabel();
233 ResultSet rs
= source
.getResultSet("SELECT FactCategoryId FROM FactCategory WHERE FactCategory = '"+label
+"'");
237 logger
.warn("FactCategory is not distinct: " + label
);
239 result
= rs
.getInt(1) ;
242 } catch (SQLException e
) {
243 logger
.error(e
.getMessage());
247 featureMap
.put(feature
, result
);
253 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
256 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
257 return standardMethodParameter
;