Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WFSSearchChannel: get region from a property #883

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Default handler for WFS Search channel filter and title
Expand Down Expand Up @@ -89,12 +90,8 @@ public String getTitle(List<SelectItem> list) {
}

public String getTitle(List<SelectItem> list, String separator) {
StringBuilder buf = new StringBuilder();
for(SelectItem item : list) {
buf.append(item.getValue());
buf.append(separator);
}
// drop last separator (', ')
return buf.substring(0, buf.length()-separator.length());
return list.stream()
.map(SelectItem::getValue)
.collect(Collectors.joining(separator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class WFSSearchChannel extends SearchChannel {
public static final String GT_GEOM_MULTILINESTRING = "MULTILINESTRING";
public static final String GT_GEOM_MULTIPOLYGON = "MULTIPOLYGON";

static final String CONFIG_REGION_PROPERTY = "region-property";

private WFSSearchChannelsConfiguration config;
private PermissionService permissionsService;

Expand Down Expand Up @@ -114,6 +116,17 @@ private void setupDefaults(SearchResultItem item) {
item.setLocationTypeCode(JSONHelper.getStringFromJSON(defaults, "locationType", ""));
}

/**
* From database oskari_wfs_search_channels-table config-column:
* {
* "region-property": "foobar",
* ...
* }
*/
private String getRegionProperty() {
return config.getConfig().optString(CONFIG_REGION_PROPERTY, null);
}

public String getId() {
return ID_PREFIX + config.getId();
}
Expand Down Expand Up @@ -205,63 +218,93 @@ public ChannelSearchResult doSearch(SearchCriteria searchCriteria) {

log.debug("[WFSSEARCH] Response from server: " + resp);

JSONArray featuresArr = resp.getJSONArray("features");

for (int i = 0; i < featuresArr.length(); i++) {
SearchResultItem item = new SearchResultItem();
JSONObject featureJSON = featuresArr.getJSONObject(i);

item.setType(config.getName(searchCriteria.getLocale()));

setupDefaults(item);
item.setTitle(getTitle(featureJSON));

if (featureJSON.has("geometry")) {
JSONObject featuresObj_geometry = featureJSON.getJSONObject("geometry");

String geomType = featuresObj_geometry.getString("type").toUpperCase();
GeometryJSON geom = new GeometryJSON(3);
if (geomType.equals(GT_GEOM_POLYGON)) {
Polygon polygon = geom.readPolygon(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(polygon));
item.setLat(polygon.getCentroid().getCoordinate().y);
item.setLon(polygon.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_LINESTRING)) {
LineString lineGeom = geom.readLine(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(lineGeom));
item.setLat(lineGeom.getCentroid().getCoordinate().y);
item.setLon(lineGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_POINT)) {
org.locationtech.jts.geom.Point pointGeom = geom.readPoint(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(pointGeom));
item.setLat(pointGeom.getCentroid().getCoordinate().y);
item.setLon(pointGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTIPOLYGON)) {
MultiPolygon polygon = geom.readMultiPolygon(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(polygon));
item.setLat(polygon.getCentroid().getCoordinate().y);
item.setLon(polygon.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTILINESTRING)) {
MultiLineString lineGeom = geom.readMultiLine(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(lineGeom));
item.setLat(lineGeom.getCentroid().getCoordinate().y);
item.setLon(lineGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTIPOINT)) {
MultiPoint pointGeom = geom.readMultiPoint(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(pointGeom));
item.setLat(pointGeom.getCentroid().getCoordinate().y);
item.setLon(pointGeom.getCentroid().getCoordinate().x);
}
}
searchResultList.addItem(item);
}

parseResponse(searchCriteria, resp, searchResultList);
} catch (Exception e) {
log.error(e, "[WFSSEARCH] Failed to search locations from register of WFSSearchChannel");
}
return searchResultList;
}

protected void parseResponse(SearchCriteria sc, JSONObject resp, ChannelSearchResult result)
throws Exception {
String type = config.getName(sc.getLocale());

JSONArray featuresArr = resp.getJSONArray("features");
for (int i = 0; i < featuresArr.length(); i++) {
JSONObject featureJSON = featuresArr.getJSONObject(i);
SearchResultItem item = parseResultItem(featureJSON);
item.setType(type);
result.addItem(item);
}
}

protected SearchResultItem parseResultItem(JSONObject feature) throws Exception {
SearchResultItem item = new SearchResultItem();

setupDefaults(item);
item.setTitle(getTitle(feature));

if (feature.has("geometry")) {
JSONObject featuresObj_geometry = feature.getJSONObject("geometry");

String geomType = featuresObj_geometry.getString("type").toUpperCase();
GeometryJSON geom = new GeometryJSON(3);
if (geomType.equals(GT_GEOM_POLYGON)) {
Polygon polygon = geom.readPolygon(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(polygon));
item.setLat(polygon.getCentroid().getCoordinate().y);
item.setLon(polygon.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_LINESTRING)) {
LineString lineGeom = geom.readLine(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(lineGeom));
item.setLat(lineGeom.getCentroid().getCoordinate().y);
item.setLon(lineGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_POINT)) {
org.locationtech.jts.geom.Point pointGeom = geom.readPoint(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(pointGeom));
item.setLat(pointGeom.getCentroid().getCoordinate().y);
item.setLon(pointGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTIPOLYGON)) {
MultiPolygon polygon = geom.readMultiPolygon(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(polygon));
item.setLat(polygon.getCentroid().getCoordinate().y);
item.setLon(polygon.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTILINESTRING)) {
MultiLineString lineGeom = geom.readMultiLine(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(lineGeom));
item.setLat(lineGeom.getCentroid().getCoordinate().y);
item.setLon(lineGeom.getCentroid().getCoordinate().x);
} else if (geomType.equals(GT_GEOM_MULTIPOINT)) {
MultiPoint pointGeom = geom.readMultiPoint(featuresObj_geometry.toString());
item.addValue(PARAM_GEOMETRY, WKTHelper.getWKT(pointGeom));
item.setLat(pointGeom.getCentroid().getCoordinate().y);
item.setLon(pointGeom.getCentroid().getCoordinate().x);
}
}

String region = getRegion(feature);
if (region != null) {
item.setRegion(region);
}

return item;
}

protected String getRegion(JSONObject featureJSON) throws Exception {
String regionProperty = getRegionProperty();
if (regionProperty == null) {
return null;
}

JSONObject properties = featureJSON.optJSONObject("properties");
if (properties == null) {
return null;
}

Object value = properties.opt(regionProperty);
return value == null ? null : value.toString();
}

/**
* Get title from feature
* @param featureJSON
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package fi.nls.oskari.search.channel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.json.JSONObject;
import org.junit.BeforeClass;
import org.junit.Test;

import fi.mml.portti.service.search.SearchResultItem;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.wfs.WFSSearchChannelsConfiguration;

public class WFSSearchChannelTest {

private static JSONObject dinagatIslands;
private static JSONObject pointFeature;

@BeforeClass
public static void setup() throws Exception {
String dinagat = ""
+ "{"
+ " 'type': 'Feature',"
+ " 'geometry': {"
+ " 'type': 'Point',"
+ " 'coordinates': [125.6, 10.1]"
+ " },"
+ " 'properties': {"
+ " 'name': 'Dinagat Islands',"
+ " 'region': 'Caraga'"
+ " }"
+ "}";
dinagat.replace('\'', '"');
dinagatIslands = new JSONObject(dinagat);

String point = ""
+ "{"
+ " 'type': 'Feature',"
+ " 'geometry': {"
+ " 'type': 'Point',"
+ " 'coordinates': [102.0, 0.5]"
+ " },"
+ " 'properties': {"
+ " 'prop0': 'value0'"
+ " }"
+ "}";
point.replace('\'', '"');
pointFeature = new JSONObject(point);
}

@Test
public void whenConfigNotSetRegionIsNotReadFromProperty() throws Exception {
WFSSearchChannelsConfiguration cfg = new WFSSearchChannelsConfiguration();

WFSSearchChannel ch = new WFSSearchChannel(cfg);

assertNull(ch.getRegion(dinagatIslands));
assertNull(ch.getRegion(pointFeature));
}

@Test
public void whenConfigIsSetRegionIsReadFromProperty() throws Exception {
WFSSearchChannelsConfiguration cfg = new WFSSearchChannelsConfiguration();

cfg.setConfig(JSONHelper.createJSONObject(WFSSearchChannel.CONFIG_REGION_PROPERTY, "region"));

WFSSearchChannel ch = new WFSSearchChannel(cfg);

assertEquals("Caraga", ch.getRegion(dinagatIslands));
assertNull(ch.getRegion(pointFeature));
}

@Test
public void whenDefaultIsSetRegionIsOverriddenFromProperty() throws Exception {
WFSSearchChannelsConfiguration cfg = new WFSSearchChannelsConfiguration();

JSONObject config = new JSONObject();
config.put(WFSSearchChannel.CONFIG_REGION_PROPERTY, "region");
config.put("defaults", JSONHelper.createJSONObject("region", "My Default Region"));
cfg.setConfig(config);

WFSSearchChannel ch = new WFSSearchChannel(cfg);

SearchResultItem item;

item = ch.parseResultItem(dinagatIslands);
assertEquals("Caraga", item.getRegion());

item = ch.parseResultItem(pointFeature);
assertEquals("My Default Region", item.getRegion());
}

}