Ordered input for easier comparison
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / caryo / CaryoTaxonImport.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.io.caryo;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.io.common.DbImportBase;
24 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25 import eu.etaxonomy.cdm.io.common.Source;
26 import eu.etaxonomy.cdm.model.agent.Person;
27 import eu.etaxonomy.cdm.model.agent.Team;
28 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
29 import eu.etaxonomy.cdm.model.common.Annotation;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.common.Language;
32 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
33 import eu.etaxonomy.cdm.model.common.TimePeriod;
34 import eu.etaxonomy.cdm.model.name.BotanicalName;
35 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
36 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
37 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
38 import eu.etaxonomy.cdm.model.name.Rank;
39 import eu.etaxonomy.cdm.model.reference.IArticle;
40 import eu.etaxonomy.cdm.model.reference.IBook;
41 import eu.etaxonomy.cdm.model.reference.IBookSection;
42 import eu.etaxonomy.cdm.model.reference.IJournal;
43 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
44 import eu.etaxonomy.cdm.model.reference.Reference;
45 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
46 import eu.etaxonomy.cdm.model.taxon.Classification;
47 import eu.etaxonomy.cdm.model.taxon.Synonym;
48 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
49 import eu.etaxonomy.cdm.model.taxon.Taxon;
50 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
52
53
54 /**
55 * @author a.mueller
56 * @created 20.02.2010
57 */
58 @Component
59 public class CaryoTaxonImport extends DbImportBase<CaryoImportState, CaryoImportConfigurator> {
60 private static final Logger logger = Logger.getLogger(CaryoTaxonImport.class);
61
62 private final int modCount = 10000;
63 private static final String pluralString = "taxa";
64 private static final String dbTableName = "CARYOPHYLLALES";
65
66
67
68 private final Map<String, Taxon> familyMap = new HashMap<String, Taxon>();
69 private final Map<String, Person> personMap = new HashMap<String, Person>();
70 private final Map<String, Team> teamMap = new HashMap<String, Team>();
71 private final Map<String, TeamOrPersonBase> inAuthorMap = new HashMap<String, TeamOrPersonBase>();
72 private final Map<String, IJournal> journalMap = new HashMap<String, IJournal>();
73 private final Map<String, IBook> bookMap = new HashMap<String, IBook>();
74
75
76 private Classification classification;
77
78
79
80 public CaryoTaxonImport(){
81 super(dbTableName, pluralString);
82 }
83
84
85
86
87 /* (non-Javadoc)
88 * @see eu.etaxonomy.cdm.io.common.DbImportBase#getIdQuery(eu.etaxonomy.cdm.io.common.DbImportStateBase)
89 */
90 @Override
91 protected String getIdQuery(CaryoImportState state) {
92 String strRecordQuery =
93 " SELECT ID " +
94 " FROM " + dbTableName +
95 " ORDER BY id ";
96 return strRecordQuery;
97 }
98
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.cdm.io.common.DbImportBase#getRecordQuery(eu.etaxonomy.cdm.io.common.DbImportConfiguratorBase)
102 */
103 @Override
104 protected String getRecordQuery(CaryoImportConfigurator config) {
105 String strRecordQuery =
106 " SELECT t.* " +
107 " FROM " + getTableName() + " t " +
108 " WHERE ( t.ID IN (" + ID_LIST_TOKEN + ") )";
109 return strRecordQuery;
110 }
111
112
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
116 */
117 @Override
118 public boolean doPartition(ResultSetPartitioner partitioner, CaryoImportState state) {
119 boolean success = true;
120
121 Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
122
123 // Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
124
125
126 classification = getClassification(state);
127
128 try {
129 doFamilies(state);
130 doAuthors(state);
131 doInAuthors(state);
132 doJournals(state);
133 doBooks(state);
134
135 ResultSet rs = partitioner.getResultSet();
136
137 int i = 0;
138 Reference<?> sec = state.getTransactionalSourceReference();
139
140 //for each reference
141 while (rs.next()){
142
143 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
144
145 Integer id = rs.getInt("Id");
146 Integer taxonId = rs.getInt("NCUGenID");
147 String genus = rs.getString("Genus");
148 String family = rs.getString("Family");
149 String pages = rs.getString("Pages");
150 String autoren = rs.getString("Autoren");
151 String typeStr = rs.getString("Type");
152 String nomStatusStr = rs.getString("NomenclaturalStatus");
153 String basioStr = rs.getString("Basionym");
154
155 // ,[EtInCitation]
156 // ,[Gender]
157
158 // ,[Basionym]
159 // ,[OriginalCitation]
160
161
162 BotanicalName name = BotanicalName.NewInstance(Rank.GENUS());
163 name.setGenusOrUninomial(genus);
164 makeAuthors(name, autoren, id);
165 INomenclaturalReference nomRef = makeNomRef(state, rs, id);
166 name.setNomenclaturalReference(nomRef);
167 name.setNomenclaturalMicroReference(pages);
168 makeStatus(name, nomStatusStr, id);
169
170
171 Taxon taxon = Taxon.NewInstance(name, state.getTransactionalSourceReference());
172 handleTypes(state, rs, taxon, typeStr, id);
173 handleBasionym(state, rs, taxon, basioStr, id);
174
175 Taxon parent = familyMap.get(family);
176
177 classification.addParentChild(parent, taxon, sec, null);
178
179 taxon.addSource(OriginalSourceType.Import, String.valueOf(taxonId), "NCUGenID", sec, null);
180
181
182
183 objectsToSave.add(taxon);
184
185 }
186
187 logger.warn(pluralString + " to save: " + objectsToSave.size());
188 getTaxonService().save(objectsToSave);
189
190 return success;
191 } catch (SQLException e) {
192 logger.error("SQLException:" + e);
193 return false;
194 }
195 }
196
197 private void handleBasionym(CaryoImportState state, ResultSet rs, Taxon taxon, String basioStr, Integer id) {
198 if (StringUtils.isNotBlank(basioStr)){
199 BotanicalName name = (BotanicalName) taxon.getName();
200 BotanicalName basionym = BotanicalName.PARSED_REFERENCE(basioStr);
201 if (basionym.hasProblem()){
202 logger.warn("Problem when parsing basionym ("+id+"): " + basioStr);
203 }
204 name.addBasionym(basionym);
205 Synonym syn = Synonym.NewInstance(basionym, state.getTransactionalSourceReference());
206 taxon.addSynonym(syn, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
207 getTaxonService().save(syn);
208 }
209
210 }
211
212
213
214
215 private void handleTypes(CaryoImportState state, ResultSet rs, Taxon taxon, String origType, Integer id) {
216 NameTypeDesignation desig = NameTypeDesignation.NewInstance();
217 String type = origType;
218 if (StringUtils.isBlank(type) || "to be designated".equalsIgnoreCase(type)){
219 return;
220 }else{
221 BotanicalName name = (BotanicalName)taxon.getName();
222 BotanicalName typeName = BotanicalName.NewInstance(Rank.SPECIES());
223 if ("not designated".equalsIgnoreCase(type)){
224 desig.setNotDesignated(true);
225 }else{
226 String genus = name.getGenusOrUninomial();
227 typeName.setGenusOrUninomial(genus);
228 if (! type.startsWith(genus.substring(0,1) + ". " )){
229 int i = type.indexOf(" ");
230 String genusOrig = type.substring(0, i);
231 logger.info("First genus letter not recognized: " + genusOrig + "-" + genus + ":"+ id);
232 typeName.setGenusOrUninomial(genusOrig);
233 type = type.substring(i + 1).trim();
234 }else{
235 type = type.substring(3);
236 }
237 int i = type.indexOf(" ");
238 if (i <= 0){
239 logger.warn("No space: " + type +"; " + id);
240 }else{
241 String species = type.substring(0, i);
242 typeName.setSpecificEpithet(species);
243 type = type.substring(i + 1).trim();
244
245 int posBracket = type.indexOf("(", 2);
246 if (posBracket > 0){
247 String bracket = type.substring(posBracket);
248 // logger.warn("Type has bracket("+id+"): " + bracket);
249 taxon.addAnnotation(Annotation.NewInstance("Type-bracket: " + bracket, Language.DEFAULT()));
250 type = type.substring(0, posBracket).trim();
251 }else{
252 Taxon speciesTaxon = Taxon.NewInstance(typeName, state.getTransactionalSourceReference());
253 classification.addParentChild(taxon, speciesTaxon, null, null);
254 }
255 type = makeTypeNomStatus(typeName, type);
256
257
258 makeAuthors(typeName, type, id);
259 }
260
261 desig.setTypeName(typeName);
262 }
263 name.addTypeDesignation(desig, true);
264 }
265
266
267 }
268
269
270
271
272 private String makeTypeNomStatus(BotanicalName typeName, String type) {
273 if (type.endsWith(", nom. illeg.")){
274 type = type.replaceAll(", nom. illeg.", "");
275 typeName.addStatus(NomenclaturalStatus.NewInstance(NomenclaturalStatusType.ILLEGITIMATE()));
276 }
277 return type;
278 }
279
280
281
282
283 private void makeStatus(BotanicalName name, String nomStatusStr, Integer id) throws SQLException {
284 // ,[NomenclaturalStatus]
285
286 if (StringUtils.isNotBlank(nomStatusStr)){
287 NomenclaturalStatusType nomStatusType;
288 try {
289 nomStatusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(nomStatusStr, name);
290 } catch (UnknownCdmTypeException e) {
291 if (nomStatusStr.startsWith("nom. rej. prop.")){
292 nomStatusType = NomenclaturalStatusType.REJECTED_PROP();
293 logger.info("in favour not supported ("+id+"): " + nomStatusStr);
294 }else if (nomStatusStr.startsWith("nom. rej. in favour")){
295 nomStatusType = NomenclaturalStatusType.REJECTED();
296 logger.info("in favour not supported ("+id+"): " + nomStatusStr);
297 }else if (nomStatusStr.startsWith("nom. cons. against")){
298 nomStatusType = NomenclaturalStatusType.CONSERVED();
299 logger.info("against not supported ("+id+"): " + nomStatusStr);
300 }else if (nomStatusStr.startsWith("nom. cons. prop. against")){
301 nomStatusType = NomenclaturalStatusType.CONSERVED_PROP();
302 logger.info("against not supported ("+id+"): " + nomStatusStr);
303 }else{
304 logger.warn("Unknown status type ("+id+"): " + nomStatusStr);
305 nomStatusType = NomenclaturalStatusType.DOUBTFUL();
306 }
307 }
308
309 NomenclaturalStatus status = NomenclaturalStatus.NewInstance(nomStatusType);
310 name.addStatus(status);
311 }
312
313 }
314
315
316
317
318 private INomenclaturalReference makeNomRef(CaryoImportState state, ResultSet rs, Integer id) throws SQLException {
319 INomenclaturalReference result;
320 String periodicalTitle = rs.getString("PeriodicalTitle");
321 String volume = rs.getString("PeriodicalVolume");
322 String bookTitle = rs.getString("BookTitle");
323 String inAutorStr = rs.getString("InAutor");
324 String autorenStr = rs.getString("Autoren");
325
326 TeamOrPersonBase<?> author = getNomRefAuthor(autorenStr, id);
327 if (StringUtils.isNotBlank(periodicalTitle)){
328 IJournal journal = journalMap.get(periodicalTitle);
329 if (journal == null){
330 logger.warn("Journal not found: " + periodicalTitle + ";" + id);
331 }
332 IArticle article = ReferenceFactory.newArticle();
333 article.setInJournal(journal);
334 article.setVolume(volume);
335 result = article;
336 }else if (StringUtils.isNotBlank(bookTitle)){
337 IBook book = bookMap.get(bookTitle);
338 if (inAutorStr != null){
339 IBookSection section = ReferenceFactory.newBookSection();
340 section.setInBook(book);
341 TeamOrPersonBase<?> inAuthor = getInAuthor(inAutorStr);
342 book.setAuthorship(inAuthor);
343 result = section;
344 }else{
345 result = book;
346 }
347 }else{
348 logger.warn("No nomRef found: " + id);
349 result = null;
350 }
351 if (result != null){
352 result.setAuthorship(author);
353 makeDate(state, rs, result, id);
354 }
355 return result;
356 }
357
358 private void makeDate(CaryoImportState state, ResultSet rs, INomenclaturalReference ref, Integer id) throws SQLException {
359 TimePeriod tp = TimePeriod.NewInstance();
360 String pre1 = rs.getString("DatePre1");
361 String pre2 = rs.getString("DatePre2");
362 Float year1 = nullSafeFloat(rs, "DateYear1");
363 Float year2 = nullSafeFloat(rs, "DateYear2");
364 if (year2 == 0.0 ){
365 year2 = null;
366 }
367 String modi1 = rs.getString("DateModi1");
368 String modi2 = rs.getString("DateModi2");
369 String date = rs.getString("Date");
370
371 tp.setStartYear(year1.intValue());
372 Integer[] preDate1 = getDay(pre1,id);
373 // tp.setStartMonth(preDate1[1]);
374 // tp.setStartDay(preDate1[0]);
375 if (year2 != null){
376 tp.setEndYear(year2.intValue());
377 }
378 Integer[] preDate2 = getDay(pre2, id);
379 // tp.setEndMonth(preDate2[1]);
380 // tp.setEndDay(preDate2[0]);
381
382 // if (StringUtils.isNotBlank(modi1) || StringUtils.isNotBlank(modi2)){
383 // tp.setFreeText(date);
384 // }
385 ref.setDatePublished(tp);
386 }
387
388
389
390
391 private Integer[] getDay(String pre, Integer id) {
392 Integer[] result = new Integer[2];
393 if (! StringUtils.isBlank(pre)){
394 try {
395 String[] split = pre.split("\\s");
396 String monthStr;
397 if (split.length > 2){
398 logger.warn("L > 2: " + pre);
399 monthStr = "";
400 }else if(split.length == 2){
401 result[0] = Integer.valueOf(split[0]);
402 monthStr = split[1];
403 }else{
404 monthStr = split[0];
405 }
406 Integer month;
407 if ("Jan".equalsIgnoreCase(monthStr)){
408 month = 1;
409 }else if ("Feb".equalsIgnoreCase(monthStr)){
410 month = 2;
411 }else if ("Mar".equalsIgnoreCase(monthStr)){
412 month = 3;
413 }else if ("Apr".equalsIgnoreCase(monthStr)){
414 month = 4;
415 }else if ("Mai".equalsIgnoreCase(monthStr)){
416 month = 5;
417 }else if ("Jun".equalsIgnoreCase(monthStr)){
418 month = 6;
419 }else if ("Jul".equalsIgnoreCase(monthStr)){
420 month = 7;
421 }else if ("Aug".equalsIgnoreCase(monthStr)){
422 month = 8;
423 }else if ("Sep".equalsIgnoreCase(monthStr)){
424 month = 9;
425 }else if ("Oct".equalsIgnoreCase(monthStr)){
426 month = 10;
427 }else if ("Nov".equalsIgnoreCase(monthStr)){
428 month = 11;
429 }else if ("Dec".equalsIgnoreCase(monthStr)){
430 month = 12;
431 }else{
432 logger.warn("Unknown month ("+id+"): " + monthStr );
433 month = null;
434 }
435 result[1]= month;
436 } catch (Exception e) {
437 e.printStackTrace();
438 }
439 }
440 return result;
441 }
442
443
444 private TeamOrPersonBase<?> getInAuthor(String inAutorStr) {
445 if (StringUtils.isBlank(inAutorStr)){
446 return null;
447 }
448 TeamOrPersonBase<?> inAuthor = inAuthorMap.get(inAutorStr);
449 if (inAuthor == null){
450 logger.warn("Inauthor not found: " + inAutorStr);
451 }
452 return inAuthor;
453 }
454
455
456
457 private void makeAuthors(BotanicalName name, String autoren, Integer id) {
458 String[] parsedAuthorTeams = getParsedAuthors(autoren);
459 name.setBasionymAuthorship(getTeam(parsedAuthorTeams[0], id));
460 name.setExBasionymAuthorship(getTeam(parsedAuthorTeams[1], id));
461 name.setCombinationAuthorship(getTeam(parsedAuthorTeams[2], id));
462 name.setExCombinationAuthorship(getTeam(parsedAuthorTeams[3], id));
463
464 }
465
466 private TeamOrPersonBase<?> getNomRefAuthor(String authorStr, Integer id) {
467 String[] parsedAuthorTeams = getParsedAuthors(authorStr);
468 TeamOrPersonBase<?> team = getTeam(parsedAuthorTeams[2], id);
469 return team;
470 }
471
472
473 private TeamOrPersonBase<?> getTeam(String author, Integer id) {
474 if (StringUtils.isBlank(author)){
475 return null;
476 }
477 TeamOrPersonBase<?> result;
478 if (personMap.get(author) != null){
479 result = personMap.get(author);
480 }else{
481 result = teamMap.get(author);
482 }
483 if (result == null){
484 logger.warn("Team not found ("+id+"): " + author);
485 }
486 return result;
487 }
488
489
490 private void doInAuthors(CaryoImportState state) throws SQLException {
491 Source source = state.getConfig().getSource();
492 String sql = "SELECT DISTINCT inAutor FROM " + getTableName() + " WHERE inAutor IS NOT NULL AND inAutor <> '' ";
493 ResultSet rs = source.getResultSet(sql);
494 while (rs.next()){
495 String inAutorStr = rs.getString("inAutor");
496 if (inAuthorMap.get(inAutorStr) == null){
497 Team team = Team.NewTitledInstance(inAutorStr, inAutorStr);
498
499 inAuthorMap.put(inAutorStr, team);
500 getAgentService().save(team);
501 }
502 }
503
504 }
505
506
507 private void doAuthors(CaryoImportState state) throws SQLException {
508 Source source = state.getConfig().getSource();
509 String sql = "SELECT DISTINCT Autoren FROM " + getTableName() + " WHERE Autoren IS NOT NULL AND Autoren <> '' ";
510 ResultSet rs = source.getResultSet(sql);
511 doTypeAuthors(state);
512 while (rs.next()){
513 String autorenStr = rs.getString("Autoren");
514 String[] parsedAuthorTeams = getParsedAuthors(autorenStr);
515 for (String teamStr : parsedAuthorTeams){
516 doTeam(teamStr);
517 }
518 }
519 }
520
521
522
523
524 private void doTypeAuthors(CaryoImportState state) {
525 doTeam("Dinter & Derenb.");
526 doTeam("Marloth");
527 doTeam("Engl.");
528 doTeam("Kensit");
529 doTeam("Sond.");
530 doTeam("L. f.");
531 doTeam("Dinter & A. Berger");
532 doTeam("Schltr.");
533 doTeam("Dinter & Berger");
534 doTeam("Poir.");
535 doTeam("J. C. Wendl.");
536 doTeam("Baker & Clarke");
537 doTeam("Vahl");
538 doTeam("Nicolai");
539 doTeam("Gürke");
540 doTeam("Cels");
541 doTeam("Dams");
542 doTeam("Coult.");
543 doTeam("A. Weber");
544 doTeam("Vaupel");
545 doTeam("Gay");
546 doTeam("Pall.");
547 doTeam("Moq. & Coss.");
548 doTeam("Durieu & Moq.");
549 doTeam("Lag. & Rodrigues");
550 doTeam("M. Martens & Galeotti");
551 doTeam("Steud.");
552 doTeam("Aitch. & Hemsl.");
553 doTeam("Ikonn.-Gal.");
554 doTeam("Freitag");
555 doTeam("Regel");
556 doTeam("Ledeb.");
557 doTeam("Schur");
558 doTeam("Asch.");
559 doTeam("G. Forst.");
560 doTeam("Gray");
561 doTeam("Curran");
562 doTeam("Donn. Sm.");
563 doTeam("Diels");
564 doTeam("Colla");
565 doTeam("Miers");
566 doTeam("Gillis");
567 doTeam("Royle");
568 doTeam("Monv.");
569 doTeam("Werderm. & Backeb.");
570 doTeam("Wright");
571 doTeam("Meyen");
572 doTeam("Runge");
573 doTeam("Böd.");
574 doTeam("Rol.-Goss.");
575 doTeam("Poselg.");
576 doTeam("Andreae & Backeberg");
577 doTeam("Miq.");
578 doTeam("Rol.");
579 doTeam("Backeb. & Voll");
580 doTeam("Engelm. & Bigelow");
581 doTeam("Pfeiffer & Otto");
582 doTeam("Humb. & Bonpl.");
583 doTeam("Schmalh.");
584 doTeam("Preobr.");
585 doTeam("Labill.");
586 doTeam("Barkoudah");
587 doTeam("Regel & Schmalh.");
588 doTeam("Cambess.");
589 doTeam("Pax & K. Hoff.");
590 doTeam("Bergeret");
591 doTeam("Walp.");
592 doTeam("Huds.");
593 doTeam("Kit.");
594 doTeam("Schott, Nymann & Kotschy");
595 doTeam("Boiss. & Buhse");
596 doTeam("Medik.");
597 doTeam("Coss. & Germ.");
598 doTeam("Moss");
599 doTeam("Pax & Hoffm.");
600 doTeam("Schischk.");
601 doTeam("Lipsch.");
602 doTeam("Maerkl.");
603 doTeam("Vierh.");
604 doTeam("Exell");
605
606 }
607
608
609
610
611 /**
612 * @param teamStr
613 * @return
614 */
615 protected void doTeam(String teamStr) {
616 if (StringUtils.isBlank(teamStr)){
617 return;
618 }
619 String[] parsedTeam = parseTeam(teamStr);
620 if (parsedTeam.length == 1){
621 savePerson(parsedTeam[0]);
622 }else{
623 Team team = teamMap.get(teamStr);
624 if (team == null){
625 team = Team.NewInstance();
626 for (String member : parsedTeam){
627 Person person = savePerson(member);
628 team.addTeamMember(person);
629 }
630 teamMap.put(teamStr, team);
631 getAgentService().saveOrUpdate(team);
632 }
633 }
634 return;
635 }
636
637 private String[] parseTeam(String teamStr) {
638 String[] split = teamStr.split("[&,]");
639 for (int i = 0; i < split.length; i++){
640 split[i] = split[i].trim();
641 }
642 return split;
643 }
644
645 private Person savePerson(String personStr) {
646 Person result = personMap.get(personStr);
647 if (result == null ){
648 Person person = Person.NewTitledInstance(personStr);
649 personMap.put(personStr, person);
650 getAgentService().save(person);
651 result = person;
652 }
653 return result;
654 }
655
656
657
658
659 private String[] getParsedAuthors(String autorenStr) {
660 String[] result = new String[4];
661 String basioFull = null;
662 String origFull;
663
664 String[] split = autorenStr.split("\\)");
665 if (split.length > 1){
666 basioFull = split[0].replace("(", "").trim();
667 origFull = split[1].trim();
668 }else{
669 origFull = split[0].trim();
670 }
671 String[] splitBasio = splitExAuthors(basioFull);
672 String[] splitOrig = splitExAuthors(origFull);
673 result[0] = splitBasio[0];
674 result[1] = splitBasio[1];
675 result[2] = splitOrig[0];
676 result[3] = splitOrig[1];
677
678 return result;
679 }
680
681
682
683
684 private String[] splitExAuthors(String author) {
685 String[] result = new String[2];
686 if (author != null){
687 String[] split = author.split("\\sex\\s");
688 if (split.length > 1){
689 result[0] = split[1].trim();
690 result[1] = split[0].trim();
691 }else{
692 result[0] = split[0].trim();
693 }
694 }
695 return result;
696 }
697
698
699
700
701 private void doBooks(CaryoImportState state) throws SQLException {
702 Source source = state.getConfig().getSource();
703 String sql = "SELECT DISTINCT BookTitle FROM " + getTableName() + " WHERE BookTitle IS NOT NULL AND BookTitle <> '' ";
704 ResultSet rs = source.getResultSet(sql);
705 while (rs.next()){
706 String bookStr = rs.getString("BookTitle");
707 if (bookMap.get(bookStr) == null ){
708
709 IBook book = ReferenceFactory.newBook();
710
711 book.setTitle(bookStr);
712
713 bookMap.put(bookStr, book);
714 getReferenceService().save((Reference<?>)book);
715 }
716 }
717 }
718
719
720
721
722 private void doJournals(CaryoImportState state) throws SQLException {
723 Source source = state.getConfig().getSource();
724 String sqlPeriodical = "SELECT DISTINCT PeriodicalTitle FROM " + getTableName() + " WHERE PeriodicalTitle IS NOT NULL AND PeriodicalTitle <> '' ";
725 ResultSet rs = source.getResultSet(sqlPeriodical);
726 while (rs.next()){
727 String periodical = rs.getString("PeriodicalTitle");
728 if (journalMap.get(periodical) == null ){
729
730 Reference<?> journal = ReferenceFactory.newJournal();
731
732 journal.setTitle(periodical);
733
734 journalMap.put(periodical, journal);
735 getReferenceService().save(journal);
736 }
737 }
738 }
739
740
741
742
743 private void doFamilies(CaryoImportState state) throws SQLException {
744 Source source = state.getConfig().getSource();
745 String sqlFamily = "SELECT DISTINCT Family FROM " + getTableName() + " WHERE Family IS NOT NULL";
746 ResultSet rs = source.getResultSet(sqlFamily);
747 while (rs.next()){
748 String family = rs.getString("family");
749 if (familyMap.get(family) == null ){
750
751 BotanicalName name = BotanicalName.NewInstance(Rank.FAMILY());
752 name.setGenusOrUninomial(family);
753 Taxon taxon = Taxon.NewInstance(name, state.getTransactionalSourceReference());
754 classification.addChildTaxon(taxon, null, null);
755 // taxon.addSource(id, idNamespace, citation, null);
756
757 familyMap.put(family, taxon);
758 getTaxonService().save(taxon);
759 }
760 }
761
762 }
763
764 private Classification getClassification(CaryoImportState state) {
765 if (this.classification == null){
766 String name = state.getConfig().getClassificationName();
767 Reference<?> reference = state.getTransactionalSourceReference();
768 this.classification = Classification.NewInstance(name, reference, Language.DEFAULT());
769 if (state.getConfig().getClassificationUuid() != null){
770 classification.setUuid(state.getConfig().getClassificationUuid());
771 }
772 getClassificationService().save(classification);
773 }
774 return this.classification;
775 }
776
777
778
779
780
781 @Override
782 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, CaryoImportState state) {
783 // String nameSpace;
784 // Class cdmClass;
785 // Set<String> idSet;
786 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
787 // try{
788 // Set<String> taxonIdSet = new HashSet<String>();
789 //
790 // while (rs.next()){
791 //// handleForeignKey(rs, taxonIdSet, "taxonId");
792 // }
793 //
794 // //taxon map
795 // nameSpace = TAXON_NAMESPACE;
796 // cdmClass = Taxon.class;
797 // idSet = taxonIdSet;
798 // Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
799 // result.put(nameSpace, objectMap);
800 //
801 //
802 // } catch (SQLException e) {
803 // throw new RuntimeException(e);
804 // }
805 return result;
806 }
807
808 /* (non-Javadoc)
809 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
810 */
811 @Override
812 protected boolean doCheck(CaryoImportState state){
813 return true;
814 }
815
816
817 /* (non-Javadoc)
818 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
819 */
820 @Override
821 protected boolean isIgnore(CaryoImportState state){
822 return ! state.getConfig().isDoTaxa();
823 }
824
825
826
827
828
829
830
831 }