ref #9932 latest changes to MexicoEflora imports
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraRefOtherBooksImport.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.mexico;
11
12 import java.sql.ResultSet;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.stereotype.Component;
20
21 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.reference.Reference;
24 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
25
26 /**
27 * @author a.mueller
28 * @since 08.02.2022
29 */
30 @Component
31 public class MexicoEfloraRefOtherBooksImport extends MexicoEfloraReferenceImportBase {
32
33 private static final long serialVersionUID = -1186364983750790695L;
34 private static final Logger logger = Logger.getLogger(MexicoEfloraRefOtherBooksImport.class);
35
36 private static final String pluralString = "Other books";
37 private static final String dbTableName = "RefOtherBooks";
38
39 public MexicoEfloraRefOtherBooksImport(){
40 super(dbTableName, pluralString);
41 }
42
43 @Override
44 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
45
46 boolean success = true ;
47 Set<Reference> refsToSave = new HashSet<>();
48
49 ResultSet rs = partitioner.getResultSet();
50 try{
51 while (rs.next()){
52
53 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
54
55 //create TaxonName element
56 String type = rs.getString("PubType");
57 int refId = rs.getInt("CONABIO-BIB-ID");
58 String authorStr = rs.getString("Author");
59 String yearStr = rs.getString("Year");
60 String titleStr = rs.getString("Title");
61 String volumeStr = rs.getString("Volume");
62 String pagesStr = rs.getString("Pages");
63 //exported as Excel table
64 // String observacionesStr = rs.getString("Observaciones");
65 String urlStr = rs.getString("URL");
66 String doiStr = rs.getString("DOI");
67 String isbnStr = rs.getString("ISBN");
68
69 try {
70 Reference ref = ReferenceFactory.newBook();
71 //type
72 if (!"B".equals(type)) {
73 logger.warn(refId + ": Type not 'B'");
74 }
75
76 //author
77 handleAuthorStr(state, authorStr, ref, refId);
78
79 //year
80 handleYearStr(state, yearStr, ref, refId);
81
82 //title
83 handleTitleStr(state, titleStr, ref, refId);
84
85 //pages
86 if (isNotBlank(pagesStr)) {
87 ref.setPages(pagesStr);
88 }
89
90 //concat
91 if (isNotBlank(volumeStr)) {
92 ref.setVolume(volumeStr);
93 }else {
94 logger.info(refId + ": No volume");
95 }
96
97 //url
98 handleUrlStr(state, urlStr, ref, refId);
99
100 //doi
101 handleDoiStr(state, doiStr, ref, refId);
102
103 //issn
104 if (isNotBlank(isbnStr) && !"NA".equals(isbnStr)) {
105 if (isbnStr.startsWith("ISBN:")) {
106 isbnStr = isbnStr.replace("ISBN:", "").trim();
107 }else if (isbnStr.startsWith("ISSN ")) {
108 isbnStr = isbnStr.replace("ISSN ", "").trim();
109 }
110 ref.setIsbn(isbnStr);
111 }
112
113 //register id and make import source
114 handleId(state, refId, ref);
115
116 partitioner.startDoSave();
117 refsToSave.add(ref);
118 } catch (Exception e) {
119 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
120 success = false;
121 }
122 }
123 } catch (Exception e) {
124 logger.error("SQLException:" + e);
125 return false;
126 }
127
128 getReferenceService().save(refsToSave);
129 return success;
130 }
131
132 @Override
133 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
134
135 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
136 return result;
137 }
138
139 @Override
140 protected String getTableName() {
141 return dbTableName;
142 }
143
144 @Override
145 public String getPluralString() {
146 return pluralString;
147 }
148 }