2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.mexico
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
13 import java
.util
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.CommonTaxonName
;
26 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
35 public class MexicoEfloraCommonNameRefImport
extends MexicoEfloraImportBase
{
37 private static final long serialVersionUID
= -1712022169834400067L;
38 private static final Logger logger
= Logger
.getLogger(MexicoEfloraCommonNameRefImport
.class);
40 protected static final String NAMESPACE
= "CommonNameRef";
42 private static final String pluralString
= "common name sources";
43 private static final String dbTableName
= "Eflora_RelBiblioNomComun";
45 public MexicoEfloraCommonNameRefImport(){
46 super(dbTableName
, pluralString
);
50 protected String
getIdQuery(MexicoEfloraImportState state
) {
51 String sql
= " SELECT IdCombinado "
52 + " FROM " + dbTableName
53 + " ORDER BY IdCombinado ";
58 protected String
getRecordQuery(MexicoEfloraImportConfigurator config
) {
59 String sqlSelect
= " SELECT * ";
60 String sqlFrom
= " FROM " + dbTableName
;
61 String sqlWhere
= " WHERE ( IdCombinado IN (" + ID_LIST_TOKEN
+ ") )";
63 String strRecordQuery
=sqlSelect
+ " " + sqlFrom
+ " " + sqlWhere
;
64 return strRecordQuery
;
68 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, MexicoEfloraImportState state
) {
70 boolean success
= true ;
72 @SuppressWarnings("unchecked")
73 Map
<String
, CommonTaxonName
> commonNameMap
= partitioner
.getObjectMap(MexicoEfloraCommonNameImport
.NAMESPACE
);
75 @SuppressWarnings("unchecked")
76 Map
<String
, Reference
> referenceMap
= partitioner
.getObjectMap(MexicoEfloraReferenceImportBase
.NAMESPACE
);
78 ResultSet rs
= partitioner
.getResultSet();
82 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
84 //create TaxonName element
85 String idCombi
= rs
.getString("IdCombinado");
86 int idBibliografia
= rs
.getInt("IdBibliografia");
87 String observaciones
= rs
.getString("Observaciones");
89 // String idNomComun = rs.getString("IdNomComun");
92 CommonTaxonName commonName
= commonNameMap
.get(idCombi
);
94 Reference ref
= referenceMap
.get(String
.valueOf(idBibliografia
));
95 // String detail = state.getRefDetailMap().get(idBibliografia);
98 if (commonName
!= null) {
99 DescriptionElementSource source
= commonName
.addPrimaryTaxonomicSource(ref
, detail
);
101 TaxonName nameUsedInSource
= getNameUsedInSource(state
, observaciones
);
102 source
.setNameUsedInSource(nameUsedInSource
);
103 //TODO other observaciones
105 logger
.warn("Source not found for " + idCombi
+ " and bibID: " + idBibliografia
);
108 logger
.warn("CommonName not found for " + idCombi
);
111 partitioner
.startDoSave();
112 } catch (Exception e
) {
114 logger
.warn("An exception (" +e
.getMessage()+") occurred when trying to create common name for id " + idCombi
+ ".");
118 } catch (Exception e
) {
119 logger
.error("SQLException:" + e
);
123 logger
.warn("Finished partition");
128 private TaxonName
getNameUsedInSource(MexicoEfloraImportState state
, String observaciones
) {
129 // TODO named used in source for common names
134 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, MexicoEfloraImportState state
) {
138 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
141 Set
<String
> commonNameIdSet
= new HashSet
<>();
142 Set
<String
> referenceIdSet
= new HashSet
<>();
144 handleForeignKey(rs
, referenceIdSet
, "IdBibliografia");
145 handleForeignKey(rs
, commonNameIdSet
, "IdCombinado");
149 nameSpace
= MexicoEfloraCommonNameImport
.NAMESPACE
;
150 Map
<UUID
,String
> uuidMap
= new HashMap
<>();
151 commonNameIdSet
.stream().forEach(cnId
->uuidMap
.put(state
.getCommonNameMap().get(cnId
),cnId
));
152 @SuppressWarnings({ "rawtypes", "unchecked" })
153 List
<CommonTaxonName
> commonNames
= (List
)getDescriptionElementService().find(uuidMap
.keySet());
154 Map
<String
, CommonTaxonName
> commonNameMap
= new HashMap
<>();
155 commonNames
.stream().forEach(cn
->commonNameMap
.put(uuidMap
.get(cn
.getUuid()), cn
));
156 result
.put(nameSpace
, commonNameMap
);
159 nameSpace
= MexicoEfloraReferenceImportBase
.NAMESPACE
;
160 idSet
= referenceIdSet
;
161 Map
<String
, Reference
> referenceMap
= getCommonService().getSourcedObjectsByIdInSourceC(Reference
.class, idSet
, nameSpace
);
162 result
.put(nameSpace
, referenceMap
);
164 } catch (SQLException e
) {
165 throw new RuntimeException(e
);
171 protected String
getTableName() {
176 public String
getPluralString() {
181 protected boolean doCheck(MexicoEfloraImportState state
){
186 protected boolean isIgnore(MexicoEfloraImportState state
){
187 return ! state
.getConfig().isDoTaxa();