Project

General

Profile

Download (4.66 KB) Statistics
| Branch: | Tag: | Revision:
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.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.taxon.Synonym;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26

    
27
/**
28
 * @author a.mueller
29
 * @created 11.03.2010
30
 * @version 1.0
31
 */
32
public abstract class DbImportDescriptionElementCreationMapperBase<ELEMENT extends DescriptionElementBase, STATE extends DbImportStateBase<?,?>> extends DbImportObjectCreationMapperBase<ELEMENT, STATE> {
33
	private static final Logger logger = Logger.getLogger(DbImportDescriptionElementCreationMapperBase.class);
34
	
35
//******************************* ATTRIBUTES ***************************************/
36
	protected String taxonNamespace;
37
	protected String dbTaxonFkAttribute;
38
	protected boolean isImageGallery = false;
39
	
40
	
41
	
42
//********************************* CONSTRUCTOR ****************************************/
43
	/**
44
	 * @param mappingImport
45
	 */
46
	protected DbImportDescriptionElementCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace, String dbTaxonFkAttribute, String taxonNamespace) {
47
		super(dbIdAttribute, objectToCreateNamespace);
48
		this.taxonNamespace = taxonNamespace;
49
		this.dbTaxonFkAttribute = dbTaxonFkAttribute;
50
	}
51

    
52
//************************************ METHODS *******************************************/
53

    
54
	/* (non-Javadoc)
55
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
56
	 */
57
	@Override
58
	protected ELEMENT doInvoke(ResultSet rs, ELEMENT element) throws SQLException {
59
		Taxon taxon = getAcceptedTaxon(rs);
60
		if (taxon != null){
61
			addDescriptionElement(taxon, element);
62
			return element;
63
		}else{
64
			logger.warn("Taxon could not be determined. Description element was not add to any description or taxon");
65
			return element;
66
		}
67
		
68
	}
69

    
70
	/**
71
	 * @param taxonFk
72
	 * @return
73
	 * @throws SQLException 
74
	 */
75
	protected Taxon getAcceptedTaxon(ResultSet rs) throws SQLException {
76
		String taxonFk = getForeignKey(rs, dbTaxonFkAttribute);
77
		TaxonBase taxonBase = (TaxonBase)getRelatedObject(taxonNamespace, taxonFk);
78
		Taxon taxon = null;
79
		if (taxonBase == null){
80
			logger.warn("TaxonBase not found: " + taxonFk);
81
		}else if (taxonBase instanceof Taxon){
82
			taxon = (Taxon)taxonBase;
83
			
84
		}else if (taxonBase instanceof Synonym){
85
			Synonym synonym = CdmBase.deproxy(taxonBase, Synonym.class);
86
			Set<Taxon> taxa = synonym.getAcceptedTaxa();
87
			if (taxa.size() == 0){
88
				logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has no accepted taxon. Can't define a taxon to add the description element to");
89
			}else if (taxa.size() == 1){
90
				taxon = taxa.iterator().next();
91
			}else{
92
				logger.warn("Synonym '" + synonym.getTitleCache() + "' ("+ taxonFk + ") has more than one accepted taxon. Can't decide which one to take");
93
			}
94
		}else{ //null
95
			throw new IllegalStateException("TaxonBase must either be null, Taxon or Synonym but was something else");
96
		}
97
		return taxon;
98
	}
99

    
100

    
101
	
102
	/**
103
	 * Adds a description element to the taxon's first description which is not an image gallery.
104
	 * If no such description exists a new one is generated. Returns the element or, if null if taxon is null.
105
	 * @param taxon
106
	 * @param element
107
	 */
108
	protected ELEMENT addDescriptionElement(Taxon taxon, ELEMENT element) {
109
		if (taxon == null){
110
			return null;
111
		}else{
112
			TaxonDescription description = getTaxonDescription(taxon, isImageGallery);
113
			description.addElement(element);
114
			return element;
115
		}
116
	}
117

    
118
	/**
119
	 * @param taxon
120
	 * @return
121
	 */
122
	protected TaxonDescription getTaxonDescription(Taxon taxon, boolean isImageGallery) {
123
		Set<TaxonDescription> descriptions = taxon.getDescriptions();
124
		TaxonDescription description = null;
125
		if (descriptions.size() > 0){
126
			for (TaxonDescription desc : descriptions){
127
				if ( desc.isImageGallery() == isImageGallery ){
128
					description = desc;
129
					break;
130
				}
131
			}
132
		}
133
		if (description == null){
134
			description = TaxonDescription.NewInstance(taxon, isImageGallery);
135
		}
136
		return description;
137
	}
138

    
139
	
140
}
(9-9/40)