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>
|
28
|
extends CdmIoMapping {
|
29
|
|
30
|
private static final Logger logger = Logger.getLogger(DbImportMapping.class);
|
31
|
|
32
|
private boolean isInitialized = false;
|
33
|
private Class<? extends CdmBase> destinationClass;
|
34
|
private DbImportMapping<STATE, CONFIG> secondPathMapping;
|
35
|
private boolean blankToNull = false;
|
36
|
|
37
|
public boolean initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass){
|
38
|
if (!isInitialized){
|
39
|
// this.dbTableName = tableName;
|
40
|
this.destinationClass = destinationClass;
|
41
|
for (CdmMapperBase mapper: this.mapperList){
|
42
|
if (mapper instanceof IDbImportMapper){
|
43
|
((IDbImportMapper<DbImportStateBase<?,?>,? extends CdmBase>) mapper ).initialize(state, destinationClass);
|
44
|
}else{
|
45
|
logger.warn("Mapper type " + mapper.getClass().getSimpleName() + " not yet implemented for DB import mapping");
|
46
|
}
|
47
|
}
|
48
|
isInitialized = true;
|
49
|
if (secondPathMapping != null){
|
50
|
secondPathMapping.initialize(state, destinationClass);
|
51
|
}
|
52
|
}
|
53
|
return true;
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public void addMapper(CdmAttributeMapperBase mapper){
|
58
|
super.addMapper(mapper);
|
59
|
if (mapper instanceof DbStringMapper){
|
60
|
((DbStringMapper)mapper).setBlankToNull(isBlankToNull());
|
61
|
}
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Invokes the second path mapping if one has been defined
|
66
|
* @param rs
|
67
|
* @param objectsToSave
|
68
|
* @return
|
69
|
* @throws SQLException
|
70
|
*/
|
71
|
public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave) throws SQLException{
|
72
|
return invoke(rs, objectsToSave, false);
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
|
77
|
*/
|
78
|
public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave, boolean secondPath) throws SQLException{
|
79
|
boolean result = true;
|
80
|
if (secondPath == true && secondPathMapping != null){
|
81
|
return secondPathMapping.invoke(rs, objectsToSave);
|
82
|
} else {
|
83
|
CdmBase objectToSave = null;
|
84
|
// try {
|
85
|
for (CdmMapperBase mapper : this.mapperList){
|
86
|
if (mapper instanceof IDbImportMapper){
|
87
|
@SuppressWarnings("unchecked")
|
88
|
IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper<DbImportStateBase<?,?>,CdmBase>)mapper;
|
89
|
try {
|
90
|
objectToSave = dbMapper.invoke(rs, objectToSave);
|
91
|
} catch (Exception e) {
|
92
|
result = false;
|
93
|
logger.error("Error occurred in mapping.invoke of mapper " + dbMapper.toString());
|
94
|
e.printStackTrace();
|
95
|
continue;
|
96
|
}
|
97
|
}else{
|
98
|
logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());
|
99
|
}
|
100
|
}
|
101
|
if (objectToSave != null){
|
102
|
objectsToSave.add(objectToSave);
|
103
|
}else{
|
104
|
logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");
|
105
|
}
|
106
|
return result;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
public void setSecondPathMapping(DbImportMapping<STATE, CONFIG> secondPathMapping){
|
111
|
this.secondPathMapping = secondPathMapping;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
|
116
|
* @return
|
117
|
*/
|
118
|
public boolean isBlankToNull() {
|
119
|
return blankToNull;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* @see #isBlankToNull()
|
124
|
* @param blankToNull
|
125
|
*/
|
126
|
public void setBlankToNull(boolean blankToNull) {
|
127
|
this.blankToNull = blankToNull;
|
128
|
}
|
129
|
}
|