From 87547ed2d591b3418836393942685c039ab62909 Mon Sep 17 00:00:00 2001 From: mar Date: Tue, 27 Jan 2015 19:49:43 +0100 Subject: [PATCH 1/2] poprawki,poprawki, edycja gis --- .../edu/bazy/controllers/GisController.java | 43 ++++++++++++--- .../agh/edu/bazy/dbApi/gis/GisDbApi.java | 10 ++-- .../edu/bazy/dbApi/object/ObjectDbApi.java | 55 +++++++++++++++++++ .../hello/agh/edu/bazy/model/Location.java | 12 ++++ .../java/hello/agh/edu/bazy/model/Point.java | 22 ++++++++ .../agh/edu/bazy/path/APIServicePaths.java | 4 +- .../hello/agh/edu/bazy/row/gis/GisPoint.java | 9 +++ .../row/mappers/gis/PolygonRowMapper.java | 17 +++--- .../row/mappers/gis/PolygonsRowMapper.java | 14 ++--- .../row/mappers/object/PointRowMapper.java | 22 ++++++++ 10 files changed, 181 insertions(+), 27 deletions(-) create mode 100644 src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java create mode 100644 src/main/java/hello/agh/edu/bazy/model/Point.java create mode 100644 src/main/java/hello/agh/edu/bazy/row/mappers/object/PointRowMapper.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 d9548c9..faf4c4b 100644 --- a/src/main/java/hello/agh/edu/bazy/controllers/GisController.java +++ b/src/main/java/hello/agh/edu/bazy/controllers/GisController.java @@ -1,7 +1,9 @@ package hello.agh.edu.bazy.controllers; 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.path.APIServicePaths; import hello.agh.edu.bazy.row.gis.GisPoint; import org.springframework.beans.factory.annotation.Autowired; @@ -21,23 +23,50 @@ public class GisController { @Autowired GisDbApi gisDbApi; + @Autowired + ObjectDbApi objectDbApi; + @RequestMapping(APIServicePaths.GET_ALL_LAMPS) @ResponseBody List getLamps() { - return gisDbApi.getAllLamps(); + + List points = gisDbApi.getAllLamps(); + for (int i = 0; i < points.size(); i++) { + long id = points.get(i).getId(); + Location location = objectDbApi.getObject(id); + points.get(i).setDescription(location.getDescription()); + } + return points; } @RequestMapping(value = APIServicePaths.GET_LAMP + "/{id}") @ResponseBody - List getLamp(@PathVariable("id") Long id) { - List polygon = gisDbApi.getPolygon(id); - return gisDbApi.getLamp(id); + Location getLamp(@PathVariable("id") Long id) { + Location location = objectDbApi.getObject(id); + GisPoint points = gisDbApi.getLamp(id).get(0); + location.setY(points.getY()); + location.setX(points.getX()); + return location; } - @RequestMapping(value = APIServicePaths.PUT_LAMP + "/{id}", method = RequestMethod.PUT) + @RequestMapping(value = APIServicePaths.GET_POLYGON + "{id}", method = RequestMethod.PUT) @ResponseBody - int putLamp(@RequestBody Location location, @PathVariable("id") Long id) { - return gisDbApi.putLamp(id, location.getX(), location.getY()); + List getPolygon(@PathVariable("id") long id) { + return gisDbApi.getPolygon(id); + } + + @RequestMapping(value = APIServicePaths.GET_POLYGON) + @ResponseBody + List> getPolygons() { + return gisDbApi.getPolygons(); + } + + @RequestMapping(value = APIServicePaths.PUT_LAMP, method = RequestMethod.PUT) + @ResponseBody + long putLamp(@RequestBody Location location) { + long id = objectDbApi.putObject(location.getDescription()); + gisDbApi.putLamp(id, location.getX(), location.getY()); + return id; } @RequestMapping(value = APIServicePaths.DELETE_LAMP + "/{id}", method = RequestMethod.DELETE) 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 3f3aaec..3765521 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,5 +1,6 @@ package hello.agh.edu.bazy.dbApi.gis; +import hello.agh.edu.bazy.model.Point; 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; @@ -22,6 +23,7 @@ public class GisDbApi { 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_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))) "; JdbcTemplate gisJdbcTemplate; @@ -57,16 +59,16 @@ public class GisDbApi { return gisJdbcTemplate.update(sql); } - public List> getPolygons(){ + public List> getPolygons(){ return gisJdbcTemplate.query(GET_POLYGONS, new PolygonsRowMapper()); } - public List getPolygon(long id){ - List> polygons =gisJdbcTemplate.query(GET_POLYGON, new Object[]{id}, new PolygonRowMapper()); + public List getPolygon(long id){ + List> polygons =gisJdbcTemplate.query(GET_POLYGON, new Object[]{id}, new PolygonRowMapper()); return polygons.size()>0?polygons.get(0):null; } - //public Lamp get + public void setGisJdbcTemplate(JdbcTemplate gisJdbcTemplate) { this.gisJdbcTemplate = gisJdbcTemplate; } 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 new file mode 100644 index 0000000..77eb1df --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/dbApi/object/ObjectDbApi.java @@ -0,0 +1,55 @@ +package hello.agh.edu.bazy.dbApi.object; + +import hello.agh.edu.bazy.model.Location; +import hello.agh.edu.bazy.postgresql.gis.GisDataSource; +import hello.agh.edu.bazy.row.mappers.object.PointRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.PreparedStatementCreator; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Component; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +/** + * Created by mar on 27.01.15. + */ +@Component +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 = ? "; + JdbcTemplate objectJdbcTemplate; + KeyHolder keyHolder = new GeneratedKeyHolder(); + + public ObjectDbApi(){ + GisDataSource dataSource = new GisDataSource(); + dataSource.setDriverClassName("org.postgresql.Driver"); + dataSource.setUrl("jdbc:postgresql://10.156.207.136:5432/objectsdb"); + dataSource.setPassword("ztb2015"); + dataSource.setUsername("postgres"); + JdbcTemplate template = new JdbcTemplate(); + template.setDataSource(dataSource); + objectJdbcTemplate = template; + } + + 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); + return ps; + } + },keyHolder); + return keyHolder.getKey().longValue(); + } + + public Location getObject(long id){ + return objectJdbcTemplate.query(GET_LOCATION,new Object[]{id}, new PointRowMapper()).get(0); + } + +} diff --git a/src/main/java/hello/agh/edu/bazy/model/Location.java b/src/main/java/hello/agh/edu/bazy/model/Location.java index 8f8bb2c..abf4f52 100644 --- a/src/main/java/hello/agh/edu/bazy/model/Location.java +++ b/src/main/java/hello/agh/edu/bazy/model/Location.java @@ -1,11 +1,14 @@ package hello.agh.edu.bazy.model; +import java.util.List; + /** * Created by Marcin on 2015-01-25. */ public class Location { private double x; private double y; + private String description; public double getX() { return x; @@ -22,4 +25,13 @@ public class Location { public void setY(double y) { this.y = y; } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + } diff --git a/src/main/java/hello/agh/edu/bazy/model/Point.java b/src/main/java/hello/agh/edu/bazy/model/Point.java new file mode 100644 index 0000000..b432478 --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/model/Point.java @@ -0,0 +1,22 @@ +package hello.agh.edu.bazy.model; + +/** + * Created by mar on 27.01.15. + */ +public class Point { + double x; + double y; + + public Point(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return x; + } + + public void setX(double x) { + this.x = x; + } +} 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 b1319ed..6a01390 100644 --- a/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java +++ b/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java @@ -11,7 +11,6 @@ public final class APIServicePaths { - private APIServicePaths() { } @@ -28,6 +27,9 @@ 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"; + + //SENSORS public static final String SENSORS_CONTROLLER = "/sensors"; diff --git a/src/main/java/hello/agh/edu/bazy/row/gis/GisPoint.java b/src/main/java/hello/agh/edu/bazy/row/gis/GisPoint.java index c22e934..39612f2 100644 --- a/src/main/java/hello/agh/edu/bazy/row/gis/GisPoint.java +++ b/src/main/java/hello/agh/edu/bazy/row/gis/GisPoint.java @@ -7,6 +7,7 @@ public class GisPoint { private long id; private double x; private double y; + private String description; public long getId() { return id; @@ -42,4 +43,12 @@ public class GisPoint { public void setY(double y) { this.y = y; } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } } diff --git a/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonRowMapper.java b/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonRowMapper.java index a0f88ff..47bc7e4 100644 --- a/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonRowMapper.java +++ b/src/main/java/hello/agh/edu/bazy/row/mappers/gis/PolygonRowMapper.java @@ -1,8 +1,10 @@ package hello.agh.edu.bazy.row.mappers.gis; +import hello.agh.edu.bazy.model.Point; import hello.agh.edu.bazy.row.gis.GisPoint; +import org.postgis.Geometry; import org.postgis.PGgeometry; -import org.postgis.Point; +import org.postgresql.geometric.PGpolygon; import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet; @@ -13,18 +15,19 @@ import java.util.List; /** * Created by lukasgol on 26.01.15. */ -public class PolygonRowMapper implements RowMapper> { +public class PolygonRowMapper implements RowMapper> { @Override - public List mapRow(ResultSet rs, int rowNum) throws SQLException { + public List mapRow(ResultSet rs, int rowNum) throws SQLException { PGgeometry geometry = (PGgeometry) rs.getObject("polygon"); //PGPolygon int size = geometry.getGeometry().numPoints(); - List list = new ArrayList<>(); + List list = new ArrayList<>(); + Geometry geo = null; for (int i = 0; i < size; i++) { - Point p = geometry.getGeometry().getPoint(i); - GisPoint gp = new GisPoint(p.getX(), p.getY()); - list.add(gp); + org.postgis.Point p = geometry.getGeometry().getPoint(i); + Point point = new Point(p.getX(),p.getY()); + list.add(point); } return list; } 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 4e773c1..6008736 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,8 @@ package hello.agh.edu.bazy.row.mappers.gis; +import hello.agh.edu.bazy.model.Point; import hello.agh.edu.bazy.row.gis.GisPoint; -import hello.agh.edu.bazy.row.gis.GisPolygon; import org.postgis.PGgeometry; -import org.postgis.Point; import org.postgis.Polygon; import org.springframework.jdbc.core.RowMapper; @@ -15,17 +14,16 @@ 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 List mapRow(ResultSet rs, int rowNum) throws SQLException { PGgeometry geometry = (PGgeometry) rs.getObject("polygon"); int size = geometry.getGeometry().numPoints(); - List list = new ArrayList<>(); + List list = new ArrayList<>(); for (int i = 0; i < size; i++) { - Point p = geometry.getGeometry().getPoint(i); - GisPoint gp = new GisPoint(p.getX(), p.getY()); - list.add(gp); + Point p = new Point(geometry.getGeometry().getPoint(i).x,geometry.getGeometry().getPoint(i).y); + list.add(p); } return list; } diff --git a/src/main/java/hello/agh/edu/bazy/row/mappers/object/PointRowMapper.java b/src/main/java/hello/agh/edu/bazy/row/mappers/object/PointRowMapper.java new file mode 100644 index 0000000..0b3a47f --- /dev/null +++ b/src/main/java/hello/agh/edu/bazy/row/mappers/object/PointRowMapper.java @@ -0,0 +1,22 @@ +package hello.agh.edu.bazy.row.mappers.object; + +import hello.agh.edu.bazy.model.Location; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Created by mar on 27.01.15. + */ +public class PointRowMapper implements RowMapper { + + private static final String DESC = "description"; + + @Override + public Location mapRow(ResultSet rs, int rowNum) throws SQLException { + Location location = new Location(); + location.setDescription(rs.getString(DESC)); + return location; + } +} From d5a5cba4a902721cfdfef51b203ba9fd55297f97 Mon Sep 17 00:00:00 2001 From: lukasgol Date: Tue, 27 Jan 2015 20:56:05 +0100 Subject: [PATCH 2/2] refactor --- src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6a01390..ff4228f 100644 --- a/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java +++ b/src/main/java/hello/agh/edu/bazy/path/APIServicePaths.java @@ -59,7 +59,7 @@ public final class APIServicePaths { public static final String DELETE_CONFIGURATION = "/configuration"; public static final String DELETE_PROJECT = "/project"; - //PROFILE + //LOGI public static final String LOGS_CONTROLLER = "/logs"; public static final String PUT_LOG = "/log"; public static final String GET_LOGS = "/logs";