1 package eu
.etaxonomy
.cdm
.io
.berlinModel
;
3 import static eu
.etaxonomy
.cdm
.io
.berlinModel
.BerlinModelTransformer
.*;
5 import java
.net
.MalformedURLException
;
7 import java
.sql
.ResultSet
;
8 import java
.sql
.SQLException
;
9 import java
.util
.HashSet
;
12 import org
.apache
.log4j
.Logger
;
14 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
15 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
16 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
17 import eu
.etaxonomy
.cdm
.io
.source
.Source
;
18 import eu
.etaxonomy
.cdm
.model
.agent
.Agent
;
19 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
20 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
21 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
23 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
24 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
25 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
26 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationshipType
;
27 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.INomenclaturalReference
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
31 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
34 public class BerlinModelTaxonNameIO
{
35 private static final Logger logger
= Logger
.getLogger(BerlinModelTaxonNameIO
.class);
37 private static int modCount
= 5000;
40 static boolean invokeStatus(ReferenceBase berlinModelRef
, Source source
, CdmApplicationController cdmApp
,
41 boolean deleteAll
, MapWrapper
<TaxonNameBase
> taxonNameMap
,
42 MapWrapper
<ReferenceBase
> referenceMap
, MapWrapper
<Agent
> authorMap
){
46 public static boolean invoke(BerlinModelImportConfigurator bmiConfig
, CdmApplicationController cdmApp
,
47 MapWrapper
<TaxonNameBase
> taxonNameMap
, MapWrapper
<ReferenceBase
> referenceMap
, MapWrapper
<Team
> authorMap
){
49 Source source
= bmiConfig
.getSource();
52 boolean success
= true ;
54 logger
.info("start makeTaxonNames ...");
55 INameService nameService
= cdmApp
.getNameService();
56 boolean delete
= bmiConfig
.isDeleteAll();
61 //get data from database
63 "SELECT Name.* , RefDetail.RefDetailId, RefDetail.RefFk, " +
64 " RefDetail.FullRefCache, RefDetail.FullNomRefCache, RefDetail.PreliminaryFlag AS RefDetailPrelim, RefDetail.Details, " +
65 " RefDetail.SecondarySources, RefDetail.IdInSource " +
66 " FROM Name LEFT OUTER JOIN RefDetail ON Name.NomRefDetailFk = RefDetail.RefDetailId AND Name.NomRefDetailFk = RefDetail.RefDetailId AND " +
67 " Name.NomRefFk = RefDetail.RefFk AND Name.NomRefFk = RefDetail.RefFk" +
69 //strQuery += " AND Name.Created_When > '03.03.2004' ";
72 ResultSet rs
= source
.getResultSet(strQuery
) ;
78 if ((i
++ % modCount
) == 0){ logger
.info("Names handled: " + (i
-1));}
80 //create TaxonName element
81 int nameId
= rs
.getInt("nameId");
82 int rankId
= rs
.getInt("rankFk");
83 Object authorFk
= rs
.getObject("AuthorTeamFk");
84 Object exAuthorFk
= rs
.getObject("ExAuthorTeamFk");
85 Object basAuthorFk
= rs
.getObject("BasAuthorTeamFk");
86 Object exBasAuthorFk
= rs
.getObject("ExBasAuthorTeamFk");
87 Object nomRefFk
= rs
.getObject("NomRefFk");
89 Object createdWhen
= rs
.getObject("Created_When");
90 Object createdWho
= rs
.getObject("Created_Who");
91 // Object updatedWhen = rs.getObject("Updated_When");
92 // Object updatedWho = rs.getObject("Updated_who");
93 Object updatedWhen
= "";
94 Object updatedWho
= "";
95 Object notes
= rs
.getObject("notes");
98 if (logger
.isDebugEnabled()){logger
.debug(rankId
);}
99 Rank rank
= BerlinModelTransformer
.rankId2Rank(rankId
);
101 //BotanicalName name = BotanicalName.NewInstance(BerlinModelTransformer.rankId2Rank(rankId));
102 BotanicalName botanicalName
= BotanicalName
.NewInstance(rank
);
105 dbAttrName
= "supraGenericName";
107 dbAttrName
= "genus";
109 cdmAttrName
= "genusOrUninomial";
110 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
112 dbAttrName
= "genusSubdivisionEpi";
113 cdmAttrName
= "infraGenericEpithet";
114 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
116 dbAttrName
= "speciesEpi";
117 cdmAttrName
= "specificEpithet";
118 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
121 dbAttrName
= "infraSpeciesEpi";
122 cdmAttrName
= "infraSpecificEpithet";
123 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
125 dbAttrName
= "unnamedNamePhrase";
126 cdmAttrName
= "appendedPhrase";
127 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
129 dbAttrName
= "preliminaryFlag";
130 cdmAttrName
= "XX" + "protectedTitleCache";
131 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
133 dbAttrName
= "HybridFormulaFlag";
134 cdmAttrName
= "isHybridFormula";
135 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
137 dbAttrName
= "MonomHybFlag";
138 cdmAttrName
= "isMonomHybrid";
139 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
141 dbAttrName
= "BinomHybFlag";
142 cdmAttrName
= "isBinomHybrid";
143 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
145 dbAttrName
= "TrinomHybFlag";
146 cdmAttrName
= "isTrinomHybrid";
147 success
&= ImportHelper
.addBooleanValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
151 // dbAttrName = "notes";
152 // cdmAttrName = "isTrinomHybrid";
153 // ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
166 dbAttrName
= "details";
167 cdmAttrName
= "nomenclaturalMicroReference";
168 success
&= ImportHelper
.addStringValue(rs
, botanicalName
, dbAttrName
, cdmAttrName
);
174 if (authorMap
!= null){
175 botanicalName
.setCombinationAuthorTeam(getAuthorTeam(authorMap
, authorFk
, nameId
));
176 botanicalName
.setExCombinationAuthorTeam(getAuthorTeam(authorMap
, exAuthorFk
, nameId
));
177 botanicalName
.setBasionymAuthorTeam(getAuthorTeam(authorMap
, basAuthorFk
, nameId
));
178 botanicalName
.setExBasionymAuthorTeam(getAuthorTeam(authorMap
, exBasAuthorFk
, nameId
));
181 //nomenclatural Reference
182 if (referenceMap
!= null){
183 if (nomRefFk
!= null){
184 int nomRefFkInt
= (Integer
)nomRefFk
;
185 ReferenceBase nomenclaturalReference
= referenceMap
.get(nomRefFkInt
);
186 if (nomenclaturalReference
== null){
188 logger
.warn("Nomenclatural reference (nomRefFk = " + nomRefFkInt
+ ") for TaxonName (nameId = " + nameId
+ ")"+
189 " was not found in reference store. Relation was not set!!");
190 }else if (! INomenclaturalReference
.class.isAssignableFrom(nomenclaturalReference
.getClass())){
191 logger
.error("Nomenclatural reference (nomRefFk = " + nomRefFkInt
+ ") for TaxonName (nameId = " + nameId
+ ")"+
192 " is not assignable from INomenclaturalReference. Relation was not set!! (Class = " + nomenclaturalReference
.getClass()+ ")");
194 botanicalName
.setNomenclaturalReference((INomenclaturalReference
)nomenclaturalReference
);
200 String createdAnnotationString
= "Berlin Model record was created By: " + String
.valueOf(createdWho
) + " (" + String
.valueOf(createdWhen
) + ") " +
201 " and updated By: " + String
.valueOf(updatedWho
) + " (" + String
.valueOf(updatedWhen
) + ")";
202 Annotation annotation
= Annotation
.NewInstance(createdAnnotationString
, Language
.ENGLISH());
203 annotation
.setCommentator(bmiConfig
.getCommentator());
205 // URL linkbackUrl = new URL("http:\\www.abc.de");
206 // annotation.setLinkbackUrl(linkbackUrl);
207 // } catch (MalformedURLException e) {
208 // logger.warn("MalformedURLException");
210 botanicalName
.addAnnotation(annotation
);
213 String notesString
= String
.valueOf(notes
);
214 if (notesString
.length() > 254 ){
215 notesString
= notesString
.substring(0, 250) + "...";
217 Annotation notesAnnotation
= Annotation
.NewInstance(notesString
, null);
218 //notes.setCommentator(bmiConfig.getCommentator());
219 botanicalName
.addAnnotation(notesAnnotation
);
224 Marker marker
= Marker
.NewInstance(MarkerType
.TO_BE_CHECKED() ,flag
);
225 botanicalName
.addMarker(marker
);
229 ImportHelper
.setOriginalSource(botanicalName
, bmiConfig
.getSourceReference(), nameId
);
231 taxonNameMap
.put(nameId
, botanicalName
);
234 catch (UnknownCdmTypeException e
) {
235 logger
.warn("Name with id " + nameId
+ " has unknown rankId " + rankId
+ " and could not be saved.");
239 } //while rs.hasNext()
240 logger
.info(i
+ " names handled");
241 nameService
.saveTaxonNameAll(taxonNameMap
.objects());
243 // makeNameSpecificData(nameMap);
245 logger
.info("end makeTaxonNames ...");
247 } catch (SQLException e
) {
248 logger
.error("SQLException:" + e
);
254 private static Team
getAuthorTeam(MapWrapper
<Team
> authorMap
, Object teamIdObject
, int nameId
){
255 if (teamIdObject
== null){
258 int teamId
= (Integer
)teamIdObject
;
259 Team team
= authorMap
.get(teamId
);
262 logger
.warn("AuthorTeam (teamId = " + teamId
+ ") for TaxonName (nameId = " + nameId
+ ")"+
263 " was not found in authorTeam store. Relation was not set!!");
272 public static boolean invokeRelations(BerlinModelImportConfigurator bmiConfig
, CdmApplicationController cdmApp
,
273 MapWrapper
<TaxonNameBase
> nameMap
, MapWrapper
<ReferenceBase
> referenceMap
){
275 Set
<TaxonNameBase
> nameStore
= new HashSet
<TaxonNameBase
>();
276 Source source
= bmiConfig
.getSource();
280 logger
.info("start makeNameRelationships ...");
282 INameService nameService
= cdmApp
.getNameService();
283 boolean delete
= bmiConfig
.isDeleteAll();
286 //get data from database
288 " SELECT RelName.*, FromName.nameId as name1Id, ToName.nameId as name2Id " +
289 " FROM Name as FromName INNER JOIN " +
290 " RelName ON FromName.NameId = RelName.NameFk1 INNER JOIN " +
291 " Name AS ToName ON RelName.NameFk2 = ToName.NameId "+
293 ResultSet rs
= source
.getResultSet(strQuery
) ;
299 if ((i
++ % modCount
) == 0){ logger
.info("RelName handled: " + (i
-1));}
301 int relNameId
= rs
.getInt("RelNameId");
302 int name1Id
= rs
.getInt("name1Id");
303 int name2Id
= rs
.getInt("name2Id");
304 int relRefFk
= rs
.getInt("refFk");
305 int relQualifierFk
= rs
.getInt("relNameQualifierFk");
307 TaxonNameBase nameFrom
= nameMap
.get(name1Id
);
308 TaxonNameBase nameTo
= nameMap
.get(name2Id
);
311 ReferenceBase citation
= null;
312 String microcitation
= null;
314 if (nameFrom
!= null && nameTo
!= null){
315 if (relQualifierFk
== NAME_REL_IS_BASIONYM_FOR
){
316 //TODO references, mikroref, etc
317 nameTo
.setBasionym(nameFrom
);
318 }else if (relQualifierFk
== NAME_REL_IS_LATER_HOMONYM_OF
){
319 String rule
= null; //TODO
320 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.LATER_HOMONYM(), rule
) ;
322 }else if (relQualifierFk
== NAME_REL_IS_REPLACED_SYNONYM_FOR
){
323 String rule
= null; //TODO
324 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.REPLACED_SYNONYM(), rule
) ;
326 }else if (relQualifierFk
== NAME_REL_IS_TYPE_OF
){
327 logger
.warn("NameRelationShipType " + relQualifierFk
+ " not yet implemented");
329 }else if (relQualifierFk
== NAME_REL_IS_ORTHOGRAPHIC_VARIANT_OF
){
330 String rule
= null; //TODO
331 nameFrom
.addRelationshipToName(nameTo
, NameRelationshipType
.ORTHOGRAPHIC_VARIANT(), rule
) ;
335 logger
.warn("NameRelationShipType " + relQualifierFk
+ " not yet implemented");
337 nameStore
.add(nameFrom
);
345 if (nameFrom
== null){
346 logger
.warn("from TaxonName for RelName (" + relNameId
+ ") does not exist in store");
349 logger
.warn("to TaxonNames for RelName (" + relNameId
+ ") does not exist in store");
353 logger
.info("TaxonName to save: " + nameStore
.size());
354 nameService
.saveTaxonNameAll(nameStore
);
356 logger
.info("end makeRelName ...");
358 } catch (SQLException e
) {
359 logger
.error("SQLException:" + e
);