mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +02:00
first quick implementation of to support json table schema
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
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 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<String, Object> 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<String, Object> getConstraints() {
|
||||
return constraints;
|
||||
}
|
||||
|
||||
public void setConstraints(Map<String, Object> constraints) {
|
||||
this.constraints = constraints;
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
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.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;
|
||||
}
|
||||
}
|
||||
@@ -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<String, Map<String, Object>> 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();
|
||||
if (fieldConfigurations != null) {
|
||||
List<String> headerNames = new ArrayList<>();
|
||||
for (FieldConfiguration fieldConfiguration: fieldConfigurations) {
|
||||
headerNames.add(fieldConfiguration.getName());
|
||||
}
|
||||
return headerNames.toArray(new String[headerNames.size()]);
|
||||
}
|
||||
|
||||
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<String> getValueOfRuleFor(String column) {
|
||||
return (List<String>)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<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 (columnConfigurations != null) {
|
||||
Map rulesForColumn = columnConfigurations.get(column);
|
||||
if (rulesForColumn != null) {
|
||||
return columnConfigurations.get(column).get(key);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ public class ValidationFileReader implements FileReader<ValidationConfiguration>
|
||||
@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() {
|
||||
|
||||
@@ -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,68 +136,90 @@ 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());
|
||||
}
|
||||
|
||||
Boolean integerRule = validationConfig.getIntegerRuleFor(column);
|
||||
if (integerRule != null && integerRule) {
|
||||
add(column, new IntegerValidation());
|
||||
private void initializeColumnWithRules(FieldConfiguration column) {
|
||||
|
||||
if (column.getType() != null) {
|
||||
if (column.getType().equals("number")) {
|
||||
add(column.getName(), new DoubleValidation());
|
||||
}
|
||||
|
||||
Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
if (column.getConstraints() != null) {
|
||||
Boolean notEmptyRule = (Boolean)column.getConstraints().get("required");
|
||||
if (notEmptyRule != null && notEmptyRule) {
|
||||
add(column, new NotEmptyValidation());
|
||||
add(column.getName(), new NotEmptyValidation());
|
||||
}
|
||||
|
||||
Boolean uniqueRule = validationConfig.getUniqueRuleFor(column);
|
||||
Boolean uniqueRule = (Boolean)column.getConstraints().get("unique");
|
||||
if (uniqueRule != null && uniqueRule) {
|
||||
add(column, new UniqueValidation(columnValueProvider, column));
|
||||
add(column.getName(), new UniqueValidation(columnValueProvider, column.getName()));
|
||||
}
|
||||
|
||||
String dateRule = validationConfig.getDateRuleFor(column);
|
||||
if (dateRule != null && !dateRule.trim().isEmpty()) {
|
||||
add(column, new DateValidation(dateRule));
|
||||
}
|
||||
|
||||
Integer minLength = validationConfig.getMinLengthRuleFor(column);
|
||||
Integer minLength = doubleToInteger((Double)column.getConstraints().get("minLength"));
|
||||
if (minLength != null) {
|
||||
add(column, new MinLengthValidation(minLength));
|
||||
add(column.getName(), new MinLengthValidation(minLength));
|
||||
}
|
||||
|
||||
Integer maxLength = validationConfig.getMaxLengthRuleFor(column);
|
||||
Integer maxLength = doubleToInteger((Double)column.getConstraints().get("maxLength"));
|
||||
if (maxLength != null) {
|
||||
add(column, new MaxLengthValidation(maxLength));
|
||||
add(column.getName(), new MaxLengthValidation(maxLength));
|
||||
}
|
||||
|
||||
String regexp = validationConfig.getRegexpRuleFor(column);
|
||||
String regexp = (String)column.getConstraints().get("pattern");
|
||||
if (regexp != null && !regexp.trim().isEmpty()) {
|
||||
add(column, new RegExpValidation(regexp));
|
||||
add(column.getName(), new RegExpValidation(regexp));
|
||||
}
|
||||
|
||||
String groovy = validationConfig.getGroovyRuleFor(column);
|
||||
String groovy = (String)column.getConstraints().get("groovy");
|
||||
if (groovy != null && !groovy.trim().isEmpty()) {
|
||||
add(column, new GroovyValidation(groovy));
|
||||
add(column.getName(), new GroovyValidation(groovy));
|
||||
}
|
||||
List<String> valueOfRule = validationConfig.getValueOfRuleFor(column);
|
||||
|
||||
List<String> valueOfRule = (List<String>)column.getConstraints().get("enum");
|
||||
if (valueOfRule != null && !valueOfRule.isEmpty()) {
|
||||
add(column, new ValueOfValidation(valueOfRule));
|
||||
add(column.getName(), new ValueOfValidation(valueOfRule));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ValidationError isHeaderValid(String[] headerNames) {
|
||||
@@ -227,4 +251,9 @@ public class Validator {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Integer doubleToInteger(Double value) {
|
||||
if (value == null) return null;
|
||||
return (int)Math.round(value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user