|
1
|
|
|
/* |
|
2
|
|
|
* |
|
3
|
|
|
* Copyright 2014, Armenak Grigoryan, Matthew Eaton, and individual contributors as indicated |
|
4
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
|
5
|
|
|
* full listing of individual contributors. |
|
6
|
|
|
* |
|
7
|
|
|
* This is free software; you can redistribute it and/or modify it |
|
8
|
|
|
* under the terms of the GNU Lesser General Public License as |
|
9
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
|
10
|
|
|
* the License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This software is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15
|
|
|
* Lesser General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
package com.strider.datadefender.extractor; |
|
19
|
|
|
|
|
20
|
|
|
import com.strider.datadefender.DbConfig; |
|
21
|
|
|
import com.strider.datadefender.database.DatabaseException; |
|
22
|
|
|
import com.strider.datadefender.database.IDbFactory; |
|
23
|
|
|
|
|
24
|
|
|
import java.io.BufferedWriter; |
|
25
|
|
|
import java.io.File; |
|
26
|
|
|
import java.io.FileWriter; |
|
27
|
|
|
import java.io.IOException; |
|
28
|
|
|
import java.sql.ResultSet; |
|
29
|
|
|
import java.sql.SQLException; |
|
30
|
|
|
import java.sql.Statement; |
|
31
|
|
|
import java.util.List; |
|
32
|
|
|
|
|
33
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
34
|
|
|
|
|
35
|
|
|
import lombok.extern.log4j.Log4j2; |
|
36
|
|
|
import me.tongfei.progressbar.ProgressBar; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Entry point for RDBMS data generator |
|
40
|
|
|
* |
|
41
|
|
|
* @author Matt Eaton |
|
42
|
|
|
*/ |
|
43
|
|
|
@Log4j2 |
|
44
|
|
|
public class DataExtractor implements IExtractor { |
|
45
|
|
|
|
|
46
|
|
|
final IDbFactory dbFactory; |
|
47
|
|
|
final DbConfig config; |
|
48
|
|
|
final List<String> tableColumns; |
|
49
|
|
|
|
|
50
|
|
|
public DataExtractor(IDbFactory dbFactory, DbConfig config, List<String> tableColumns) { |
|
51
|
|
|
this.dbFactory = dbFactory; |
|
52
|
|
|
this.config = config; |
|
53
|
|
|
this.tableColumns = tableColumns; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Rename current data set file to .bak-N, where N is an incremented integer |
|
58
|
|
|
* @param file Path and name of file to rename |
|
59
|
|
|
* @return true if renamed successfully, false otherwise |
|
60
|
|
|
*/ |
|
61
|
|
|
private boolean backupExistingFile(final String file) { |
|
62
|
|
|
|
|
63
|
|
|
final File sourceFile = new File(file); |
|
64
|
|
|
|
|
65
|
|
|
if (!sourceFile.exists()) { |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
int count = 1; |
|
70
|
|
|
while (new File(file + ".bak-" + count).exists()) { |
|
71
|
|
|
count++; |
|
72
|
|
|
} |
|
73
|
|
|
return sourceFile.renameTo(new File(file + ".bak-" + count)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Generate data to be used by anoymizer. |
|
78
|
|
|
* @param dbFactory |
|
79
|
|
|
* @param Requirement requirement file |
|
80
|
|
|
* @throws com.strider.datadefender.database.DatabaseException |
|
81
|
|
|
*/ |
|
82
|
|
|
@Override |
|
83
|
|
|
public void extract() throws DatabaseException { |
|
84
|
|
|
|
|
85
|
|
|
// Iterate over the requirement and generate data sets |
|
86
|
|
|
for (String tableColumn : tableColumns) { |
|
87
|
|
|
|
|
88
|
|
|
String table = StringUtils.substringBefore(tableColumn, "."); |
|
89
|
|
|
String column = StringUtils.substringAfter(tableColumn, "."); |
|
90
|
|
|
String fileName = table + "_" + column + ".txt"; |
|
91
|
|
|
|
|
92
|
|
|
log.info("Table column [{}] into [{}]. Start ...", tableColumn, fileName); |
|
93
|
|
|
|
|
94
|
|
|
// Backup existing data set file |
|
95
|
|
|
if (!backupExistingFile(fileName)) { |
|
96
|
|
|
throw new DatabaseException( |
|
97
|
|
|
"Unable to rename existing data set file " |
|
98
|
|
|
+ fileName |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
int total = 0; |
|
103
|
|
|
String count = "SELECT COUNT(DISTINCT " + column + ") FROM " + table; |
|
104
|
|
|
try (Statement stmt = dbFactory.getConnection().createStatement(); |
|
105
|
|
|
ResultSet rs = stmt.executeQuery(count);) { |
|
106
|
|
|
if (rs.next()) { |
|
107
|
|
|
total = rs.getInt(1); |
|
108
|
|
|
} |
|
109
|
|
|
} catch (SQLException e) { |
|
110
|
|
|
throw new DatabaseException(e.toString(), e); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
String query = "SELECT DISTINCT " + column + " FROM " + table; |
|
114
|
|
|
try ( |
|
115
|
|
|
ProgressBar pb = new ProgressBar("Extracting " + tableColumn + "...", total); |
|
116
|
|
|
Statement stmt = dbFactory.getConnection().createStatement(); |
|
117
|
|
|
ResultSet rs = stmt.executeQuery(query); |
|
118
|
|
|
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); |
|
119
|
|
|
) { |
|
120
|
|
|
// Write each column value to data set file |
|
121
|
|
|
while (rs.next()) { |
|
122
|
|
|
String col = rs.getString(1); |
|
123
|
|
|
if (StringUtils.isNotBlank(col)) { |
|
124
|
|
|
bw.write(col); |
|
125
|
|
|
bw.newLine(); |
|
126
|
|
|
} |
|
127
|
|
|
pb.step(); |
|
128
|
|
|
} |
|
129
|
|
|
} catch (IOException|SQLException e) { |
|
130
|
|
|
throw new DatabaseException(e.toString(), e); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
log.info("Table column {}. End ...", tableColumn); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|