ref #9932 latest changes to MexicoEflora imports
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraRefArticlesImport.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.IJournal;
24 import eu.etaxonomy.cdm.model.reference.Reference;
25 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
26
27 /**
28 * @author a.mueller
29 * @since 08.02.2022
30 */
31 @Component
32 public class MexicoEfloraRefArticlesImport extends MexicoEfloraReferenceImportBase {
33
34 private static final long serialVersionUID = -1186364983750790695L;
35
36 private static final Logger logger = Logger.getLogger(MexicoEfloraRefArticlesImport.class);
37
38 private static final String pluralString = "Articles";
39 private static final String dbTableName = "RefArticles";
40 private static final Map<String,IJournal> journalMap = new HashMap<>();
41
42 public MexicoEfloraRefArticlesImport(){
43 super(dbTableName, pluralString);
44 }
45
46 @Override
47 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
48
49 boolean success = true ;
50 Set<Reference> refsToSave = new HashSet<>();
51
52 ResultSet rs = partitioner.getResultSet();
53 try{
54 while (rs.next()){
55
56 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
57
58 //create TaxonName element
59 int refId = rs.getInt("CONABIO-BIB-ID");
60 String type = rs.getString("PubType");
61 String authorStr = rs.getString("Author");
62 String yearStr = rs.getString("Year");
63 String articleTitleStr = rs.getString("ArticleTitle");
64 String journalTitleStr = rs.getString("JournalTitle");
65 String concat = rs.getString("Concatenation");
66 //TODO _minor V and P in articles
67 String vStr = rs.getString("V");
68 String pStr = rs.getString("P");
69 String urlStr = rs.getString("URL");
70 String doiStr = rs.getString("DOI");
71 String issnStr = rs.getString("ISSN");
72
73 try {
74 Reference ref = ReferenceFactory.newArticle();
75 //type
76 if (!"A".equals(type)) {
77 logger.warn(refId + ": Type not 'A'");
78 }
79
80 //author
81 handleAuthorStr(state, authorStr, ref, refId);
82
83 //year
84 handleYearStr(state, yearStr, ref, refId);
85
86 //articleTitle
87 handleTitleStr(state, articleTitleStr, ref, refId);
88
89 //journalTitle
90 if (isNotBlank(journalTitleStr)) {
91
92 IJournal journal = journalMap.get(journalTitleStr);
93 if (journal == null) {
94 journal = ReferenceFactory.newJournal();
95 journal.setTitle(journalTitleStr);
96 journalMap.put(journalTitleStr, journal);
97 }
98
99 ref.setInJournal(journal);
100 }else {
101 logger.warn(refId + ": No journal title");
102 }
103
104 //concat
105 if (isNotBlank(concat)) {
106 String[] split = concat.split(":");
107 String volume = split[0];
108 ref.setVolume(volume);
109 if (split.length > 1) {
110 String pages = split[1];
111 ref.setPages(pages);
112 }
113 }else {
114 logger.info(refId + ": No volume");
115 }
116
117 //url
118 handleUrlStr(state, urlStr, ref, refId);
119
120 //doi
121 handleDoiStr(state, doiStr, ref, refId);
122
123 //issn
124 if (isNotBlank(issnStr) && !"NA".equals(issnStr)) {
125 if (issnStr.startsWith("ISSN:")) {
126 issnStr = issnStr.replace("ISSN:", "").trim();
127 }else if (issnStr.startsWith("ISSN ")) {
128 issnStr = issnStr.replace("ISSN ", "").trim();
129 }
130 ref.getInJournal().setIssn(issnStr);
131 }
132
133 //register id and make import source
134 handleId(state, refId, ref);
135
136 partitioner.startDoSave();
137 refsToSave.add(ref);
138 } catch (Exception e) {
139 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
140 success = false;
141 }
142 }
143 } catch (Exception e) {
144 logger.error("SQLException:" + e);
145 return false;
146 }
147
148 getReferenceService().save(refsToSave);
149 return success;
150 }
151
152
153 @Override
154 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
155
156 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
157 return result;
158 }
159
160 @Override
161 protected String getTableName() {
162 return dbTableName;
163 }
164
165 @Override
166 public String getPluralString() {
167 return pluralString;
168 }
169
170 }