1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.io.common.mapping;
|
12
|
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
19
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
20
|
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
24
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
25
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
29
|
|
30
|
/**
|
31
|
* @author a.mueller
|
32
|
* @created 11.03.2010
|
33
|
* @version 1.0
|
34
|
*/
|
35
|
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> {
|
36
|
private static final Logger logger = Logger.getLogger(DbImportDescriptionElementCreationMapperBase.class);
|
37
|
|
38
|
//******************************* ATTRIBUTES ***************************************/
|
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
|
|
47
|
//********************************* CONSTRUCTOR ****************************************/
|
48
|
/**
|
49
|
* @param mappingImport
|
50
|
*/
|
51
|
protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) {
|
52
|
super(dbIdAttribute, objectToCreateNamespace);
|
53
|
this.taxonNamespace = taxonNamespace;
|
54
|
this.dbTaxonFkAttribute = dbTaxonFkAttribute;
|
55
|
}
|
56
|
|
57
|
//************************************ METHODS *******************************************/
|
58
|
|
59
|
public DbImportDescriptionElementCreationMapperBase setSource(String dbCitationAttribute, String sourceNamespace, String dbMicroCitationAttribute){
|
60
|
this.dbCitationAttribute = dbCitationAttribute;
|
61
|
this.sourceNamespace = sourceNamespace;
|
62
|
this.dbMicroCitationAttribute = dbMicroCitationAttribute;
|
63
|
return this;
|
64
|
}
|
65
|
|
66
|
/* (non-Javadoc)
|
67
|
* @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
|
68
|
*/
|
69
|
@Override
|
70
|
protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException {
|
71
|
Taxon taxon = getAcceptedTaxon(rs);
|
72
|
if (taxon != null){
|
73
|
addDescriptionElement(taxon, element);
|
74
|
}else{
|
75
|
logger.info("Taxon could not be determined. Description element was not add to any description or taxon");
|
76
|
}
|
77
|
//Source
|
78
|
if (CdmUtils.isNotEmpty(dbCitationAttribute)){
|
79
|
addSource(rs, element);
|
80
|
}
|
81
|
return element;
|
82
|
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @param rs
|
87
|
* @param element
|
88
|
* @throws SQLException
|
89
|
*/
|
90
|
private void addSource(ResultSet rs, ELEMENT element) throws SQLException {
|
91
|
String microCitation = getStringDbValue(rs, dbMicroCitationAttribute);
|
92
|
ReferenceBase citation = (ReferenceBase) getState().getRelatedObject(sourceNamespace, String.valueOf(rs.getObject(dbCitationAttribute)));
|
93
|
element.addSource(null, null, citation, microCitation);
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* @param taxonFk
|
98
|
* @return
|
99
|
* @throws SQLException
|
100
|
*/
|
101
|
protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException {
|
102
|
String taxonFk = getForeignKey(rs, dbTaxonFkAttribute);
|
103
|
TaxonBase taxonBase = (TaxonBase)getRelatedObject(taxonNamespace, taxonFk);
|
104
|
Taxon taxon = null;
|
105
|
if (taxonBase == null){
|
106
|
logger.warn("TaxonBase not found: " + taxonFk);
|
107
|
}else if (taxonBase instanceof Taxon){
|
108
|
taxon = (Taxon)taxonBase;
|
109
|
|
110
|
}else if (taxonBase instanceof Synonym){
|
111
|
Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
|
112
|
Set<Taxon> taxa = synonym.getAcceptedTaxa();
|
113
|
if (taxa.size() == 0){
|
114
|
logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to");
|
115
|
}else if (taxa.size() == 1){
|
116
|
taxon = taxa.iterator().next();
|
117
|
}else{
|
118
|
logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has more than one accepted taxon. Can't decide which one to take");
|
119
|
}
|
120
|
}else{ //null
|
121
|
throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else");
|
122
|
}
|
123
|
return taxon;
|
124
|
}
|
125
|
|
126
|
|
127
|
|
128
|
/**
|
129
|
* Adds a description element to the taxon's first description which is not an image gallery.
|
130
|
* If no such description exists a new one is generated. Returns the element or, if null if taxon is null.
|
131
|
* @param taxon
|
132
|
* @param element
|
133
|
*/
|
134
|
protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) {
|
135
|
if (taxon == null){
|
136
|
return null;
|
137
|
}else{
|
138
|
TaxonDescription description = getTaxonDescription(taxon, isImageGallery);
|
139
|
description.addElement(element);
|
140
|
return element;
|
141
|
}
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* @param taxon
|
146
|
* @return
|
147
|
*/
|
148
|
protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) {
|
149
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
150
|
TaxonDescription description = null;
|
151
|
if (descriptions.size() > 0){
|
152
|
for (TaxonDescription desc : descriptions){
|
153
|
if ( desc.isImageGallery() == isImageGallery ){
|
154
|
description = desc;
|
155
|
break;
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
if (description == null){
|
160
|
description = TaxonDescription.NewInstance(taxon, isImageGallery);
|
161
|
}
|
162
|
return description;
|
163
|
}
|
164
|
|
165
|
|
166
|
}
|