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