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
|
*/
|
27
|
public class DbImportMapping<STATE extends DbImportStateBase, CONFIG extends IImportConfigurator> extends CdmIoMapping {
|
28
|
private static final Logger logger = Logger.getLogger(DbImportMapping.class);
|
29
|
|
30
|
private boolean isInitialized = false;
|
31
|
private Class<? extends CdmBase> destinationClass;
|
32
|
private DbImportMapping<STATE, CONFIG> secondPathMapping;
|
33
|
private boolean blankToNull = false;
|
34
|
|
35
|
public DbImportMapping(){
|
36
|
// this.dbTableName = tableName;
|
37
|
}
|
38
|
|
39
|
public boolean initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass){
|
40
|
if (!isInitialized){
|
41
|
// this.dbTableName = tableName;
|
42
|
this.destinationClass = destinationClass;
|
43
|
for (CdmMapperBase mapper: this.mapperList){
|
44
|
if (mapper instanceof IDbImportMapper){
|
45
|
((IDbImportMapper<DbImportStateBase<?,?>,? extends CdmBase>) mapper ).initialize(state, destinationClass);
|
46
|
}else{
|
47
|
logger.warn("Mapper type " + mapper.getClass().getSimpleName() + " not yet implemented for DB import mapping");
|
48
|
}
|
49
|
}
|
50
|
isInitialized = true;
|
51
|
if (secondPathMapping != null){
|
52
|
secondPathMapping.initialize(state, destinationClass);
|
53
|
}
|
54
|
}
|
55
|
return true;
|
56
|
}
|
57
|
|
58
|
@Override
|
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
|
@SuppressWarnings("unchecked")
|
95
|
IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper<DbImportStateBase<?,?>,CdmBase>)mapper;
|
96
|
try {
|
97
|
objectToSave = dbMapper.invoke(rs, objectToSave);
|
98
|
} catch (Exception e) {
|
99
|
result = false;
|
100
|
logger.error("Error occurred in mapping.invoke of mapper " + dbMapper.toString());
|
101
|
e.printStackTrace();
|
102
|
continue;
|
103
|
}
|
104
|
}else{
|
105
|
logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());
|
106
|
}
|
107
|
}
|
108
|
if (objectToSave != null){
|
109
|
objectsToSave.add(objectToSave);
|
110
|
}else{
|
111
|
logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");
|
112
|
}
|
113
|
return result;
|
114
|
}
|
115
|
}
|
116
|
|
117
|
public void setSecondPathMapping(DbImportMapping<STATE, CONFIG> secondPathMapping){
|
118
|
this.secondPathMapping = secondPathMapping;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
|
123
|
* @return
|
124
|
*/
|
125
|
public boolean isBlankToNull() {
|
126
|
return blankToNull;
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* @see #isBlankToNull()
|
131
|
* @param blankToNull
|
132
|
*/
|
133
|
public void setBlankToNull(boolean blankToNull) {
|
134
|
this.blankToNull = blankToNull;
|
135
|
}
|
136
|
|
137
|
// /**
|
138
|
// * @return the berlinModelTableName
|
139
|
// */
|
140
|
// public String getDbTableName() {
|
141
|
// return dbTableName;
|
142
|
// }
|
143
|
//
|
144
|
// /**
|
145
|
// * @param berlinModelTableName the berlinModelTableName to set
|
146
|
// */
|
147
|
// public void setDbTableName(String dbTableName) {
|
148
|
// this.dbTableName = dbTableName;
|
149
|
// }
|
150
|
//
|
151
|
//
|
152
|
// protected List<CdmAttributeMapperBase> getAttributeMapperList(){
|
153
|
// List<CdmAttributeMapperBase> list = this.mapperList;
|
154
|
// return list;
|
155
|
// }
|
156
|
|
157
|
|
158
|
}
|