ref #9932 latest changes to MexicoEflora imports
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraRefWebSitesImport.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 package eu.etaxonomy.cdm.io.mexico;
10
11 import java.sql.ResultSet;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.stereotype.Component;
19
20 import eu.etaxonomy.cdm.common.URI;
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 MexicoEfloraRefWebSitesImport extends MexicoEfloraReferenceImportBase {
32
33 private static final long serialVersionUID = -1186364983750790695L;
34 private static final Logger logger = Logger.getLogger(MexicoEfloraRefWebSitesImport.class);
35
36 private static final String pluralString = "Websites";
37 private static final String dbTableName = "RefWebSites";
38
39 public MexicoEfloraRefWebSitesImport(){
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 urlStr = rs.getString("URL");
62 String issnStr = rs.getString("ISBN");
63
64 try {
65 Reference ref = ReferenceFactory.newBook();
66 //type
67 if (!"B".equals(type)) {
68 logger.warn(refId + ": Type not 'B'");
69 }
70
71 //author
72 handleAuthorStr(state, authorStr, ref, refId);
73
74 //year
75 handleYearStr(state, yearStr, ref, refId);
76
77 //title
78 handleTitleStr(state, titleStr, ref, refId);
79
80 //url
81 if (isNotBlank(urlStr)) {
82 URI uri;
83 try {
84 uri = URI.fromString(urlStr);
85 ref.setUri(uri);
86 } catch (Exception e) {
87 logger.warn(refId + ": parse exception for " + urlStr);
88 }
89 }else {
90 //do not report anymore, is in doc file already
91 logger.info(refId + ": No uri");
92 }
93
94 //issn
95 if (isNotBlank(issnStr) && !"NA".equals(issnStr)) {
96 if (issnStr.startsWith("ISSN ")) {
97 issnStr = issnStr.replace("ISSN ", "").trim();
98 }
99 ref.setIssn(issnStr);
100 }
101
102 //register id and make import source
103 handleId(state, refId, ref);
104
105 partitioner.startDoSave();
106 refsToSave.add(ref);
107 } catch (Exception e) {
108 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
109 success = false;
110 }
111 }
112 } catch (Exception e) {
113 logger.error("SQLException:" + e);
114 return false;
115 }
116
117 getReferenceService().save(refsToSave);
118 return success;
119 }
120
121 @Override
122 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
123
124 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
125 return result;
126 }
127
128 @Override
129 protected String getTableName() {
130 return dbTableName;
131 }
132
133 @Override
134 public String getPluralString() {
135 return pluralString;
136 }
137 }