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.extractor.IExtractor; |
19
|
|
|
import com.strider.datadefender.extractor.DataExtractor; |
20
|
|
|
import com.strider.datadefender.database.IDbFactory; |
21
|
|
|
|
22
|
|
|
import java.util.concurrent.Callable; |
23
|
|
|
import java.util.List; |
24
|
|
|
|
25
|
|
|
import org.apache.commons.lang3.StringUtils; |
26
|
|
|
|
27
|
|
|
import picocli.CommandLine.ArgGroup; |
28
|
|
|
import picocli.CommandLine.Command; |
29
|
|
|
import picocli.CommandLine.Model.CommandSpec; |
30
|
|
|
import picocli.CommandLine.ParameterException; |
31
|
|
|
import picocli.CommandLine.Parameters; |
32
|
|
|
import picocli.CommandLine.Spec; |
33
|
|
|
|
34
|
|
|
import lombok.extern.log4j.Log4j2; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* "extract" picocli subcommand, configures and executes the data extractor. |
38
|
|
|
* |
39
|
|
|
* @author Zaahid Bateson |
40
|
|
|
*/ |
41
|
|
|
@Command( |
42
|
|
|
name = "extract", |
43
|
|
|
version = "1.0", |
44
|
|
|
mixinStandardHelpOptions = true, |
45
|
|
|
description = "Run data extraction utility -- generates files out of table " |
46
|
|
|
+ "columns with the name 'table_columnName.txt' for each column " |
47
|
|
|
+ "requested." |
48
|
|
|
) |
49
|
|
|
@Log4j2 |
50
|
|
|
public class Extract implements Callable<Integer> { |
51
|
|
|
|
52
|
|
|
@ArgGroup(exclusive = false, multiplicity = "1", heading = "Database connection settings%n") |
53
|
|
|
private DbConfig dbConfig; |
54
|
|
|
|
55
|
|
|
@Spec |
56
|
|
|
private CommandSpec spec; |
57
|
|
|
|
58
|
|
|
private List<String> tableColumns; |
59
|
|
|
|
60
|
|
|
@Parameters(paramLabel = "columns", description = "Generate data for the specified table.columName(s)") |
61
|
|
|
public void setTableColumns(List<String> tableColumns) { |
62
|
|
|
for (String tableColumn : tableColumns) { |
63
|
|
|
int loc = tableColumn.indexOf("."); |
64
|
|
|
if (loc < 1 || loc >= tableColumn.length() - 1) { |
65
|
|
|
throw new ParameterException( |
66
|
|
|
spec.commandLine(), |
67
|
|
|
String.format( |
68
|
|
|
"Columns must be specified in the form [table].[columnName], found: %s", |
69
|
|
|
tableColumn |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
this.tableColumns = tableColumns; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
@Override |
78
|
|
|
public Integer call() throws Exception { |
79
|
|
|
System.out.println(""); |
80
|
|
|
System.out.println("Starting data extractor"); |
81
|
|
|
log.info("Datasource URL: {}, vendor: {}, schema: {}", dbConfig.getUrl(), dbConfig.getVendor(), dbConfig.getSchema()); |
82
|
|
|
log.info("Username: {}, Password provided: {}", dbConfig.getUsername(), (StringUtils.isNotBlank(dbConfig.getPassword()) ? "yes" : "no")); |
83
|
|
|
log.info("Extracting data from: {}", tableColumns); |
84
|
|
|
|
85
|
|
|
IDbFactory factory = IDbFactory.get(dbConfig); |
86
|
|
|
|
87
|
|
|
final IExtractor extractor = new DataExtractor( |
88
|
|
|
factory, |
89
|
|
|
dbConfig, |
90
|
|
|
tableColumns |
91
|
|
|
); |
92
|
|
|
try { |
93
|
|
|
extractor.extract(); |
94
|
|
|
} catch (DataDefenderException e) { |
95
|
|
|
log.error(e.getMessage()); |
96
|
|
|
log.debug("Exception occurred during data extraction", e); |
97
|
|
|
return 1; |
98
|
|
|
} catch (Throwable e) { |
99
|
|
|
log.error(e.getMessage(), e); |
100
|
|
|
return 1; |
101
|
|
|
} |
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|