Project

General

Profile

Download (5.61 KB) Statistics
| Branch: | Tag: | Revision:
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.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.common.OriginalSourceType;
22
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
23
import eu.etaxonomy.cdm.model.description.TaxonDescription;
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
 * @created 11.03.2010
32
 */
33
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> {
34
	private static final Logger logger = Logger.getLogger(DbImportDescriptionElementCreationMapperBase.class);
35

    
36
//******************************* ATTRIBUTES ***************************************/
37
	protected String taxonNamespace;
38
	protected String dbTaxonFkAttribute;
39
	protected boolean isImageGallery = false;
40
	protected String dbCitationAttribute;  //if there is a single source available
41
	protected String sourceNamespace;
42
	protected String dbMicroCitationAttribute;
43

    
44

    
45
//********************************* CONSTRUCTOR ****************************************/
46
	/**
47
	 * @param mappingImport
48
	 */
49
	protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) {
50
		super(dbIdAttribute, objectToCreateNamespace);
51
		this.taxonNamespace = taxonNamespace;
52
		this.dbTaxonFkAttribute = dbTaxonFkAttribute;
53
	}
54

    
55
//************************************ METHODS *******************************************/
56

    
57
	public DbImportDescriptionElementCreationMapperBase<ELEMENT,STATE> setSource(String dbCitationAttribute, String sourceNamespace, String dbMicroCitationAttribute){
58
		this.dbCitationAttribute = dbCitationAttribute;
59
		this.sourceNamespace = sourceNamespace;
60
		this.dbMicroCitationAttribute = dbMicroCitationAttribute;
61
		return this;
62
	}
63

    
64
	/* (non-Javadoc)
65
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
66
	 */
67
	@Override
68
	protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException {
69
		Taxon taxon = getAcceptedTaxon(rs);
70
		if (taxon != null){
71
			addDescriptionElement(taxon, element);
72
		}else{
73
			logger.info("Taxon could not be determined. Description element was not add to any description or taxon");
74
		}
75
		//Source
76
		if (StringUtils.isNotBlank(dbCitationAttribute)){
77
			addSource(rs, element);
78
		}
79
		return element;
80

    
81
	}
82

    
83
	/**
84
	 * @param rs
85
	 * @param element
86
	 * @throws SQLException
87
	 */
88
	private void addSource(ResultSet rs, ELEMENT element) throws SQLException {
89
		String microCitation = getStringDbValue(rs, dbMicroCitationAttribute);
90
		Reference citation = (Reference) getState().getRelatedObject(sourceNamespace, String.valueOf(rs.getObject(dbCitationAttribute)));
91
		element.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, citation, microCitation);
92
	}
93

    
94
	/**
95
	 * @param taxonFk
96
	 * @return
97
	 * @throws SQLException
98
	 */
99
	protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException {
100
		String taxonFk = getForeignKey(rs, dbTaxonFkAttribute);
101
		TaxonBase<?> taxonBase = (TaxonBase<?>)getRelatedObject(taxonNamespace, taxonFk);
102
		Taxon taxon = null;
103
		if (taxonBase == null){
104
			logger.warn("TaxonBase not found: " + taxonFk);
105
		}else if (taxonBase instanceof Taxon){
106
			taxon = (Taxon)taxonBase;
107

    
108
		}else if (taxonBase instanceof Synonym){
109
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
110
			Taxon accTaxon = synonym.getAcceptedTaxon();
111
			if (accTaxon == null){
112
				logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to");
113
			}else {
114
				taxon = accTaxon;
115
			}
116
		}else{ //null
117
			throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else");
118
		}
119
		return taxon;
120
	}
121

    
122

    
123
	/**
124
	 * Adds a description element to the taxon's first description which is not an image gallery.
125
	 * If no such description exists a new one is generated. Returns the element or, if null if taxon is null.
126
	 * @param taxon
127
	 * @param element
128
	 */
129
	protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) {
130
		if (taxon == null){
131
			return null;
132
		}else{
133
			TaxonDescription description = getTaxonDescription(taxon, isImageGallery);
134
			description.addElement(element);
135
			return element;
136
		}
137
	}
138

    
139
	/**
140
	 * @param taxon
141
	 * @return
142
	 */
143
	protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) {
144
		Set<TaxonDescription> descriptions = taxon.getDescriptions();
145
		TaxonDescription description = null;
146
		if (descriptions.size() > 0){
147
			for (TaxonDescription desc : descriptions){
148
				if ( desc.isImageGallery() == isImageGallery ){
149
					description = desc;
150
					break;
151
				}
152
			}
153
		}
154
		if (description == null){
155
			description = TaxonDescription.NewInstance(taxon, isImageGallery);
156
		}
157
		return description;
158
	}
159

    
160

    
161
}
(10-10/51)