AutoresolvePackage(ClassAndFunctionRegistry)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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
 */
17
package com.strider.datadefender.requirement;
18
19
import com.strider.datadefender.requirement.plan.GlobalPlan;
20
import com.strider.datadefender.requirement.registry.ClassAndFunctionRegistry;
21
22
import java.util.List;
23
import java.util.stream.Collectors;
24
import javax.xml.bind.Unmarshaller;
25
26
import javax.xml.bind.annotation.XmlAccessType;
27
import javax.xml.bind.annotation.XmlAccessorType;
28
import javax.xml.bind.annotation.XmlAttribute;
29
import javax.xml.bind.annotation.XmlElement;
30
import javax.xml.bind.annotation.XmlElementWrapper;
31
import javax.xml.bind.annotation.XmlRootElement;
32
33
import org.apache.commons.collections4.CollectionUtils;
34
import org.apache.commons.lang3.StringUtils;
35
36
import lombok.AccessLevel;
37
import lombok.Data;
38
import lombok.Getter;
39
import lombok.Setter;
40
import lombok.extern.log4j.Log4j2;
41
42
/**
43
 * JAXB class that defines elements in Requirement.xml file
44
 *
45
 * @author Armenak Grigoryan
46
 */
47
@Data
48
@Log4j2
49
@XmlAccessorType(XmlAccessType.NONE)
50
@XmlRootElement(name = "anonymizer")
51
public class Requirement {
52
53
    @XmlAccessorType(XmlAccessType.NONE)
54
    @Data
55
    public static class AutoresolvePackage {
56
        @XmlAttribute
57
        private String name;
58
59
        @Getter(AccessLevel.NONE)
60
        @Setter(AccessLevel.NONE)
61
        private ClassAndFunctionRegistry registry;
62
63
        public AutoresolvePackage() {
64
            this(ClassAndFunctionRegistry.singleton());
65
        }
66
67
        public AutoresolvePackage(String name) {
68
            this();
69
            this.name = name;
70
        }
71
72
        public AutoresolvePackage(String name, boolean register) {
73
            this(name);
74
            if (register) {
75
                registry.registerAutoResolvePackage(name);
76
            }
77
        }
78
79
        public AutoresolvePackage(ClassAndFunctionRegistry registry) {
80
            this.registry = registry;
81
        }
82
83
        public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
84
            log.debug("Found autoresolve package: {}", name);
85
            registry.registerAutoResolvePackage(name);
86
        }
87
    }
88
89
    @XmlElement(name = "anonymizer-version")
90
    private double anonymizerVersion = 2.0d;
91
92
    @XmlElement
93
    private String project;
94
95
    @XmlElement(name = "project-version")
96
    private String version;
97
98
    @XmlElementWrapper(name = "autoresolve-classes")
99
    @XmlElement(name = "package")
100
    private List<AutoresolvePackage> autoresolve;
101
102
    @XmlElementWrapper(name = "column-plans")
103
    @XmlElement(name = "plan")
104
    private List<GlobalPlan> plans;
105
106
    @XmlElementWrapper(name = "tables")
107
    @XmlElement(name = "table")
108
    private List<Table> tables;
109
110
    /**
111
     * Returns a list of Table elements that match entries in the passed filter.
112
     * Attempts to filter out differences with schema, so 'schema.tablename'  matches 'tablename'.
113
     *
114
     * @param filter
115
     * @return
116
     */
117
    public List<Table> getFilteredTables(List<String> filter) {
118
        if (CollectionUtils.isEmpty(filter)) {
119
            return tables;
120
        }
121
        return tables.stream().filter((req) ->
122
            filter.stream().anyMatch((s) -> {
123
                String r = req.getName();
124
                if (s.equalsIgnoreCase(r)) {
125
                    return true;
126
                } else if (s.contains(".") && !r.contains(".")) {
127
                    return StringUtils.endsWithIgnoreCase(s, "." + r);
128
                } else if (r.contains(".") && !s.contains(".")) {
129
                    return StringUtils.endsWithIgnoreCase(r, "." + s);
130
                }
131
                return false;
132
            })
133
        ).collect(Collectors.toList());
134
    }
135
}
136