ref #9932 handle Plantae and Tracheophyta
[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 //do not report anymore, is in doc file already
99 logger.info(refId + ": No uri");
100 }
101
102 //issn
103 if (isNotBlank(issnStr) && !"NA".equals(issnStr)) {
104 if (issnStr.startsWith("ISSN ")) {
105 issnStr = issnStr.replace("ISSN ", "").trim();
106 }
107 ref.setIssn(issnStr);
108 }
109
110 //register id and make import source
111 handleId(state, refId, ref);
112
113 partitioner.startDoSave();
114 refsToSave.add(ref);
115 } catch (Exception e) {
116 logger.warn("An exception (" +e.getMessage()+") occurred when creating reference with id " + refId + ". Reference could not be saved.");
117 success = false;
118 }
119 }
120 } catch (Exception e) {
121 logger.error("SQLException:" + e);
122 return false;
123 }
124
125 getReferenceService().save(refsToSave);
126 return success;
127 }
128
129 @Override
130 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
131
132 String nameSpace;
133 Set<String> idSet;
134 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
135
136 try{
137 Set<String> nameIdSet = new HashSet<>();
138 Set<String> referenceIdSet = new HashSet<>();
139 while (rs.next()){
140 // handleForeignKey(rs, nameIdSet, "PTNameFk");
141 // handleForeignKey(rs, referenceIdSet, "PTRefFk");
142 }
143
144 //reference map
145 nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
146 idSet = referenceIdSet;
147 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
148 result.put(nameSpace, referenceMap);
149
150 } catch (SQLException e) {
151 throw new RuntimeException(e);
152 }
153 return result;
154 }
155
156 @Override
157 protected String getTableName() {
158 return dbTableName;
159 }
160
161 @Override
162 public String getPluralString() {
163 return pluralString;
164 }
165 }