2 * Copyright (C) 2015 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
.edaphobase
;
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
.codehaus
.plexus
.util
.StringUtils
;
22 import org
.springframework
.stereotype
.Component
;
24 import eu
.etaxonomy
.cdm
.io
.common
.IPartitionedIO
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
26 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
27 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.UndefinedTransformerMethodException
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
32 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
34 import eu
.etaxonomy
.cdm
.model
.name
.IZoologicalName
;
35 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameFactory
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
38 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 public class EdaphobaseTaxonImport
extends EdaphobaseImportBase
{
50 private static final long serialVersionUID
= -9138378836474086070L;
51 private static final Logger logger
= Logger
.getLogger(EdaphobaseTaxonImport
.class);
53 private static final String tableName
= "tax_taxon";
55 private static final String pluralString
= "taxa";
57 private static final Object AUTHOR_NAMESPACE
= "tax_author_name";
63 public EdaphobaseTaxonImport() {
64 super(tableName
, pluralString
);
68 protected String
getIdQuery(EdaphobaseImportState state
) {
69 return "SELECT DISTINCT taxon_id FROM tax_taxon t "
70 + " ORDER BY taxon_id";
74 protected String
getRecordQuery(EdaphobaseImportConfigurator config
) {
75 String result
= " SELECT DISTINCT t.*, r.value as rankStr, pr.value as parentRankStr, ppr.value as grandParentRankStr, "
76 + " pt.name as parentName, ppt.name as grandParentName "
77 + " FROM tax_taxon t "
78 + " LEFT JOIN tax_taxon pt ON t.parent_taxon_fk = pt.taxon_id "
79 + " LEFT JOIN tax_taxon ppt ON pt.parent_taxon_fk = ppt.taxon_id"
80 + " LEFT OUTER JOIN tax_rank_en r ON r.element_id = t.tax_rank_fk "
81 + " LEFT OUTER JOIN tax_rank_en pr ON pr.element_id = pt.tax_rank_fk "
82 + " LEFT OUTER JOIN tax_rank_en ppr ON ppr.element_id = ppt.tax_rank_fk "
83 + " WHERE t.taxon_id IN (@IDSET)";
84 result
= result
.replace("@IDSET", IPartitionedIO
.ID_LIST_TOKEN
);
89 protected void doInvoke(EdaphobaseImportState state
) {
90 super.doInvoke(state
);
95 public boolean doPartition(ResultSetPartitioner partitioner
, EdaphobaseImportState state
) {
96 ResultSet rs
= partitioner
.getResultSet();
97 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
100 makeSingleTaxon(state
, rs
, taxaToSave
);
102 } catch (SQLException
| UndefinedTransformerMethodException e
) {
106 getTaxonService().saveOrUpdate(taxaToSave
);
114 * @throws SQLException
115 * @throws UndefinedTransformerMethodException
117 private void makeSingleTaxon(EdaphobaseImportState state
, ResultSet rs
, Set
<TaxonBase
> taxaToSave
)
118 throws SQLException
, UndefinedTransformerMethodException
{
119 Integer id
= nullSafeInt(rs
, "taxon_id");
120 Integer year
= nullSafeInt(rs
, "tax_year");
121 boolean isBrackets
= rs
.getBoolean("tax_brackets");
122 String remark
= rs
.getString("remark");
123 String nameStr
= rs
.getString("name");
124 String authorName
= rs
.getString("tax_author_name");
127 Integer nomRefId
= nullSafeInt(rs
, "tax_document");
128 boolean isValid
= rs
.getBoolean("valid");
129 boolean idDeleted
= rs
.getBoolean("deleted");
130 String displayString
= rs
.getString("display_string");
131 Integer version
= nullSafeInt(rs
, "versionfield");
132 String pages
= rs
.getString("pages");
133 String treeIndex
= rs
.getString("path_to_root");
134 // Integer rankFk = nullSafeInt(rs, "tax_rank_fk");
135 String nameAddition
= rs
.getString("name_addition");
136 String officialRemark
= rs
.getString("official_remark");
137 boolean isGroup
= rs
.getBoolean("taxonomic_group");
138 String rankStr
= rs
.getString("rankStr");
139 String parentRankStr
= rs
.getString("parentRankStr");
140 String grandParentRankStr
= rs
.getString("grandParentRankStr");
141 String parentNameStr
= rs
.getString("parentName");
142 String grandParentNameStr
= rs
.getString("grandParentName");
144 TaxonBase
<?
> taxonBase
;
147 Rank rank
= makeRank(state
, rankStr
);
148 checkRankMarker(state
, rank
);
149 IZoologicalName name
= TaxonNameFactory
.NewZoologicalInstance(rank
);
150 setNamePart(nameStr
, rank
, name
);
151 Rank parentRank
= makeRank(state
, parentRankStr
);
152 setNamePart(parentNameStr
, parentRank
, name
);
153 Rank parentParentRank
= makeRank(state
, grandParentRankStr
);
154 setNamePart(grandParentNameStr
, parentParentRank
, name
);
155 if (parentParentRank
!= null && parentParentRank
.isLower(Rank
.GENUS()) || isBlank(name
.getGenusOrUninomial()) ){
156 logger
.warn("Grandparent rank is lower than genus for " + name
.getTitleCache() + " (edapho-id: " + id
+ "; cdm-id: " + name
.getId());
160 if (StringUtils
.isNotBlank(authorName
)){
161 TeamOrPersonBase
<?
> author
= state
.getRelatedObject(AUTHOR_NAMESPACE
, authorName
, TeamOrPersonBase
.class);
163 logger
.warn("Author not found in state: " + authorName
);
166 name
.setBasionymAuthorship(author
);
167 name
.setOriginalPublicationYear(year
);
169 name
.setCombinationAuthorship(author
);
170 name
.setPublicationYear(year
);
176 if (nomRefId
!= null){
177 Reference nomRef
= state
.getRelatedObject(REFERENCE_NAMESPACE
, String
.valueOf(nomRefId
), Reference
.class);
179 logger
.warn("Reference " + nomRefId
+ " could not be found");
181 name
.setNomenclaturalReference(nomRef
);
183 name
.setNomenclaturalMicroReference(StringUtils
.isBlank(pages
)?
null : pages
);
186 Reference secRef
= state
.getRelatedObject(REFERENCE_NAMESPACE
, state
.getConfig().getSecUuid().toString(), Reference
.class);
188 secRef
= makeSecRef(state
);
191 taxonBase
= Taxon
.NewInstance(name
, secRef
);
193 taxonBase
= Synonym
.NewInstance(name
, secRef
);
195 handleTaxonomicGroupMarker(state
, taxonBase
, isGroup
);
196 taxaToSave
.add(taxonBase
);
199 doNotes(taxonBase
, remark
);
202 ImportHelper
.setOriginalSource(taxonBase
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
203 ImportHelper
.setOriginalSource(name
, state
.getTransactionalSourceReference(), id
, TAXON_NAMESPACE
);
210 * @throws UndefinedTransformerMethodException
212 private void checkRankMarker(EdaphobaseImportState state
, Rank rank
) throws UndefinedTransformerMethodException
{
214 Set
<Marker
> markers
= rank
.getMarkers();
215 if ( markers
.size() == 0){ //we assume that no markers exist. at least not for markers of unused ranks
216 UUID edaphoRankMarkerTypeUuid
= state
.getTransformer().getMarkerTypeUuid("EdaphoRankMarker");
217 MarkerType marker
= getMarkerType(state
, edaphoRankMarkerTypeUuid
, "Edaphobase rank", "Rank used in Edaphobase", "EdaRk" );
218 Representation rep
= Representation
.NewInstance("Rang, verwendet in Edaphobase", "Edaphobase Rang", "EdaRg", Language
.GERMAN());
219 marker
.addRepresentation(rep
);
220 rank
.addMarker(Marker
.NewInstance(marker
, true));
221 getTermService().saveOrUpdate(rank
);
230 private void handleTaxonomicGroupMarker(EdaphobaseImportState state
, TaxonBase
<?
> taxonBase
, boolean isGroup
) {
235 MarkerType markerType
= getMarkerType(state
, state
.getTransformer().getMarkerTypeUuid("TaxGrossgruppe"), "Tax. Gruppe", "Taxonomische Grossgruppe", "TGG", null, Language
.GERMAN());
236 if (taxonBase
.isInstanceOf(Synonym
.class)){
237 logger
.warn("Syonym is marked as 'taxonomische Grossgruppe'");
239 taxonBase
.addMarker(Marker
.NewInstance(markerType
, true));
240 } catch (UndefinedTransformerMethodException e
) {
249 private Reference
makeSecRef(EdaphobaseImportState state
) {
250 Reference ref
= ReferenceFactory
.newDatabase();
251 ref
.setTitle(state
.getConfig().getEdaphobaseSecundumTitle());
252 ref
.setUuid(state
.getConfig().getSecUuid());
253 state
.addRelatedObject(REFERENCE_NAMESPACE
, ref
.getUuid().toString(), ref
);
254 getReferenceService().save(ref
);
259 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
260 EdaphobaseImportState state
) {
261 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
262 Map
<String
, TeamOrPersonBase
<?
>> authorMap
= new HashMap
<>();
263 Set
<String
> authorSet
= new HashSet
<>();
264 Set
<String
> referenceIdSet
= new HashSet
<String
>();
268 String authorStr
= rs
.getString("tax_author_name");
269 authorSet
.add(authorStr
);
270 handleForeignKey(rs
, referenceIdSet
, "tax_document");
272 } catch (SQLException e
) {
277 Set
<UUID
> uuidSet
= new HashSet
<>();
278 for (String authorStr
: authorSet
){
279 UUID uuid
= state
.getAuthorUuid(authorStr
);
282 List
<TeamOrPersonBase
<?
>> authors
= (List
)getAgentService().find(uuidSet
);
283 Map
<UUID
, TeamOrPersonBase
<?
>> authorUuidMap
= new HashMap
<>();
284 for (TeamOrPersonBase
<?
> author
: authors
){
285 authorUuidMap
.put(author
.getUuid(), author
);
288 for (String authorStr
: authorSet
){
289 UUID uuid
= state
.getAuthorUuid(authorStr
);
290 TeamOrPersonBase
<?
> author
= authorUuidMap
.get(uuid
);
291 authorMap
.put(authorStr
, author
);
293 result
.put(AUTHOR_NAMESPACE
, authorMap
);
296 String nameSpace
= REFERENCE_NAMESPACE
;
297 Class
<?
> cdmClass
= Reference
.class;
298 Set
<String
> idSet
= referenceIdSet
;
299 Map
<String
, Reference
> referenceMap
= (Map
<String
, Reference
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
300 result
.put(nameSpace
, referenceMap
);
303 UUID secUuid
= state
.getConfig().getSecUuid();
304 Reference secRef
= getReferenceService().find(secUuid
);
305 referenceMap
.put(secUuid
.toString(), secRef
);
310 private void setNamePart(String nameStr
, Rank rank
, IZoologicalName name
) {
312 if (rank
.isSupraGeneric() || rank
.isGenus()){
313 if (StringUtils
.isBlank(name
.getGenusOrUninomial())){
314 name
.setGenusOrUninomial(nameStr
);
316 }else if (rank
.isInfraGeneric()){
317 if (StringUtils
.isBlank(name
.getInfraGenericEpithet())){
318 name
.setInfraGenericEpithet(nameStr
);
320 }else if (rank
.isSpeciesAggregate() || rank
.isSpecies()){
321 if (StringUtils
.isBlank(name
.getSpecificEpithet())){
322 name
.setSpecificEpithet(nameStr
);
324 }else if (rank
.isInfraSpecific()){
325 if (StringUtils
.isBlank(name
.getInfraSpecificEpithet())){
326 name
.setInfraSpecificEpithet(nameStr
);
332 private Rank
makeRank(EdaphobaseImportState state
, String rankStr
) {
335 rank
= state
.getTransformer().getRankByKey(rankStr
);
336 } catch (UndefinedTransformerMethodException e
) {
343 protected boolean doCheck(EdaphobaseImportState state
) {
348 protected boolean isIgnore(EdaphobaseImportState state
) {
349 return ! state
.getConfig().isDoTaxa();