Project

General

Profile

« Previous | Next » 

Revision cdda449c

Added by Andreas Müller over 1 year ago

cleanup

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/mapping/DbImportNameTypeDesignationMapper.java
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;import org.apache.logging.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.TaxonName;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23

  
24
/**
25
 * @author a.mueller
26
 * @since 12.05.2009
27
 * @version 1.0
28
 */
29
/**
30
 * @author a.mueller
31
 * @since 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 = LogManager.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
	@Override
76
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
77
		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

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

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

  
100

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

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

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

  
118
		NameTypeDesignationStatus status = this.designationStatus;
119
		if (designationStatusValue != null){
120
			//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
			}
125
		}
126
		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
127

  
128
		return typifiedName;
129
	}
130

  
131
	/**
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

  
149
	/**
150
	 * Checks if cdmBase is of type Taxon
151
	 * @param fromObject
152
	 */
153
	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
			logger.warn(warning);
157
			throw new IllegalArgumentException(warning);
158
		}
159
		return (cdmBase.deproxy(cdmBase, TaxonName.class));
160
	}
161

  
162

  
163
}
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;import org.apache.logging.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.TaxonName;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23

  
24
/**
25
 * @author a.mueller
26
 * @since 12.05.2009
27
 * @version 1.0
28
 */
29
/**
30
 * @author a.mueller
31
 * @since 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 = LogManager.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
	@Override
76
    public CdmBase invoke(ResultSet rs, CdmBase cdmBase) throws SQLException {
77
		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

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

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

  
100

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

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

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

  
118
		NameTypeDesignationStatus status = this.designationStatus;
119
		if (designationStatusValue != null){
120
			//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
			}
125
		}
126
		typifiedName.addNameTypeDesignation(typeName, citation, microCitation, originalNameString, status, addToAllHomotypicNames);
127

  
128
		return typifiedName;
129
	}
130

  
131
	/**
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

  
149
	/**
150
	 * Checks if cdmBase is of type Taxon
151
	 * @param fromObject
152
	 */
153
	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
			logger.warn(warning);
157
			throw new IllegalArgumentException(warning);
158
		}
159
		return (cdmBase.deproxy(cdmBase, TaxonName.class));
160
	}
161

  
162

  
163
}

Also available in: Unified diff