1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common.mapping;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
20
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
21
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
22
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
23
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
|
24
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @since 11.03.2010
|
32
|
*/
|
33
|
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> {
|
34
|
|
35
|
private static final Logger logger = LogManager.getLogger(DbImportDescriptionElementCreationMapperBase.class);
|
36
|
|
37
|
//******************************* ATTRIBUTES ***************************************/
|
38
|
|
39
|
protected String taxonNamespace;
|
40
|
protected String dbTaxonFkAttribute;
|
41
|
protected boolean isImageGallery = false;
|
42
|
protected String dbCitationAttribute; //if there is a single source available
|
43
|
protected String sourceNamespace;
|
44
|
protected String dbMicroCitationAttribute;
|
45
|
|
46
|
//********************************* CONSTRUCTOR ****************************************/
|
47
|
|
48
|
protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) {
|
49
|
super(dbIdAttribute, objectToCreateNamespace);
|
50
|
this.taxonNamespace = taxonNamespace;
|
51
|
this.dbTaxonFkAttribute = dbTaxonFkAttribute;
|
52
|
}
|
53
|
|
54
|
//************************************ METHODS *******************************************/
|
55
|
|
56
|
public DbImportDescriptionElementCreationMapperBase<ELEMENT,STATE> setSource(String dbCitationAttribute, String sourceNamespace, String dbMicroCitationAttribute){
|
57
|
this.dbCitationAttribute = dbCitationAttribute;
|
58
|
this.sourceNamespace = sourceNamespace;
|
59
|
this.dbMicroCitationAttribute = dbMicroCitationAttribute;
|
60
|
return this;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException {
|
65
|
Taxon taxon = getAcceptedTaxon(rs);
|
66
|
if (taxon != null){
|
67
|
element = addDescriptionElement(taxon, element);
|
68
|
}else{
|
69
|
logger.info("Taxon could not be determined. Description element was not add to any description or taxon");
|
70
|
}
|
71
|
//Source
|
72
|
if (StringUtils.isNotBlank(dbCitationAttribute)){
|
73
|
addSource(rs, element);
|
74
|
}
|
75
|
return element;
|
76
|
}
|
77
|
|
78
|
private void addSource(ResultSet rs, ELEMENT element) throws SQLException {
|
79
|
String microCitation = getStringDbValue(rs, dbMicroCitationAttribute);
|
80
|
Reference citation = (Reference) getState().getRelatedObject(sourceNamespace, String.valueOf(rs.getObject(dbCitationAttribute)));
|
81
|
element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation);
|
82
|
}
|
83
|
|
84
|
protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException {
|
85
|
String taxonFk = getForeignKey(rs, dbTaxonFkAttribute);
|
86
|
TaxonBase<?> taxonBase = (TaxonBase<?>)getRelatedObject(taxonNamespace, taxonFk);
|
87
|
Taxon taxon = null;
|
88
|
if (taxonBase == null){
|
89
|
logger.warn("TaxonBase not found: " + taxonFk);
|
90
|
}else if (taxonBase instanceof Taxon){
|
91
|
taxon = (Taxon)taxonBase;
|
92
|
|
93
|
}else if (taxonBase instanceof Synonym){
|
94
|
Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
|
95
|
Taxon accTaxon = synonym.getAcceptedTaxon();
|
96
|
if (accTaxon == null){
|
97
|
logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to");
|
98
|
}else {
|
99
|
taxon = accTaxon;
|
100
|
}
|
101
|
}else{ //null
|
102
|
throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else");
|
103
|
}
|
104
|
return taxon;
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Adds a description element to the taxon's first description which is not an image gallery.
|
109
|
* If no such description exists a new one is generated. Returns the element or, if null if taxon is null.
|
110
|
*/
|
111
|
protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) {
|
112
|
if (taxon == null){
|
113
|
return null;
|
114
|
}else{
|
115
|
TaxonDescription description = getTaxonDescription(taxon, isImageGallery);
|
116
|
description.addElement(element);
|
117
|
return element;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) {
|
122
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
123
|
TaxonDescription description = null;
|
124
|
if (descriptions.size() > 0){
|
125
|
for (TaxonDescription desc : descriptions){
|
126
|
if ( desc.isImageGallery() == isImageGallery ){
|
127
|
description = desc;
|
128
|
break;
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
if (description == null){
|
133
|
description = TaxonDescription.NewInstance(taxon, isImageGallery);
|
134
|
}
|
135
|
return description;
|
136
|
}
|
137
|
}
|