Exclude(boolean)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 6
rs 10
1
/*
2
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender.requirement;
17
18
import java.util.Arrays;
19
import java.util.List;
20
import javax.xml.bind.annotation.XmlAccessType;
21
import javax.xml.bind.annotation.XmlAccessorType;
22
import javax.xml.bind.annotation.XmlAttribute;
23
24
import org.apache.commons.lang3.StringUtils;
25
26
import lombok.Data;
27
import lombok.NoArgsConstructor;
28
29
/**
30
 * JAXB class definition for <Exclude> tags defining exclusion rules.
31
 *
32
 * The exclude tag has a Name attribute, and either an Equals attribute, a Like
33
 * attribute (corresponding to an SQL LIKE comparison), or a Null attribute.
34
 * The Name attribute defines the column's name in the table, and Equals,
35
 * Like, and Null attributes define the column's excluded value.
36
 *
37
 * The Name attribute is optional if the exclude tag is part of a Column tag's
38
 * exclusion list (the name is then the Column's name).  The Name attribute can
39
 * still be included to indicate it should be excluded on the basis of the value
40
 * of another column - however the other column must either be declared as a key
41
 * or as a Column elsewhere in the table definition.  This restriction does not
42
 * apply to table exclusions.  Any column can be used for a table exclusion.
43
 *
44
 * Excluding empty values is possible by setting the Equals attribute to a blank
45
 * value.  The Like attribute requires a value.
46
 *
47
 * Exclude nulls with the optional Null attribute (takes a boolean value).
48
 *
49
 * Inclusions can be defined with the NotLike and NotEquals attribute.  In a
50
 * list of <Exclusions> and associated <Exclude> tags, a column's
51
 * value must match at least one NotLike or NotEquals definition if any are
52
 * defined.
53
 *
54
 * Note that column exclusions are not actual SQL exclusions - so some
55
 * differences may apply, for instance in SQL an = comparison may be case
56
 * insensitive depending on the character encoding used on the table, and the
57
 * type of database being queried.
58
 *
59
 * @see Table::getExclusions
60
 * @see Column::getExclusions
61
 * @author Zaahid Bateson
62
 */
63
@XmlAccessorType(XmlAccessType.NONE)
64
@NoArgsConstructor
65
@Data
66
public class Exclude {
67
68
    /**
69
     * Sets both the equals value to an empty string, and excludeNulls to "true"
70
     * for the "IgnoreEmpty" shortcut.
71
     */
72
    Exclude(boolean ignoreEmpty) {
73
        if (ignoreEmpty) {
74
            equals = "";
75
            excludeNull = true;
76
        }
77
    }
78
79
    /**
80
     * The column name in the database
81
     */
82
    @XmlAttribute
83
    private String name;
84
85
    /**
86
     * The excluded value if the value should match with an SQL = comparison in
87
     * the WHERE clause
88
     */
89
    @XmlAttribute
90
    private String equals;
91
92
    /**
93
     * The excluded value if the value should match with an SQL LIKE comparison
94
     * in the WHERE clause
95
     */
96
    @XmlAttribute
97
    private String like;
98
99
    /**
100
     * The excluded value if the value should match with an SQL != comparison in
101
     * the WHERE clause.
102
     *
103
     * NotEquals values represent "inclusions", where one NotEquals value must
104
     * be matched in a series of listed <Exclusions> for the column's value to
105
     * be anonymized.
106
     */
107
    @XmlAttribute(name = "not-equals")
108
    private String notEquals;
109
110
    /**
111
     * The excluded value if the value should match with an SQL NOT LIKE
112
     * comparison in the WHERE clause.
113
     *
114
     * NotLike values represent "inclusions", where one NotEquals value must be
115
     * matched in a series of listed <Exclusions> for the column's value to be
116
     * anonymized.
117
     */
118
    @XmlAttribute(name = "not-like")
119
    private String notLike;
120
121
    /**
122
     * The excluded value includes nulls.
123
     */
124
    @XmlAttribute(name = "null")
125
    private boolean excludeNull;
126
127
    /**
128
     * Uses an "IN ()" clause to exclude a large range of values.
129
     */
130
    @XmlAttribute(name = "in")
131
    private String excludeIn;
132
133
    /**
134
     * Uses an "NOT IN ()" clause to exclude a large range of values.
135
     */
136
    @XmlAttribute(name = "not-in")
137
    private String excludeNotIn;
138
139
    /**
140
     * Separator String for In attribute, defaults to ",".
141
     */
142
    @XmlAttribute(name = "in-separator")
143
    private String inSeparator = ",";
144
145
    /**
146
     * Generates a List for "IN" exclusions using the set separator.
147
     * 
148
     * @return
149
     */
150
    public List<String> getExcludeInList() {
151
        if (StringUtils.isBlank(excludeIn)) {
152
            return List.of();
153
        }
154
        return Arrays.asList(
155
            StringUtils.splitByWholeSeparator(excludeIn, inSeparator)
156
        );
157
    }
158
159
    /**
160
     * Generates a List for "NOT IN" exclusions using the set separator.
161
     *
162
     * @return
163
     */
164
    public List<String> getExcludeNotInList() {
165
        if (StringUtils.isBlank(excludeNotIn)) {
166
            return List.of();
167
        }
168
        return Arrays.asList(
169
            StringUtils.splitByWholeSeparator(excludeNotIn, inSeparator)
170
        );
171
    }
172
}
173