Project

General

Profile

Download (5.25 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.out;
11

    
12
import java.sql.PreparedStatement;
13
import java.sql.SQLException;
14
import java.util.ArrayList;
15
import java.util.List;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.DbExportConfiguratorBase;
20
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21
import eu.etaxonomy.cdm.io.common.Source;
22
import eu.etaxonomy.cdm.io.common.mapping.CdmIoMapping;
23
import eu.etaxonomy.cdm.io.common.mapping.CdmMapperBase;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25

    
26
/**
27
 * @author a.mueller
28
 * @created 12.05.2009
29
 * @version 1.0
30
 */
31
public class CdmDbExportMapping<STATE extends DbExportStateBase<CONFIG, TRANSFORM>, CONFIG extends DbExportConfiguratorBase<STATE, TRANSFORM>, TRANSFORM extends IExportTransformer> extends CdmIoMapping {
32
	private static final Logger logger = Logger.getLogger(CdmDbExportMapping.class);
33
	
34
	private PreparedStatement preparedStatement;
35
	private String dbTableName;
36
	private List<CollectionExportMapping<STATE,CONFIG, TRANSFORM>> collectionMappingList = new ArrayList<CollectionExportMapping<STATE,CONFIG, TRANSFORM>>();
37
	
38

    
39
	public CdmDbExportMapping(String tableName){
40
		this.dbTableName = tableName;
41
	}
42
	
43
	public boolean initialize(STATE state) throws SQLException{
44
		CONFIG config = state.getConfig();
45
		Source db = config.getDestination();
46
		
47
		try {
48
			IndexCounter index;
49
			String strPreparedStatement = prepareStatement();
50
			logger.debug(strPreparedStatement);
51
			this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);
52
			index = new IndexCounter(1);
53
			
54
			for (CdmMapperBase mapper : this.mapperList){
55
				if (mapper instanceof IDbExportMapper){
56
					IDbExportMapper<DbExportStateBase<?,TRANSFORM>,TRANSFORM> dbMapper = (IDbExportMapper)mapper;
57
					dbMapper.initialize(preparedStatement, index, state, dbTableName);
58
				}else{
59
					logger.warn("mapper "+mapper.toString() + "," + mapper.getClass().getName() +" is not of type " + IDbExportMapper.class.getSimpleName());
60
				}
61
			}
62
			for (CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping : this.collectionMappingList ){
63
				collectionMapping.initialize(state);
64
			}
65
			return true;
66
		} catch (SQLException e) {
67
			logger.warn("SQL Exception");
68
			throw e;
69
		}
70
	}
71

    
72
	
73
	public boolean invoke(CdmBase cdmBase) throws SQLException{
74
		try {
75
			boolean result = true;
76
			for (CdmMapperBase mapper : this.mapperList){
77
				if (mapper instanceof ObjectChangeMapper){
78
					ObjectChangeMapper changeMapper = (ObjectChangeMapper)mapper;
79
					cdmBase = changeMapper.getNewObject(cdmBase);
80
				}else if (mapper instanceof IDbExportMapper){
81
					IDbExportMapper<DbExportStateBase<?,TRANSFORM>, TRANSFORM> dbMapper = (IDbExportMapper)mapper;
82
					try {
83
						result &= dbMapper.invoke(cdmBase);
84
					} catch (Exception e) {
85
						result = false;
86
						logger.error("Error occurred in mapping.invoke");
87
						e.printStackTrace();
88
						continue;
89
					}
90
				}else {
91
					logger.warn("mapper " + mapper.toString() + "is not of required type " + IDbExportMapper.class.getSimpleName());
92
				}
93
			}
94
			int count = preparedStatement.executeUpdate();
95
			if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);
96
			for (CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping : this.collectionMappingList ){
97
				result &= collectionMapping.invoke(cdmBase);
98
			}
99
			return result;
100
		} catch(Exception e){
101
			e.printStackTrace();
102
			logger.error(e.getMessage() + ": " + cdmBase.toString());
103
			return false;
104
		}
105
	}
106
	
107
	
108
	public void addCollectionMapping(CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping){
109
		this.collectionMappingList.add(collectionMapping);
110
	}
111
	
112
	protected String prepareStatement(){
113
		String sqlInsert = "INSERT INTO " + getDbTableName() + " (";
114
		String sqlValues = ") VALUES(";
115
		String sqlEnd = ")";
116
		String attributes = "";
117
		String values = "";
118
		for (String attribute : this.getDestinationAttributeList()){
119
			attributes +=  "," + attribute;
120
			values += ",?";
121
		}
122
		attributes = attributes.substring(1); //delete first ','
123
		values = values.substring(1); //delete first ','
124
		String result = sqlInsert + attributes + sqlValues + values + sqlEnd;
125
		return result;
126
	}
127

    
128
	/**
129
	 * @return the berlinModelTableName
130
	 */
131
	public String getDbTableName() {
132
		return dbTableName;
133
	}
134

    
135
	/**
136
	 * @param berlinModelTableName the berlinModelTableName to set
137
	 */
138
	public void setDbTableName(String dbTableName) {
139
		this.dbTableName = dbTableName;
140
	}
141

    
142
	/**
143
	 * @return the preparedStatement
144
	 */
145
	protected PreparedStatement getPreparedStatement() {
146
		return preparedStatement;
147
	}
148

    
149
	/**
150
	 * @param preparedStatement the preparedStatement to set
151
	 */
152
	protected void setPreparedStatement(PreparedStatement preparedStatement) {
153
		this.preparedStatement = preparedStatement;
154
	}
155
	
156
//	protected List<CdmAttributeMapperBase> getAttributeMapperList(){
157
//		List<CdmAttributeMapperBase> list = this.mapperList;
158
//		return list;
159
//	}
160
	
161
	
162
}
(1-1/40)