1
|
|
|
/* |
2
|
|
|
* |
3
|
|
|
* Copyright 2014-1018, Armenak Grigoryan, 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
|
|
|
package com.strider.datadefender.discoverer; |
18
|
|
|
|
19
|
|
|
import com.strider.datadefender.DataDefenderException; |
20
|
|
|
import com.strider.datadefender.database.metadata.IMetaData; |
21
|
|
|
import com.strider.datadefender.database.metadata.TableMetaData; |
22
|
|
|
import com.strider.datadefender.report.ReportUtil; |
23
|
|
|
import com.strider.datadefender.utils.Score; |
24
|
|
|
import com.strider.datadefender.database.IDbFactory; |
25
|
|
|
import com.strider.datadefender.database.metadata.TableMetaData.ColumnMetaData; |
26
|
|
|
|
27
|
|
|
import java.io.IOException; |
28
|
|
|
import java.sql.SQLException; |
29
|
|
|
import java.util.ArrayList; |
30
|
|
|
import java.util.LinkedHashSet; |
31
|
|
|
import java.util.List; |
32
|
|
|
import java.util.regex.Pattern; |
33
|
|
|
import java.util.stream.Collectors; |
34
|
|
|
|
35
|
|
|
import org.apache.commons.collections.CollectionUtils; |
36
|
|
|
import org.apache.commons.lang3.StringUtils; |
37
|
|
|
|
38
|
|
|
import lombok.extern.log4j.Log4j2; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @author Armenak Grigoryan |
42
|
|
|
*/ |
43
|
|
|
@Log4j2 |
44
|
|
|
public class ColumnDiscoverer { |
45
|
|
|
|
46
|
|
|
private IDbFactory factory; |
47
|
|
|
private List<Pattern> patterns; |
48
|
|
|
|
49
|
|
|
public ColumnDiscoverer(IDbFactory factory, List<Pattern> patterns) { |
50
|
|
|
this.factory = factory; |
51
|
|
|
this.patterns = patterns; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public List<ColumnMetaData> discover() throws DataDefenderException, IOException, SQLException { |
55
|
|
|
log.info("Column discovery in process"); |
56
|
|
|
|
57
|
|
|
final IMetaData metaData = factory.fetchMetaData(); |
58
|
|
|
final List<TableMetaData> list = metaData.getMetaData(); |
59
|
|
|
|
60
|
|
|
List<ColumnMetaData> columns = list.stream().flatMap((t) -> t.getColumns().stream()) |
61
|
|
|
.filter((c) -> patterns.stream().anyMatch((p) -> p.matcher(c.getColumnName().toLowerCase()).matches())) |
62
|
|
|
.collect(Collectors.toList()); |
63
|
|
|
|
64
|
|
|
log.info("Preparing report ..."); |
65
|
|
|
|
66
|
|
|
// Report column names |
67
|
|
|
List<ColumnMetaData> uniqueMatches = null; |
68
|
|
|
if (CollectionUtils.isNotEmpty(columns)) { |
69
|
|
|
uniqueMatches = new ArrayList<>(new LinkedHashSet<>(columns)); |
70
|
|
|
log.info("-----------------"); |
71
|
|
|
log.info("List of suspects:"); |
72
|
|
|
log.info("-----------------"); |
73
|
|
|
uniqueMatches.sort((a, b) -> a.compareTo(b)); |
74
|
|
|
|
75
|
|
|
final Score score = new Score(); |
76
|
|
|
|
77
|
|
|
for (final ColumnMetaData entry : uniqueMatches) { |
78
|
|
|
|
79
|
|
|
// Row count |
80
|
|
|
final int rowCount = ReportUtil.rowCount(factory, entry.getTable().getTableName()); |
81
|
|
|
|
82
|
|
|
// Getting 5 sample values |
83
|
|
|
final List<String> sampleDataList = ReportUtil.sampleData(factory, entry); |
84
|
|
|
|
85
|
|
|
// Output |
86
|
|
|
log.info("Column : " + entry); |
87
|
|
|
log.info(StringUtils.repeat('=', entry.toString().length() + 30)); |
88
|
|
|
log.info("Number of rows in the table: " + rowCount); |
89
|
|
|
log.info("Score : " + score.columnScore(rowCount)); |
90
|
|
|
log.info("Sample data"); |
91
|
|
|
log.info(StringUtils.repeat('-', 11)); |
92
|
|
|
|
93
|
|
|
for (final String sampleData : sampleDataList) { |
94
|
|
|
log.info(sampleData); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
log.info(""); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
log.info("Overall score: " + score.dataStoreScore()); |
101
|
|
|
} else { |
102
|
|
|
log.info("No suspects have been found. Please refine your criteria."); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return uniqueMatches; |
106
|
|
|
} |
107
|
|
|
} |