SearchFeatureCommand implements CommandHasRequest<SearchFeatureRequest, SearchFeatureResponse> { @Override public void execute(SearchFeatureRequest request, SearchFeatureResponse response) throws Exception { String layerId = request.getLayerId(); if (null == layerId) { throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "layer"); } String crs = request.getCrs(); if (null == crs) { throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "crs"); } Filter filter = createFilter(request, layerId); log.debug("filter to apply : {}", filter); List<InternalFeature> features = layerService.getFeatures(layerId, geoService.getCrs(request.getCrs()), filter, null, request.getFeatureIncludes(), request.getOffSet(), request.getMax()); response.setLayerId(layerId); int max = request.getMax(); if (max == SearchFeatureRequest.MAX_UNLIMITED) { max = features.size(); } if (max > features.size()) { max = features.size(); } Feature[] maxList = new Feature[max]; for (int i = 0; i < max; i++) { Feature feature = converter.toDto(features.get(i)); feature.setCrs(crs); maxList[i] = feature; } response.setFeatures(maxList); }  @Override SearchFeatureRequest getEmptyCommandRequest(); @Override SearchFeatureResponse getEmptyCommandResponse(); @Override void execute(SearchFeatureRequest request, SearchFeatureResponse response);  }
@Test public void testSearchOneCriterionNoLimit() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); request.setMax(SearchFeatureRequest.MAX_UNLIMITED); SearchCriterion searchCriterion = new SearchCriterion(); searchCriterion.setAttributeName(REGION_ATTRIBUTE); searchCriterion.setOperator("like"); searchCriterion.setValue("'%1'"); request.setCriteria(new SearchCriterion[] {searchCriterion}); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); List<Feature> features = Arrays.asList(response.getFeatures()); Assert.assertNotNull(features); Assert.assertEquals(2, features.size()); List<String> actual = new ArrayList<String>(); for (Feature feature : features) { actual.add(feature.getLabel()); } Assert.assertTrue(actual.contains("Country 1")); Assert.assertTrue(actual.contains("Country 2")); }
@Test public void testSearchOneCriterionOffset() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); request.setMax(SearchFeatureRequest.MAX_UNLIMITED); request.setOffSet(2); SearchCriterion searchCriterion = new SearchCriterion(); searchCriterion.setAttributeName(REGION_ATTRIBUTE); searchCriterion.setOperator("like"); searchCriterion.setValue("'%1'"); request.setCriteria(new SearchCriterion[] {searchCriterion}); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); List<Feature> features = Arrays.asList(response.getFeatures()); Assert.assertNotNull(features); Assert.assertEquals(0, features.size()); }
@Test public void testSearchOneCriterionLimit() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); request.setMax(3); SearchCriterion searchCriterion = new SearchCriterion(); searchCriterion.setAttributeName(REGION_ATTRIBUTE); searchCriterion.setOperator("like"); searchCriterion.setValue("'R%'"); request.setCriteria(new SearchCriterion[] {searchCriterion}); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); List<Feature> features = Arrays.asList(response.getFeatures()); Assert.assertNotNull(features); Assert.assertEquals(3, features.size()); List<String> actual = new ArrayList<String>(); for (Feature feature : features) { actual.add(feature.getLabel()); } int count = 0; if (actual.contains("Country 1")) { count++; } if (actual.contains("Country 2")) { count++; } if (actual.contains("Country 3")) { count++; } if (actual.contains("Country 4")) { count++; } Assert.assertEquals(3, count); }
@Test public void testSearchMultipleCriteria() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); SearchCriterion searchCriterion1 = new SearchCriterion(); searchCriterion1.setAttributeName(REGION_ATTRIBUTE); searchCriterion1.setOperator("like"); searchCriterion1.setValue("'%egion 1'"); SearchCriterion searchCriterion2 = new SearchCriterion(); searchCriterion2.setAttributeName(REGION_ATTRIBUTE); searchCriterion2.setOperator("like"); searchCriterion2.setValue("'%egion 2'"); request.setCriteria(new SearchCriterion[] {searchCriterion1, searchCriterion2}); request.setBooleanOperator("or"); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); List<Feature> features = Arrays.asList(response.getFeatures()); Assert.assertNotNull(features); Assert.assertEquals(4, features.size()); List<String> actual = new ArrayList<String>(); for (Feature feature : features) { actual.add(feature.getLabel()); } Assert.assertTrue(actual.contains("Country 1")); Assert.assertTrue(actual.contains("Country 2")); Assert.assertTrue(actual.contains("Country 3")); Assert.assertTrue(actual.contains("Country 4")); }
@Test public void testSearchNoCriteria() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); List<Feature> features = Arrays.asList(response.getFeatures()); Assert.assertNotNull(features); Assert.assertEquals(4, features.size()); List<String> actual = new ArrayList<String>(); for (Feature feature : features) { actual.add(feature.getLabel()); } Assert.assertTrue(actual.contains("Country 1")); Assert.assertTrue(actual.contains("Country 2")); Assert.assertTrue(actual.contains("Country 3")); Assert.assertTrue(actual.contains("Country 4")); }
@Test public void testSearchTransform() throws Exception { SearchFeatureRequest request = new SearchFeatureRequest(); request.setLayerId(LAYER_ID); request.setCrs("EPSG:4326"); request.setMax(3); SearchCriterion searchCriterion = new SearchCriterion(); searchCriterion.setAttributeName(NAME_ATTRIBUTE); searchCriterion.setOperator("like"); searchCriterion.setValue("'%3'"); request.setCriteria(new SearchCriterion[] {searchCriterion}); SearchFeatureResponse response = (SearchFeatureResponse) dispatcher.execute( SearchFeatureRequest.COMMAND, request, null, "en"); Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); Feature[] features = response.getFeatures(); Assert.assertNotNull(features); Assert.assertEquals(1, features.length); Assert.assertEquals("Country 3", features[0].getLabel()); Geometry geometry = features[0].getGeometry(); Assert.assertNotNull(geometry); Coordinate coor = geometry.getGeometries()[0].getGeometries()[0].getCoordinates()[0]; Assert.assertEquals(-1, coor.getX(), DOUBLE_TOLERANCE); Assert.assertEquals(0, coor.getY(), DOUBLE_TOLERANCE); request.setCrs("EPSG:900913"); response = (SearchFeatureResponse) dispatcher.execute(SearchFeatureRequest.COMMAND, request, null, "en"); if (response.isError()) { response.getErrors().get(0).printStackTrace(); } Assert.assertFalse(response.isError()); Assert.assertEquals(LAYER_ID, response.getLayerId()); features = response.getFeatures(); Assert.assertNotNull(features); Assert.assertEquals(1, features.length); Assert.assertEquals("Country 3", features[0].getLabel()); geometry = features[0].getGeometry(); Assert.assertNotNull(geometry); coor = geometry.getGeometries()[0].getGeometries()[0].getCoordinates()[0]; Assert.assertEquals(-111319.49079327357, coor.getX(), DOUBLE_TOLERANCE); Assert.assertEquals(0, coor.getY(), DOUBLE_TOLERANCE); }