Project

General

Profile

« Previous | Next » 

Revision d17f8af9

Added by Andreas Müller over 15 years ago

Berlin Model Export

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/berlinModel/out/BerlinModelExportMapping.java
12 12

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

  
17 18
import org.apache.log4j.Logger;
18 19

  
......
32 33
	private static final Logger logger = Logger.getLogger(BerlinModelExportMapping.class);
33 34
	
34 35
	private PreparedStatement preparedStatement;
35
	private String berlinModelTableName;
36
	private boolean doExecute = true;
36
	private String dbTableName;
37
	private List<CollectionExportMapping> collectionMappingList = new ArrayList<CollectionExportMapping>();
37 38
	
38
	public BerlinModelExportMapping(String tableName){
39
		this(tableName, true);
40
	}
41 39

  
42
	public BerlinModelExportMapping(String tableName, boolean doExecute){
43
		this.berlinModelTableName = tableName;
44
		this.doExecute = doExecute;
40
	public BerlinModelExportMapping(String tableName){
41
		this.dbTableName = tableName;
45 42
	}
46 43
	
47 44
	public boolean initialize(BerlinModelExportState<?> state) throws SQLException{
48
		return this.initialize(state, null);
49
	}
50
	
51
	public boolean initialize(BerlinModelExportState<?> state, PreparedStatement stmt) throws SQLException{
52 45
		BerlinModelExportConfigurator bmeConfig = (BerlinModelExportConfigurator)state.getConfig();
53 46
		Source db = bmeConfig.getDestination();
54 47
		
55 48
		try {
56
			if (stmt ==null){
57
				String strPreparedStatement = preparedStatement();
58
				logger.debug(strPreparedStatement);
59
				this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);
60
			}else{
61
				this.preparedStatement = stmt;
62
			}
63
			IndexCounter index = new IndexCounter(1);
49
			IndexCounter index;
50
			String strPreparedStatement = prepareStatement();
51
			logger.debug(strPreparedStatement);
52
			this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);
53
			index = new IndexCounter(1);
54
			
64 55
			for (CdmAttributeMapperBase mapper : this.mapperList){
65 56
				if (mapper instanceof IDbExportMapper){
66 57
					IDbExportMapper<DbExportState<?>> dbMapper = (IDbExportMapper)mapper;
67
					dbMapper.initialize(preparedStatement, index, state, berlinModelTableName);
58
					dbMapper.initialize(preparedStatement, index, state, dbTableName);
68 59
				}else{
69 60
					logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());
70 61
				}
71 62
			}
63
			for (CollectionExportMapping collectionMapping : this.collectionMappingList ){
64
				collectionMapping.initialize(state);
65
			}
72 66
			return true;
73 67
		} catch (SQLException e) {
74 68
			logger.warn("SQL Exception");
75 69
			throw e;
76 70
		}
77 71
	}
72

  
78 73
	
79 74
	public boolean invoke(CdmBase cdmBase) throws SQLException{
80 75
		try {
......
94 89
					logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());
95 90
				}
96 91
			}
97
			if (doExecute){
98
				int count = preparedStatement.executeUpdate();
99
				if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);
92
			int count = preparedStatement.executeUpdate();
93
			if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);
94
			for (CollectionExportMapping collectionMapping : this.collectionMappingList ){
95
				result &= collectionMapping.invoke(cdmBase);
100 96
			}
101 97
			return result;
102 98
		} catch(SQLException e){
......
107 103
	}
108 104
	
109 105
	
106
	public void addCollectionMapping(CollectionExportMapping collectionMapping){
107
		this.collectionMappingList.add(collectionMapping);
108
	}
110 109
	
111
	private String preparedStatement(){
112
		String sqlInsert = "INSERT INTO " + getBerlinModelTableName() + " (";
110
	protected String prepareStatement(){
111
		String sqlInsert = "INSERT INTO " + getDbTableName() + " (";
113 112
		String sqlValues = ") VALUES(";
114 113
		String sqlEnd = ")";
115 114
		String attributes = "";
......
127 126
	/**
128 127
	 * @return the berlinModelTableName
129 128
	 */
130
	public String getBerlinModelTableName() {
131
		return berlinModelTableName;
129
	public String getDbTableName() {
130
		return dbTableName;
132 131
	}
133 132

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

  
141 140
	/**
142 141
	 * @return the preparedStatement
143 142
	 */
144
	public PreparedStatement getPreparedStatement() {
143
	protected PreparedStatement getPreparedStatement() {
145 144
		return preparedStatement;
146 145
	}
146

  
147
	/**
148
	 * @param preparedStatement the preparedStatement to set
149
	 */
150
	protected void setPreparedStatement(PreparedStatement preparedStatement) {
151
		this.preparedStatement = preparedStatement;
152
	}
153
	
147 154
	
148 155
	
149 156
	

Also available in: Unified diff