Files
SmartCSV.fx/src/main/java/ninja/javafx/smartcsv/validation/Validator.java

227 lines
8.5 KiB
Java
Raw Normal View History

2015-11-28 23:06:14 +01:00
/*
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package ninja.javafx.smartcsv.validation;
import java.util.HashMap;
2015-12-04 16:22:21 +01:00
import java.util.List;
2015-11-28 23:06:14 +01:00
import java.util.Map;
/**
* This class checks all the validations defined in the
* Config against a given value
*/
public class Validator {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// member variables
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private ValidationConfiguration validationConfig;
private Map<String, Map<Validation.Type, Validation>> columnValidationMap = new HashMap<>();
2015-11-28 23:06:14 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// constructors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* JSON configuration for this validator
*
2015-11-28 23:06:14 +01:00
* @param validationConfig
*/
public Validator(ValidationConfiguration validationConfig) {
this.validationConfig = validationConfig;
initColumnValidations();
2015-11-28 23:06:14 +01:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2016-07-22 20:45:26 +02:00
// public methods
2015-11-28 23:06:14 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public boolean needsColumnValidation(String column) {
Map<Validation.Type, Validation> validationMap = columnValidationMap.get(column);
if (validationMap != null) {
return validationMap.containsKey(Validation.Type.UNIQUE);
}
return false;
}
2015-11-28 23:06:14 +01:00
/**
* checks if the value is valid for the column configuration
*
2015-11-28 23:06:14 +01:00
* @param column the column name
* @param value the value to check
2015-12-07 22:41:59 +01:00
* @return ValidationError with information if valid and if not which getMessage happened
2015-11-28 23:06:14 +01:00
*/
public ValidationError isValid(Integer row, String column, String value) {
2015-12-07 22:41:59 +01:00
ValidationError result = null;
2016-07-22 20:45:26 +02:00
if (hasConfig()) {
ValidationError error = ValidationError.withLineNumber(row);
Map<Validation.Type, Validation> validationMap = columnValidationMap.get(column);
if (validationMap != null) {
for (Validation validation: validationMap.values()) {
if (validation.canBeChecked(value)) {
validation.check(row, value, error);
}
}
}
if (!error.isEmpty()) {
result = error;
2015-11-28 23:06:14 +01:00
}
}
return result;
}
2016-07-22 20:45:26 +02:00
public boolean hasConfig() {
return validationConfig != null;
}
2015-11-28 23:06:14 +01:00
public void reinitializeColumn(String column) {
clear(column);
initializeColumnWithRules(column);
}
2015-11-28 23:06:14 +01:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void add(String column, Validation validation) {
Map<Validation.Type, Validation> validationMap = columnValidationMap.get(column);
if (validationMap == null) {
validationMap = new HashMap<>();
columnValidationMap.put(column, validationMap);
}
validationMap.put(validation.getType(), validation);
}
private void clear(String column) {
Map<Validation.Type, Validation> validationMap = columnValidationMap.get(column);
if (validationMap != null) {
validationMap.clear();
}
}
private void initColumnValidations() {
if (hasConfig()) {
String[] columns = validationConfig.headerNames();
for (String column : columns) {
initializeColumnWithRules(column);
}
}
}
2015-11-28 23:06:14 +01:00
private void initializeColumnWithRules(String column) {
Boolean alphaNumeric = validationConfig.getAlphanumericRuleFor(column);
if (alphaNumeric != null && alphaNumeric) {
add(column, new AlphaNumericValidation());
}
2015-11-28 23:06:14 +01:00
Boolean doubleRule = validationConfig.getDoubleRuleFor(column);
if (doubleRule != null && doubleRule) {
add(column, new DoubleValidation());
}
2015-11-28 23:06:14 +01:00
Boolean integerRule = validationConfig.getIntegerRuleFor(column);
if (integerRule != null && integerRule) {
add(column, new IntegerValidation());
}
2015-11-28 23:06:14 +01:00
Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column);
if (notEmptyRule != null && notEmptyRule) {
add(column, new NotEmptyValidation());
}
2015-12-17 23:41:20 +01:00
Boolean uniqueRule = validationConfig.getUniqueRuleFor(column);
if (uniqueRule != null && uniqueRule) {
add(column, new UniqueValidation());
}
2015-11-28 23:06:14 +01:00
String dateRule = validationConfig.getDateRuleFor(column);
if (dateRule != null && !dateRule.trim().isEmpty()) {
add(column, new DateValidation(dateRule));
}
2015-11-28 23:06:14 +01:00
Integer minLength = validationConfig.getMinLengthRuleFor(column);
if (minLength != null) {
add(column, new MinLengthValidation(minLength));
}
2015-12-17 23:55:08 +01:00
Integer maxLength = validationConfig.getMaxLengthRuleFor(column);
if (maxLength != null) {
add(column, new MaxLengthValidation(maxLength));
}
2015-11-28 23:06:14 +01:00
String regexp = validationConfig.getRegexpRuleFor(column);
if (regexp != null && !regexp.trim().isEmpty()) {
add(column, new RegExpValidation(regexp));
}
String groovy = validationConfig.getGroovyRuleFor(column);
if (groovy != null && !groovy.trim().isEmpty()) {
add(column, new GroovyValidation(groovy));
2015-11-28 23:06:14 +01:00
}
List<String> valueOfRule = validationConfig.getValueOfRuleFor(column);
if (valueOfRule != null && !valueOfRule.isEmpty()) {
add(column, new ValueOfValidation(valueOfRule));
2015-11-28 23:06:14 +01:00
}
}
2015-12-07 22:41:59 +01:00
public ValidationError isHeaderValid(String[] headerNames) {
ValidationError result = null;
2015-12-04 16:22:21 +01:00
if (validationConfig != null) {
String[] headerNamesConfig = validationConfig.headerNames();
if (headerNamesConfig != null) {
if (headerNames.length != headerNamesConfig.length) {
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
Integer.toString(headerNames.length),
Integer.toString(headerNamesConfig.length));
return result;
}
2015-12-04 16:22:21 +01:00
ValidationError error = ValidationError.withoutLineNumber();
2015-12-07 22:41:59 +01:00
for (int i = 0; i < headerNamesConfig.length; i++) {
if (!headerNamesConfig[i].equals(headerNames[i])) {
error.add("validation.message.header.match",
Integer.toString(i),
headerNamesConfig[i],
headerNames[i]);
2015-12-17 23:41:20 +01:00
}
2015-12-04 16:22:21 +01:00
}
if (!error.isEmpty()) {
result = error;
}
2015-12-04 16:22:21 +01:00
}
}
return result;
}
2015-11-28 23:06:14 +01:00
}