From 20e7c76adc796baf61d2a08058facf7e45fadb97 Mon Sep 17 00:00:00 2001 From: mar Date: Tue, 27 Jan 2015 21:50:31 +0100 Subject: [PATCH] modyfikacja gisa dadany delete --- .../edu/bazy/controllers/GisController.java | 22 ++++++++++----- .../agh/edu/bazy/dbApi/gis/GisDbApi.java | 5 ++-- .../edu/bazy/dbApi/object/ObjectDbApi.java | 24 ++++++++++------- .../java/hello/agh/edu/bazy/model/Point.java | 8 ++++++ .../hello/agh/edu/bazy/model/Polygon.java | 27 +++++++++++++++++++ .../agh/edu/bazy/path/APIServicePaths.java | 4 ++- .../row/mappers/gis/PolygonsRowMapper.java | 13 +++++---- 7 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 src/main/java/hello/agh/edu/bazy/model/Polygon.java diff --git a/src/main/java/hello/agh/edu/bazy/controllers/GisController.java b/src/main/java/hello/agh/edu/bazy/controllers/GisController.java index faf4c4b..bfbd175 100644 --- a/src/main/java/hello/agh/edu/bazy/controllers/GisController.java +++ b/src/main/java/hello/agh/edu/bazy/controllers/GisController.java @@ -4,6 +4,7 @@ import hello.agh.edu.bazy.dbApi.gis.GisDbApi; import hello.agh.edu.bazy.dbApi.object.ObjectDbApi; import hello.agh.edu.bazy.model.Location; import hello.agh.edu.bazy.model.Point; +import hello.agh.edu.bazy.model.Polygon; import hello.agh.edu.bazy.path.APIServicePaths; import hello.agh.edu.bazy.row.gis.GisPoint; import org.springframework.beans.factory.annotation.Autowired; @@ -41,15 +42,17 @@ public class GisController { @RequestMapping(value = APIServicePaths.GET_LAMP + "/{id}") @ResponseBody - Location getLamp(@PathVariable("id") Long id) { + Location getLamp(@PathVariable("id") Long id) throws Exception { Location location = objectDbApi.getObject(id); - GisPoint points = gisDbApi.getLamp(id).get(0); - location.setY(points.getY()); - location.setX(points.getX()); + List points = gisDbApi.getLamp(id); + if (points.size() == 0) throw new Exception("No lapm found id: " + id); + GisPoint p = points.get(0); + location.setY(p.getY()); + location.setX(p.getX()); return location; } - @RequestMapping(value = APIServicePaths.GET_POLYGON + "{id}", method = RequestMethod.PUT) + @RequestMapping(value = APIServicePaths.GET_POLYGON + "/{id}") @ResponseBody List getPolygon(@PathVariable("id") long id) { return gisDbApi.getPolygon(id); @@ -57,7 +60,7 @@ public class GisController { @RequestMapping(value = APIServicePaths.GET_POLYGON) @ResponseBody - List> getPolygons() { + List getPolygons() { return gisDbApi.getPolygons(); } @@ -71,8 +74,13 @@ public class GisController { @RequestMapping(value = APIServicePaths.DELETE_LAMP + "/{id}", method = RequestMethod.DELETE) @ResponseBody - int deleteLamp(@PathVariable("id") Long id) { + int deletePoint(@PathVariable("id") Long id) { return gisDbApi.deleteLamp(id); } + @RequestMapping(value = APIServicePaths.DELETE_POLYGON + "/{id}", method = RequestMethod.DELETE) + @ResponseBody + int deleteLamp(@PathVariable("id") Long id) { + return objectDbApi.deleteObject(id); + } } diff --git a/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java b/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java index 3765521..3cb366d 100644 --- a/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java +++ b/src/main/java/hello/agh/edu/bazy/dbApi/gis/GisDbApi.java @@ -1,6 +1,7 @@ package hello.agh.edu.bazy.dbApi.gis; import hello.agh.edu.bazy.model.Point; +import hello.agh.edu.bazy.model.Polygon; import hello.agh.edu.bazy.postgresql.gis.GisDataSource; import hello.agh.edu.bazy.row.gis.GisPoint; import hello.agh.edu.bazy.row.mappers.gis.PointRowMapper; @@ -21,7 +22,7 @@ public class GisDbApi { private static String PUT_LAMP = "INSERT INTO public.gis_points(gis_points_id, way, highway) VALUES (@, st_geomfromtext('POINT(@ @)',4326), 'street_lamp');"; private static final String GET_LAMP = "SELECT gis_points_id,st_transform(way,4326) FROM gis_points WHERE gis_points_id = ?;"; private static final String DELETE_LAMP = "DELETE FROM public.gis_points WHERE gis_points_id = ?"; - private static final String GET_POLYGONS = "SELECT st_transform(way,4326) as polygon FROM gis_polygons;"; + private static final String GET_POLYGONS = "SELECT gis_polygons_id,st_transform(way,4326) as polygon FROM gis_polygons;"; private static final String GET_POLYGON = "SELECT st_transform(way,4326) as polygon FROM gis_polygons WHERE gis_polygons_id = ?"; private static final String PUT_POLYGON = "INSERT INTO public.gis_polygons(gis_polygons_id, way, highway) VALUES (?, (SELECT ST_MakePolygon(ST_AddPoint(foo.open_line, ST_StartPoint(foo.open_line))) "; @@ -59,7 +60,7 @@ public class GisDbApi { return gisJdbcTemplate.update(sql); } - public List> getPolygons(){ + public List getPolygons(){ return gisJdbcTemplate.query(GET_POLYGONS, new PolygonsRowMapper()); } diff --git a/src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java b/src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java index 77eb1df..673d23e 100644 --- a/src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java +++ b/src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java @@ -18,14 +18,15 @@ import java.util.List; * Created by mar on 27.01.15. */ @Component -public class ObjectDbApi { +public class ObjectDbApi { private static final String LOCATION_ID = "location_id"; private static final String PUT_OBJECT = "insert into \"LOCALIZATIONS\" values (DEFAULT,?)"; - private static final String GET_LOCATION = "select s.sensor_id, l.* from \"SENSORS_LOCATION\" s JOIN \"LOCALIZATIONS\" l ON s.location_id = l.location_id where s.location_id = ? "; + private static final String GET_LOCATION = "select * from \"LOCALIZATIONS\" where location_id = ?"; + private static final String DELETE_LOCATION = "delete from \"LOCALIZATIONS\" where location_id = ?"; JdbcTemplate objectJdbcTemplate; KeyHolder keyHolder = new GeneratedKeyHolder(); - public ObjectDbApi(){ + public ObjectDbApi() { GisDataSource dataSource = new GisDataSource(); dataSource.setDriverClassName("org.postgresql.Driver"); dataSource.setUrl("jdbc:postgresql://10.156.207.136:5432/objectsdb"); @@ -36,20 +37,23 @@ public class ObjectDbApi { objectJdbcTemplate = template; } - public long putObject(final String type){ + public long putObject(final String type) { objectJdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { - PreparedStatement ps = con.prepareStatement(PUT_OBJECT,new String[]{LOCATION_ID}); - ps.setString(1,type); + PreparedStatement ps = con.prepareStatement(PUT_OBJECT, new String[]{LOCATION_ID}); + ps.setString(1, type); return ps; } - },keyHolder); + }, keyHolder); return keyHolder.getKey().longValue(); } - public Location getObject(long id){ - return objectJdbcTemplate.query(GET_LOCATION,new Object[]{id}, new PointRowMapper()).get(0); + public Location getObject(long id) { + return objectJdbcTemplate.query(GET_LOCATION, new Object[]{id}, new PointRowMapper()).get(0); } -} + public int deleteObject(long id){ + return objectJdbcTemplate.update(DELETE_LOCATION,new Object[]{id}); + } +} \ No newline at end of file diff --git a/src/main/java/hello/agh/edu/bazy/model/Point.java b/src/main/java/hello/agh/edu/bazy/model/Point.java index b432478..b0477a2 100644 --- a/src/main/java/hello/agh/edu/bazy/model/Point.java +++ b/src/main/java/hello/agh/edu/bazy/model/Point.java @@ -19,4 +19,12 @@ public class Point { public void setX(double x) { this.x = x; } + + public double getY() { + return y; + } + + public void setY(double y) { + this.y = y; + } } diff --git a/src/main/java/hello/agh/edu/bazy/model/Polygon.java b/src/main/java/hello/agh/edu/bazy/model/Polygon.java new file mode 100644 index 0000000..81fa992 --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/model/Polygon.java @@ -0,0 +1,27 @@ +package hello.agh.edu.bazy.model; + +import java.util.List; + +/** + * Created by mar on 27.01.15. + */ +public class Polygon { + private long id; + private List polygon; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getPolygon() { + return polygon; + } + + public void setPolygon(List polygon) { + this.polygon = polygon; + } +} diff --git a/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java b/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java index ff4228f..a9c92f5 100644 --- a/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java +++ b/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java @@ -10,6 +10,7 @@ package hello.agh.edu.bazy.path; public final class APIServicePaths { + ; private APIServicePaths() { } @@ -27,7 +28,8 @@ public final class APIServicePaths { public static final String PUT_LAMP = "/lamp"; public static final String DELETE_LAMP = "/lamp"; public static final String GET_LAMP = "/lamp"; - public static final String GET_POLYGON = "polygon"; + public static final String GET_POLYGON = "/polygon"; + public static final String DELETE_POLYGON = "/polygon"; diff --git a/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonsRowMapper.java b/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonsRowMapper.java index 6008736..c76ab20 100644 --- a/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonsRowMapper.java +++ b/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonsRowMapper.java @@ -1,9 +1,9 @@ package hello.agh.edu.bazy.row.mappers.gis; import hello.agh.edu.bazy.model.Point; +import hello.agh.edu.bazy.model.Polygon; import hello.agh.edu.bazy.row.gis.GisPoint; import org.postgis.PGgeometry; -import org.postgis.Polygon; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; @@ -14,10 +14,9 @@ import java.util.List; /** * Created by lukasgol on 26.01.15. */ -public class PolygonsRowMapper implements RowMapper> { - +public class PolygonsRowMapper implements RowMapper{ @Override - public List mapRow(ResultSet rs, int rowNum) throws SQLException { + public Polygon mapRow(ResultSet rs, int rowNum) throws SQLException { PGgeometry geometry = (PGgeometry) rs.getObject("polygon"); int size = geometry.getGeometry().numPoints(); List list = new ArrayList<>(); @@ -25,6 +24,10 @@ public class PolygonsRowMapper implements RowMapper> { Point p = new Point(geometry.getGeometry().getPoint(i).x,geometry.getGeometry().getPoint(i).y); list.add(p); } - return list; + Polygon p = new Polygon(); + p.setId(rs.getLong("gis_polygons_id")); + p.setPolygon(list); + return p; } + } \ No newline at end of file