little fluent API to shorten boild plate code

This commit is contained in:
Andreas Billmann
2015-12-17 21:32:52 +01:00
parent 5121b998c5
commit c9d13d4d58
2 changed files with 57 additions and 45 deletions

View File

@@ -26,23 +26,29 @@
package ninja.javafx.smartcsv.validation; package ninja.javafx.smartcsv.validation;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* Created by Andreas on 28.11.2015. * This class holds all the error messages
* for a single cell and the information in
* which row the cell is
*/ */
public class ValidationError { public class ValidationError {
private List<ValidationMessage> messages; private List<ValidationMessage> messages = new ArrayList<>();
private Integer lineNumber; private Integer lineNumber;
public ValidationError(List<ValidationMessage> messages) { private ValidationError(Integer lineNumber) {
this(messages, -1); this.lineNumber = lineNumber;
} }
public ValidationError(List<ValidationMessage> messages, Integer lineNumber) { public static ValidationError withLineNumber(int lineNumber) {
this.messages = messages; return new ValidationError(lineNumber);
this.lineNumber = lineNumber; }
public static ValidationError withoutLineNumber() {
return new ValidationError(-1);
} }
public Integer getLineNumber() { public Integer getLineNumber() {
@@ -53,4 +59,12 @@ public class ValidationError {
return messages; return messages;
} }
public ValidationError add(String key, String... parameters) {
messages.add(new ValidationMessage(key, parameters));
return this;
}
public boolean isEmpty() {
return messages.isEmpty();
}
} }

View File

@@ -32,12 +32,10 @@ import groovy.lang.GroovyShell;
import groovy.lang.Script; import groovy.lang.Script;
import org.codehaus.groovy.control.CompilationFailedException; import org.codehaus.groovy.control.CompilationFailedException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.Collections.singletonList;
import static org.apache.commons.validator.GenericValidator.*; import static org.apache.commons.validator.GenericValidator.*;
/** /**
@@ -86,20 +84,20 @@ public class Validator {
Config columnConfig = getColumnConfig(columnSectionConfig, column); Config columnConfig = getColumnConfig(columnSectionConfig, column);
if (columnConfig != null) { if (columnConfig != null) {
List<ValidationMessage> errorMessages = new ArrayList<>(); ValidationError error = ValidationError.withLineNumber(lineNumber);
checkBlankOrNull(columnConfig, value, errorMessages); checkBlankOrNull(columnConfig, value, error);
if (value != null) { if (value != null) {
checkRegularExpression(columnConfig, value, errorMessages); checkRegularExpression(columnConfig, value, error);
checkAlphaNumeric(columnConfig, value, errorMessages); checkAlphaNumeric(columnConfig, value, error);
checkDate(columnConfig, value, errorMessages); checkDate(columnConfig, value, error);
checkMaxLength(columnConfig, value, errorMessages); checkMaxLength(columnConfig, value, error);
checkMinLength(columnConfig, value, errorMessages); checkMinLength(columnConfig, value, error);
checkInteger(columnConfig, value, errorMessages); checkInteger(columnConfig, value, error);
checkGroovy(column, columnConfig, value, errorMessages); checkGroovy(column, columnConfig, value, error);
} }
if (!errorMessages.isEmpty()) { if (!error.isEmpty()) {
result = new ValidationError(errorMessages, lineNumber); result = error;
} }
} }
} }
@@ -112,7 +110,7 @@ public class Validator {
// private methods // private methods
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void checkGroovy(String column, Config columnConfig, String value, List<ValidationMessage> result) { private void checkGroovy(String column, Config columnConfig, String value, ValidationError error) {
String groovyScript = getString(columnConfig, "groovy"); String groovyScript = getString(columnConfig, "groovy");
if (groovyScript != null) { if (groovyScript != null) {
@@ -130,15 +128,15 @@ public class Validator {
try { try {
groovyResult = script.run(); groovyResult = script.run();
} catch (CompilationFailedException e) { } catch (CompilationFailedException e) {
result.add(new ValidationMessage("groovy script '"+groovyScript+"' throws exception: "+e.getMessage())); error.add("groovy script '"+groovyScript+"' throws exception: "+e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
if (groovyResult == null) { if (groovyResult == null) {
result.add(new ValidationMessage("groovy script '"+groovyScript+"' returns null")); error.add("groovy script '"+groovyScript+"' returns null");
} }
if (!isScriptResultTrue(groovyResult)) { if (!isScriptResultTrue(groovyResult)) {
result.add(new ValidationMessage(groovyResult.toString())); error.add(groovyResult.toString());
} }
} }
@@ -148,62 +146,62 @@ public class Validator {
return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true"); return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true");
} }
private void checkBlankOrNull(Config columnConfig, String value, List<ValidationMessage> result) { private void checkBlankOrNull(Config columnConfig, String value, ValidationError error) {
if (getBoolean(columnConfig, "not empty")) { if (getBoolean(columnConfig, "not empty")) {
if (isBlankOrNull(value)) { if (isBlankOrNull(value)) {
result.add(new ValidationMessage("validation.message.not.empty")); error.add("validation.message.not.empty");
} }
} }
} }
private void checkInteger(Config columnConfig, String value, List<ValidationMessage> result) { private void checkInteger(Config columnConfig, String value, ValidationError error) {
if (getBoolean(columnConfig, "integer")) { if (getBoolean(columnConfig, "integer")) {
if (!isInt(value)) { if (!isInt(value)) {
result.add(new ValidationMessage("validation.message.integer")); error.add("validation.message.integer");
} }
} }
} }
private void checkMinLength(Config columnConfig, String value, List<ValidationMessage> result) { private void checkMinLength(Config columnConfig, String value, ValidationError error) {
Integer minLength = getInteger(columnConfig, "minlength"); Integer minLength = getInteger(columnConfig, "minlength");
if (minLength != null) { if (minLength != null) {
if (!minLength(value, minLength)) { if (!minLength(value, minLength)) {
result.add(new ValidationMessage("has not min length of " + minLength)); error.add("has not min length of " + minLength);
} }
} }
} }
private void checkMaxLength(Config columnConfig, String value, List<ValidationMessage> result) { private void checkMaxLength(Config columnConfig, String value, ValidationError error) {
Integer maxLength = getInteger(columnConfig, "maxlength"); Integer maxLength = getInteger(columnConfig, "maxlength");
if (maxLength != null) { if (maxLength != null) {
if (!maxLength(value, maxLength)) { if (!maxLength(value, maxLength)) {
result.add(new ValidationMessage("has not max length of " + maxLength)); error.add("has not max length of " + maxLength);
} }
} }
} }
private void checkDate(Config columnConfig, String value, List<ValidationMessage> result) { private void checkDate(Config columnConfig, String value, ValidationError error) {
String dateformat = getString(columnConfig, "date"); String dateformat = getString(columnConfig, "date");
if (dateformat != null && !dateformat.trim().isEmpty()) { if (dateformat != null && !dateformat.trim().isEmpty()) {
if (!isDate(value, dateformat, true)) { if (!isDate(value, dateformat, true)) {
result.add(new ValidationMessage("is not a date of format " + dateformat)); error.add("is not a date of format " + dateformat);
} }
} }
} }
private void checkAlphaNumeric(Config columnConfig, String value, List<ValidationMessage> result) { private void checkAlphaNumeric(Config columnConfig, String value, ValidationError error) {
if (getBoolean(columnConfig, "alphanumeric")) { if (getBoolean(columnConfig, "alphanumeric")) {
if (!matchRegexp(value, "[0-9a-zA-Z]*")) { if (!matchRegexp(value, "[0-9a-zA-Z]*")) {
result.add(new ValidationMessage("validation.message.alphanumeric")); error.add("validation.message.alphanumeric");
} }
} }
} }
private void checkRegularExpression(Config columnConfig, String value, List<ValidationMessage> result) { private void checkRegularExpression(Config columnConfig, String value, ValidationError error) {
String regexp = getString(columnConfig, "regexp"); String regexp = getString(columnConfig, "regexp");
if (regexp != null && !regexp.trim().isEmpty()) { if (regexp != null && !regexp.trim().isEmpty()) {
if (!matchRegexp(value, regexp)) { if (!matchRegexp(value, regexp)) {
result.add(new ValidationMessage("does not match " + regexp)); error.add("does not match " + regexp);
} }
} }
} }
@@ -239,29 +237,29 @@ public class Validator {
List<String> headerConfig = headerSectionConfig.getStringList("list"); List<String> headerConfig = headerSectionConfig.getStringList("list");
if (headerConfig != null) { if (headerConfig != null) {
if (headerNames.length != headerConfig.size()) { if (headerNames.length != headerConfig.size()) {
result = new ValidationError(singletonList(new ValidationMessage("number of headers is not correct! there are " + result = ValidationError.withoutLineNumber().add("number of headers is not correct! there are " +
headerNames.length + headerNames.length +
" but there should be " + " but there should be " +
headerConfig.size()))); headerConfig.size());
return result; return result;
} }
List<ValidationMessage> errorMessages = new ArrayList<>(); ValidationError error = ValidationError.withoutLineNumber();
for(int i=0; i<headerConfig.size(); i++) { for(int i=0; i<headerConfig.size(); i++) {
String header = headerConfig.get(i); String header = headerConfig.get(i);
if (!header.equals(headerNames[i])) { if (!header.equals(headerNames[i])) {
errorMessages.add(new ValidationMessage("header number " + error.add("header number " +
i + i +
" does not match \"" + " does not match \"" +
header + header +
"\" should be \"" + "\" should be \"" +
headerNames[i] + headerNames[i] +
"\"")); "\"");
} }
} }
if (!errorMessages.isEmpty()) { if (!error.isEmpty()) {
result = new ValidationError(errorMessages); result = error;
} }
} }
} }