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
|
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
19
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
20
|
|
21
|
/**
|
22
|
* This mapper removes objects from the stream of objects to be mapped.
|
23
|
* This is a very perliminarye implementation. In future there will be at least an
|
24
|
* interface to implement and some more parameter to better define which values to include/exclude.
|
25
|
*
|
26
|
* Current implementation
|
27
|
*
|
28
|
* @author a.mueller
|
29
|
* @created 13.02.2012
|
30
|
*/
|
31
|
public class DbSimpleFilterMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> {
|
32
|
private static final Logger logger = Logger.getLogger(DbSimpleFilterMapper.class);
|
33
|
|
34
|
|
35
|
|
36
|
public static DbSimpleFilterMapper NewSingleNullAttributeInstance(String cdmFilterCriterionAttribute, String reason){
|
37
|
return new DbSimpleFilterMapper(null, cdmFilterCriterionAttribute, null, reason);
|
38
|
}
|
39
|
|
40
|
//*************************** VARIABLES ***************************************************************//
|
41
|
private String filterReason;
|
42
|
|
43
|
//*************************** CONSTRUCTOR ***************************************************************//
|
44
|
|
45
|
/**
|
46
|
* @param dbAttributString
|
47
|
* @param cdmAttributeString
|
48
|
* @param defaultValue
|
49
|
*/
|
50
|
protected DbSimpleFilterMapper(String dbAttributString, String cdmAttributeString, Object defaultValue, String filterReason) {
|
51
|
super(cdmAttributeString, dbAttributString, defaultValue);
|
52
|
this.filterReason = filterReason;
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
public void initialize(PreparedStatement stmt, IndexCounter index, DbExportStateBase state, String tableName) {
|
57
|
String attributeName = this.getDestinationAttribute();
|
58
|
String localReason = "";
|
59
|
if (StringUtils.isNotBlank(filterReason)){
|
60
|
localReason = " (" + filterReason +")";
|
61
|
}
|
62
|
logger.info(" Some objects are filtered on " + attributeName + "." + localReason);
|
63
|
exportMapperHelper.initializeNull(stmt, state, tableName);
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
|
68
|
if (isFiltered(cdmBase)){
|
69
|
return false;
|
70
|
}else{
|
71
|
return true; // do nothing
|
72
|
}
|
73
|
}
|
74
|
|
75
|
|
76
|
private boolean isFiltered(CdmBase cdmBase) {
|
77
|
Object value = super.getValue(cdmBase);
|
78
|
return (value == null);
|
79
|
}
|
80
|
|
81
|
/* (non-Javadoc)
|
82
|
* @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
|
83
|
*/
|
84
|
@Override
|
85
|
public Class getTypeClass() {
|
86
|
return null; //not needed
|
87
|
}
|
88
|
|
89
|
|
90
|
@Override
|
91
|
protected int getSqlType() {
|
92
|
return -1; // not needed
|
93
|
}
|
94
|
|
95
|
|
96
|
|
97
|
|
98
|
}
|