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
.CategoricalData
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
27 import eu
.etaxonomy
.cdm
.model
.description
.State
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
38 public class MexicoEfloraFactImport
extends MexicoEfloraImportBase
{
40 private static final long serialVersionUID
= 8097679811768529307L;
41 private static final Logger logger
= Logger
.getLogger(MexicoEfloraFactImport
.class);
43 protected static final String NAMESPACE
= "Facts";
45 private static final String pluralString
= "facts";
46 private static final String dbTableName
= "Eflora_RelBiblioNombreCatalogoNombre";
48 public MexicoEfloraFactImport(){
49 super(dbTableName
, pluralString
);
53 protected String
getIdQuery(MexicoEfloraImportState state
) {
54 String sql
= " SELECT id "
55 + " FROM " + dbTableName
56 + " ORDER BY IdCAT, IdCatNombre, IdBibliografia ";
61 protected String
getRecordQuery(MexicoEfloraImportConfigurator config
) {
62 String sqlSelect
= " SELECT f.*, t.uuid taxonUuid ";
63 String sqlFrom
= " FROM " + dbTableName
+ " f LEFT JOIN " + MexicoEfloraTaxonImport
.dbTableName
+ " t ON f.IdCAT = t.IdCAT ";
64 String sqlWhere
= " WHERE ( Id IN (" + ID_LIST_TOKEN
+ ") )";
65 String sqlOrderBy
= " ORDER BY IdCAT, IdCatNombre, IdBibliografia";
67 String strRecordQuery
=sqlSelect
+ " " + sqlFrom
+ " " + sqlWhere
+ sqlOrderBy
;
68 return strRecordQuery
;
71 private CategoricalData lastFact
;
72 private String lastIdCat
= "-1";
73 private int lastIdCatNombre
= -1;
75 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, MexicoEfloraImportState state
) {
77 Reference sourceReference
= this.getSourceReference(state
.getConfig().getSourceReference());
78 //hope this is transaction save
79 if (lastFact
!= null) {
80 lastFact
= (CategoricalData
)getDescriptionElementService().find(lastFact
.getUuid());
82 boolean success
= true ;
84 @SuppressWarnings("unchecked")
85 Map
<String
, TaxonBase
<?
>> taxonMap
= partitioner
.getObjectMap(MexicoEfloraTaxonImport
.NAMESPACE
);
86 @SuppressWarnings("unchecked")
87 Map
<String
, Reference
> referenceMap
= partitioner
.getObjectMap(MexicoEfloraReferenceImportBase
.NAMESPACE
);
89 ResultSet rs
= partitioner
.getResultSet();
93 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
95 //create TaxonName element
96 int id
= rs
.getInt("id"); //only for partitioning and logging
97 String idCAT
= rs
.getString("IdCAT");
98 int idCatNombre
= rs
.getInt("IdCatNombre");
99 String uuidTaxonStr
= rs
.getString("taxonUuid");
101 int idBibliografia
= rs
.getInt("IdBibliografia");
102 //TODO observaciones in facts
103 String observaciones
= rs
.getString("Observaciones");
106 CategoricalData categoricalData
;
107 if (idCAT
.equals(lastIdCat
) && idCatNombre
== lastIdCatNombre
) {
108 categoricalData
= lastFact
;
110 categoricalData
= makeCategoricalData(state
, idCatNombre
);
111 Feature lastFeature
= lastFact
== null?
null : lastFact
.getFeature();
112 if (idCAT
.equals(lastIdCat
) && categoricalData
.getFeature().equals(lastFeature
)) {
114 //add the single new state to the existing categorical data
115 //TODO not fully correct if bibliography differs for the single states;
116 State newState
= categoricalData
.getStatesOnly().stream().findFirst().orElse(null);
117 if (newState
!= null) {
118 lastFact
.addStateData(newState
);
120 categoricalData
= lastFact
;
121 // lastIdCatNombre = idCatNombre;
123 //new categorical data
124 TaxonBase
<?
> taxonBase
= taxonMap
.get(uuidTaxonStr
);
126 if (taxonBase
.isInstanceOf(Taxon
.class)) {
127 taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
129 logger
.warn(idCatNombre
+ ": Taxon is not accepted: " + idCAT
);
133 //TODO source reference correct?
134 TaxonDescription description
= this.getTaxonDescription(taxon
, sourceReference
,
136 description
.addElement(categoricalData
);
137 lastFact
= categoricalData
;
139 lastIdCatNombre
= idCatNombre
;
142 handleBibliografia(state
, referenceMap
, categoricalData
, idBibliografia
, id
);
144 partitioner
.startDoSave();
145 } catch (Exception e
) {
147 logger
.warn("An exception (" +e
.getMessage()+") occurred when trying to create fact for id " + id
+ ".");
151 } catch (Exception e
) {
152 logger
.error("SQLException:" + e
);
155 logger
.warn("Next partition");
160 private void handleBibliografia(MexicoEfloraImportState state
, Map
<String
, Reference
> referenceMap
,
161 CategoricalData categoricalData
, int idBibliografia
,
163 Reference ref
= referenceMap
== null ?
null : referenceMap
.get(String
.valueOf(idBibliografia
));
164 // String detail = state.getRefDetailMap().get(idBibliografia);
165 String detail
= null;
168 if (categoricalData
!= null) {
169 categoricalData
.addPrimaryTaxonomicSource(ref
, detail
);
171 logger
.warn("Fact does not exist: " + id
);
174 logger
.warn("Source not found for " + id
+ " and bibID: " + idBibliografia
);
178 private CategoricalData
makeCategoricalData(MexicoEfloraImportState importState
,
180 Feature feature
= getFeature(importState
, idCatNombre
);
181 State state
= getState(importState
, idCatNombre
);
182 CategoricalData categoricalData
= CategoricalData
.NewInstance(state
, feature
);
183 return categoricalData
;
186 private State
getState(MexicoEfloraImportState importState
, int idCatNombre
) {
187 State result
= importState
.getStateMap().get(idCatNombre
);
188 if (result
== null) {
189 logger
.warn("State does not exist: " + idCatNombre
);
194 private Feature
getFeature(MexicoEfloraImportState importState
, int idCatNombre
) {
195 return importState
.getFeatureMap().get(idCatNombre
);
199 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, MexicoEfloraImportState state
) {
203 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
206 Set
<UUID
> taxonIdSet
= new HashSet
<>();
207 Set
<String
> referenceIdSet
= new HashSet
<>();
209 handleForeignUuidKey(rs
, taxonIdSet
, "taxonUuid");
210 handleForeignKey(rs
, referenceIdSet
, "IdBibliografia");
214 nameSpace
= MexicoEfloraTaxonImport
.NAMESPACE
;
215 @SuppressWarnings("rawtypes")
216 Map
<String
, TaxonBase
> taxonMap
= new HashMap
<>();
217 @SuppressWarnings("rawtypes")
218 List
<TaxonBase
> taxa
= getTaxonService().find(taxonIdSet
);
219 taxa
.stream().forEach(t
->taxonMap
.put(t
.getUuid().toString(), t
));
220 result
.put(nameSpace
, taxonMap
);
223 nameSpace
= MexicoEfloraReferenceImportBase
.NAMESPACE
;
224 idSet
= referenceIdSet
;
225 Map
<String
, Reference
> referenceMap
= getCommonService().getSourcedObjectsByIdInSourceC(Reference
.class, idSet
, nameSpace
);
226 result
.put(nameSpace
, referenceMap
);
228 } catch (SQLException e
) {
229 throw new RuntimeException(e
);
235 protected String
getTableName() {
240 public String
getPluralString() {
245 protected boolean doCheck(MexicoEfloraImportState state
){
250 protected boolean isIgnore(MexicoEfloraImportState state
){
251 return ! state
.getConfig().isDoTaxa();