5b22c1a06d6059c661e30621f6fbd8dc7169e60c
[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.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.log4j.Logger;
20 import org.springframework.stereotype.Component;
21
22 import eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelReferenceImport;
23 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
27
28 /**
29 * @author a.mueller
30 * @since 08.02.2022
31 */
32 @Component
33 public class MexicoEfloraRefOtherBooksImport extends MexicoEfloraReferenceImportBase {
34
35 private static final long serialVersionUID = -1186364983750790695L;
36 private static final Logger logger = Logger.getLogger(MexicoEfloraRefOtherBooksImport.class);
37
38 public static final String NAMESPACE = "OtherBooks";
39
40 private static final String pluralString = "Other books";
41 private static final String dbTableName = "RefOtherBooks";
42
43 public MexicoEfloraRefOtherBooksImport(){
44 super(dbTableName, pluralString);
45 }
46
47 @Override
48 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
49
50 boolean success = true ;
51 MexicoEfloraImportConfigurator config = state.getConfig();
52 Set<Reference> refsToSave = new HashSet<>();
53
54 @SuppressWarnings("unchecked")
55 Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
56
57 ResultSet rs = partitioner.getResultSet();
58 try{
59 while (rs.next()){
60
61 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
62
63 //create TaxonName element
64 String type = rs.getString("PubType");
65 int refId = rs.getInt("CONABIO-BIB-ID");
66 String authorStr = rs.getString("Author");
67 String yearStr = rs.getString("Year");
68 String titleStr = rs.getString("Title");
69 String volumeStr = rs.getString("Volume");
70 String pagesStr = rs.getString("Pages");
71 String observacionesStr = rs.getString("Observaciones");
72 String urlStr = rs.getString("URL");
73 String doiStr = rs.getString("DOI");
74 String isbnStr = rs.getString("ISBN");
75
76 try {
77 Reference ref = ReferenceFactory.newBook();
78 //type
79 if (!"B".equals(type)) {
80 logger.warn(refId + ": Type not 'B'");
81 }
82
83 //author
84 handleAuthorStr(state, authorStr, ref, refId);
85
86 //year
87 handleYearStr(state, yearStr, ref, refId);
88
89 //title
90 handleTitleStr(state, titleStr, ref, refId);
91
92 //concat
93 if (isNotBlank(volumeStr)) {
94 ref.setVolume(volumeStr);
95 }else {
96 logger.info(refId + ": No volume");
97 }
98
99 //url
100 handleUrlStr(state, urlStr, ref, refId);
101
102 //doi
103 handleDoiStr(state, doiStr, ref, refId);
104
105 //issn
106 if (isNotBlank(isbnStr) && !"NA".equals(isbnStr)) {
107 if (isbnStr.startsWith("ISBN:")) {
108 isbnStr = isbnStr.replace("ISBN:", "").trim();
109 }else if (isbnStr.startsWith("ISSN ")) {
110 isbnStr = isbnStr.replace("ISSN ", "").trim();
111 }
112 ref.setIsbn(isbnStr);
113 }
114
115 partitioner.startDoSave();
116 refsToSave.add(ref);
117 } catch (Exception e) {
118 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
119 success = false;
120 }
121 }
122 } catch (Exception e) {
123 logger.error("SQLException:" + e);
124 return false;
125 }
126
127 getReferenceService().save(refsToSave);
128 return success;
129 }
130
131 @Override
132 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
133
134 String nameSpace;
135 Set<String> idSet;
136 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
137
138 try{
139 Set<String> nameIdSet = new HashSet<>();
140 Set<String> referenceIdSet = new HashSet<>();
141 while (rs.next()){
142 // handleForeignKey(rs, nameIdSet, "PTNameFk");
143 // handleForeignKey(rs, referenceIdSet, "PTRefFk");
144 }
145
146 //reference map
147 nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
148 idSet = referenceIdSet;
149 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
150 result.put(nameSpace, referenceMap);
151
152 } catch (SQLException e) {
153 throw new RuntimeException(e);
154 }
155 return result;
156 }
157
158 @Override
159 protected String getTableName() {
160 return dbTableName;
161 }
162
163 @Override
164 public String getPluralString() {
165 return pluralString;
166 }
167 }