Project

General

Profile

Download (4.48 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
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
20
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 12.05.2009
26
 * @version 1.0
27
 */
28
public class DbImportMapping<STATE extends DbImportStateBase, CONFIG extends IImportConfigurator> extends CdmIoMapping {
29
	private static final Logger logger = Logger.getLogger(DbImportMapping.class);
30
	
31
	private boolean isInitialized = false;;
32
	private Class<? extends CdmBase> destinationClass;
33
	private DbImportMapping<STATE, CONFIG> secondPathMapping;
34
	private boolean blankToNull = false;
35
	
36
	public DbImportMapping(){
37
//		this.dbTableName = tableName;
38
	}
39
	
40
	public boolean initialize(DbImportStateBase state, Class<? extends CdmBase> destinationClass){
41
		if (!isInitialized){
42
			//	this.dbTableName = tableName;
43
			this.destinationClass = destinationClass;
44
			for (CdmMapperBase mapper: this.mapperList){
45
				if (mapper instanceof IDbImportMapper){
46
					((IDbImportMapper) mapper).initialize(state, destinationClass);
47
				}else{
48
					logger.warn("Mapper type " + mapper.getClass().getSimpleName() + " not yet implemented for DB import mapping");
49
				}
50
			}
51
			isInitialized = true;
52
			if (secondPathMapping != null){
53
				secondPathMapping.initialize(state, destinationClass);
54
			}
55
		}
56
		return true;
57
	}
58

    
59
	public void addMapper(CdmAttributeMapperBase mapper){
60
		super.addMapper(mapper);
61
		if (mapper instanceof DbStringMapper){
62
			((DbStringMapper)mapper).setBlankToNull(isBlankToNull());
63
		}
64
	}
65
	
66
	/**
67
	 * Invokes the second path mapping if one has been defined
68
	 * @param rs
69
	 * @param objectsToSave
70
	 * @return
71
	 * @throws SQLException
72
	 */
73
	public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave) throws SQLException{
74
		return invoke(rs, objectsToSave, false);
75
	}	
76
	
77
	/**
78
	 * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
79
	 * @param rs
80
	 * @param objectsToSave
81
	 * @param secondPath
82
	 * @return
83
	 * @throws SQLException
84
	 */
85
	public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave, boolean secondPath) throws SQLException{
86
		boolean result = true;
87
		if (secondPath == true && secondPathMapping != null){
88
			return secondPathMapping.invoke(rs, objectsToSave);
89
		} else {
90
			CdmBase objectToSave = null;
91
			//		try {
92
			for (CdmMapperBase mapper : this.mapperList){
93
				if (mapper instanceof IDbImportMapper){
94
					IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper)mapper;
95
					try {
96
						objectToSave = dbMapper.invoke(rs, objectToSave);
97
					} catch (Exception e) {
98
						result = false;
99
						logger.error("Error occurred in mapping.invoke of mapper " + this.toString());
100
						e.printStackTrace();
101
						continue;
102
					}
103
				}else{
104
					logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());
105
				}
106
			}
107
			if (objectToSave != null){
108
				objectsToSave.add(objectToSave);
109
			}else{
110
				logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");
111
			}
112
			return result;
113
	}
114
	}
115
	
116
	public void setSecondPathMapping(DbImportMapping secondPathMapping){
117
		this.secondPathMapping = secondPathMapping;
118
	}
119

    
120
	/**
121
	 * If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
122
	 * @return
123
	 */
124
	public boolean isBlankToNull() {
125
		return blankToNull;
126
	}
127

    
128
	/**
129
	 * @see #isBlankToNull()
130
	 * @param blankToNull
131
	 */
132
	public void setBlankToNull(boolean blankToNull) {
133
		this.blankToNull = blankToNull;
134
	}
135

    
136
//	/**
137
//	 * @return the berlinModelTableName
138
//	 */
139
//	public String getDbTableName() {
140
//		return dbTableName;
141
//	}
142
//
143
//	/**
144
//	 * @param berlinModelTableName the berlinModelTableName to set
145
//	 */
146
//	public void setDbTableName(String dbTableName) {
147
//		this.dbTableName = dbTableName;
148
//	}
149
//
150
//	
151
//	protected List<CdmAttributeMapperBase> getAttributeMapperList(){
152
//		List<CdmAttributeMapperBase> list = this.mapperList;
153
//		return list;
154
//	}
155
	
156
	
157
}
(21-21/51)