mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
validation rules are now editable through context menu
This commit is contained in:
@@ -50,54 +50,107 @@ public class ValidationConfiguration {
|
||||
return headerConfiguration.getNames();
|
||||
}
|
||||
|
||||
public Boolean integerRuleFor(String column) {
|
||||
if (noRulesFor(column)) return FALSE;
|
||||
return defaultValue((Boolean)columnConfigurations.get(column).get("integer"), FALSE);
|
||||
public Boolean getIntegerRuleFor(String column) {
|
||||
return (Boolean)getValue(column, "integer");
|
||||
}
|
||||
|
||||
public Boolean doubleRuleFor(String column) {
|
||||
if (noRulesFor(column)) return FALSE;
|
||||
return defaultValue((Boolean)columnConfigurations.get(column).get("double"), FALSE);
|
||||
public Boolean getDoubleRuleFor(String column) {
|
||||
return (Boolean)getValue(column, "double");
|
||||
}
|
||||
|
||||
public Boolean notEmptyRuleFor(String column) {
|
||||
if (noRulesFor(column)) return FALSE;
|
||||
return defaultValue((Boolean)columnConfigurations.get(column).get("not empty"), FALSE);
|
||||
public Boolean getNotEmptyRuleFor(String column) {
|
||||
return (Boolean)getValue(column, "not empty");
|
||||
}
|
||||
|
||||
public Integer minLengthRuleFor(String column) {
|
||||
public Integer getMinLengthRuleFor(String column) {
|
||||
return doubleToInteger((Double)getValue(column, "minlength"));
|
||||
}
|
||||
|
||||
public Integer getMaxLengthRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return doubleToInteger((Double)columnConfigurations.get(column).get("minlength"));
|
||||
return doubleToInteger((Double)getValue(column, "maxlength"));
|
||||
}
|
||||
|
||||
public Integer maxLengthRuleFor(String column) {
|
||||
public String getDateRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return doubleToInteger((Double)columnConfigurations.get(column).get("maxlength"));
|
||||
return (String)getValue(column, "date");
|
||||
}
|
||||
|
||||
public String dateRuleFor(String column) {
|
||||
public Boolean getAlphanumericRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)columnConfigurations.get(column).get("date");
|
||||
return (Boolean)getValue(column, "alphanumeric");
|
||||
}
|
||||
|
||||
public Boolean alphanumericRuleFor(String column) {
|
||||
if (noRulesFor(column)) return FALSE;
|
||||
return defaultValue((Boolean)columnConfigurations.get(column).get("alphanumeric"), FALSE);
|
||||
}
|
||||
|
||||
public String regexpRuleFor(String column) {
|
||||
public String getRegexpRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)columnConfigurations.get(column).get("regexp");
|
||||
return (String)getValue(column, "regexp");
|
||||
}
|
||||
|
||||
public List<String> valueOfRuleFor(String column) {
|
||||
public List<String> getValueOfRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (List<String>)columnConfigurations.get(column).get("value of");
|
||||
return (List<String>)getValue(column, "value of");
|
||||
}
|
||||
|
||||
public String groovyRuleFor(String column) {
|
||||
public String getGroovyRuleFor(String column) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return (String)columnConfigurations.get(column).get("groovy");
|
||||
return (String)getValue(column, "groovy");
|
||||
}
|
||||
|
||||
public void setIntegerRuleFor(String column, Boolean value) {
|
||||
setValue(column, value, "integer");
|
||||
}
|
||||
|
||||
public void setDoubleRuleFor(String column, Boolean value) {
|
||||
setValue(column, value, "double");
|
||||
}
|
||||
|
||||
public void setNotEmptyRuleFor(String column, Boolean value) {
|
||||
setValue(column, value, "not empty");
|
||||
}
|
||||
|
||||
public void setMinLengthRuleFor(String column, Integer value) {
|
||||
setValue(column, value, "minlength");
|
||||
}
|
||||
|
||||
public void setMaxLengthRuleFor(String column, Integer value) {
|
||||
setValue(column, value, "maxlength");
|
||||
}
|
||||
|
||||
public void setDateRuleFor(String column, String value) {
|
||||
setValue(column, value, "date");
|
||||
}
|
||||
|
||||
public void setAlphanumericRuleFor(String column, Boolean value) {
|
||||
setValue(column, value, "alphanumeric");
|
||||
}
|
||||
|
||||
public void setRegexpRuleFor(String column, String value) {
|
||||
setValue(column, value, "regexp");
|
||||
}
|
||||
|
||||
public void setValueOfRuleFor(String column, List<String> value) {
|
||||
setValue(column, value, "value of");
|
||||
}
|
||||
|
||||
public void setGroovyRuleFor(String column, String value) {
|
||||
setValue(column, value, "groovy");
|
||||
}
|
||||
|
||||
private void setValue(String column, Object value, String key) {
|
||||
if (!columnConfigurations.containsKey(column)) {
|
||||
columnConfigurations.put(column, new HashMap<>());
|
||||
}
|
||||
|
||||
if (value == null && columnConfigurations.get(column).containsKey(key)) {
|
||||
columnConfigurations.get(column).remove(key);
|
||||
} else {
|
||||
columnConfigurations.get(column).put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private Object getValue(String column, String key) {
|
||||
if (noRulesFor(column)) return null;
|
||||
return columnConfigurations.get(column).get(key);
|
||||
}
|
||||
|
||||
private boolean noHeader() {
|
||||
@@ -108,11 +161,6 @@ public class ValidationConfiguration {
|
||||
return columnConfigurations == null || columnConfigurations.get(column) == null;
|
||||
}
|
||||
|
||||
private <T> T defaultValue(T value, T defaultValue) {
|
||||
if (value == null) return defaultValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
private Integer doubleToInteger(Double value) {
|
||||
if (value == null) return null;
|
||||
return (int)Math.round(value);
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ValidationFileReader implements FileReader {
|
||||
config = new GsonBuilder().create().fromJson(new java.io.FileReader(file), ValidationConfiguration.class);
|
||||
}
|
||||
|
||||
public Validator getValidator() {
|
||||
return new Validator(config);
|
||||
public ValidationConfiguration getValidationConfiguration() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
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 com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import ninja.javafx.smartcsv.FileWriter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
/**
|
||||
* file writer for the validation configuration
|
||||
*/
|
||||
@Service
|
||||
public class ValidationFileWriter implements FileWriter {
|
||||
|
||||
private ValidationConfiguration validationConfiguration;
|
||||
|
||||
public void setValidationConfiguration(ValidationConfiguration validationConfiguration) {
|
||||
this.validationConfiguration = validationConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(File file) throws IOException {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
Files.write(file.toPath(), gson.toJson(validationConfiguration).getBytes());
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,7 @@ public class Validator {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private void checkGroovy(String column, String value, ValidationError error) {
|
||||
String groovyScript = validationConfig.groovyRuleFor(column);
|
||||
String groovyScript = validationConfig.getGroovyRuleFor(column);
|
||||
if (groovyScript != null) {
|
||||
|
||||
Script script = scriptCache.get(column);
|
||||
@@ -143,7 +143,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkValueOf(String column, String value, ValidationError error) {
|
||||
List<String> values = validationConfig.valueOfRuleFor(column);
|
||||
List<String> values = validationConfig.getValueOfRuleFor(column);
|
||||
if (values != null) {
|
||||
if (!values.contains(value)) {
|
||||
String commaSeparated = values.stream().collect(joining(", "));
|
||||
@@ -153,7 +153,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkBlankOrNull(String column, String value, ValidationError error) {
|
||||
if (validationConfig.notEmptyRuleFor(column)) {
|
||||
if (validationConfig.getNotEmptyRuleFor(column) != null && validationConfig.getNotEmptyRuleFor(column)) {
|
||||
if (isBlankOrNull(value)) {
|
||||
error.add("validation.message.not.empty");
|
||||
}
|
||||
@@ -161,7 +161,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkInteger(String column, String value, ValidationError error) {
|
||||
if (validationConfig.integerRuleFor(column)) {
|
||||
if (validationConfig.getIntegerRuleFor(column) != null && validationConfig.getIntegerRuleFor(column)) {
|
||||
if (!isInt(value)) {
|
||||
error.add("validation.message.integer");
|
||||
}
|
||||
@@ -169,7 +169,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkDouble(String column, String value, ValidationError error) {
|
||||
if (validationConfig.doubleRuleFor(column)) {
|
||||
if (validationConfig.getDoubleRuleFor(column) != null && validationConfig.getDoubleRuleFor(column)) {
|
||||
if (!isDouble(value)) {
|
||||
error.add("validation.message.double");
|
||||
}
|
||||
@@ -177,7 +177,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkMinLength(String column, String value, ValidationError error) {
|
||||
Integer minLength = validationConfig.minLengthRuleFor(column);
|
||||
Integer minLength = validationConfig.getMinLengthRuleFor(column);
|
||||
if (minLength != null) {
|
||||
if (!minLength(value, minLength)) {
|
||||
error.add("validation.message.min.length", minLength.toString());
|
||||
@@ -186,7 +186,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkMaxLength(String column, String value, ValidationError error) {
|
||||
Integer maxLength = validationConfig.maxLengthRuleFor(column);
|
||||
Integer maxLength = validationConfig.getMaxLengthRuleFor(column);
|
||||
if (maxLength != null) {
|
||||
if (!maxLength(value, maxLength)) {
|
||||
error.add("validation.message.max.length", maxLength.toString());
|
||||
@@ -195,7 +195,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkDate(String column, String value, ValidationError error) {
|
||||
String dateformat = validationConfig.dateRuleFor(column);
|
||||
String dateformat = validationConfig.getDateRuleFor(column);
|
||||
if (dateformat != null && !dateformat.trim().isEmpty()) {
|
||||
if (!isDate(value, dateformat, true)) {
|
||||
error.add("validation.message.date.format", dateformat);
|
||||
@@ -204,7 +204,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkAlphaNumeric(String column, String value, ValidationError error) {
|
||||
if (validationConfig.alphanumericRuleFor(column)) {
|
||||
if (validationConfig.getAlphanumericRuleFor(column) != null && validationConfig.getAlphanumericRuleFor(column)) {
|
||||
if (!matchRegexp(value, "[0-9a-zA-Z]*")) {
|
||||
error.add("validation.message.alphanumeric");
|
||||
}
|
||||
@@ -212,7 +212,7 @@ public class Validator {
|
||||
}
|
||||
|
||||
private void checkRegularExpression(String column, String value, ValidationError error) {
|
||||
String regexp = validationConfig.regexpRuleFor(column);
|
||||
String regexp = validationConfig.getRegexpRuleFor(column);
|
||||
if (regexp != null && !regexp.trim().isEmpty()) {
|
||||
if (!matchRegexp(value, regexp)) {
|
||||
error.add("validation.message.regexp", regexp);
|
||||
|
||||
Reference in New Issue
Block a user