(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / BerlinModelTaxonNameIO.java
1 package eu.etaxonomy.cdm.io.berlinModel;
2
3 import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.*;
4
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.HashSet;
10 import java.util.Set;
11
12 import org.apache.log4j.Logger;
13
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;
32
33
34 public class BerlinModelTaxonNameIO {
35 private static final Logger logger = Logger.getLogger(BerlinModelTaxonNameIO.class);
36
37 private static int modCount = 5000;
38
39 //TODO
40 static boolean invokeStatus(ReferenceBase berlinModelRef, Source source, CdmApplicationController cdmApp,
41 boolean deleteAll, MapWrapper<TaxonNameBase> taxonNameMap,
42 MapWrapper<ReferenceBase> referenceMap, MapWrapper<Agent> authorMap){
43 return false;
44 }
45
46 public static boolean invoke(BerlinModelImportConfigurator bmiConfig, CdmApplicationController cdmApp,
47 MapWrapper<TaxonNameBase> taxonNameMap, MapWrapper<ReferenceBase> referenceMap, MapWrapper<Team> authorMap){
48
49 Source source = bmiConfig.getSource();
50 String dbAttrName;
51 String cdmAttrName;
52 boolean success = true ;
53
54 logger.info("start makeTaxonNames ...");
55 INameService nameService = cdmApp.getNameService();
56 boolean delete = bmiConfig.isDeleteAll();
57
58 try {
59
60
61 //get data from database
62 String strQuery =
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" +
68 " WHERE (1=1) ";
69 //strQuery += " AND Name.Created_When > '03.03.2004' ";
70
71
72 ResultSet rs = source.getResultSet(strQuery) ;
73
74 int i = 0;
75 //for each reference
76 while (rs.next()){
77
78 if ((i++ % modCount) == 0){ logger.info("Names handled: " + (i-1));}
79
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");
88
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");
96
97 try {
98 if (logger.isDebugEnabled()){logger.debug(rankId);}
99 Rank rank = BerlinModelTransformer.rankId2Rank(rankId);
100 //FIXME
101 //BotanicalName name = BotanicalName.NewInstance(BerlinModelTransformer.rankId2Rank(rankId));
102 BotanicalName botanicalName = BotanicalName.NewInstance(rank);
103
104 if (rankId < 40){
105 dbAttrName = "supraGenericName";
106 }else{
107 dbAttrName = "genus";
108 }
109 cdmAttrName = "genusOrUninomial";
110 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
111
112 dbAttrName = "genusSubdivisionEpi";
113 cdmAttrName = "infraGenericEpithet";
114 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
115
116 dbAttrName = "speciesEpi";
117 cdmAttrName = "specificEpithet";
118 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
119
120
121 dbAttrName = "infraSpeciesEpi";
122 cdmAttrName = "infraSpecificEpithet";
123 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
124
125 dbAttrName = "unnamedNamePhrase";
126 cdmAttrName = "appendedPhrase";
127 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
128
129 dbAttrName = "preliminaryFlag";
130 cdmAttrName = "XX" + "protectedTitleCache";
131 success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
132
133 dbAttrName = "HybridFormulaFlag";
134 cdmAttrName = "isHybridFormula";
135 success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
136
137 dbAttrName = "MonomHybFlag";
138 cdmAttrName = "isMonomHybrid";
139 success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
140
141 dbAttrName = "BinomHybFlag";
142 cdmAttrName = "isBinomHybrid";
143 success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
144
145 dbAttrName = "TrinomHybFlag";
146 cdmAttrName = "isTrinomHybrid";
147 success &= ImportHelper.addBooleanValue(rs, botanicalName, dbAttrName, cdmAttrName);
148
149 //botanicalName.s
150
151 // dbAttrName = "notes";
152 // cdmAttrName = "isTrinomHybrid";
153 // ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
154
155 //TODO
156 //Created
157 //Note
158 //makeAuthorTeams
159 //CultivarGroupName
160 //CultivarName
161 //Source_Acc
162 //OrthoProjection
163
164 //Details
165
166 dbAttrName = "details";
167 cdmAttrName = "nomenclaturalMicroReference";
168 success &= ImportHelper.addStringValue(rs, botanicalName, dbAttrName, cdmAttrName);
169
170 //TODO
171 //preliminaryFlag
172
173 //authorTeams
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));
179 }
180
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){
187 //TODO
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()+ ")");
193 }else{
194 botanicalName.setNomenclaturalReference((INomenclaturalReference)nomenclaturalReference);
195 }
196 }
197 }
198
199 //refId
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());
204 // try {
205 // URL linkbackUrl = new URL("http:\\www.abc.de");
206 // annotation.setLinkbackUrl(linkbackUrl);
207 // } catch (MalformedURLException e) {
208 // logger.warn("MalformedURLException");
209 // }
210 botanicalName.addAnnotation(annotation);
211
212 if (notes != null){
213 String notesString = String.valueOf(notes);
214 if (notesString.length() > 254 ){
215 notesString = notesString.substring(0, 250) + "...";
216 }
217 Annotation notesAnnotation = Annotation.NewInstance(notesString, null);
218 //notes.setCommentator(bmiConfig.getCommentator());
219 botanicalName.addAnnotation(notesAnnotation);
220 }
221
222
223 boolean flag = true;
224 Marker marker = Marker.NewInstance(MarkerType.TO_BE_CHECKED() ,flag);
225 botanicalName.addMarker(marker);
226
227
228 //nameId
229 ImportHelper.setOriginalSource(botanicalName, bmiConfig.getSourceReference(), nameId);
230
231 taxonNameMap.put(nameId, botanicalName);
232
233 }
234 catch (UnknownCdmTypeException e) {
235 logger.warn("Name with id " + nameId + " has unknown rankId " + rankId + " and could not be saved.");
236 success = false;
237 }
238
239 } //while rs.hasNext()
240 logger.info(i + " names handled");
241 nameService.saveTaxonNameAll(taxonNameMap.objects());
242
243 // makeNameSpecificData(nameMap);
244
245 logger.info("end makeTaxonNames ...");
246 return success;
247 } catch (SQLException e) {
248 logger.error("SQLException:" + e);
249 return false;
250 }
251
252 }
253
254 private static Team getAuthorTeam(MapWrapper<Team> authorMap, Object teamIdObject, int nameId){
255 if (teamIdObject == null){
256 return null;
257 }else {
258 int teamId = (Integer)teamIdObject;
259 Team team = authorMap.get(teamId);
260 if (team == null){
261 //TODO
262 logger.warn("AuthorTeam (teamId = " + teamId + ") for TaxonName (nameId = " + nameId + ")"+
263 " was not found in authorTeam store. Relation was not set!!");
264 return null;
265 }else{
266 return team;
267 }
268 }
269 }
270
271
272 public static boolean invokeRelations(BerlinModelImportConfigurator bmiConfig, CdmApplicationController cdmApp,
273 MapWrapper<TaxonNameBase> nameMap, MapWrapper<ReferenceBase> referenceMap){
274
275 Set<TaxonNameBase> nameStore = new HashSet<TaxonNameBase>();
276 Source source = bmiConfig.getSource();
277 String dbAttrName;
278 String cdmAttrName;
279
280 logger.info("start makeNameRelationships ...");
281
282 INameService nameService = cdmApp.getNameService();
283 boolean delete = bmiConfig.isDeleteAll();
284
285 try {
286 //get data from database
287 String strQuery =
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 "+
292 " WHERE (1=1)";
293 ResultSet rs = source.getResultSet(strQuery) ;
294
295 int i = 0;
296 //for each reference
297 while (rs.next()){
298
299 if ((i++ % modCount) == 0){ logger.info("RelName handled: " + (i-1));}
300
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");
306
307 TaxonNameBase nameFrom = nameMap.get(name1Id);
308 TaxonNameBase nameTo = nameMap.get(name2Id);
309
310 //TODO
311 ReferenceBase citation = null;
312 String microcitation = null;
313
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) ;
321 //TODO reference
322 }else if (relQualifierFk == NAME_REL_IS_REPLACED_SYNONYM_FOR){
323 String rule = null; //TODO
324 nameFrom.addRelationshipToName(nameTo, NameRelationshipType.REPLACED_SYNONYM(), rule) ;
325 //TODO reference
326 }else if (relQualifierFk == NAME_REL_IS_TYPE_OF){
327 logger.warn("NameRelationShipType " + relQualifierFk + " not yet implemented");
328 //TODO reference
329 }else if (relQualifierFk == NAME_REL_IS_ORTHOGRAPHIC_VARIANT_OF){
330 String rule = null; //TODO
331 nameFrom.addRelationshipToName(nameTo, NameRelationshipType.ORTHOGRAPHIC_VARIANT(), rule) ;
332 //TODO reference
333 }else {
334 //TODO
335 logger.warn("NameRelationShipType " + relQualifierFk + " not yet implemented");
336 }
337 nameStore.add(nameFrom);
338
339 //TODO
340 //Reference
341 //ID
342 //etc.
343 }else{
344 //TODO
345 if (nameFrom == null){
346 logger.warn("from TaxonName for RelName (" + relNameId + ") does not exist in store");
347 }
348 if (nameTo == null){
349 logger.warn("to TaxonNames for RelName (" + relNameId + ") does not exist in store");
350 }
351 }
352 }
353 logger.info("TaxonName to save: " + nameStore.size());
354 nameService.saveTaxonNameAll(nameStore);
355
356 logger.info("end makeRelName ...");
357 return true;
358 } catch (SQLException e) {
359 logger.error("SQLException:" + e);
360 return false;
361 }
362
363 }
364
365 }