Commit cbec755a authored by Andrea Aime's avatar Andrea Aime
Browse files

[GEOS-9192] GeoServer UI let's you save duplicate grids names

parent b77c0e8a
......@@ -5,7 +5,12 @@
*/
package org.geoserver.gwc.web.gridset;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.measure.Unit;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
......@@ -303,6 +308,37 @@ abstract class AbstractGridSetPage extends GeoServerSecuredPage {
return crsPanel;
}
protected GridSet toGridSet(AjaxRequestTarget target, Form<?> form, GridSetInfo info)
throws Exception {
final GridSet newGridset = GridSetBuilder.build(info);
// the creation above can fill in the blanks of empty UI names, here is where we can
// check if the names are actually unique
List<String> names =
IntStream.range(0, newGridset.getNumLevels())
.mapToObj(i -> newGridset.getGrid(i).getName())
.sorted()
.collect(Collectors.toList());
Set<String> duplicates = new LinkedHashSet<>();
for (int i = 1; i < names.size(); i++) {
String prevName = names.get(i - 1);
if (prevName.equals(names.get(i))) {
duplicates.add(prevName);
}
}
if (!duplicates.isEmpty()) {
// set back to make the duplicates evident
for (int i = 0; i < newGridset.getNumLevels(); i++) {
info.getLevels().get(i).setName(names.get(i));
}
throw new IllegalArgumentException(
"Tile matrix names should not include duplicates, but the following were found: "
+ duplicates
+ ". Mind, if you left some names empty, GeoServer has automatically added in some names for you.");
}
return newGridset;
}
/** @author groldan */
protected static class GridSetCRSPanel extends CRSPanel {
private static final long serialVersionUID = 1L;
......
......@@ -42,13 +42,10 @@ public class GridSetEditPage extends AbstractGridSetPage {
@Override
protected void onSave(AjaxRequestTarget target, Form<?> form) {
GridSetInfo info = (GridSetInfo) form.getModelObject();
GWC gwc = GWC.get();
final GridSet newGridset;
GridSet newGridset = null;
try {
newGridset = GridSetBuilder.build(info);
} catch (IllegalStateException e) {
newGridset = toGridSet(target, form, info);
} catch (Exception e) {
form.error(e.getMessage());
target.add(form);
return;
......@@ -56,6 +53,7 @@ public class GridSetEditPage extends AbstractGridSetPage {
try {
// TODO: warn and eliminate caches
GWC gwc = GWC.get();
gwc.modifyGridSet(originalName, newGridset);
doReturn(GridSetsPage.class);
} catch (Exception e) {
......
......@@ -22,11 +22,10 @@ public class GridSetNewPage extends AbstractGridSetPage {
@Override
protected void onSave(AjaxRequestTarget target, Form<?> form) {
GridSetInfo info = (GridSetInfo) form.getModelObject();
GridSet gridset;
GridSet gridset = null;
try {
gridset = GridSetBuilder.build(info);
} catch (IllegalStateException e) {
gridset = toGridSet(target, form, info);
} catch (Exception e) {
form.error(e.getMessage());
target.add(form);
return;
......
......@@ -64,10 +64,9 @@ public class TileMatrixSetEditor extends FormComponentPanel<List<Grid>> {
@Override
public void validate(IValidatable<List<Grid>> validatable) {
ValidationError error = new ValidationError();
List<Grid> grids = validatable.getValue();
if (grids == null || grids.size() == 0) {
ValidationError error = new ValidationError();
error.setMessage(
new ResourceModel("TileMatrixSetEditor.validation.empty").getObject());
validatable.error(error);
......@@ -79,6 +78,7 @@ public class TileMatrixSetEditor extends FormComponentPanel<List<Grid>> {
Grid curr = grids.get(i);
if (curr.getResolution() >= prev.getResolution()) {
ValidationError error = new ValidationError();
String message =
"Each resolution should be lower than it's prior one. Res["
+ i
......@@ -95,6 +95,7 @@ public class TileMatrixSetEditor extends FormComponentPanel<List<Grid>> {
}
if (curr.getScaleDenominator() >= prev.getScaleDenominator()) {
ValidationError error = new ValidationError();
String message =
"Each scale denominator should be lower "
+ "than it's prior one. Scale["
......
......@@ -186,4 +186,30 @@ public class GridSetNewPageTest extends GeoServerWicketTestSupport {
GridSet created = gridSetBroker.get(gridsetName);
assertEquals(numLevels, created.getNumLevels());
}
@Test
public void testDuplicateNames() {
PageParameters params = new PageParameters().add("template", "EPSG:4326");
GridSetNewPage page = new GridSetNewPage(params);
tester.startPage(page);
FormTester ft = tester.newFormTester("gridSetForm");
ft.setValue("name:border:border_body:paramValue", "customWGS84");
// add two zoom levels
tester.executeAjaxEvent("gridSetForm:addZoomLevel", "click");
tester.executeAjaxEvent("gridSetForm:addZoomLevel", "click");
ft = tester.newFormTester("gridSetForm");
ft.setValue("tileMatrixSetEditor:container:table:gridLevels:0:name", "AName");
ft.setValue("tileMatrixSetEditor:container:table:gridLevels:1:name", "AName");
ft.submit();
// submit
tester.executeAjaxEvent("gridSetForm:save", "click");
tester.assertErrorMessages(
"Tile matrix names should not include duplicates, but the following were found: [AName]. Mind, if you left some names empty, GeoServer has automatically added in some names for you.");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment