Project

General

Profile

Download (4.84 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.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19

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

    
27
/**
28
 * @author a.mueller
29
 * @since 12.05.2009
30
 */
31
public class CdmDbExportMapping<STATE extends DbExportStateBase<CONFIG, TRANSFORM>, CONFIG extends DbExportConfiguratorBase<STATE, TRANSFORM, Source>, TRANSFORM extends IExportTransformer>
32
        extends CdmIoMapping {
33

    
34
    private static final Logger logger = LogManager.getLogger();
35

    
36
	private PreparedStatement preparedStatement;
37
	private String dbTableName;
38
	private List<CollectionExportMapping<STATE,CONFIG, TRANSFORM>> collectionMappingList = new ArrayList<>();
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
	public boolean invoke(CdmBase cdmBase){
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()) {
96
                logger.debug("Number of rows affected: " + count);
97
            }
98
			for (CollectionExportMapping<STATE,CONFIG, TRANSFORM> collectionMapping : this.collectionMappingList ){
99
				result &= collectionMapping.invoke(cdmBase);
100
			}
101
			return result;
102
		} catch(Exception e){
103
			e.printStackTrace();
104
			logger.error(e.getMessage() + ": " + cdmBase.toString());
105
			return false;
106
		}
107
	}
108

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

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

    
129
	public String getDbTableName() {
130
		return dbTableName;
131
	}
132
	public void setDbTableName(String dbTableName) {
133
		this.dbTableName = dbTableName;
134
	}
135

    
136
	protected PreparedStatement getPreparedStatement() {
137
		return preparedStatement;
138
	}
139
	protected void setPreparedStatement(PreparedStatement preparedStatement) {
140
		this.preparedStatement = preparedStatement;
141
	}
142

    
143
//	protected List<CdmAttributeMapperBase> getAttributeMapperList(){
144
//		List<CdmAttributeMapperBase> list = this.mapperList;
145
//		return list;
146
//	}
147
}
(1-1/44)