diff --git a/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java b/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java index aaae261..085707e 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/validation/ValidationEditorController.java @@ -228,71 +228,71 @@ public class ValidationEditorController extends FXMLController { public void updateConfiguration() { - if (enableIntegerRule.isSelected()) { - validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), enableIntegerRule.isSelected()); - } else { - validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), null); - } - - if (enableNotEmptyRule.isSelected()) { - validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), enableNotEmptyRule.isSelected()); - } else { - validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), null); - } - - if (enableUniqueRule.isSelected()) { - validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), enableUniqueRule.isSelected()); - } else { - validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), null); - } - - if (enableDoubleRule.isSelected()) { - validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), enableDoubleRule.isSelected()); - } else { - validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), null); - } - - if (enableAlphanumericRule.isSelected()) { - validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), enableAlphanumericRule.isSelected()); - } else { - validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), null); - } - - if (enableDateRule.isSelected()) { - validationConfiguration.setDateRuleFor(selectedColumn.getValue(), dateformatRuleTextField.getText()); - } else { - validationConfiguration.setDateRuleFor(selectedColumn.getValue(), null); - } - - if (enableGroovyRule.isSelected()) { - validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), groovyRuleTextArea.getText()); - } else { - validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), null); - } - - if (enableMinLengthRule.isSelected()) { - validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), minLengthSpinner.getValue()); - } else { - validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), null); - } - - if (enableMaxLengthRule.isSelected()) { - validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), maxLengthSpinner.getValue()); - } else { - validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), null); - } - - if (enableRegexpRule.isSelected()) { - validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), regexpRuleTextField.getText()); - } else { - validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), null); - } - - if (enableValueOfRule.isSelected()) { - validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), asList(valueOfRuleTextField.getText().split(", "))); - } else { - validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), null); - } +// if (enableIntegerRule.isSelected()) { +// validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), enableIntegerRule.isSelected()); +// } else { +// validationConfiguration.setIntegerRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableNotEmptyRule.isSelected()) { +// validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), enableNotEmptyRule.isSelected()); +// } else { +// validationConfiguration.setNotEmptyRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableUniqueRule.isSelected()) { +// validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), enableUniqueRule.isSelected()); +// } else { +// validationConfiguration.setUniqueRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableDoubleRule.isSelected()) { +// validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), enableDoubleRule.isSelected()); +// } else { +// validationConfiguration.setDoubleRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableAlphanumericRule.isSelected()) { +// validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), enableAlphanumericRule.isSelected()); +// } else { +// validationConfiguration.setAlphanumericRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableDateRule.isSelected()) { +// validationConfiguration.setDateRuleFor(selectedColumn.getValue(), dateformatRuleTextField.getText()); +// } else { +// validationConfiguration.setDateRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableGroovyRule.isSelected()) { +// validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), groovyRuleTextArea.getText()); +// } else { +// validationConfiguration.setGroovyRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableMinLengthRule.isSelected()) { +// validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), minLengthSpinner.getValue()); +// } else { +// validationConfiguration.setMinLengthRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableMaxLengthRule.isSelected()) { +// validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), maxLengthSpinner.getValue()); +// } else { +// validationConfiguration.setMaxLengthRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableRegexpRule.isSelected()) { +// validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), regexpRuleTextField.getText()); +// } else { +// validationConfiguration.setRegexpRuleFor(selectedColumn.getValue(), null); +// } +// +// if (enableValueOfRule.isSelected()) { +// validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), asList(valueOfRuleTextField.getText().split(", "))); +// } else { +// validationConfiguration.setValueOfRuleFor(selectedColumn.getValue(), null); +// } } @@ -315,66 +315,66 @@ public class ValidationEditorController extends FXMLController { private void updateForm() { - updateCheckBox( - validationConfiguration.getNotEmptyRuleFor(getSelectedColumn()), - enableNotEmptyRule - ); - - updateCheckBox( - validationConfiguration.getIntegerRuleFor(getSelectedColumn()), - enableIntegerRule - ); - - updateCheckBox( - validationConfiguration.getDoubleRuleFor(getSelectedColumn()), - enableDoubleRule - ); - - updateCheckBox( - validationConfiguration.getAlphanumericRuleFor(getSelectedColumn()), - enableAlphanumericRule - ); - - updateCheckBox( - validationConfiguration.getUniqueRuleFor(getSelectedColumn()), - enableUniqueRule - ); - - updateSpinner( - minLengthSpinner, - validationConfiguration.getMinLengthRuleFor(getSelectedColumn()), - enableMinLengthRule - ); - - updateSpinner( - maxLengthSpinner, - validationConfiguration.getMaxLengthRuleFor(getSelectedColumn()), - enableMaxLengthRule - ); - - updateTextInputControl( - dateformatRuleTextField, - validationConfiguration.getDateRuleFor(getSelectedColumn()), - enableDateRule - ); - - updateTextInputControl( - regexpRuleTextField, - validationConfiguration.getRegexpRuleFor(getSelectedColumn()), - enableRegexpRule - ); - - updateTextInputControl( - valueOfRuleTextField, - validationConfiguration.getValueOfRuleFor(getSelectedColumn()), - enableValueOfRule - ); - - updateCodeAreaControl( - groovyRuleTextArea, - validationConfiguration.getGroovyRuleFor(getSelectedColumn()), - enableGroovyRule - ); +// updateCheckBox( +// validationConfiguration.getNotEmptyRuleFor(getSelectedColumn()), +// enableNotEmptyRule +// ); +// +// updateCheckBox( +// validationConfiguration.getIntegerRuleFor(getSelectedColumn()), +// enableIntegerRule +// ); +// +// updateCheckBox( +// validationConfiguration.getDoubleRuleFor(getSelectedColumn()), +// enableDoubleRule +// ); +// +// updateCheckBox( +// validationConfiguration.getAlphanumericRuleFor(getSelectedColumn()), +// enableAlphanumericRule +// ); +// +// updateCheckBox( +// validationConfiguration.getUniqueRuleFor(getSelectedColumn()), +// enableUniqueRule +// ); +// +// updateSpinner( +// minLengthSpinner, +// validationConfiguration.getMinLengthRuleFor(getSelectedColumn()), +// enableMinLengthRule +// ); +// +// updateSpinner( +// maxLengthSpinner, +// validationConfiguration.getMaxLengthRuleFor(getSelectedColumn()), +// enableMaxLengthRule +// ); +// +// updateTextInputControl( +// dateformatRuleTextField, +// validationConfiguration.getDateRuleFor(getSelectedColumn()), +// enableDateRule +// ); +// +// updateTextInputControl( +// regexpRuleTextField, +// validationConfiguration.getRegexpRuleFor(getSelectedColumn()), +// enableRegexpRule +// ); +// +// updateTextInputControl( +// valueOfRuleTextField, +// validationConfiguration.getValueOfRuleFor(getSelectedColumn()), +// enableValueOfRule +// ); +// +// updateCodeAreaControl( +// groovyRuleTextArea, +// validationConfiguration.getGroovyRuleFor(getSelectedColumn()), +// enableGroovyRule +// ); } private void updateCheckBox(Boolean value, CheckBox ruleEnabled) { diff --git a/src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java b/src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java deleted file mode 100644 index 84650dd..0000000 --- a/src/main/java/ninja/javafx/smartcsv/validation/AlphaNumericValidation.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - The MIT License (MIT) - ----------------------------------------------------------------------------- - - Copyright (c) 2015 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 static org.apache.commons.validator.GenericValidator.matchRegexp; - -/** - * Checks if the value is alpha numeric - */ -public class AlphaNumericValidation extends EmptyValueIsValid { - @Override - public void check(int row, String value, ValidationError error) { - if (!matchRegexp(value, "[0-9a-zA-Z]*")) { - error.add("validation.message.alphanumeric"); - } - } - - @Override - public Type getType() { - return Type.ALPHANUMERIC; - } - - -} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/DateFormatHelper.java b/src/main/java/ninja/javafx/smartcsv/validation/DateFormatHelper.java new file mode 100644 index 0000000..f80fc7a --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/DateFormatHelper.java @@ -0,0 +1,38 @@ +package ninja.javafx.smartcsv.validation; + +/** + * @author abi + */ +public class DateFormatHelper { + + + public static String dateFormat(String format, String defaultFormat) { + + if (format != null && !format.trim().isEmpty()) { + format = format.trim(); + if (format.startsWith("fmt:")) { + format = format.substring(4); + format = format.replace("%Y", "yyyy"); + format = format.replace("%y", "yy"); + format = format.replace("%m", "MM"); + format = format.replace("%d", "dd"); + format = format.replace("%a", "E"); + format = format.replace("%A", "EEEE"); + format = format.replace("%w", "F"); + format = format.replace("%b", "MMM"); + format = format.replace("%B", "MMMMM"); + format = format.replace("%H", "HH"); + format = format.replace("%I", "hh"); + format = format.replace("%p", "a"); + format = format.replace("%M", "mm"); + format = format.replace("%S", "ss"); + format = format.replace("%z", "Z"); + format = format.replace("%Z", "z"); + format = format.replace("%j", "DDD"); + format = format.replace("%U", "ww"); + return format; + } + } + return defaultFormat; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/FieldConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/FieldConfiguration.java new file mode 100644 index 0000000..5937a7d --- /dev/null +++ b/src/main/java/ninja/javafx/smartcsv/validation/FieldConfiguration.java @@ -0,0 +1,74 @@ +package ninja.javafx.smartcsv.validation; + +import java.util.List; +import java.util.Map; + +/** + * @author abi + */ +public class FieldConfiguration { + + private String name; + private String title; + private String type; + private String description; + private String format; + private Object missingValue; + private Map constraints; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getFormat() { + return format; + } + + public void setFormat(String format) { + this.format = format; + } + + public Object getMissingValue() { + return missingValue; + } + + public void setMissingValue(Object missingValue) { + this.missingValue = missingValue; + } + + public Map getConstraints() { + return constraints; + } + + public void setConstraints(Map constraints) { + this.constraints = constraints; + } +} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java deleted file mode 100644 index 1bcabea..0000000 --- a/src/main/java/ninja/javafx/smartcsv/validation/HeaderConfiguration.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - The MIT License (MIT) - ----------------------------------------------------------------------------- - - Copyright (c) 2015 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.annotations.SerializedName; - -/** - * header configuration for the validation - */ -public class HeaderConfiguration { - - @SerializedName("list") - private String[] names ; - - public String[] getNames() { - return names; - } - - public void setNames(String[] names) { - this.names = names; - } -} diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java index 848c42c..0eddd14 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationConfiguration.java @@ -28,149 +28,44 @@ package ninja.javafx.smartcsv.validation; import com.google.gson.annotations.SerializedName; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; /** * validation configuration */ public class ValidationConfiguration { - @SerializedName("headers") - private HeaderConfiguration headerConfiguration = new HeaderConfiguration(); + @SerializedName("fields") + private FieldConfiguration[] fieldConfigurations; - @SerializedName("columns") - private Map> columnConfigurations = new HashMap<>(); + public FieldConfiguration[] getFieldConfigurations() { + return fieldConfigurations; + } + + public void setFieldConfigurations(FieldConfiguration[] fieldConfigurations) { + this.fieldConfigurations = fieldConfigurations; + } public String[] headerNames() { - if (noHeader()) return null; - return headerConfiguration.getNames(); - } - - public void setHeaderNames(String[] headerNames) { - headerConfiguration.setNames(headerNames); - } - - public Boolean getUniqueRuleFor(String column) { - return (Boolean)getValue(column, "unique"); - } - - public Boolean getIntegerRuleFor(String column) { - return (Boolean)getValue(column, "integer"); - } - - public Boolean getDoubleRuleFor(String column) { - return (Boolean)getValue(column, "double"); - } - - public Boolean getNotEmptyRuleFor(String column) { - return (Boolean)getValue(column, "not empty"); - } - - public Integer getMinLengthRuleFor(String column) { - return doubleToInteger((Double)getValue(column, "minlength")); - } - - public Integer getMaxLengthRuleFor(String column) { - Double value = (Double)getValue(column, "maxlength"); - return value != null ? doubleToInteger(value) : null; - } - - public String getDateRuleFor(String column) { - return (String)getValue(column, "date"); - } - - public Boolean getAlphanumericRuleFor(String column) { - return (Boolean)getValue(column, "alphanumeric"); - } - - public String getRegexpRuleFor(String column) { - return (String)getValue(column, "regexp"); - } - - public List getValueOfRuleFor(String column) { - return (List)getValue(column, "value of"); - } - - public String getGroovyRuleFor(String column) { - 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 setUniqueRuleFor(String column, Boolean value) { - setValue(column, value, "unique"); - } - - - public void setMinLengthRuleFor(String column, Integer value) { - setValue(column, value == null ? null : value.doubleValue(), "minlength"); - } - - public void setMaxLengthRuleFor(String column, Integer value) { - setValue(column, value == null ? null : value.doubleValue(), "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 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 (columnConfigurations != null) { - Map rulesForColumn = columnConfigurations.get(column); - if (rulesForColumn != null) { - return columnConfigurations.get(column).get(key); + if (fieldConfigurations != null) { + List headerNames = new ArrayList<>(); + for (FieldConfiguration fieldConfiguration: fieldConfigurations) { + headerNames.add(fieldConfiguration.getName()); } + return headerNames.toArray(new String[headerNames.size()]); } + return null; } - private boolean noHeader() { - return headerConfiguration == null; + public void setHeaderNames(String[] header) { + fieldConfigurations = new FieldConfiguration[header.length]; + int i = 0; + for (String headerName: header) { + fieldConfigurations[i] = new FieldConfiguration(); + fieldConfigurations[i].setName(headerName); + i++; + } } - - private Integer doubleToInteger(Double value) { - if (value == null) return null; - return (int)Math.round(value); - } - } diff --git a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java index 45e8a60..c51e256 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/ValidationFileReader.java @@ -42,6 +42,7 @@ public class ValidationFileReader implements FileReader @Override public void read(File file) throws IOException { config = new GsonBuilder().create().fromJson(new java.io.FileReader(file), ValidationConfiguration.class); + System.out.println("brechpunkt"); } public ValidationConfiguration getContent() { diff --git a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java index cd620f4..8dada75 100644 --- a/src/main/java/ninja/javafx/smartcsv/validation/Validator.java +++ b/src/main/java/ninja/javafx/smartcsv/validation/Validator.java @@ -32,6 +32,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static ninja.javafx.smartcsv.validation.DateFormatHelper.dateFormat; + /** * This class checks all the validations defined in the * Config against a given value @@ -134,70 +136,92 @@ public class Validator { private void initColumnValidations() { if (hasConfig()) { - String[] columns = validationConfig.headerNames(); - for (String column : columns) { + for (FieldConfiguration column : validationConfig.getFieldConfigurations()) { initializeColumnWithRules(column); } } } - private void initializeColumnWithRules(String column) { - Boolean alphaNumeric = validationConfig.getAlphanumericRuleFor(column); - if (alphaNumeric != null && alphaNumeric) { - add(column, new AlphaNumericValidation()); + private void initializeColumnWithRules(String columnName) { + if (hasConfig()) { + for (FieldConfiguration column : validationConfig.getFieldConfigurations()) { + if (column.getName().equals(columnName)) { + initializeColumnWithRules(column); + break; + } + } } - Boolean doubleRule = validationConfig.getDoubleRuleFor(column); - if (doubleRule != null && doubleRule) { - add(column, new DoubleValidation()); + } + + private void initializeColumnWithRules(FieldConfiguration column) { + + if (column.getType() != null) { + if (column.getType().equals("number")) { + add(column.getName(), new DoubleValidation()); + } + + if (column.getType().equals("integer")) { + add(column.getName(), new IntegerValidation()); + } + + if (column.getType().equals("date")) { + String format = dateFormat(column.getFormat(), "YYYY-MM-DD"); + add(column.getName(), new DateValidation(format)); + } + + if (column.getType().equals("datetime")) { + String format = dateFormat(column.getFormat(), "YYYY-MM-DDThh:mm:ssZ"); + add(column.getName(), new DateValidation(format)); + } + + if (column.getType().equals("time")) { + String format = dateFormat(column.getFormat(), "hh:mm:ss"); + add(column.getName(), new DateValidation(format)); + } } - Boolean integerRule = validationConfig.getIntegerRuleFor(column); - if (integerRule != null && integerRule) { - add(column, new IntegerValidation()); - } + if (column.getConstraints() != null) { + Boolean notEmptyRule = (Boolean)column.getConstraints().get("required"); + if (notEmptyRule != null && notEmptyRule) { + add(column.getName(), new NotEmptyValidation()); + } - Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column); - if (notEmptyRule != null && notEmptyRule) { - add(column, new NotEmptyValidation()); - } + Boolean uniqueRule = (Boolean)column.getConstraints().get("unique"); + if (uniqueRule != null && uniqueRule) { + add(column.getName(), new UniqueValidation(columnValueProvider, column.getName())); + } - Boolean uniqueRule = validationConfig.getUniqueRuleFor(column); - if (uniqueRule != null && uniqueRule) { - add(column, new UniqueValidation(columnValueProvider, column)); - } + Integer minLength = doubleToInteger((Double)column.getConstraints().get("minLength")); + if (minLength != null) { + add(column.getName(), new MinLengthValidation(minLength)); + } - String dateRule = validationConfig.getDateRuleFor(column); - if (dateRule != null && !dateRule.trim().isEmpty()) { - add(column, new DateValidation(dateRule)); - } + Integer maxLength = doubleToInteger((Double)column.getConstraints().get("maxLength")); + if (maxLength != null) { + add(column.getName(), new MaxLengthValidation(maxLength)); + } - Integer minLength = validationConfig.getMinLengthRuleFor(column); - if (minLength != null) { - add(column, new MinLengthValidation(minLength)); - } + String regexp = (String)column.getConstraints().get("pattern"); + if (regexp != null && !regexp.trim().isEmpty()) { + add(column.getName(), new RegExpValidation(regexp)); + } - Integer maxLength = validationConfig.getMaxLengthRuleFor(column); - if (maxLength != null) { - add(column, new MaxLengthValidation(maxLength)); - } + String groovy = (String)column.getConstraints().get("groovy"); + if (groovy != null && !groovy.trim().isEmpty()) { + add(column.getName(), new GroovyValidation(groovy)); + } - 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)); - } - List valueOfRule = validationConfig.getValueOfRuleFor(column); - if (valueOfRule != null && !valueOfRule.isEmpty()) { - add(column, new ValueOfValidation(valueOfRule)); + List valueOfRule = (List)column.getConstraints().get("enum"); + if (valueOfRule != null && !valueOfRule.isEmpty()) { + add(column.getName(), new ValueOfValidation(valueOfRule)); + } } } + + public ValidationError isHeaderValid(String[] headerNames) { ValidationError result = null; if (validationConfig != null) { @@ -227,4 +251,9 @@ public class Validator { } return result; } + + private Integer doubleToInteger(Double value) { + if (value == null) return null; + return (int)Math.round(value); + } }