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
);
204 handleExampleIdentifiers(taxonBase
, id
);
208 static Map
<Integer
,UUID
> idMap
= new HashMap
<>();
210 idMap
.put(86594, UUID
.fromString("715c2370-45a4-450c-99f7-e196758979ca")); //Aporrectodea caliginosa
211 idMap
.put(86593, UUID
.fromString("230f1a69-5dcd-4829-a01c-17490a2fdf34")); //Aporrectodea
212 idMap
.put(86684, UUID
.fromString("0982dc0e-1a79-45a0-8abc-8166625b94b8")); //Achaeta
213 idMap
.put(104328, UUID
.fromString("15f0b5f8-44e4-4ae1-8b40-f36f0a049b27")); //Chamaedrilus
214 idMap
.put(97537, UUID
.fromString("899c62e3-a116-4c5b-b22a-c76e761cc32e")); //Araeolaimoides caecus
221 private void handleExampleIdentifiers(TaxonBase
<?
> taxonBase
, Integer id
) {
222 if (idMap
.get(id
) != null){
223 taxonBase
.setUuid(idMap
.get(id
));
230 * @throws UndefinedTransformerMethodException
232 private void checkRankMarker(EdaphobaseImportState state
, Rank rank
) throws UndefinedTransformerMethodException
{
235 Set
<Marker
> markers
= rank
.getMarkers();
236 if ( markers
.size() == 0){ //we assume that no markers exist. at least not for markers of unused ranks
237 UUID edaphoRankMarkerTypeUuid
= state
.getTransformer().getMarkerTypeUuid("EdaphoRankMarker");
238 MarkerType marker
= getMarkerType(state
, edaphoRankMarkerTypeUuid
, "Edaphobase rank", "Rank used in Edaphobase", "EdaRk" );
239 Representation rep
= Representation
.NewInstance("Rang, verwendet in Edaphobase", "Edaphobase Rang", "EdaRg", Language
.GERMAN());
240 marker
.addRepresentation(rep
);
241 rank
.addMarker(Marker
.NewInstance(marker
, true));
242 getTermService().saveOrUpdate(rank
);
245 logger
.warn("Rank is null and marker can not be set");
254 private void handleTaxonomicGroupMarker(EdaphobaseImportState state
, TaxonBase
<?
> taxonBase
, boolean isGroup
) {
259 MarkerType markerType
= getMarkerType(state
, state
.getTransformer().getMarkerTypeUuid("TaxGrossgruppe"), "Tax. Gruppe", "Taxonomische Grossgruppe", "TGG", null, Language
.GERMAN());
260 if (taxonBase
.isInstanceOf(Synonym
.class)){
261 logger
.warn("Syonym is marked as 'taxonomische Grossgruppe'");
263 taxonBase
.addMarker(Marker
.NewInstance(markerType
, true));
264 } catch (UndefinedTransformerMethodException e
) {
273 private Reference
makeSecRef(EdaphobaseImportState state
) {
274 Reference ref
= ReferenceFactory
.newDatabase();
275 ref
.setTitle(state
.getConfig().getEdaphobaseSecundumTitle());
276 ref
.setUuid(state
.getConfig().getSecUuid());
277 state
.addRelatedObject(REFERENCE_NAMESPACE
, ref
.getUuid().toString(), ref
);
278 getReferenceService().save(ref
);
283 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
,
284 EdaphobaseImportState state
) {
285 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<>();
286 Map
<String
, TeamOrPersonBase
<?
>> authorMap
= new HashMap
<>();
287 Set
<String
> authorSet
= new HashSet
<>();
288 Set
<String
> referenceIdSet
= new HashSet
<String
>();
292 String authorStr
= rs
.getString("tax_author_name");
293 authorSet
.add(authorStr
);
294 handleForeignKey(rs
, referenceIdSet
, "tax_document");
296 } catch (SQLException e
) {
301 Set
<UUID
> uuidSet
= new HashSet
<>();
302 for (String authorStr
: authorSet
){
303 UUID uuid
= state
.getAuthorUuid(authorStr
);
306 List
<TeamOrPersonBase
<?
>> authors
= (List
)getAgentService().find(uuidSet
);
307 Map
<UUID
, TeamOrPersonBase
<?
>> authorUuidMap
= new HashMap
<>();
308 for (TeamOrPersonBase
<?
> author
: authors
){
309 authorUuidMap
.put(author
.getUuid(), author
);
312 for (String authorStr
: authorSet
){
313 UUID uuid
= state
.getAuthorUuid(authorStr
);
314 TeamOrPersonBase
<?
> author
= authorUuidMap
.get(uuid
);
315 authorMap
.put(authorStr
, author
);
317 result
.put(AUTHOR_NAMESPACE
, authorMap
);
320 String nameSpace
= REFERENCE_NAMESPACE
;
321 Class
<?
> cdmClass
= Reference
.class;
322 Set
<String
> idSet
= referenceIdSet
;
323 Map
<String
, Reference
> referenceMap
= (Map
<String
, Reference
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, idSet
, nameSpace
);
324 result
.put(nameSpace
, referenceMap
);
327 UUID secUuid
= state
.getConfig().getSecUuid();
328 Reference secRef
= getReferenceService().find(secUuid
);
329 referenceMap
.put(secUuid
.toString(), secRef
);
334 private void setNamePart(String nameStr
, Rank rank
, IZoologicalName name
) {
336 if (rank
.isSupraGeneric() || rank
.isGenus()){
337 if (StringUtils
.isBlank(name
.getGenusOrUninomial())){
338 name
.setGenusOrUninomial(nameStr
);
340 }else if (rank
.isInfraGeneric()){
341 if (StringUtils
.isBlank(name
.getInfraGenericEpithet())){
342 name
.setInfraGenericEpithet(nameStr
);
344 }else if (rank
.isSpeciesAggregate() || rank
.isSpecies()){
345 if (StringUtils
.isBlank(name
.getSpecificEpithet())){
346 name
.setSpecificEpithet(nameStr
);
348 }else if (rank
.isInfraSpecific()){
349 if (StringUtils
.isBlank(name
.getInfraSpecificEpithet())){
350 name
.setInfraSpecificEpithet(nameStr
);
356 private Rank
makeRank(EdaphobaseImportState state
, String rankStr
) {
359 rank
= state
.getTransformer().getRankByKey(rankStr
);
360 } catch (UndefinedTransformerMethodException e
) {
367 protected boolean doCheck(EdaphobaseImportState state
) {
372 protected boolean isIgnore(EdaphobaseImportState state
) {
373 return ! state
.getConfig().isDoTaxa();