dc475512979f5962e530b7e1339f826e76d2d154
[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 //register id and make import source
116 handleId(state, refId, ref, null);
117
118 partitioner.startDoSave();
119 refsToSave.add(ref);
120 } catch (Exception e) {
121 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
122 success = false;
123 }
124 }
125 } catch (Exception e) {
126 logger.error("SQLException:" + e);
127 return false;
128 }
129
130 getReferenceService().save(refsToSave);
131 return success;
132 }
133
134 @Override
135 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
136
137 String nameSpace;
138 Set<String> idSet;
139 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
140
141 try{
142 Set<String> nameIdSet = new HashSet<>();
143 Set<String> referenceIdSet = new HashSet<>();
144 while (rs.next()){
145 // handleForeignKey(rs, nameIdSet, "PTNameFk");
146 // handleForeignKey(rs, referenceIdSet, "PTRefFk");
147 }
148
149 //reference map
150 nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
151 idSet = referenceIdSet;
152 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
153 result.put(nameSpace, referenceMap);
154
155 } catch (SQLException e) {
156 throw new RuntimeException(e);
157 }
158 return result;
159 }
160
161 @Override
162 protected String getTableName() {
163 return dbTableName;
164 }
165
166 @Override
167 public String getPluralString() {
168 return pluralString;
169 }
170 }