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.
10 package eu
.etaxonomy
.cdm
.io
.mexico
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
23 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelReferenceImport
;
24 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelTaxonNameImport
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
28 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
29 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
30 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
31 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
38 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
46 public class MexicoEfloraTaxonImport
extends MexicoEfloraImportBase
{
48 private static final long serialVersionUID
= -1186364983750790695L;
50 private static final Logger logger
= Logger
.getLogger(MexicoEfloraTaxonImport
.class);
52 public static final String NAMESPACE
= "Taxon";
54 private static final String pluralString
= "Taxa";
55 private static final String dbTableName
= "Efloa_Taxonomia4CDM2 ";
59 * How should the publish flag in table PTaxon be interpreted
60 * NO_MARKER: No marker is set
63 public enum PublishMarkerChooser
{
69 boolean doMark(boolean value
){
71 return this == ALL
|| this == ONLY_TRUE
;
73 return this == ALL
|| this == ONLY_FALSE
;
78 public MexicoEfloraTaxonImport(){
79 super(dbTableName
, pluralString
);
83 protected String
getIdQuery(MexicoEfloraImportState state
) {
84 String sql
= " SELECT IdCAT "
85 + " FROM " + dbTableName
91 protected String
getRecordQuery(MexicoEfloraImportConfigurator config
) {
92 String sqlSelect
= " SELECT * ";
93 String sqlFrom
= " FROM " + dbTableName
;
94 String sqlWhere
= " WHERE ( IdCAT IN (" + ID_LIST_TOKEN
+ ") )";
96 String strRecordQuery
=sqlSelect
+ " " + sqlFrom
+ " " + sqlWhere
;
97 return strRecordQuery
;
101 protected boolean doCheck(MexicoEfloraImportState state
){
102 //IOValidator<BerlinModelImportState> validator = new BerlinModelTaxonImportValidator();
103 //return validator.validate(state);
108 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner
, MexicoEfloraImportState state
) {
110 boolean success
= true ;
111 MexicoEfloraImportConfigurator config
= state
.getConfig();
112 @SuppressWarnings("rawtypes")
113 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
114 // @SuppressWarnings("unchecked")
115 // Map<String, TaxonName> taxonNameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
116 @SuppressWarnings("unchecked")
117 Map
<String
, Reference
> refMap
= partitioner
.getObjectMap(BerlinModelReferenceImport
.REFERENCE_NAMESPACE
);
119 ResultSet rs
= partitioner
.getResultSet();
123 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
125 //create TaxonName element
126 String taxonId
= rs
.getString("IdCAT");
127 String status
= rs
.getString("EstatusNombre");
128 String rankStr
= rs
.getString("CategoriaTaxonomica");
129 String nameStr
= rs
.getString("Nombre");
130 String autorStr
= rs
.getString("AutorSinAnio");
131 String fullNameStr
= nameStr
+ " " + autorStr
;
132 String annotationStr
= rs
.getString("AnotacionTaxon");
133 // String type = rs.getString("NomPublicationType");
134 String year
= rs
.getString("Anio");
135 int secFk
= rs
.getInt("IdBibliografiaSec");
136 int nameId
= rs
.getInt("idNombre");
139 //IdCATRel => Accepted Taxon => TaxonRel
140 //IdCAT_AscendenteHerarquico4CDM => Parent => TaxonRel
141 //IdCAT_BasNomOrig => Basionyme der akzeptierten Taxa => TaxonRel
143 Rank rank
= Rank
.GENUS(); //FIXME
144 NonViralNameParserImpl parser
= NonViralNameParserImpl
.NewInstance();
145 TaxonName taxonName
= (TaxonName
)parser
.parseFullName(fullNameStr
);
147 String refFkStr
= String
.valueOf(secFk
);
148 Reference sec
= refMap
.get(refFkStr
);
150 TaxonBase
<?
> taxonBase
;
154 if ("aceptado".equals(status
)){
155 taxon
= Taxon
.NewInstance(taxonName
, sec
);
157 }else if ("sinĂ³nimo".equals(status
)){
158 synonym
= Synonym
.NewInstance(taxonName
, sec
);
162 logger
.error("Status not yet implemented: " + status
);
166 DefinedTerm taxonIdType
= DefinedTerm
.IDENTIFIER_NAME_IPNI();
167 taxonBase
.addIdentifier(taxonId
, taxonIdType
);
170 String namePhrase
= rs
.getString("NamePhrase");
171 if (StringUtils
.isNotBlank(namePhrase
)){
172 taxonBase
.setAppendedPhrase(namePhrase
);
176 // boolean excludeNotes = state.getConfig().isTaxonNoteAsFeature() && taxonBase.isInstanceOf(Taxon.class);
177 // String notes = rs.getString("Notes");
179 // doIdCreatedUpdatedNotes(state, taxonBase, rs, taxonId, NAMESPACE, false);
180 // if (excludeNotes && notes != null){
181 // makeTaxonomicNote(state, CdmBase.deproxy(taxonBase, Taxon.class), rs.getString("Notes"));
184 partitioner
.startDoSave();
185 taxaToSave
.add(taxonBase
);
186 } catch (Exception e
) {
187 logger
.warn("An exception (" +e
.getMessage()+") occurred when creating taxon with id " + taxonId
+ ". Taxon could not be saved.");
191 } catch (Exception e
) {
192 logger
.error("SQLException:" + e
);
196 getTaxonService().save(taxaToSave
);
201 protected String
getIdInSource(MexicoEfloraImportState state
, ResultSet rs
) throws SQLException
{
202 String id
= rs
.getString("idInSource");
206 private void makeTaxonomicNote(MexicoEfloraImportState state
, Taxon taxon
, String notes
) {
207 if (isNotBlank(notes
)){
208 TaxonDescription desc
= getTaxonDescription(taxon
, false, true);
209 desc
.setDefault(true); //hard coded for Salvador, not used elsewhere as far as I can see
210 TextData textData
= TextData
.NewInstance(Feature
.NOTES() , notes
, Language
.SPANISH_CASTILIAN(), null);
211 desc
.addElement(textData
);
216 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, MexicoEfloraImportState state
) {
220 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
223 Set
<String
> nameIdSet
= new HashSet
<>();
224 Set
<String
> referenceIdSet
= new HashSet
<>();
226 handleForeignKey(rs
, nameIdSet
, "PTNameFk");
227 handleForeignKey(rs
, referenceIdSet
, "PTRefFk");
231 nameSpace
= BerlinModelTaxonNameImport
.NAMESPACE
;
233 Map
<String
, TaxonName
> nameMap
= getCommonService().getSourcedObjectsByIdInSourceC(TaxonName
.class, idSet
, nameSpace
);
234 result
.put(nameSpace
, nameMap
);
237 nameSpace
= BerlinModelReferenceImport
.REFERENCE_NAMESPACE
;
238 idSet
= referenceIdSet
;
239 Map
<String
, Reference
> referenceMap
= getCommonService().getSourcedObjectsByIdInSourceC(Reference
.class, idSet
, nameSpace
);
240 result
.put(nameSpace
, referenceMap
);
242 } catch (SQLException e
) {
243 throw new RuntimeException(e
);
249 protected String
getTableName() {
254 public String
getPluralString() {
259 protected boolean isIgnore(MexicoEfloraImportState state
){
260 return ! state
.getConfig().isDoTaxa();