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
.taxon
.Synonym
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
44 public class EdaphobaseReferenceImport
extends EdaphobaseImportBase
{
45 private static final long serialVersionUID
= -9138378836474086070L;
46 private static final Logger logger
= Logger
.getLogger(EdaphobaseReferenceImport
.class);
48 private static final String tableName
= "tax_taxon";
50 private static final String pluralString
= "taxa";
52 private static final Object AUTHOR_NAMESPACE
= "tax_author_name";
58 public EdaphobaseReferenceImport() {
59 super(tableName
, pluralString
);
63 protected String
getIdQuery(EdaphobaseImportState state
) {
64 return "SELECT DISTINCT taxon_id FROM tax_taxon t "
65 + " ORDER BY taxon_id";
69 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
70 String result
= " SELECT DISTINCT t.*, r.value as rankStr, pr.value as parentRankStr, ppr.value as grandParentRankStr, "
71 + " pt.name as parentName, ppt.name as grandParentName "
72 + " FROM tax_taxon t "
73 + " LEFT JOIN tax_taxon pt ON t.parent_taxon_fk = pt.taxon_id "
74 + " LEFT JOIN tax_taxon ppt ON pt.parent_taxon_fk = ppt.taxon_id"
75 + " LEFT OUTER JOIN tax_rank_en r ON r.element_id = t.tax_rank_fk "
76 + " LEFT OUTER JOIN tax_rank_en pr ON pr.element_id = pt.tax_rank_fk "
77 + " LEFT OUTER JOIN tax_rank_en ppr ON pr.element_id = ppt.tax_rank_fk "
78 + " WHERE t.taxon_id IN (@IDSET)";
79 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
84 protected void doInvoke(EdaphobaseImportState state
) {
85 super.doInvoke(state
);
90 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
91 ResultSet rs
= partitioner
.getResultSet();
92 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
100 // // "JPACollection"
102 // // "JPABibliography"
108 // // "JPACollectionObject"
109 // // "JPACollectionContainer"
113 Integer id
= nullSafeInt(rs
, "taxon_id");
114 Integer year
= nullSafeInt(rs
, "tax_year");
115 boolean isBrackets
= rs
.getBoolean("tax_brackets");
116 String remark
= rs
.getString("remark");
117 String nameStr
= rs
.getString("name");
118 String authorName
= rs
.getString("tax_author_name");
122 boolean isValid
= rs
.getBoolean("valid");
123 boolean idDeleted
= rs
.getBoolean("deleted");
124 String displayString
= rs
.getString("display_string");
125 Integer version
= nullSafeInt(rs
, "versionfield");
126 String pages
= rs
.getString("pages");
127 String treeIndex
= rs
.getString("path_to_root");
128 // Integer rankFk = nullSafeInt(rs, "tax_rank_fk");
129 String nameAddition
= rs
.getString("name_addition");
130 String officialRemark
= rs
.getString("official_remark");
131 boolean isGroup
= rs
.getBoolean("taxonomic_group");
132 String rankStr
= rs
.getString("rankStr");
133 String parentRankStr
= rs
.getString("parentRankStr");
134 String grandParentRankStr
= rs
.getString("grandParentRankStr");
135 String parentNameStr
= rs
.getString("parentName");
136 String grandParentNameStr
= rs
.getString("grandParentName");
139 TaxonBase
<?
> taxonBase
;
140 Reference
<?
> sec
= null; //TODO
143 Rank rank
= makeRank(state
, rankStr
);
144 ZoologicalName name
= ZoologicalName
.NewInstance(rank
);
145 setNamePart(nameStr
, rank
, name
);
146 Rank parentRank
= makeRank(state
, parentRankStr
);
147 setNamePart(parentNameStr
, parentRank
, name
);
148 Rank parentParentRank
= makeRank(state
, grandParentRankStr
);
149 setNamePart(grandParentNameStr
, parentParentRank
, name
);
152 if (StringUtils
.isNotBlank(authorName
)){
153 TeamOrPersonBase
<?
> author
= state
.getRelatedObject(AUTHOR_NAMESPACE
, authorName
, TeamOrPersonBase
.class);
155 logger
.warn("Author not found in state: " + authorName
);
158 name
.setBasionymAuthorship(author
);
159 name
.setOriginalPublicationYear(year
);
161 name
.setCombinationAuthorship(author
);
162 name
.setPublicationYear(year
);
169 taxonBase
= Taxon
.NewInstance(name
, sec
);
171 taxonBase
= Synonym
.NewInstance(name
, sec
);
173 taxaToSave
.add(taxonBase
);
176 doNotes(taxonBase
, remark
);
179 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
180 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
183 } catch (SQLException e
) {
184 // TODO Auto-generated catch block
188 getTaxonService().saveOrUpdate(taxaToSave
);
194 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
195 EdaphobaseImportState state
) {
196 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
197 Map
<String
, TeamOrPersonBase
<?
>> authorMap
= new HashMap
<>();
198 Set
<String
> authorSet
= new HashSet
<>();
201 String authorStr
= rs
.getString("tax_author_name");
202 authorSet
.add(authorStr
);
204 } catch (SQLException e
) {
209 Set
<UUID
> uuidSet
= new HashSet
<>();
210 for (String authorStr
: authorSet
){
211 UUID uuid
= state
.getAuthorUuid(authorStr
);
214 List
<TeamOrPersonBase
<?
>> authors
= (List
)getAgentService().find(uuidSet
);
215 Map
<UUID
, TeamOrPersonBase
<?
>> authorUuidMap
= new HashMap
<>();
216 for (TeamOrPersonBase
<?
> author
: authors
){
217 authorUuidMap
.put(author
.getUuid(), author
);
220 for (String authorStr
: authorSet
){
221 UUID uuid
= state
.getAuthorUuid(authorStr
);
222 TeamOrPersonBase
<?
> author
= authorUuidMap
.get(uuid
);
223 authorMap
.put(authorStr
, author
);
225 result
.put(AUTHOR_NAMESPACE
, authorMap
);
230 private void setNamePart(String nameStr
, Rank rank
, ZoologicalName name
) {
232 if (rank
.isSupraGeneric() || rank
.isGenus()){
233 if (StringUtils
.isBlank(name
.getGenusOrUninomial())){
234 name
.setGenusOrUninomial(nameStr
);
236 }else if (rank
.isInfraGeneric()){
237 if (StringUtils
.isBlank(name
.getInfraGenericEpithet())){
238 name
.setInfraGenericEpithet(nameStr
);
240 }else if (rank
.isSpeciesAggregate() || rank
.isSpecies()){
241 if (StringUtils
.isBlank(name
.getSpecificEpithet())){
242 name
.setSpecificEpithet(nameStr
);
244 }else if (rank
.isInfraSpecific()){
245 if (StringUtils
.isBlank(name
.getInfraSpecificEpithet())){
246 name
.setInfraSpecificEpithet(nameStr
);
252 private Rank
makeRank(EdaphobaseImportState state
, String rankStr
) {
255 rank
= state
.getTransformer().getRankByKey(rankStr
);
256 } catch (UndefinedTransformerMethodException e
) {
263 protected boolean doCheck(EdaphobaseImportState state
) {
268 protected boolean isIgnore(EdaphobaseImportState state
) {
269 return ! state
.getConfig().isDoTaxa();