Skip to content

Commit

Permalink
Fix last of AbstractValidator use.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh committed Jan 16, 2016
1 parent 573eaec commit f854b92
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 132 deletions.
@@ -1,4 +1,4 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved /* (c) 2014, 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2014 OpenPlans * (c) 2014 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
Expand All @@ -15,7 +15,8 @@
import java.util.Set; import java.util.Set;


import org.apache.wicket.validation.IValidatable; import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator; import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.geotools.filter.FilterAttributeExtractor; import org.geotools.filter.FilterAttributeExtractor;
import org.geotools.filter.text.cql2.CQLException; import org.geotools.filter.text.cql2.CQLException;
import org.geotools.filter.text.ecql.ECQL; import org.geotools.filter.text.ecql.ECQL;
Expand All @@ -27,25 +28,45 @@
* *
* @author Andrea Aime - GeoSolutions * @author Andrea Aime - GeoSolutions
*/ */
public class ECQLValidator extends AbstractValidator<String> { public class ECQLValidator implements IValidator<String> {
private static final long serialVersionUID = 2268953695122233556L; private static final long serialVersionUID = 2268953695122233556L;


Set<String> validAttributes; Set<String> validAttributes;


public ECQLValidator setValidAttributes(String... validAttributes) { public ECQLValidator setValidAttributes(String... validAttributes) {
this.validAttributes = new LinkedHashSet<String>(Arrays.asList(validAttributes)); this.validAttributes = new LinkedHashSet<>(Arrays.asList(validAttributes));
return this; return this;
} }


public ECQLValidator setValidAttributes(Collection<String> validAttributes) { public ECQLValidator setValidAttributes(Collection<String> validAttributes) {
this.validAttributes = new LinkedHashSet<String>(validAttributes); this.validAttributes = new LinkedHashSet<>(validAttributes);
return this; return this;
} }


private String commaSeparated(Collection<String> invalids) {
StringBuilder string = new StringBuilder();

for (String attribute : invalids) {
string.append(attribute);
string.append(", ");
}
string.setLength(string.length() - 2);
return string.toString();
}

Map<String, Object> map(String... entries) {
Map<String, Object> result = new HashMap<>();
for (int i = 0; i < entries.length; i += 2) {
result.put(entries[i], entries[i + 1]);
}

return result;
}

@Override @Override
protected void onValidate(IValidatable<String> validatable) { public void validate(IValidatable<String> iv) {
// Extraction of the expression for the validation // Extraction of the expression for the validation
String expression = validatable.getValue(); String expression = iv.getValue();
if (expression == null || expression.isEmpty() || expression.trim().isEmpty()) { if (expression == null || expression.isEmpty() || expression.trim().isEmpty()) {
return; return;
} }
Expand All @@ -55,16 +76,20 @@ protected void onValidate(IValidatable<String> validatable) {
try { try {
ecql = ECQL.toExpression(expression); ecql = ECQL.toExpression(expression);
} catch (CQLException e) { } catch (CQLException e) {
error(validatable, "ecql.invalidExpression", ValidationError error = new ValidationError(this);
map("expression", expression, "error", e.getMessage())); error.setVariable("expression", expression);
error.setVariable("error", e.getMessage());
error.setMessage("ecql.invalidExpression");

iv.error(error);
} }


// Selection of a FilterAttributeExtractor // Selection of a FilterAttributeExtractor
if (ecql != null && validAttributes != null) { if (ecql != null && validAttributes != null) {
FilterAttributeExtractor filter = new FilterAttributeExtractor(); FilterAttributeExtractor filter = new FilterAttributeExtractor();
ecql.accept(filter, null); ecql.accept(filter, null);
// Extraction of the attributes, and filter out the valid ones // Extraction of the attributes, and filter out the valid ones
List<String> invalids = new ArrayList<String>(Arrays.asList(filter.getAttributeNames())); List<String> invalids = new ArrayList<>(Arrays.asList(filter.getAttributeNames()));


invalids.removeAll(validAttributes); invalids.removeAll(validAttributes);


Expand All @@ -74,32 +99,14 @@ protected void onValidate(IValidatable<String> validatable) {
String invalidList = commaSeparated(invalids); String invalidList = commaSeparated(invalids);
String validList = commaSeparated(validAttributes); String validList = commaSeparated(validAttributes);


error(validatable, "ecql.invalidAttributes", ValidationError error = new ValidationError(this);
map("expression", expression, "invalidAttributes", invalidList, error.setVariable("expression", expression);
"validAttributes", validList)); error.setVariable("invalidAttributes", invalidList);
error.setVariable("validAttributes", validList);
error.setMessage("ecql.invalidAttributes");
iv.error(error);
} }
} }

}

private String commaSeparated(Collection<String> invalids) {
StringBuilder string = new StringBuilder();

for (String attribute : invalids) {
string.append(attribute);
string.append(", ");
}
string.setLength(string.length() - 2);
return string.toString();
}

Map<String, Object> map(String... entries) {
Map<String, Object> result = new HashMap<String, Object>();
for (int i = 0; i < entries.length; i += 2) {
result.put(entries[i], entries[i + 1]);
}

return result;
} }


} }
Expand Up @@ -5,7 +5,6 @@
*/ */
package org.geoserver.wms.eo.web; package org.geoserver.wms.eo.web;


import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;


Expand All @@ -25,7 +24,8 @@
import org.apache.wicket.model.Model; import org.apache.wicket.model.Model;
import org.apache.wicket.util.convert.IConverter; import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.validation.IValidatable; import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator; import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.geoserver.catalog.CatalogBuilder; import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.CoverageInfo; import org.geoserver.catalog.CoverageInfo;
import org.geoserver.catalog.CoverageStoreInfo; import org.geoserver.catalog.CoverageStoreInfo;
Expand Down Expand Up @@ -501,14 +501,14 @@ private boolean checkDimensions(EoLayerGroupEntry entry) {
*/ */
protected abstract void onSubmit(LayerGroupInfo lg); protected abstract void onSubmit(LayerGroupInfo lg);


class GroupNameValidator extends AbstractValidator { class GroupNameValidator implements IValidator<String> {


@Override @Override
protected void onValidate(IValidatable validatable) { public void validate(IValidatable<String> iv) {
String name = (String) validatable.getValue(); String name = (String) iv.getValue();
LayerGroupInfo other = getCatalog().getLayerGroupByName(name); LayerGroupInfo other = getCatalog().getLayerGroupByName(name);
if(other != null && (layerGroupId == null || !other.getId().equals(layerGroupId))) { if(other != null && (layerGroupId == null || !other.getId().equals(layerGroupId))) {
error(validatable, "duplicateGroupNameError", Collections.singletonMap("name", name)); iv.error(new ValidationError("duplicateGroupNameError"));
} }
} }


Expand Down
5 changes: 5 additions & 0 deletions src/extension/importer/web/pom.xml
Expand Up @@ -48,6 +48,11 @@
<classifier>tests</classifier> <classifier>tests</classifier>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<type>pom</type>
</dependency>
</dependencies> </dependencies>


</project> </project>
@@ -1,18 +1,18 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved /* (c) 2014, 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans * (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
*/ */
package org.geoserver.importer.web; package org.geoserver.importer.web;


import java.util.Collections;


import org.apache.wicket.markup.html.form.TextField; import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel; import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel; import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.IValidatable; import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator; import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.geoserver.web.GeoServerApplication; import org.geoserver.web.GeoServerApplication;
import org.geoserver.web.wicket.XMLNameValidator; import org.geoserver.web.wicket.XMLNameValidator;


Expand All @@ -29,21 +29,22 @@ public NewWorkspacePanel(String id) {
TextField wst = new TextField("workspace", new PropertyModel(this, "workspace")); TextField wst = new TextField("workspace", new PropertyModel(this, "workspace"));
wst.setRequired(true); wst.setRequired(true);


wst.add(new AbstractValidator() { wst.add(new WorkspaceDoesNotExistValidator());


@Override
protected void onValidate(IValidatable validatable) {
String value = (String) validatable.getValue();
if (GeoServerApplication.get().getCatalog().getWorkspaceByName(value) != null) {
error(validatable, "NewWorkspacePanel.duplicateWorkspace", Collections
.singletonMap("workspace", value));
}

}
});
wst.add(new XMLNameValidator()); wst.add(new XMLNameValidator());


this.add(wst); this.add(wst);
} }


static class WorkspaceDoesNotExistValidator implements IValidator<String> {

@Override
public void validate(IValidatable<String> iv) {
String value = iv.getValue();
if (GeoServerApplication.get().getCatalog().getWorkspaceByName(value) != null) {
iv.error(new ValidationError("NewWorkspacePanel.duplicateWorkspace"));
}
}
}

} }
Expand Up @@ -25,9 +25,7 @@
import org.apache.wicket.model.Model; import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel; import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.visit.IVisit; import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.validation.IValidatable; import org.apache.wicket.validation.validator.RangeValidator;
import org.apache.wicket.validation.ValidationError;
import org.apache.wicket.validation.validator.AbstractValidator;
import org.geoserver.web.GeoServerBasePage; import org.geoserver.web.GeoServerBasePage;
import org.geoserver.web.netcdf.NetCDFSettingsContainer.GlobalAttribute; import org.geoserver.web.netcdf.NetCDFSettingsContainer.GlobalAttribute;
import org.geoserver.web.wicket.GeoServerAjaxFormLink; import org.geoserver.web.wicket.GeoServerAjaxFormLink;
Expand Down Expand Up @@ -78,31 +76,7 @@ public NetCDFPanel(String id, IModel<T> netcdfModel) {
dataPacking.setOutputMarkupId(true); dataPacking.setOutputMarkupId(true);
container.add(dataPacking); container.add(dataPacking);


compressionLevel.add(new AbstractValidator<Integer>() { compressionLevel.add(new RangeValidator(0, 9));

@Override
public boolean validateOnNullValue() {
return true;
}

@Override
protected void onValidate(IValidatable<Integer> validatable) {
if (validatable != null && validatable.getValue() != null) {
Integer value = validatable.getValue();
if (value < 0) {
ValidationError error = new ValidationError();
error.setMessage(new ParamResourceModel(
"NetCDFOutSettingsPanel.lowCompression", null, "").getObject());
validatable.error(error);
} else if (value > 9) {
ValidationError error = new ValidationError();
error.setMessage(new ParamResourceModel(
"NetCDFOutSettingsPanel.highCompression", null, "").getObject());
validatable.error(error);
}
}
}
});
container.add(compressionLevel); container.add(compressionLevel);


IModel<List<GlobalAttribute>> attributeModel = new PropertyModel(netcdfModel, IModel<List<GlobalAttribute>> attributeModel = new PropertyModel(netcdfModel,
Expand Down
@@ -1,4 +1,4 @@
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans * (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
Expand All @@ -11,9 +11,7 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Pattern; import java.util.regex.Pattern;
Expand Down Expand Up @@ -531,35 +529,34 @@ public String getIdValue(Object object, int index) {
} }


/** /**
* Validaes the regular expression syntax * Validates the regular expression syntax
*/ */
static class RegexpValidator extends AbstractValidator { static class RegexpValidator implements IValidator<String> {


@Override @Override
protected void onValidate(IValidatable validatable) { public void validate(IValidatable<String> iv) {
String value = (String) validatable.getValue(); String value = iv.getValue();
if(value != null) { if(value != null) {
try { try {
Pattern.compile(value); Pattern.compile(value);
} catch(PatternSyntaxException e) { } catch(PatternSyntaxException e) {
Map<String, String> map = new HashMap<String, String>(); ValidationError error = new ValidationError(this);
map.put("regexp", value); error.setVariable("regexp", value);
map.put("error", e.getMessage().replaceAll("\\^?", "")); error.setVariable("error", e.getMessage().replaceAll("\\^?", ""));
error(validatable, "invalidRegexp", map); iv.error(error);
} }
} }
} }

} }


/** /**
* Checks the sql view name is unique * Checks the sql view name is unique
*/ */
class ViewNameValidator implements IValidator { class ViewNameValidator implements IValidator<String> {


@Override @Override
public void validate(IValidatable validatable) { public void validate(IValidatable<String> validatable) {
String vtName = (String) validatable.getValue(); String vtName = validatable.getValue();


final DataStoreInfo store = getCatalog().getStore(storeId, DataStoreInfo.class); final DataStoreInfo store = getCatalog().getStore(storeId, DataStoreInfo.class);
List<FeatureTypeInfo> ftis = getCatalog().getResourcesByStore(store, FeatureTypeInfo.class); List<FeatureTypeInfo> ftis = getCatalog().getResourcesByStore(store, FeatureTypeInfo.class);
Expand All @@ -568,9 +565,6 @@ public void validate(IValidatable validatable) {
if(currvt != null) { if(currvt != null) {
if(typeInfoId == null || !typeInfoId.equals(curr.getId())) { if(typeInfoId == null || !typeInfoId.equals(curr.getId())) {
if(currvt.getName().equals(vtName)) { if(currvt.getName().equals(vtName)) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", vtName);
map.put("typeName", curr.getName());
IValidationError err = new ValidationError("duplicateSqlViewName:" + vtName); IValidationError err = new ValidationError("duplicateSqlViewName:" + vtName);
validatable.error(err); validatable.error(err);
return; return;
Expand Down

0 comments on commit f854b92

Please sign in to comment.