| Conditions | 13 |
| Total Lines | 124 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 182 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like com.strider.datadefender.discoverer.DatabaseDiscoverer.discover() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* |
||
| 122 | @SuppressWarnings("unchecked") |
||
| 123 | public List<ColumnMetaData> discover() |
||
| 124 | throws ParseException, |
||
| 125 | DataDefenderException, |
||
| 126 | IOException, |
||
| 127 | SQLException { |
||
| 128 | |||
| 129 | List<ColumnMatch> finalList = new ArrayList<>(); |
||
| 130 | |||
| 131 | try (ProgressBar pb = new ProgressBar( |
||
| 132 | "Discovering by model...", |
||
| 133 | CollectionUtils.size(config.getModels()) + CollectionUtils.size(config.getFileModels()) |
||
| 134 | )) { |
||
| 135 | for (final String sm : CollectionUtils.emptyIfNull(config.getModels())) { |
||
| 136 | log.info("********************************"); |
||
| 137 | log.info("Processing model " + sm); |
||
| 138 | log.info("********************************"); |
||
| 139 | pb.setExtraMessage("Model: " + sm); |
||
| 140 | |||
| 141 | final Model model = createModel(sm); |
||
| 142 | matches = discoverAgainstSingleModel(model); |
||
| 143 | finalList = ListUtils.union(finalList, matches); |
||
| 144 | pb.step(); |
||
| 145 | } |
||
| 146 | for (final File fm : CollectionUtils.emptyIfNull(config.getFileModels())) { |
||
| 147 | log.info("********************************"); |
||
| 148 | log.info("Processing model " + fm); |
||
| 149 | log.info("********************************"); |
||
| 150 | pb.setExtraMessage("Model: " + fm.getName()); |
||
| 151 | |||
| 152 | final Model model = createModel(fm); |
||
| 153 | matches = discoverAgainstSingleModel(model); |
||
| 154 | finalList = ListUtils.union(finalList, matches); |
||
| 155 | pb.step(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | log.info("List of suspects:"); |
||
| 160 | |||
| 161 | final Score score = new Score(); |
||
| 162 | int highRiskColumns = 0; |
||
| 163 | int rowCount = 0; |
||
| 164 | |||
| 165 | for (final ColumnMatch match : finalList) { |
||
| 166 | |||
| 167 | ColumnMetaData column = match.getColumn(); |
||
| 168 | // Row count |
||
| 169 | if (config.getCalculateScore()) { |
||
| 170 | log.debug("Counting number of rows ..."); |
||
| 171 | rowCount = ReportUtil.rowCount(factory, |
||
| 172 | column.getTable().getTableName()); |
||
| 173 | } else { |
||
| 174 | log.debug("Skipping counting number of rows ..."); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Getting 5 sample values |
||
| 178 | final List<String> sampleDataList = ReportUtil.sampleData(factory, column); |
||
| 179 | // Output |
||
| 180 | log.info("Column : " + column.toString()); |
||
| 181 | log.info(StringUtils.repeat('=', column.toString().length() + 30)); |
||
| 182 | log.info("Model : " + match.getModel()); |
||
| 183 | log.info("Number of rows in the table : " + rowCount); |
||
| 184 | |||
| 185 | if (config.getCalculateScore()) { |
||
| 186 | log.info("Score : " + score.columnScore(rowCount)); |
||
| 187 | } else { |
||
| 188 | log.info("Score : N/A"); |
||
| 189 | } |
||
| 190 | |||
| 191 | log.info("Sample data"); |
||
| 192 | log.info(StringUtils.repeat('-', 11)); |
||
| 193 | |||
| 194 | sampleDataList.forEach((sampleData) -> { |
||
| 195 | log.info(sampleData); |
||
| 196 | }); |
||
| 197 | |||
| 198 | log.info(""); |
||
| 199 | |||
| 200 | // Score calculation is evaluated with score_calculation parameter |
||
| 201 | if (config.getCalculateScore() && score.columnScore(rowCount).equals("High")) { |
||
| 202 | highRiskColumns++; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | // Only applicable when parameter table_rowcount=yes otherwise score calculation should not be done |
||
| 207 | if (config.getCalculateScore()) { |
||
| 208 | log.info("Overall score: " + score.dataStoreScore()); |
||
| 209 | log.info(""); |
||
| 210 | |||
| 211 | if ((finalList != null) && (finalList.size() > 0)) { |
||
| 212 | log.info("============================================"); |
||
| 213 | |||
| 214 | if (finalList.size() > config.getThresholdCount()) { |
||
| 215 | log.info( |
||
| 216 | "Number of PI [{}] columns is higher than defined threashold [{}]", |
||
| 217 | finalList.size(), |
||
| 218 | config.getThresholdCount() |
||
| 219 | ); |
||
| 220 | } else { |
||
| 221 | log.info( |
||
| 222 | "Number of PI [{}] columns is lower than or equal to defined threashold [{}]", |
||
| 223 | finalList.size(), |
||
| 224 | config.getThresholdCount() |
||
| 225 | ); |
||
| 226 | } |
||
| 227 | if (highRiskColumns > config.getThresholdHighRisk()) { |
||
| 228 | log.info( |
||
| 229 | "Number of High risk PI [{}] columns is higher than defined threashold [{}]", |
||
| 230 | highRiskColumns, |
||
| 231 | config.getThresholdHighRisk() |
||
| 232 | ); |
||
| 233 | } else { |
||
| 234 | log.info( |
||
| 235 | "Number of High risk PI [{}] columns is lower than or equal to defined threashold [{}]", |
||
| 236 | highRiskColumns, |
||
| 237 | config.getThresholdHighRisk() |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | log.info("Overall score: N/A"); |
||
| 243 | } |
||
| 244 | |||
| 245 | return matches.stream().map((c) -> c.getColumn()).collect(Collectors.toList()); |
||
| 246 | } |
||
| 428 |