Project

General

Profile

Download (7.03 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

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19
import eu.etaxonomy.cdm.io.common.ICdmIO;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.model.taxon.Synonym;
23
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
24
import eu.etaxonomy.cdm.model.taxon.Taxon;
25
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
27

    
28
/**
29
 * @author a.mueller
30
 * @created 02.03.2010
31
 * @param <CDM_BASE>
32
 * @param <STATE>
33
 */
34
public class DbImportSynonymMapper<STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
35
	private static final Logger logger = Logger.getLogger(DbImportSynonymMapper.class);
36

    
37
//******************************** FACTORY METHOD ***************************************************/
38

    
39
	/**
40
	 * Creates a new instance of SynonymMapper.
41
	 * @param dbFromAttribute
42
	 * @param dbToAttribute
43
	 * @param relatedObjectNamespace
44
	 * @param relTypeAttribute
45
	 * @param taxonRelationshipType this relationshiptype is taken for accepted taxa being synonyms (may be the case if data are dirty)
46
	 * @return
47
	 */
48
	public static DbImportSynonymMapper<?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String relTypeAttribute, TaxonRelationshipType taxonRelationshipType){
49
		return new DbImportSynonymMapper(dbFromAttribute, dbToAttribute, taxonRelationshipType, relatedObjectNamespace, relTypeAttribute);
50
	}
51

    
52
//******************************* ATTRIBUTES ***************************************/
53
	private String fromAttribute;
54
	private String toAttribute;
55
	private TaxonRelationshipType relType;
56
	private String relatedObjectNamespace;
57
	private String citationAttribute;
58
	private String microCitationAttribute;
59
	private String relationshipTypeAttribute;
60
	private boolean useTaxonRelationship;
61

    
62

    
63
//********************************* CONSTRUCTOR ****************************************/
64
	/**
65
	 * @param relatedObjectNamespace
66
	 * @param mappingImport
67
	 */
68
	protected DbImportSynonymMapper(String fromAttribute, String toAttribute, TaxonRelationshipType relType, String relatedObjectNamespace, String relTypeAttribute) {
69
		super();
70
		//TODO make it a single attribute mapper
71
		this.fromAttribute = fromAttribute;
72
		this.toAttribute = toAttribute;
73
		this.relType = relType;
74
		this.relatedObjectNamespace = relatedObjectNamespace;
75
		this.relationshipTypeAttribute = relTypeAttribute;
76
		if (relTypeAttribute != null){
77
			logger.warn("Synonymrelationship type not yet implemented");
78
		}
79
		this.useTaxonRelationship = (relType != null);
80
	}
81

    
82
//************************************ METHODS *******************************************/
83

    
84
	/* (non-Javadoc)
85
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
86
	 */
87
	@Override
88
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
89
		STATE state = getState();
90
		ICdmIO<?> currentImport = state.getCurrentIO();
91
		if (currentImport instanceof ICheckIgnoreMapper){
92
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
93
			if (ignoreRecord){
94
				return cdmBase;
95
			}
96
		}
97

    
98
		TaxonBase<?> fromObject = (TaxonBase<?>)getRelatedObject(rs, fromAttribute);
99
		TaxonBase<?> toObject = (TaxonBase<?>)getRelatedObject(rs, toAttribute);
100
		String fromId = rs.getObject(fromAttribute)== null ? null: String.valueOf(rs.getObject(fromAttribute));
101
		String toId = rs.getObject(toAttribute) == null? null : String.valueOf(rs.getObject(toAttribute));
102

    
103
		if (toId == null){
104
			return fromObject;
105
		}
106

    
107
		Reference citation = CdmBase.deproxy(getRelatedObject(rs, citationAttribute), Reference.class);
108
		String microCitation = null;
109
		if (citationAttribute != null){
110
			microCitation = rs.getString(microCitationAttribute);
111
		}
112

    
113

    
114
		if (fromObject == null){
115
			String warning  = "The synonym (" + fromId + ") could not be found. Synonym not added to accepted taxon";
116
			logger.warn(warning);
117
			return cdmBase;
118
		}
119
		checkSynonymType(fromObject, fromId);
120

    
121
		if (toObject == null){
122
			String warning  = "The accepted taxon (" + toId + ") could not be found. Synonym not added to accepted taxon";
123
			logger.warn(warning);
124
			return cdmBase;
125
		}
126
		Taxon taxon = checkTaxonType(toObject, "Accepted taxon", toId);
127

    
128

    
129
		if (fromObject.isInstanceOf(Synonym.class)){
130
			SynonymRelationshipType relType = SynonymRelationshipType.SYNONYM_OF();
131
			Synonym synonym = CdmBase.deproxy(fromObject, Synonym.class);
132
			taxon.addSynonym(synonym, relType, citation, microCitation);
133
		}else if (fromObject.isInstanceOf(Taxon.class)  && this.useTaxonRelationship){
134
			TaxonRelationshipType type = relType;
135
			Taxon synonymTaxon = CdmBase.deproxy(fromObject, Taxon.class);
136
			synonymTaxon.addTaxonRelation(taxon, type, citation, microCitation);
137

    
138
		}else{
139
			logger.warn("Taxon is not a synonym and accepted taxa are not allowed as synonyms: " +  fromObject.getTitleCache() + "; " + fromObject.getId());
140
		}
141
		return fromObject;
142
	}
143

    
144
	/**
145
	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
146
	 * @param rs
147
	 * @param dbAttribute
148
	 * @return
149
	 * @throws SQLException
150
	 */
151
	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
152
		CdmBase result = null;
153
		if (dbAttribute != null){
154
			Object dbValue = rs.getObject(dbAttribute);
155
			String id = String.valueOf(dbValue);
156
			DbImportStateBase<?,?> state = importMapperHelper.getState();
157
			result = state.getRelatedObject(relatedObjectNamespace, id);
158
		}
159
		return result;
160
	}
161

    
162

    
163
	/**
164
	 * Checks if cdmBase is of type Taxon
165
	 * @param fromObject
166
	 */
167
	private Taxon checkTaxonType(TaxonBase<?> taxonBase, String typeString, String id) {
168
		if (! taxonBase.isInstanceOf(Taxon.class)){
169
			String warning = typeString + " (" + id + ") is not of type Taxon but of type " + taxonBase.getClass().getSimpleName();
170
			logger.warn(warning);
171
			throw new IllegalArgumentException(warning);
172
		}
173
		return (CdmBase.deproxy(taxonBase, Taxon.class));
174
	}
175

    
176
	/**
177
	 * Checks if cdmBase is of type Synonym
178
	 * @param fromObject
179
	 */
180
	private TaxonBase<?> checkSynonymType(CdmBase cdmBase, String id) {
181
		if (! cdmBase.isInstanceOf(Synonym.class)){
182
			String warning = "Synonym (" + id + ") is not of type Synonym but of type " + cdmBase.getClass().getSimpleName();
183
			if (! this.useTaxonRelationship){
184
				logger.warn(warning);
185
				throw new IllegalArgumentException(warning);
186
			}else{
187
				logger.info(warning);
188
			}
189
		}
190
		return (CdmBase.deproxy(cdmBase, TaxonBase.class));
191
	}
192

    
193

    
194
}
(34-34/51)