com.strider.datadefender.DiscoverColumns   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 63
ccs 0
cts 20
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 28 4
A getColumnMetaData() 0 3 1
A setOutputFile(File) 0 3 1
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;
17
18
import com.strider.datadefender.database.IDbFactory;
19
import com.strider.datadefender.database.metadata.TableMetaData.ColumnMetaData;
20
import com.strider.datadefender.discoverer.ColumnDiscoverer;
21
import java.io.File;
22
23
import java.util.List;
24
import java.util.concurrent.Callable;
25
import java.util.regex.Pattern;
26
27
import picocli.CommandLine.ArgGroup;
28
import picocli.CommandLine.Command;
29
import picocli.CommandLine.Model.CommandSpec;
30
import picocli.CommandLine.Option;
31
import picocli.CommandLine.ParentCommand;
32
import picocli.CommandLine.Spec;
33
34
import lombok.extern.log4j.Log4j2;
35
import org.apache.commons.lang3.StringUtils;
36
37
38
/**
39
 * "discover" picocli subcommand, configures and executes the data discoverer.
40
 *
41
 * @author Zaahid Bateson
42
 */
43
@Command(
44
    name = "columns",
45
    version = "1.0",
46
    mixinStandardHelpOptions = true,
47
    description = "Run column discovery utility"
48
)
49
@Log4j2
50
public class DiscoverColumns implements Callable<Integer>, IRequirementCommand {
51
52
    private File outputFile;
53
54
    @Option(names = { "--column-pattern" }, description = "Regex pattern(s) to match column names", required = true)
55
    private List<Pattern> patterns;
56
57
    @ArgGroup(exclusive = false, multiplicity = "0..1", heading = "Database connection settings%n")
58
    private DbConfig dbConfig;
59
60
    @ParentCommand
61
    private Discover discover;
62
63
    @Spec
64
    private CommandSpec spec;
65
66
    private List<ColumnMetaData> results;
67
68
    @Option(names = { "-o", "--output" }, description = "Generate a requirements xml file and write it out to the specified file")
69
    public void setOutputFile(File f) {
70
        discover.setOutputFile(f);
71
    }
72
73
    @Override
74
    public Integer call() throws Exception {
75
76
        if (dbConfig == null) {
77
            dbConfig = discover.getDbConfig();
78
        } else {
79
            discover.setDbConfig(dbConfig);
80
        }
81
82
        System.out.println("Starting column discovery");
83
        log.warn("Discovery writes personal data to log files.");
84
        log.info("Datasource URL: {}, vendor: {}, schema: {}", dbConfig.getUrl(), dbConfig.getVendor(), dbConfig.getSchema());
85
        log.info("Username: {}, Password provided: {}", dbConfig.getUsername(), (StringUtils.isNotBlank(dbConfig.getPassword()) ? "yes" : "no"));
86
        
87
        IDbFactory factory = IDbFactory.get(dbConfig);
88
        ColumnDiscoverer discoverer = new ColumnDiscoverer(factory, patterns);
89
90
        try {
91
            results = discoverer.discover();
92
        } catch (Exception e) {
93
            log.error(e.getMessage());
94
            log.debug("Exception occurred during anonymization", e);
95
            return 1;
96
        }
97
98
        discover.afterSubcommand();
99
100
        return 0;
101
    }
102
103
    @Override
104
    public List<ColumnMetaData> getColumnMetaData() {
105
        return results;
106
    }
107
}
108