Skip to content

Commit

Permalink
[GEOS-8363] Creation of SQL View Layer with Parameters fails
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Oct 27, 2017
1 parent 216bb65 commit 17fa70a
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ protected void onClick(AjaxRequestTarget target, Form<?> form) {
parameters.processInputs();
if (sql != null && !"".equals(sql.trim())) {
paramProvider.refreshFromSql(sql);
parameters.setPageable(false);
target.add(parameters);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public abstract class GeoServerTablePanel<T> extends Panel {
*/
boolean[] selection;
boolean selectAllValue;


boolean pageable;


/**
* Builds a non selectable table
*/
Expand Down Expand Up @@ -436,9 +437,31 @@ public void selectObject(T object) {
* Selects a single item by index.
*/
public void selectIndex(int i) {
validateSelectionIndex(i);
selection[i] = true;
}

/**
* Un-selects a single item by index.
*/
public void unseelectIndex(int i) {
validateSelectionIndex(i);
selection[i] = false;
}

public void validateSelectionIndex(int i) {
if (selection.length <= i) {
if(dataProvider.size() <= i) {
throw new ArrayIndexOutOfBoundsException(i);
} else {
// expand selection array, the data provider likely resized and the two got misaligned
boolean[] newSelection = new boolean[(int) dataProvider.size()];
System.arraycopy(selection, 0, newSelection, 0, selection.length);
this.selection = newSelection;
}
}
}

/**
* The hidden button that will submit the form when the user
* presses enter in the text field
Expand Down Expand Up @@ -703,14 +726,15 @@ public class SelectionModel implements IModel<Boolean> {

public SelectionModel(int index) {
this.index = index;
validateSelectionIndex(index);
}

public Boolean getObject() {
return selection[index];
}

public void setObject(Boolean object) {
selection[index] = object.booleanValue();
selection[index] = object.booleanValue();
}

public void detach() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.geoserver.web.data.layer;

import com.vividsolutions.jts.io.WKTReader;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.data.test.SystemTestData;
import org.geoserver.web.GeoServerWicketTestSupport;
import org.geotools.data.DataStore;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.opengis.feature.simple.SimpleFeatureType;

import java.util.Map;

public class AbstractSqlViewPageTest extends GeoServerWicketTestSupport {

public static final String STORE_NAME = "foo";

@Override
protected void onSetUp(SystemTestData testData) throws Exception {
super.onSetUp(testData);

//setup an H2 datastore for the purpose of doing joins
Catalog cat = getCatalog();
DataStoreInfo ds = cat.getFactory().createDataStore();
ds.setName(STORE_NAME);
ds.setWorkspace(cat.getDefaultWorkspace());
ds.setEnabled(true);

Map params = ds.getConnectionParameters();
params.put("dbtype", "h2");
params.put("database", getTestData().getDataDirectoryRoot().getAbsolutePath() + "/foo");
cat.add(ds);

FeatureSource fs1 = getFeatureSource(SystemTestData.FORESTS);

DataStore store = (DataStore) ds.getDataStore(null);
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();

tb.init((SimpleFeatureType) fs1.getSchema());
//tb.remove("boundedBy");
store.createSchema(tb.buildFeatureType());

CatalogBuilder cb = new CatalogBuilder(cat);
cb.setStore(ds);

FeatureStore fs = (FeatureStore) store.getFeatureSource("Forests");
fs.addFeatures(fs1.getFeatures());
addFeature(fs, "MULTIPOLYGON (((0.008151604330777 -0.0023208963631571, 0.0086527358638763 -0.0012374917185382, 0.0097553137885805 -0.0004505798694767, 0.0156132468328575 0.001226912691216, 0.0164282119026783 0.0012863836826631, 0.0171241513076058 0.0011195104764988, 0.0181763809803841 0.0003258121477801, 0.018663180519973 -0.0007914339515293, 0.0187 -0.0054, 0.0185427596344991 -0.0062643098258021, 0.0178950534559435 -0.0072336706251426, 0.0166538015456463 -0.0078538015456464, 0.0160336706251426 -0.0090950534559435, 0.0150643098258021 -0.0097427596344991, 0.0142 -0.0099, 0.0086 -0.0099, 0.0077356901741979 -0.0097427596344991, 0.0067663293748574 -0.0090950534559435, 0.0062572403655009 -0.0082643098258021, 0.0061 -0.0074, 0.0061055767515099 -0.0046945371967831, 0.0062818025956546 -0.0038730531083409, 0.0066527358638763 -0.0032374917185382, 0.0072813143786463 -0.0026800146279973, 0.008151604330777 -0.0023208963631571)))",
"110", "Foo Forest");
addFeature(fs, "MULTIPOLYGON (((-0.0023852705061082 -0.005664537521815, -0.0026781637249217 -0.0063716443030016, -0.0033852705061082 -0.006664537521815, -0.0040923772872948 -0.0063716443030016, -0.0043852705061082 -0.005664537521815, -0.0040923772872947 -0.0049574307406285, -0.0033852705061082 -0.004664537521815, -0.0026781637249217 -0.0049574307406285, -0.0023852705061082 -0.005664537521815)))",
"111", "Bar Forest");
FeatureTypeInfo ft = cb.buildFeatureType(fs);
cat.add(ft);
}

void addFeature(FeatureStore store, String wkt, Object... atts) throws Exception {
SimpleFeatureBuilder b = new SimpleFeatureBuilder((SimpleFeatureType) store.getSchema());
b.add(new WKTReader().read(wkt));
for (Object att : atts) {
b.add(att);
}

DefaultFeatureCollection features = new DefaultFeatureCollection(null, null);
features.add(b.buildFeature(null));
store.addFeatures(features);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,19 @@
*/
package org.geoserver.web.data.layer;

import com.vividsolutions.jts.io.WKTReader;
import org.apache.wicket.Component;
import org.geoserver.catalog.Catalog;
import org.geoserver.catalog.CatalogBuilder;
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.data.test.SystemTestData;
import org.geoserver.web.GeoServerWicketTestSupport;
import org.geotools.data.DataStore;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureStore;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.jdbc.VirtualTable;
import org.geotools.jdbc.VirtualTableParameter;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertNotNull;

public class SqlViewEditPageTest extends GeoServerWicketTestSupport {
public class SqlViewEditPageTest extends AbstractSqlViewPageTest {


@Override
protected void onSetUp(SystemTestData testData) throws Exception {
super.onSetUp(testData);

//setup an H2 datastore for the purpose of doing joins
Catalog cat = getCatalog();
DataStoreInfo ds = cat.getFactory().createDataStore();
ds.setName("foo");
ds.setWorkspace(cat.getDefaultWorkspace());
ds.setEnabled(true);

Map params = ds.getConnectionParameters();
params.put("dbtype", "h2");
params.put("database", getTestData().getDataDirectoryRoot().getAbsolutePath() + "/foo");
cat.add(ds);

FeatureSource fs1 = getFeatureSource(SystemTestData.FORESTS);

DataStore store = (DataStore) ds.getDataStore(null);
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();

tb.init((SimpleFeatureType) fs1.getSchema());
//tb.remove("boundedBy");
store.createSchema(tb.buildFeatureType());

CatalogBuilder cb = new CatalogBuilder(cat);
cb.setStore(ds);

FeatureStore fs = (FeatureStore) store.getFeatureSource("Forests");
fs.addFeatures(fs1.getFeatures());
addFeature(fs, "MULTIPOLYGON (((0.008151604330777 -0.0023208963631571, 0.0086527358638763 -0.0012374917185382, 0.0097553137885805 -0.0004505798694767, 0.0156132468328575 0.001226912691216, 0.0164282119026783 0.0012863836826631, 0.0171241513076058 0.0011195104764988, 0.0181763809803841 0.0003258121477801, 0.018663180519973 -0.0007914339515293, 0.0187 -0.0054, 0.0185427596344991 -0.0062643098258021, 0.0178950534559435 -0.0072336706251426, 0.0166538015456463 -0.0078538015456464, 0.0160336706251426 -0.0090950534559435, 0.0150643098258021 -0.0097427596344991, 0.0142 -0.0099, 0.0086 -0.0099, 0.0077356901741979 -0.0097427596344991, 0.0067663293748574 -0.0090950534559435, 0.0062572403655009 -0.0082643098258021, 0.0061 -0.0074, 0.0061055767515099 -0.0046945371967831, 0.0062818025956546 -0.0038730531083409, 0.0066527358638763 -0.0032374917185382, 0.0072813143786463 -0.0026800146279973, 0.008151604330777 -0.0023208963631571)))",
"110", "Foo Forest");
addFeature(fs, "MULTIPOLYGON (((-0.0023852705061082 -0.005664537521815, -0.0026781637249217 -0.0063716443030016, -0.0033852705061082 -0.006664537521815, -0.0040923772872948 -0.0063716443030016, -0.0043852705061082 -0.005664537521815, -0.0040923772872947 -0.0049574307406285, -0.0033852705061082 -0.004664537521815, -0.0026781637249217 -0.0049574307406285, -0.0023852705061082 -0.005664537521815)))",
"111", "Bar Forest");
FeatureTypeInfo ft = cb.buildFeatureType(fs);
cat.add(ft);
}


void addFeature(FeatureStore store, String wkt, Object... atts) throws Exception {
SimpleFeatureBuilder b = new SimpleFeatureBuilder((SimpleFeatureType) store.getSchema());
b.add(new WKTReader().read(wkt));
for (Object att : atts) {
b.add(att);
}

DefaultFeatureCollection features = new DefaultFeatureCollection(null, null);
features.add(b.buildFeature(null));
store.addFeatures(features);
}

@Test
public void testSqlViewManyParameters() throws IOException {
login();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.web.data.layer;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.tester.FormTester;
import org.junit.Test;

import java.io.IOException;

public class SqlViewNewPageTest extends AbstractSqlViewPageTest {

@Test
public void testSqlViewManyParameters() throws IOException {
login();

PageParameters pp = new PageParameters();
pp.add(SQLViewAbstractPage.WORKSPACE, getCatalog().getDefaultWorkspace().getName());
pp.add(SQLViewAbstractPage.DATASTORE, AbstractSqlViewPageTest.STORE_NAME);
tester.startPage(new SQLViewNewPage(pp));


FormTester form = tester.newFormTester("form");
form.setValue("sql", "SELECT * FROM \"Forests\" where name = %FOO%");
tester.clickLink("form:guessParams", true);

// print(tester.getLastRenderedPage(), true, true);

// check it did not crash and the param has been guessed
tester.assertModelValue("form:parameters:listContainer:items:1:itemProperties:0:component:text", "FOO");
}

}

0 comments on commit 17fa70a

Please sign in to comment.