2016-02-02 03:20: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 com.google.gson.annotations.SerializedName;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* validation configuration
|
|
|
|
|
*/
|
|
|
|
|
public class ValidationConfiguration {
|
|
|
|
|
|
|
|
|
|
@SerializedName("headers")
|
|
|
|
|
private HeaderConfiguration headerConfiguration = new HeaderConfiguration();
|
|
|
|
|
|
|
|
|
|
@SerializedName("columns")
|
|
|
|
|
private Map<String, Map<String, Object>> columnConfigurations = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
public String[] headerNames() {
|
|
|
|
|
if (noHeader()) return null;
|
|
|
|
|
return headerConfiguration.getNames();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-22 23:51:08 +02:00
|
|
|
public void setHeaderNames(String[] headerNames) {
|
|
|
|
|
headerConfiguration.setNames(headerNames);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public Boolean getIntegerRuleFor(String column) {
|
|
|
|
|
return (Boolean)getValue(column, "integer");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public Boolean getDoubleRuleFor(String column) {
|
|
|
|
|
return (Boolean)getValue(column, "double");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public Boolean getNotEmptyRuleFor(String column) {
|
|
|
|
|
return (Boolean)getValue(column, "not empty");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public Integer getMinLengthRuleFor(String column) {
|
|
|
|
|
return doubleToInteger((Double)getValue(column, "minlength"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Integer getMaxLengthRuleFor(String column) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
return doubleToInteger((Double)getValue(column, "maxlength"));
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public String getDateRuleFor(String column) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
return (String)getValue(column, "date");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public Boolean getAlphanumericRuleFor(String column) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
return (Boolean)getValue(column, "alphanumeric");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public String getRegexpRuleFor(String column) {
|
|
|
|
|
if (noRulesFor(column)) return null;
|
|
|
|
|
return (String)getValue(column, "regexp");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public List<String> getValueOfRuleFor(String column) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
return (List<String>)getValue(column, "value of");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
public String getGroovyRuleFor(String column) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
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) {
|
2016-02-05 09:43:24 +01:00
|
|
|
setValue(column, value == null ? null : value.doubleValue(), "minlength");
|
2016-02-05 08:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMaxLengthRuleFor(String column, Integer value) {
|
2016-02-05 09:43:24 +01:00
|
|
|
setValue(column, value == null ? null : value.doubleValue(), "maxlength");
|
2016-02-05 08:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 08:00:32 +01:00
|
|
|
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) {
|
2016-02-02 03:20:14 +01:00
|
|
|
if (noRulesFor(column)) return null;
|
2016-02-05 08:00:32 +01:00
|
|
|
return columnConfigurations.get(column).get(key);
|
2016-02-02 03:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean noHeader() {
|
|
|
|
|
return headerConfiguration == null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean noRulesFor(String column) {
|
|
|
|
|
return columnConfigurations == null || columnConfigurations.get(column) == null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer doubleToInteger(Double value) {
|
|
|
|
|
if (value == null) return null;
|
|
|
|
|
return (int)Math.round(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|