Project

General

Profile

Download (5.73 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.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17

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

    
25
/**
26
 * @author a.mueller
27
 * @since 12.05.2009
28
 * @version 1.0
29
 */
30
/**
31
 * @author a.mueller
32
 * @since 02.03.2010
33
 * @version 1.0
34
 * @param <CDM_BASE>
35
 * @param <STATE>
36
 */
37
public class DbImportNameTypeDesignationMapper<STATE extends DbImportStateBase<?,?>, T extends IDbImportTransformed> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
38
	private static final Logger logger = LogManager.getLogger(DbImportNameTypeDesignationMapper.class);
39

    
40
//******************************** FACTORY METHOD ***************************************************/
41

    
42
	public static DbImportNameTypeDesignationMapper<?,?> NewInstance(String dbFromAttribute, String dbToAttribute, String relatedObjectNamespace, String desigStatusAttribute){
43
		return new DbImportNameTypeDesignationMapper(dbFromAttribute, dbToAttribute, null, relatedObjectNamespace, desigStatusAttribute);
44
	}
45

    
46
//******************************* ATTRIBUTES ***************************************/
47
	private String fromAttribute;
48
	private String toAttribute;
49
	private NameTypeDesignationStatus designationStatus;
50
	private String relatedObjectNamespace;
51
	private String citationAttribute;
52
	private String microCitationAttribute;
53
	private String designationStatusAttribute;
54

    
55

    
56
//********************************* CONSTRUCTOR ****************************************/
57
	/**
58
	 * @param relatedObjectNamespace
59
	 * @param mappingImport
60
	 */
61
	protected DbImportNameTypeDesignationMapper(String fromAttribute, String toAttribute, NameTypeDesignationStatus designationStatus, String relatedObjectNamespace, String desigStatusAttribute) {
62
		super();
63
		//TODO make it a single attribute mapper
64
		this.fromAttribute = fromAttribute;
65
		this.toAttribute = toAttribute;
66
		this.relatedObjectNamespace = relatedObjectNamespace;
67
		this.designationStatusAttribute = desigStatusAttribute;
68
		this.designationStatus = designationStatus;
69
	}
70

    
71
//************************************ METHODS *******************************************/
72

    
73
	/* (non-Javadoc)
74
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
75
	 */
76
	@Override
77
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
78
		STATE state = importMapperHelper.getState();
79
		CdmImportBase currentImport = state.getCurrentIO();
80
		if (currentImport instanceof ICheckIgnoreMapper){
81
			boolean ignoreRecord = ((ICheckIgnoreMapper)currentImport).checkIgnoreMapper(this, rs);
82
			if (ignoreRecord){
83
				return cdmBase;
84
			}
85
		}
86

    
87
		CdmBase fromObject = getRelatedObject(rs, fromAttribute);
88
		CdmBase toObject = getRelatedObject(rs, toAttribute);
89
		//TODO cast
90
		Reference citation = (Reference)getRelatedObject(rs, citationAttribute);
91
		String microCitation = null;
92
		if (citationAttribute != null){
93
			microCitation = rs.getString(microCitationAttribute);
94
		}
95

    
96
		Object designationStatusValue = null;
97
		if (citationAttribute != null){
98
			designationStatusValue = rs.getObject(designationStatusAttribute);
99
		}
100

    
101

    
102
		if (fromObject == null){
103
			String warning  = "Higher rank name could not be found. Name type not added to higher rank name";
104
			logger.warn(warning);
105
			return cdmBase;
106
		}
107
		TaxonName typifiedName = checkTaxonNameType(fromObject);
108

    
109
		if (toObject == null){
110
			String warning  = "Species name could not be found. Name type not added to higher rank name";
111
			logger.warn(warning);
112
			return cdmBase;
113
		}
114
		TaxonName typeName = checkTaxonNameType(toObject);
115

    
116
		boolean addToAllHomotypicNames = false; //TODO check if this is correct
117
		String originalInfo = null; //TODO what is this
118

    
119
		NameTypeDesignationStatus status = this.designationStatus;
120
		if (designationStatusValue != null){
121
			//FIXME this needs work in generics to remove casts. Or find an other solution
122
			if (currentImport instanceof IDbImportTransformed){
123
				IDbImportTransformer transformer = ((IDbImportTransformed)currentImport).getTransformer();
124
				status = transformer.transformNameTypeDesignationStatus(designationStatusValue);
125
			}
126
		}
127
		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalInfo, status, addToAllHomotypicNames);
128

    
129
		return typifiedName;
130
	}
131

    
132
	/**
133
	 *	//TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
134
	 * @param rs
135
	 * @param dbAttribute
136
	 * @return
137
	 * @throws SQLException
138
	 */
139
	protected CdmBase getRelatedObject(ResultSet rs, String dbAttribute) throws SQLException {
140
		CdmBase result = null;
141
		if (dbAttribute != null){
142
			Object dbValue = rs.getObject(dbAttribute);
143
			String id = String.valueOf(dbValue);
144
			DbImportStateBase state = importMapperHelper.getState();
145
			result = state.getRelatedObject(relatedObjectNamespace, id);
146
		}
147
		return result;
148
	}
149

    
150
	/**
151
	 * Checks if cdmBase is of type Taxon
152
	 * @param fromObject
153
	 */
154
	private TaxonName checkTaxonNameType(CdmBase cdmBase) {
155
		if (! cdmBase.isInstanceOf(TaxonName.class)){
156
			String warning = "Type name or typifier name is not of type TaxonName but " + cdmBase.getClass().getName();
157
			logger.warn(warning);
158
			throw new IllegalArgumentException(warning);
159
		}
160
		return (cdmBase.deproxy(cdmBase, TaxonName.class));
161
	}
162

    
163

    
164
}
(29-29/53)