first quick implementation of to support json table schema

This commit is contained in:
2016-09-01 00:01:13 +02:00
committed by Andreas Billmann
parent cd0ca0ed11
commit 0c391e292e
8 changed files with 336 additions and 392 deletions

View File

@@ -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();
}
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);
if (fieldConfigurations != null) {
List<String> 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);
}
}