Permalink
Please sign in to comment.
Browse files
Add Point in Poly search for States functionality and Unit test
- Loading branch information...
Showing
with
193 additions
and 18 deletions.
- +25 −7 src/main/java/org/geo/spatialsearch/rest/APIResponse.java
- +1 −1 src/main/java/org/geo/spatialsearch/service/CensusService.java
- +53 −7 src/main/java/org/geo/spatialsearch/service/impl/CensusServiceImpl.java
- +19 −0 src/main/java/org/geo/spatialsearch/util/GeometryUtil.java
- +18 −0 src/main/java/org/geo/spatialsearch/util/Message.java
- +58 −0 src/main/java/org/geo/spatialsearch/util/ValidationUtil.java
- +19 −3 src/test/java/org/geo/spatialsearch/service/CensusServiceTest.java
@@ -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; | ||
+ } | ||
+ | ||
+} |
0 comments on commit
d2ac4e5