3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.edaphobase
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.codehaus
.plexus
.util
.StringUtils
;
23 import org
.springframework
.stereotype
.Component
;
25 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
26 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
28 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
31 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
32 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
45 public class EdaphobaseTaxonImport
extends EdaphobaseImportBase
{
46 private static final long serialVersionUID
= -9138378836474086070L;
47 private static final Logger logger
= Logger
.getLogger(EdaphobaseTaxonImport
.class);
49 private static final String tableName
= "tax_taxon";
51 private static final String pluralString
= "taxa";
53 private static final Object AUTHOR_NAMESPACE
= "tax_author_name";
59 public EdaphobaseTaxonImport() {
60 super(tableName
, pluralString
);
64 protected String
getIdQuery(EdaphobaseImportState state
) {
65 return "SELECT DISTINCT taxon_id FROM tax_taxon t "
66 + " ORDER BY taxon_id";
70 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
71 String result
= " SELECT DISTINCT t.*, r.value as rankStr, pr.value as parentRankStr, ppr.value as grandParentRankStr, "
72 + " pt.name as parentName, ppt.name as grandParentName "
73 + " FROM tax_taxon t "
74 + " LEFT JOIN tax_taxon pt ON t.parent_taxon_fk = pt.taxon_id "
75 + " LEFT JOIN tax_taxon ppt ON pt.parent_taxon_fk = ppt.taxon_id"
76 + " LEFT OUTER JOIN tax_rank_en r ON r.element_id = t.tax_rank_fk "
77 + " LEFT OUTER JOIN tax_rank_en pr ON pr.element_id = pt.tax_rank_fk "
78 + " LEFT OUTER JOIN tax_rank_en ppr ON pr.element_id = ppt.tax_rank_fk "
79 + " WHERE t.taxon_id IN (@IDSET)";
80 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
85 protected void doInvoke(EdaphobaseImportState state
) {
86 super.doInvoke(state
);
91 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
92 ResultSet rs
= partitioner
.getResultSet();
93 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
96 makeSingleTaxon(state
, rs
, taxaToSave
);
99 } catch (SQLException e
) {
100 // TODO Auto-generated catch block
104 getTaxonService().saveOrUpdate(taxaToSave
);
112 * @throws SQLException
114 private void makeSingleTaxon(EdaphobaseImportState state
, ResultSet rs
, Set
<TaxonBase
> taxaToSave
)
115 throws SQLException
{
116 Integer id
= nullSafeInt(rs
, "taxon_id");
117 Integer year
= nullSafeInt(rs
, "tax_year");
118 boolean isBrackets
= rs
.getBoolean("tax_brackets");
119 String remark
= rs
.getString("remark");
120 String nameStr
= rs
.getString("name");
121 String authorName
= rs
.getString("tax_author_name");
124 Integer nomRefId
= nullSafeInt(rs
, "tax_document");
125 boolean isValid
= rs
.getBoolean("valid");
126 boolean idDeleted
= rs
.getBoolean("deleted");
127 String displayString
= rs
.getString("display_string");
128 Integer version
= nullSafeInt(rs
, "versionfield");
129 String pages
= rs
.getString("pages");
130 String treeIndex
= rs
.getString("path_to_root");
131 // Integer rankFk = nullSafeInt(rs, "tax_rank_fk");
132 String nameAddition
= rs
.getString("name_addition");
133 String officialRemark
= rs
.getString("official_remark");
134 boolean isGroup
= rs
.getBoolean("taxonomic_group");
135 String rankStr
= rs
.getString("rankStr");
136 String parentRankStr
= rs
.getString("parentRankStr");
137 String grandParentRankStr
= rs
.getString("grandParentRankStr");
138 String parentNameStr
= rs
.getString("parentName");
139 String grandParentNameStr
= rs
.getString("grandParentName");
141 TaxonBase
<?
> taxonBase
;
144 Rank rank
= makeRank(state
, rankStr
);
145 ZoologicalName name
= ZoologicalName
.NewInstance(rank
);
146 setNamePart(nameStr
, rank
, name
);
147 Rank parentRank
= makeRank(state
, parentRankStr
);
148 setNamePart(parentNameStr
, parentRank
, name
);
149 Rank parentParentRank
= makeRank(state
, grandParentRankStr
);
150 setNamePart(grandParentNameStr
, parentParentRank
, name
);
153 if (StringUtils
.isNotBlank(authorName
)){
154 TeamOrPersonBase
<?
> author
= state
.getRelatedObject(AUTHOR_NAMESPACE
, authorName
, TeamOrPersonBase
.class);
156 logger
.warn("Author not found in state: " + authorName
);
159 name
.setBasionymAuthorship(author
);
160 name
.setOriginalPublicationYear(year
);
162 name
.setCombinationAuthorship(author
);
163 name
.setPublicationYear(year
);
169 if (nomRefId
!= null){
170 Reference
<?
> nomRef
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(nomRefId
), Reference
.class);
172 logger
.warn("Reference " + nomRefId
+ " could not be found");
174 name
.setNomenclaturalReference(nomRef
);
176 name
.setNomenclaturalMicroReference(StringUtils
.isBlank(pages
)?
null : pages
);
179 Reference
<?
> secRef
= state
.getRelatedObject(REFERENCE_NAMESPACE
, state
.getConfig().getSecUuid().toString(), Reference
.class);
181 secRef
= makeSecRef(state
);
184 taxonBase
= Taxon
.NewInstance(name
, secRef
);
186 taxonBase
= Synonym
.NewInstance(name
, secRef
);
188 taxaToSave
.add(taxonBase
);
191 doNotes(taxonBase
, remark
);
194 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
195 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
203 private Reference
<?
> makeSecRef(EdaphobaseImportState state
) {
204 Reference
<?
> ref
= ReferenceFactory
.newDatabase();
205 ref
.setTitle(state
.getConfig().getEdaphobaseSecundumTitle());
206 ref
.setUuid(state
.getConfig().getSecUuid());
207 state
.addRelatedObject(REFERENCE_NAMESPACE
, ref
.getUuid().toString(), ref
);
208 getReferenceService().save(ref
);
213 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
214 EdaphobaseImportState state
) {
215 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
216 Map
<String
, TeamOrPersonBase
<?
>> authorMap
= new HashMap
<>();
217 Set
<String
> authorSet
= new HashSet
<>();
218 Set
<String
> referenceIdSet
= new HashSet
<String
>();
222 String authorStr
= rs
.getString("tax_author_name");
223 authorSet
.add(authorStr
);
224 handleForeignKey(rs
, referenceIdSet
, "tax_document");
226 } catch (SQLException e
) {
231 Set
<UUID
> uuidSet
= new HashSet
<>();
232 for (String authorStr
: authorSet
){
233 UUID uuid
= state
.getAuthorUuid(authorStr
);
236 List
<TeamOrPersonBase
<?
>> authors
= (List
)getAgentService().find(uuidSet
);
237 Map
<UUID
, TeamOrPersonBase
<?
>> authorUuidMap
= new HashMap
<>();
238 for (TeamOrPersonBase
<?
> author
: authors
){
239 authorUuidMap
.put(author
.getUuid(), author
);
242 for (String authorStr
: authorSet
){
243 UUID uuid
= state
.getAuthorUuid(authorStr
);
244 TeamOrPersonBase
<?
> author
= authorUuidMap
.get(uuid
);
245 authorMap
.put(authorStr
, author
);
247 result
.put(AUTHOR_NAMESPACE
, authorMap
);
250 String nameSpace
= REFERENCE_NAMESPACE
;
251 Class
<?
> cdmClass
= Reference
.class;
252 Set
<String
> idSet
= referenceIdSet
;
253 Map
<String
, Reference
<?
>> referenceMap
= (Map
<String
, Reference
<?
>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
254 result
.put(nameSpace
, referenceMap
);
257 UUID secUuid
= state
.getConfig().getSecUuid();
258 Reference
<?
> secRef
= getReferenceService().find(secUuid
);
259 referenceMap
.put(secUuid
.toString(), secRef
);
264 private void setNamePart(String nameStr
, Rank rank
, ZoologicalName name
) {
266 if (rank
.isSupraGeneric() || rank
.isGenus()){
267 if (StringUtils
.isBlank(name
.getGenusOrUninomial())){
268 name
.setGenusOrUninomial(nameStr
);
270 }else if (rank
.isInfraGeneric()){
271 if (StringUtils
.isBlank(name
.getInfraGenericEpithet())){
272 name
.setInfraGenericEpithet(nameStr
);
274 }else if (rank
.isSpeciesAggregate() || rank
.isSpecies()){
275 if (StringUtils
.isBlank(name
.getSpecificEpithet())){
276 name
.setSpecificEpithet(nameStr
);
278 }else if (rank
.isInfraSpecific()){
279 if (StringUtils
.isBlank(name
.getInfraSpecificEpithet())){
280 name
.setInfraSpecificEpithet(nameStr
);
286 private Rank
makeRank(EdaphobaseImportState state
, String rankStr
) {
289 rank
= state
.getTransformer().getRankByKey(rankStr
);
290 } catch (UndefinedTransformerMethodException e
) {
297 protected boolean doCheck(EdaphobaseImportState state
) {
302 protected boolean isIgnore(EdaphobaseImportState state
) {
303 return ! state
.getConfig().isDoTaxa();