Project

General

Profile

Download (5.22 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
 * @since 12.05.2009
29
 */
30
public class CdmDbExportMapping<STATE extends DbExportStateBase<CONFIG, TRANSFORM>, CONFIG extends DbExportConfiguratorBase<STATE, TRANSFORM, Source>, TRANSFORM extends IExportTransformer>
31
        extends CdmIoMapping {
32

    
33
    private static final Logger logger = Logger.getLogger(CdmDbExportMapping.class);
34

    
35
	private PreparedStatement preparedStatement;
36
	private String dbTableName;
37
	private List<CollectionExportMapping<STATE,CONFIG, TRANSFORM>> collectionMappingList = new ArrayList<>();
38

    
39

    
40
	public CdmDbExportMapping(String tableName){
41
		this.dbTableName = tableName;
42
	}
43

    
44
	public boolean initialize(STATE state) throws SQLException{
45
		CONFIG config = state.getConfig();
46
		Source db = config.getDestination();
47

    
48
		try {
49
			IndexCounter index;
50
			String strPreparedStatement = prepareStatement();
51
			logger.debug(strPreparedStatement);
52
			this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);
53
			index = new IndexCounter(1);
54

    
55
			for (CdmMapperBase mapper : this.mapperList){
56
				if (mapper instanceof IDbExportMapper){
57
					IDbExportMapper<DbExportStateBase<?,TRANSFORM>,TRANSFORM> dbMapper = (IDbExportMapper)mapper;
58
					dbMapper.initialize(preparedStatement, index, state, dbTableName);
59
				}else{
60
					logger.warn("mapper "+mapper.toString() + "," + mapper.getClass().getName() +" is not of type " + IDbExportMapper.class.getSimpleName());
61
				}
62
			}
63
			for (CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping : this.collectionMappingList ){
64
				collectionMapping.initialize(state);
65
			}
66
			return true;
67
		} catch (SQLException e) {
68
			logger.warn("SQL Exception");
69
			throw e;
70
		}
71
	}
72

    
73

    
74
	public boolean invoke(CdmBase cdmBase) throws SQLException{
75
		try {
76
			boolean result = true;
77
			for (CdmMapperBase mapper : this.mapperList){
78
				if (mapper instanceof ObjectChangeMapper){
79
					ObjectChangeMapper changeMapper = (ObjectChangeMapper)mapper;
80
					cdmBase = changeMapper.getNewObject(cdmBase);
81
				}else if (mapper instanceof IDbExportMapper){
82
					IDbExportMapper<DbExportStateBase<?,TRANSFORM>, TRANSFORM> dbMapper = (IDbExportMapper)mapper;
83
					try {
84
						result &= dbMapper.invoke(cdmBase);
85
					} catch (Exception e) {
86
						result = false;
87
						logger.error("Error occurred in mapping.invoke");
88
						e.printStackTrace();
89
						continue;
90
					}
91
				}else {
92
					logger.warn("mapper " + mapper.toString() + "is not of required type " + IDbExportMapper.class.getSimpleName());
93
				}
94
			}
95
			int count = preparedStatement.executeUpdate();
96
			if (logger.isDebugEnabled()) {
97
                logger.debug("Number of rows affected: " + count);
98
            }
99
			for (CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping : this.collectionMappingList ){
100
				result &= collectionMapping.invoke(cdmBase);
101
			}
102
			return result;
103
		} catch(Exception e){
104
			e.printStackTrace();
105
			logger.error(e.getMessage() + ": " + cdmBase.toString());
106
			return false;
107
		}
108
	}
109

    
110

    
111
	public void addCollectionMapping(CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping){
112
		this.collectionMappingList.add(collectionMapping);
113
	}
114

    
115
	protected String prepareStatement(){
116
		String sqlInsert = "INSERT INTO " + getDbTableName() + " (";
117
		String sqlValues = ") VALUES(";
118
		String sqlEnd = ")";
119
		String attributes = "";
120
		String values = "";
121
		for (String attribute : this.getDestinationAttributeList()){
122
			attributes +=  "," + attribute;
123
			values += ",?";
124
		}
125
		attributes = attributes.substring(1); //delete first ','
126
		values = values.substring(1); //delete first ','
127
		String result = sqlInsert + attributes + sqlValues + values + sqlEnd;
128
		return result;
129
	}
130

    
131
	/**
132
	 * @return the berlinModelTableName
133
	 */
134
	public String getDbTableName() {
135
		return dbTableName;
136
	}
137

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

    
145
	/**
146
	 * @return the preparedStatement
147
	 */
148
	protected PreparedStatement getPreparedStatement() {
149
		return preparedStatement;
150
	}
151

    
152
	/**
153
	 * @param preparedStatement the preparedStatement to set
154
	 */
155
	protected void setPreparedStatement(PreparedStatement preparedStatement) {
156
		this.preparedStatement = preparedStatement;
157
	}
158

    
159
//	protected List<CdmAttributeMapperBase> getAttributeMapperList(){
160
//		List<CdmAttributeMapperBase> list = this.mapperList;
161
//		return list;
162
//	}
163

    
164

    
165
}
(1-1/40)