Project

General

Profile

« Previous | Next » 

Revision f905a388

Added by Andreas Müller almost 2 years ago

cleanup

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/mapping/out/DbSimpleFilterMapper.java
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;import org.apache.logging.log4j.Logger;
19

  
20
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21
import eu.etaxonomy.cdm.model.common.CdmBase;
22

  
23
/**
24
 * This mapper removes objects from the stream of objects to be mapped.
25
 * This is a very preliminary implementation. In future there will be at least an
26
 * interface to implement and some more parameter to better define which values to include/exclude.
27
 *
28
 * Current implementation
29
 *
30
 * @author a.mueller
31
 * @since 13.02.2012
32
 */
33
public class DbSimpleFilterMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> {
34
	private static final Logger logger = LogManager.getLogger(DbSimpleFilterMapper.class);
35

  
36
	/**
37
	 * Returns a filter mapper which filters out objects which have
38
	 * <code>null</code> value for the given source attribute.
39
	 */
40
	public static DbSimpleFilterMapper NewSingleNullAttributeInstance(String cdmFilterCriterionAttribute, String reason){
41
	    Set<Object> notAllowedValues = new HashSet<>();
42
	    notAllowedValues.add(null);
43
	    return new DbSimpleFilterMapper(null, cdmFilterCriterionAttribute, null, notAllowedValues, null, reason);
44
	}
45

  
46
    public static DbSimpleFilterMapper NewAllowedValueInstance(String cdmFilterCriterionAttribute,
47
            Set<?> allowedValues, Set<?> notAllowedValues, String reason){
48
        return new DbSimpleFilterMapper(null, cdmFilterCriterionAttribute,
49
                allowedValues, notAllowedValues, null, reason);
50
    }
51

  
52
//*************************** VARIABLES ***************************************************************//
53

  
54
	private String filterReason;
55
	private Set<?> allowedValues;
56
	private Set<?> notAllowedValues;
57

  
58

  
59
//*************************** CONSTRUCTOR ***************************************************************//
60

  
61
	protected DbSimpleFilterMapper(String dbAttributString, String cdmAttributeString,
62
	        Set<?> allowedValues, Set<?> notAllowedValues, Object defaultValue, String filterReason) {
63
		super(cdmAttributeString, dbAttributString, defaultValue);
64
		this.filterReason = filterReason;
65
		this.allowedValues = allowedValues;
66
		this.notAllowedValues = notAllowedValues;
67
	}
68

  
69
	@Override
70
	public void initialize(PreparedStatement stmt, IndexCounter index, DbExportStateBase state, String tableName) {
71
		String attributeName = this.getDestinationAttribute();
72
		String localReason = "";
73
		if (StringUtils.isNotBlank(filterReason)){
74
			localReason = " (" + filterReason +")";
75
		}
76
		logger.info(" Some objects are filtered on " + attributeName + "." +  localReason);
77
		exportMapperHelper.initializeNull(stmt, state, tableName);
78
	}
79

  
80
	@Override
81
	protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
82
		if (isFiltered(cdmBase)){
83
			return false;
84
		}else{
85
			return true; // do nothing
86
		}
87
	}
88

  
89
	private boolean isFiltered(CdmBase cdmBase) {
90
		Object value = super.getValue(cdmBase);
91
		if (allowedValues != null && !allowedValues.contains(value)){
92
		    return true;  //filtered out
93
		}else if (notAllowedValues.contains(value)){
94
		    return true;
95
		}else{
96
		    return false;
97
		}
98
	}
99

  
100
	@Override
101
	public Class getTypeClass() {
102
		return null;  //not needed
103
	}
104

  
105
	@Override
106
	protected int getSqlType() {
107
		return -1;  // not needed
108
	}
109
}
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
}

Also available in: Unified diff