Project

General

Profile

Download (5.85 KB) Statistics
| Branch: | Tag: | Revision:
1 09f91999 Katja Luther
/**
2
* Copyright (C) 2007 EDIT
3 9dc896c9 Andreas Müller
* European Distributed Institute of Taxonomy
4 09f91999 Katja Luther
* http://www.e-taxonomy.eu
5 9dc896c9 Andreas Müller
*
6 09f91999 Katja Luther
* 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 9dc896c9 Andreas Müller
import eu.etaxonomy.cdm.model.name.TaxonName;
22 1d36aa54 Andreas Müller
import eu.etaxonomy.cdm.model.reference.Reference;
23 09f91999 Katja Luther
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 892efc69 Andreas Kohlbecker
public class DbImportNameTypeDesignationMapper<STATE extends DbImportStateBase<?,?>, T extends IDbImportTransformed> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
37 09f91999 Katja Luther
	private static final Logger logger = Logger.getLogger(DbImportNameTypeDesignationMapper.class);
38 9dc896c9 Andreas Müller
39 09f91999 Katja Luther
//******************************** FACTORY METHOD ***************************************************/
40 9dc896c9 Andreas Müller
41 892efc69 Andreas Kohlbecker
	public static DbImportNameTypeDesignationMapper<?,?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String desigStatusAttribute){
42 09f91999 Katja Luther
		return new DbImportNameTypeDesignationMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, desigStatusAttribute);
43
	}
44 9dc896c9 Andreas Müller
45 09f91999 Katja Luther
//******************************* 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 9dc896c9 Andreas Müller
54
55 09f91999 Katja Luther
//********************************* CONSTRUCTOR ****************************************/
56
	/**
57 9dc896c9 Andreas Müller
	 * @param relatedObjectNamespace
58 09f91999 Katja Luther
	 * @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 9dc896c9 Andreas Müller
72 09f91999 Katja Luther
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
74
	 */
75 9dc896c9 Andreas Müller
	@Override
76
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
77 09f91999 Katja Luther
		STATE state = importMapperHelper.getState();
78
		CdmImportBase currentImport = state.getCurrentIO();
79
		if (currentImport instanceof ICheckIgnoreMapper){
80
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
81
			if (ignoreRecord){
82
				return cdmBase;
83
			}
84
		}
85 9dc896c9 Andreas Müller
86 09f91999 Katja Luther
		CdmBase fromObject = getRelatedObject(rs, fromAttribute);
87
		CdmBase toObject = getRelatedObject(rs, toAttribute);
88
		//TODO cast
89 1d36aa54 Andreas Müller
		Reference citation = (Reference)getRelatedObject(rs, citationAttribute);
90 09f91999 Katja Luther
		String microCitation = null;
91
		if (citationAttribute != null){
92
			microCitation = rs.getString(microCitationAttribute);
93
		}
94 9dc896c9 Andreas Müller
95 09f91999 Katja Luther
		Object designationStatusValue = null;
96
		if (citationAttribute != null){
97
			designationStatusValue = rs.getObject(designationStatusAttribute);
98
		}
99
100
101
		if (fromObject == null){
102 892efc69 Andreas Kohlbecker
			String warning  = "Higher rank name could not be found. Name type not added to higher rank name";
103 09f91999 Katja Luther
			logger.warn(warning);
104
			return cdmBase;
105
		}
106 9dc896c9 Andreas Müller
		TaxonName typifiedName = checkTaxonNameType(fromObject);
107
108 09f91999 Katja Luther
		if (toObject == null){
109 892efc69 Andreas Kohlbecker
			String warning  = "Species name could not be found. Name type not added to higher rank name";
110 09f91999 Katja Luther
			logger.warn(warning);
111
			return cdmBase;
112
		}
113 9dc896c9 Andreas Müller
		TaxonName typeName = checkTaxonNameType(toObject);
114
115 09f91999 Katja Luther
		boolean addToAllHomotypicNames = false; //TODO check if this is correct
116
		String originalNameString = null; //TODO what is this
117 9dc896c9 Andreas Müller
118 09f91999 Katja Luther
		NameTypeDesignationStatus status = this.designationStatus;
119
		if (designationStatusValue != null){
120 892efc69 Andreas Kohlbecker
			//FIXME this needs work in generics to remove casts. Or find an other solution
121
			if (currentImport instanceof IDbImportTransformed){
122
				IDbImportTransformer transformer = ((IDbImportTransformed)currentImport).getTransformer();
123
				status = transformer.transformNameTypeDesignationStatus(designationStatusValue);
124 09f91999 Katja Luther
			}
125
		}
126 892efc69 Andreas Kohlbecker
		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
127 9dc896c9 Andreas Müller
128 892efc69 Andreas Kohlbecker
		return typifiedName;
129 09f91999 Katja Luther
	}
130 9dc896c9 Andreas Müller
131 09f91999 Katja Luther
	/**
132
	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
133
	 * @param rs
134
	 * @param dbAttribute
135
	 * @return
136
	 * @throws SQLException
137
	 */
138
	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
139
		CdmBase result = null;
140
		if (dbAttribute != null){
141
			Object dbValue = rs.getObject(dbAttribute);
142
			String id = String.valueOf(dbValue);
143
			DbImportStateBase state = importMapperHelper.getState();
144
			result = state.getRelatedObject(relatedObjectNamespace, id);
145
		}
146
		return result;
147
	}
148 9dc896c9 Andreas Müller
149 09f91999 Katja Luther
	/**
150 9dc896c9 Andreas Müller
	 * Checks if cdmBase is of type Taxon
151 09f91999 Katja Luther
	 * @param fromObject
152
	 */
153 9dc896c9 Andreas Müller
	private TaxonName checkTaxonNameType(CdmBase cdmBase) {
154
		if (! cdmBase.isInstanceOf(TaxonName.class)){
155
			String warning = "Type name or typifier name is not of type TaxonName but " + cdmBase.getClass().getName();
156 09f91999 Katja Luther
			logger.warn(warning);
157
			throw new IllegalArgumentException(warning);
158
		}
159 9dc896c9 Andreas Müller
		return (cdmBase.deproxy(cdmBase, TaxonName.class));
160 09f91999 Katja Luther
	}
161
162
163
}