1 package eu
.etaxonomy
.cdm
.io
.berlinModel
;
3 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.*;
5 import java
.sql
.ResultSet
;
6 import java
.sql
.SQLException
;
7 import java
.util
.HashSet
;
10 import org
.apache
.log4j
.Logger
;
12 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
13 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
14 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
15 import eu
.etaxonomy
.cdm
.io
.source
.Source
;
16 import eu
.etaxonomy
.cdm
.model
.agent
.Agent
;
17 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
18 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
19 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
20 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
21 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
22 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
23 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownRankException
;
25 public class BerlinModelTaxonNameIO
{
26 private static final Logger logger
= Logger
.getLogger(BerlinModelTaxonNameIO
.class);
28 private static int modCount
= 5000;
31 static boolean invokeStatus(ReferenceBase berlinModelRef
, Source source
, CdmApplicationController cdmApp
,
32 boolean deleteAll
, MapWrapper
<TaxonNameBase
> taxonNameMap
,
33 MapWrapper
<ReferenceBase
> referenceMap
, MapWrapper
<Agent
> authorMap
){
37 public static boolean invoke(ReferenceBase berlinModelRef
, Source source
, CdmApplicationController cdmApp
,
38 boolean deleteAll
, MapWrapper
<TaxonNameBase
> taxonNameMap
,
39 MapWrapper
<ReferenceBase
> referenceMap
, MapWrapper
<Agent
> authorMap
){
44 boolean success
= true ;
46 logger
.info("start makeTaxonNames ...");
47 INameService nameService
= cdmApp
.getNameService();
48 boolean delete
= deleteAll
;
53 //get data from database
55 "SELECT Name.* , RefDetail.RefDetailId, RefDetail.RefFk, " +
56 " RefDetail.FullRefCache, RefDetail.FullNomRefCache, RefDetail.PreliminaryFlag AS RefDetailPrelim, RefDetail.Details, " +
57 " RefDetail.SecondarySources, RefDetail.IdInSource " +
58 " FROM Name LEFT OUTER JOIN RefDetail ON Name.NomRefDetailFk = RefDetail.RefDetailId AND Name.NomRefDetailFk = RefDetail.RefDetailId AND " +
59 " Name.NomRefFk = RefDetail.RefFk AND Name.NomRefFk = RefDetail.RefFk" +
61 //strQuery += " AND Name.Created_When > '03.03.2004' ";
64 ResultSet rs
= source
.getResultSet(strQuery
) ;
70 if ((i
++ % modCount
) == 0){ logger
.info("Names handled: " + (i
-1));}
72 //create TaxonName element
73 int nameId
= rs
.getInt("nameId");
74 int rankId
= rs
.getInt("rankFk");
75 Object nomRefFk
= rs
.getObject("NomRefFk");
78 if (logger
.isDebugEnabled()){logger
.debug(rankId
);}
79 Rank rank
= BerlinModelTransformer
.rankId2Rank(rankId
);
81 //BotanicalName name = BotanicalName.NewInstance(BerlinModelTransformer.rankId2Rank(rankId));
82 BotanicalName botanicalName
= new BotanicalName(rank
);
85 dbAttrName
= "supraGenericName";
89 cdmAttrName
= "genusOrUninomial";
90 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
92 dbAttrName
= "genusSubdivisionEpi";
93 cdmAttrName
= "infraGenericEpithet";
94 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
96 dbAttrName
= "speciesEpi";
97 cdmAttrName
= "specificEpithet";
98 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
101 dbAttrName
= "infraSpeciesEpi";
102 cdmAttrName
= "infraSpecificEpithet";
103 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
105 dbAttrName
= "unnamedNamePhrase";
106 cdmAttrName
= "appendedPhrase";
107 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
109 dbAttrName
= "preliminaryFlag";
110 cdmAttrName
= "XX" + "protectedTitleCache";
111 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
113 dbAttrName
= "HybridFormulaFlag";
114 cdmAttrName
= "isHybridFormula";
115 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
117 dbAttrName
= "MonomHybFlag";
118 cdmAttrName
= "isMonomHybrid";
119 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
121 dbAttrName
= "BinomHybFlag";
122 cdmAttrName
= "isBinomHybrid";
123 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
125 dbAttrName
= "TrinomHybFlag";
126 cdmAttrName
= "isTrinomHybrid";
127 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
131 // dbAttrName = "notes";
132 // cdmAttrName = "isTrinomHybrid";
133 // ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
146 dbAttrName
= "details";
147 cdmAttrName
= "nomenclaturalMicroReference";
148 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
153 if (referenceMap
!= null){
154 if (nomRefFk
!= null){
155 int nomRefFkInt
= (Integer
)nomRefFk
;
156 ReferenceBase nomenclaturalReference
= referenceMap
.get(nomRefFkInt
);
157 if (nomenclaturalReference
== null){
159 logger
.warn("Nomenclatural reference (nomRefFk = " + nomRefFkInt
+ ") for TaxonName (nameId = " + nameId
+ ")"+
160 " was not found in reference store. Relation was not set!!");
161 }else if (! INomenclaturalReference
.class.isAssignableFrom(nomenclaturalReference
.getClass())){
162 logger
.error("Nomenclatural reference (nomRefFk = " + nomRefFkInt
+ ") for TaxonName (nameId = " + nameId
+ ")"+
163 " is not assignable from INomenclaturalReference. Relation was not set!! (Class = " + nomenclaturalReference
.getClass()+ ")");
165 botanicalName
.setNomenclaturalReference((INomenclaturalReference
)nomenclaturalReference
);
172 // Annotation annotation = new Annotation("Berlin Model nameId: " + String.valueOf(refId), Language.DEFAULT());
173 // botanicalName.addAnnotations(annotation);
176 ImportHelper
.setOriginalSource(botanicalName
, berlinModelRef
, nameId
);
178 taxonNameMap
.put(nameId
, botanicalName
);
180 } catch (UnknownRankException e
) {
181 logger
.warn("Name with id " + nameId
+ " has unknown rankId " + rankId
+ " and could not be saved.");
185 } //while rs.hasNext()
186 logger
.info(i
+ " names handled");
187 nameService
.saveTaxonNameAll(taxonNameMap
.objects());
189 // makeNameSpecificData(nameMap);
191 logger
.info("end makeTaxonNames ...");
193 } catch (SQLException e
) {
194 logger
.error("SQLException:" + e
);
200 public static boolean invokeRelations(ReferenceBase berlinModelRef
, Source source
, CdmApplicationController cdmApp
, boolean deleteAll
,
201 MapWrapper
<TaxonNameBase
> nameMap
, MapWrapper
<ReferenceBase
> referenceMap
){
203 Set
<TaxonNameBase
> nameStore
= new HashSet
<TaxonNameBase
>();
208 logger
.info("start makeNameRelationships ...");
210 INameService nameService
= cdmApp
.getNameService();
211 boolean delete
= deleteAll
;
214 //get data from database
216 " SELECT RelName.*, FromName.nameId as name1Id, ToName.nameId as name2Id " +
217 " FROM Name as FromName INNER JOIN " +
218 " RelName ON FromName.NameId = RelName.NameFk1 INNER JOIN " +
219 " Name AS ToName ON RelName.NameFk2 = ToName.NameId "+
221 ResultSet rs
= source
.getResultSet(strQuery
) ;
227 if ((i
++ % modCount
) == 0){ logger
.info("RelName handled: " + (i
-1));}
229 int relNameId
= rs
.getInt("RelNameId");
230 int name1Id
= rs
.getInt("name1Id");
231 int name2Id
= rs
.getInt("name2Id");
232 int relRefFk
= rs
.getInt("refFk");
233 int relQualifierFk
= rs
.getInt("relNameQualifierFk");
235 TaxonNameBase nameFrom
= nameMap
.get(name1Id
);
236 TaxonNameBase nameTo
= nameMap
.get(name2Id
);
239 ReferenceBase citation
= null;
240 String microcitation
= null;
242 if (nameFrom
!= null && nameTo
!= null){
243 if (relQualifierFk
== NAME_REL_IS_BASIONYM_FOR
){
244 //TODO references, mikroref, etc
245 nameTo
.setBasionym(nameFrom
);
246 }else if (relQualifierFk
== NAME_REL_IS_LATER_HOMONYM_OF
){
247 String rule
= null; //TODO
248 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.LATER_HOMONYM(), rule
) ;
250 }else if (relQualifierFk
== NAME_REL_IS_REPLACED_SYNONYM_FOR
){
251 String rule
= null; //TODO
252 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.REPLACED_SYNONYM(), rule
) ;
254 }else if (relQualifierFk
== NAME_REL_IS_TYPE_OF
){
255 logger
.warn("NameRelationShipType " + relQualifierFk
+ " not yet implemented");
257 }else if (relQualifierFk
== NAME_REL_IS_ORTHOGRAPHIC_VARIANT_OF
){
258 String rule
= null; //TODO
259 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.ORTHOGRAPHIC_VARIANT(), rule
) ;
263 logger
.warn("NameRelationShipType " + relQualifierFk
+ " not yet implemented");
265 nameStore
.add(nameFrom
);
273 if (nameFrom
== null){
274 logger
.warn("from TaxonName for RelName (" + relNameId
+ ") does not exist in store");
277 logger
.warn("to TaxonNames for RelName (" + relNameId
+ ") does not exist in store");
281 logger
.info("TaxonName to save: " + nameStore
.size());
282 nameService
.saveTaxonNameAll(nameStore
);
284 logger
.info("end makeRelName ...");
286 } catch (SQLException e
) {
287 logger
.error("SQLException:" + e
);