Permalink
Browse files

Add Point in Poly search for States functionality and Unit test

  • Loading branch information...
1 parent 0d36592 commit d2ac4e501d7cc3a83cea58504dc95517672d3f65 @jmarin jmarin committed Sep 28, 2012
@@ -1,5 +1,8 @@
package org.geo.spatialsearch.rest;
+import java.util.ArrayList;
+import java.util.List;
+
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@@ -17,11 +20,14 @@
@XmlRootElement(name = "Response")
public class APIResponse {
+ @XmlElement(name = "message")
+ protected List<String> messageList = new ArrayList<String>();
+
@XmlAttribute
- private String status;
+ protected Long responseTime;
- @XmlElement
- private API api;
+ @XmlAttribute
+ private String status;
public void setStatus(String status) {
this.status = status;
@@ -31,11 +37,23 @@ public String getStatus() {
return status;
}
- public void setApi(API api) {
- this.api = api;
+ public Long getResponseTime() {
+ return responseTime;
}
- public API getApi() {
- return api;
+ public void setResponseTime(Long responseTime) {
+ this.responseTime = responseTime;
}
+
+ public List<String> getMessageList() {
+ if (messageList == null) {
+ messageList = new ArrayList<String>();
+ }
+ return messageList;
+ }
+
+ public void setMessageList(List<String> messageList) {
+ this.messageList = messageList;
+ }
+
}
@@ -15,5 +15,5 @@ public CensusGeoBaseObject findById(CensusGeographyEnum geographyType,
Long id);
public CensusLookupResponse findByCoordinates(
- CensusGeographyEnum geographyType, double latitude, double longitude);
+ CensusGeographyEnum geographyType, double longitude, double latitude);
}
@@ -1,39 +1,85 @@
package org.geo.spatialsearch.service.impl;
+import java.util.List;
+
+import javax.ws.rs.core.Response.Status;
+
import org.geo.spatialsearch.census.model.CensusGeoBaseObject;
import org.geo.spatialsearch.census.model.CensusGeographyEnum;
import org.geo.spatialsearch.census.model.State2010;
+import org.geo.spatialsearch.census.rest.CensusLookupBaseResponse;
import org.geo.spatialsearch.census.rest.CensusLookupResponse;
import org.geo.spatialsearch.dao.HibernateDAO;
+import org.geo.spatialsearch.model.Envelope;
import org.geo.spatialsearch.service.CensusService;
+import org.geo.spatialsearch.util.GeometryUtil;
+import org.geo.spatialsearch.util.Message;
+import org.geo.spatialsearch.util.ValidationUtil;
+import org.hibernate.Criteria;
+import org.hibernate.spatial.criterion.SpatialRestrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
+import com.vividsolutions.jts.geom.Point;
+
@Transactional
public class CensusServiceImpl implements CensusService {
@Autowired
@Qualifier("state2010DAO")
private HibernateDAO<State2010, Long> state2010DAO;
-
+
@Override
+ @Transactional(readOnly = true)
public CensusGeoBaseObject findById(CensusGeographyEnum geographyType,
Long id) {
CensusGeoBaseObject geoEntity = null;
switch (geographyType) {
- case STATE2010:
- geoEntity = state2010DAO.findById(id);
- break;
+ case STATE2010:
+ geoEntity = state2010DAO.findById(id);
+ break;
}
return geoEntity;
}
@Override
+ @Transactional(readOnly = true)
public CensusLookupResponse findByCoordinates(
- CensusGeographyEnum geographyType, double latitude, double longitude) {
- // TODO Auto-generated method stub
- return null;
+ CensusGeographyEnum geographyType, double longitude, double latitude) {
+
+ // WGS84 hard coded for now
+ Point point = GeometryUtil.getPoint(longitude, latitude, 4326);
+ CensusLookupResponse apiResponse = new CensusLookupResponse();
+ CensusLookupBaseResponse censusLookupBaseResponse = new CensusLookupBaseResponse();
+ apiResponse.setCensusLookupBaseResponse(censusLookupBaseResponse);
+ List<String> messages = apiResponse.getMessageList();
+ ValidationUtil.isValidCensusGeographyType(apiResponse, geographyType);
+ if (messages.isEmpty()) {
+ switch (geographyType) {
+ case STATE2010:
+ findStateByPoint(apiResponse, point);
+ break;
+ }
+ apiResponse.setStatus(Status.OK.getReasonPhrase());
+ }
+ return apiResponse;
+ }
+
+ @Transactional(readOnly = true)
+ private void findStateByPoint(CensusLookupResponse apiResponse, Point point) {
+ final Criteria stateCriteria = state2010DAO.createCriteria();
+ stateCriteria.add(SpatialRestrictions.contains("geometry", point));
+ State2010 state = state2010DAO.findByCriteria(stateCriteria);
+ if (state != null) {
+ state.setEnvelope(new Envelope(state.getGeometry()
+ .getEnvelopeInternal()));
+ apiResponse.getCensusLookupBaseResponse().getStates().add(state);
+ }
+ ValidationUtil.isEmptyResult(apiResponse, apiResponse
+ .getCensusLookupBaseResponse().getStates(),
+ Message.NO_STATE_RESULTS_FOUND, null);
+
}
}
@@ -0,0 +1,19 @@
+package org.geo.spatialsearch.util;
+
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import com.vividsolutions.jts.geom.Point;
+
+
+public final class GeometryUtil {
+
+ public static Point getPoint(double longitude, double latitude, int srid) {
+ GeometryFactory geometryFactory = new GeometryFactory();
+ Coordinate coordinate = new Coordinate();
+ coordinate.x = longitude;
+ coordinate.y = latitude;
+ Point point = geometryFactory.createPoint(coordinate);
+ point.setSRID(srid);
+ return point;
+ }
+}
@@ -0,0 +1,18 @@
+package org.geo.spatialsearch.util;
+
+public enum Message {
+
+ GEOGRAPHY_TYPE_NOT_VALID("Malformed URL: Geography Type not valid"), NO_STATE_RESULTS_FOUND(
+ "No State results found"), NO_RESULTS_FOUND("No results found");
+
+ private String message;
+
+ private Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+}
@@ -0,0 +1,58 @@
+package org.geo.spatialsearch.util;
+
+import java.util.List;
+
+import javax.ws.rs.core.Response.Status;
+
+import org.geo.spatialsearch.census.model.CensusGeographyEnum;
+import org.geo.spatialsearch.rest.APIResponse;
+import org.springframework.util.StringUtils;
+
+public final class ValidationUtil {
+
+ public static boolean isValidCensusGeographyType(APIResponse apiResponse,
+ CensusGeographyEnum geographyType) {
+ if (!StringUtils.hasLength(geographyType.toString())) {
+ apiResponse.getMessageList().add(
+ Message.GEOGRAPHY_TYPE_NOT_VALID.getMessage());
+ apiResponse.setStatus(Status.BAD_REQUEST.getReasonPhrase());
+ return false;
+ }
+ return true;
+ }
+
+ public static boolean isEmptyResult(APIResponse apiResponse, List<?> results) {
+ if (results.isEmpty()) {
+ String message = new StringBuffer().append(
+ Message.NO_RESULTS_FOUND.getMessage()).toString();
+ apiResponse.getMessageList().add(message);
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isEmptyResult(APIResponse apiResponse,
+ List<?> results, Message message, Status status) {
+ if (results.isEmpty()) {
+ apiResponse.getMessageList().add(message.getMessage());
+ if (status != null) {
+ apiResponse.setStatus(status.getReasonPhrase());
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isEmptyResult(APIResponse apiResponse, Object result,
+ Message message, Status status) {
+ if (result == null) {
+ apiResponse.getMessageList().add(message.getMessage());
+ if (status != null) {
+ apiResponse.setStatus(status.getReasonPhrase());
+ }
+ return true;
+ }
+ return false;
+ }
+
+}
@@ -3,19 +3,23 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import java.util.List;
+
import org.geo.spatialsearch.census.model.CensusGeographyEnum;
import org.geo.spatialsearch.census.model.State2010;
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import com.vividsolutions.jts.geom.Coordinate;
+
@RunWith(SpringJUnit4ClassRunner.class)
-//specifies the Spring configuration to load for this test fixture
+// specifies the Spring configuration to load for this test fixture
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
-
public class CensusServiceTest {
@Autowired
@@ -26,9 +30,21 @@
public void testStateById() {
Long id = 1L;
State2010 state = (State2010) censusService.findById(
- CensusGeographyEnum.STATE2010 , id);
+ CensusGeographyEnum.STATE2010, id);
assertNotNull(state);
assertEquals(state.getGeoid(), "36");
}
+ @Test
+ public void testStateByPoint() {
+ Coordinate coordinate = new Coordinate();
+ coordinate.x = -73.9813281969;
+ coordinate.y = 40.7557761697;
+ List<State2010> states = censusService
+ .findByCoordinates(CensusGeographyEnum.STATE2010, coordinate.x,
+ coordinate.y).getCensusLookupBaseResponse().getStates();
+ Assert.assertFalse(states.isEmpty());
+ State2010 state2010 = states.get(0);
+ assertEquals(state2010.getName().trim(), "New York");
+ }
}

0 comments on commit d2ac4e5

Please sign in to comment.