9808be316aa78a02e6f2f4cd586d43e1cd604d82
[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.sql.SQLException;
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.common.URI;
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 MexicoEfloraRefWebSitesImport extends MexicoEfloraReferenceImportBase {
34
35 private static final long serialVersionUID = -1186364983750790695L;
36 private static final Logger logger = Logger.getLogger(MexicoEfloraRefWebSitesImport.class);
37
38 public static final String NAMESPACE = "WebSites";
39
40 private static final String pluralString = "Websites";
41 private static final String dbTableName = "RefWebSites";
42
43 public MexicoEfloraRefWebSitesImport(){
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 urlStr = rs.getString("URL");
70 String issnStr = rs.getString("ISBN");
71
72 try {
73 Reference ref = ReferenceFactory.newBook();
74 //type
75 if (!"B".equals(type)) {
76 logger.warn(refId + ": Type not 'B'");
77 }
78
79 //author
80 handleAuthorStr(state, authorStr, ref, refId);
81
82 //year
83 handleYearStr(state, yearStr, ref, refId);
84
85 //title
86 handleTitleStr(state, titleStr, ref, refId);
87
88 //url
89 if (isNotBlank(urlStr)) {
90 URI uri;
91 try {
92 uri = URI.fromString(urlStr);
93 ref.setUri(uri);
94 } catch (Exception e) {
95 logger.warn(refId + ": parse exception for " + urlStr);
96 }
97 }else {
98 logger.warn(refId + ": No uri");
99 }
100
101 //issn
102 if (isNotBlank(issnStr) && !"NA".equals(issnStr)) {
103 if (issnStr.startsWith("ISSN ")) {
104 issnStr = issnStr.replace("ISSN ", "").trim();
105 }
106 ref.setIssn(issnStr);
107 }
108
109 partitioner.startDoSave();
110 refsToSave.add(ref);
111 } catch (Exception e) {
112 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
113 success = false;
114 }
115 }
116 } catch (Exception e) {
117 logger.error("SQLException:" + e);
118 return false;
119 }
120
121 getReferenceService().save(refsToSave);
122 return success;
123 }
124
125 @Override
126 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
127
128 String nameSpace;
129 Set<String> idSet;
130 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
131
132 try{
133 Set<String> nameIdSet = new HashSet<>();
134 Set<String> referenceIdSet = new HashSet<>();
135 while (rs.next()){
136 // handleForeignKey(rs, nameIdSet, "PTNameFk");
137 // handleForeignKey(rs, referenceIdSet, "PTRefFk");
138 }
139
140 //reference map
141 nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
142 idSet = referenceIdSet;
143 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
144 result.put(nameSpace, referenceMap);
145
146 } catch (SQLException e) {
147 throw new RuntimeException(e);
148 }
149 return result;
150 }
151
152 @Override
153 protected String getTableName() {
154 return dbTableName;
155 }
156
157 @Override
158 public String getPluralString() {
159 return pluralString;
160 }
161 }