Project

General

Profile

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

    
15
import org.apache.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.io.common.CdmImportBase;
18
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
21
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23

    
24
/**
25
 * @author a.mueller
26
 * @created 12.05.2009
27
 * @version 1.0
28
 */
29
/**
30
 * @author a.mueller
31
 * @created 02.03.2010
32
 * @version 1.0
33
 * @param <CDM_BASE>
34
 * @param <STATE>
35
 */
36
public class DbImportNameTypeDesignationMapper<STATE extends DbImportStateBase<?,?>, T extends IDbImportTransformed> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
37
	private static final Logger logger = Logger.getLogger(DbImportNameTypeDesignationMapper.class);
38
	
39
//******************************** FACTORY METHOD ***************************************************/
40
	
41
	public static DbImportNameTypeDesignationMapper<?,?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String desigStatusAttribute){
42
		return new DbImportNameTypeDesignationMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, desigStatusAttribute);
43
	}
44
	
45
//******************************* ATTRIBUTES ***************************************/
46
	private String fromAttribute;
47
	private String toAttribute;
48
	private NameTypeDesignationStatus designationStatus;
49
	private String relatedObjectNamespace;
50
	private String citationAttribute;
51
	private String microCitationAttribute;
52
	private String designationStatusAttribute;
53
	
54
	
55
//********************************* CONSTRUCTOR ****************************************/
56
	/**
57
	 * @param relatedObjectNamespace 
58
	 * @param mappingImport
59
	 */
60
	protected DbImportNameTypeDesignationMapper(String fromAttribute, String toAttribute, NameTypeDesignationStatus designationStatus, String relatedObjectNamespace, String desigStatusAttribute) {
61
		super();
62
		//TODO make it a single attribute mapper
63
		this.fromAttribute = fromAttribute;
64
		this.toAttribute = toAttribute;
65
		this.relatedObjectNamespace = relatedObjectNamespace;
66
		this.designationStatusAttribute = desigStatusAttribute;
67
		this.designationStatus = designationStatus;
68
	}
69

    
70
//************************************ METHODS *******************************************/
71
	
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
74
	 */
75
	public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
76
		STATE state = importMapperHelper.getState();
77
		CdmImportBase currentImport = state.getCurrentIO();
78
		if (currentImport instanceof ICheckIgnoreMapper){
79
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
80
			if (ignoreRecord){
81
				return cdmBase;
82
			}
83
		}
84
		
85
		CdmBase fromObject = getRelatedObject(rs, fromAttribute);
86
		CdmBase toObject = getRelatedObject(rs, toAttribute);
87
		//TODO cast
88
		Reference citation = (Reference)getRelatedObject(rs, citationAttribute);
89
		String microCitation = null;
90
		if (citationAttribute != null){
91
			microCitation = rs.getString(microCitationAttribute);
92
		}
93
		
94
		Object designationStatusValue = null;
95
		if (citationAttribute != null){
96
			designationStatusValue = rs.getObject(designationStatusAttribute);
97
		}
98

    
99

    
100
		if (fromObject == null){
101
			String warning  = "Higher rank name could not be found. Name type not added to higher rank name";
102
			logger.warn(warning);
103
			return cdmBase;
104
		}
105
		TaxonNameBase typifiedName = checkTaxonNameBaseType(fromObject);
106
		
107
		if (toObject == null){
108
			String warning  = "Species name could not be found. Name type not added to higher rank name";
109
			logger.warn(warning);
110
			return cdmBase;
111
		}
112
		TaxonNameBase typeName = checkTaxonNameBaseType(toObject);
113
		
114
		boolean addToAllHomotypicNames = false; //TODO check if this is correct
115
		String originalNameString = null; //TODO what is this
116
		
117
		NameTypeDesignationStatus status = this.designationStatus;
118
		if (designationStatusValue != null){
119
			//FIXME this needs work in generics to remove casts. Or find an other solution
120
			if (currentImport instanceof IDbImportTransformed){
121
				IDbImportTransformer transformer = ((IDbImportTransformed)currentImport).getTransformer();
122
				status = transformer.transformNameTypeDesignationStatus(designationStatusValue);
123
			}
124
		}
125
		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
126
		
127
		return typifiedName;
128
	}
129
	
130
	/**
131
	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
132
	 * @param rs
133
	 * @param dbAttribute
134
	 * @return
135
	 * @throws SQLException
136
	 */
137
	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
138
		CdmBase result = null;
139
		if (dbAttribute != null){
140
			Object dbValue = rs.getObject(dbAttribute);
141
			String id = String.valueOf(dbValue);
142
			DbImportStateBase state = importMapperHelper.getState();
143
			result = state.getRelatedObject(relatedObjectNamespace, id);
144
		}
145
		return result;
146
	}
147
	
148
	/**
149
	 * Checks if cdmBase is of type Taxon 
150
	 * @param fromObject
151
	 */
152
	private TaxonNameBase checkTaxonNameBaseType(CdmBase cdmBase) {
153
		if (! cdmBase.isInstanceOf(TaxonNameBase.class)){
154
			String warning = "Type name or typifier name is not of type TaxonNameBase but " + cdmBase.getClass().getName();
155
			logger.warn(warning);
156
			throw new IllegalArgumentException(warning);
157
		}
158
		return (cdmBase.deproxy(cdmBase, TaxonNameBase.class));
159
	}
160

    
161

    
162
}
(28-28/51)