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