This commit is contained in:
Marcin Michałek committed 2015-01-27 21:05:27 +01:00
commit a2291465d3
10 files changed
+182 -28

No files matched your search

@@ -1,7 +1,9 @@
package hello.agh.edu.bazy.controllers; package hello.agh.edu.bazy.controllers;
import hello.agh.edu.bazy.dbApi.gis.GisDbApi; 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.Location;
import hello.agh.edu.bazy.model.Point;
import hello.agh.edu.bazy.path.APIServicePaths; import hello.agh.edu.bazy.path.APIServicePaths;
import hello.agh.edu.bazy.row.gis.GisPoint; import hello.agh.edu.bazy.row.gis.GisPoint;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -21,23 +23,50 @@ public class GisController {
@Autowired @Autowired
GisDbApi gisDbApi; GisDbApi gisDbApi;
@Autowired
ObjectDbApi objectDbApi;
@RequestMapping(APIServicePaths.GET_ALL_LAMPS) @RequestMapping(APIServicePaths.GET_ALL_LAMPS)
@ResponseBody @ResponseBody
List<GisPoint> getLamps() { List<GisPoint> getLamps() {
return gisDbApi.getAllLamps();
List<GisPoint> 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}") @RequestMapping(value = APIServicePaths.GET_LAMP + "/{id}")
@ResponseBody @ResponseBody
List<GisPoint> getLamp(@PathVariable("id") Long id) { Location getLamp(@PathVariable("id") Long id) {
List<GisPoint> polygon = gisDbApi.getPolygon(id); Location location = objectDbApi.getObject(id);
return gisDbApi.getLamp(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 @ResponseBody
int putLamp(@RequestBody Location location, @PathVariable("id") Long id) { List<Point> getPolygon(@PathVariable("id") long id) {
return gisDbApi.putLamp(id, location.getX(), location.getY()); return gisDbApi.getPolygon(id);
}
@RequestMapping(value = APIServicePaths.GET_POLYGON)
@ResponseBody
List<List<Point>> 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) @RequestMapping(value = APIServicePaths.DELETE_LAMP + "/{id}", method = RequestMethod.DELETE)
@@ -1,5 +1,6 @@
package hello.agh.edu.bazy.dbApi.gis; 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.postgresql.gis.GisDataSource;
import hello.agh.edu.bazy.row.gis.GisPoint; import hello.agh.edu.bazy.row.gis.GisPoint;
import hello.agh.edu.bazy.row.mappers.gis.PointRowMapper; 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 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 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 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; JdbcTemplate gisJdbcTemplate;
@@ -57,16 +59,16 @@ public class GisDbApi {
return gisJdbcTemplate.update(sql); return gisJdbcTemplate.update(sql);
} }
public List<List<GisPoint>> getPolygons(){ public List<List<Point>> getPolygons(){
return gisJdbcTemplate.query(GET_POLYGONS, new PolygonsRowMapper()); return gisJdbcTemplate.query(GET_POLYGONS, new PolygonsRowMapper());
} }
public List<GisPoint> getPolygon(long id){ public List<Point> getPolygon(long id){
List<List<GisPoint>> polygons =gisJdbcTemplate.query(GET_POLYGON, new Object[]{id}, new PolygonRowMapper()); List<List<Point>> polygons =gisJdbcTemplate.query(GET_POLYGON, new Object[]{id}, new PolygonRowMapper());
return polygons.size()>0?polygons.get(0):null; return polygons.size()>0?polygons.get(0):null;
} }
//public Lamp get
public void setGisJdbcTemplate(JdbcTemplate gisJdbcTemplate) { public void setGisJdbcTemplate(JdbcTemplate gisJdbcTemplate) {
this.gisJdbcTemplate = gisJdbcTemplate; this.gisJdbcTemplate = gisJdbcTemplate;
} }
@@ -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);
}
}
@@ -1,11 +1,14 @@
package hello.agh.edu.bazy.model; package hello.agh.edu.bazy.model;
import java.util.List;
/** /**
* Created by Marcin on 2015-01-25. * Created by Marcin on 2015-01-25.
*/ */
public class Location { public class Location {
private double x; private double x;
private double y; private double y;
private String description;
public double getX() { public double getX() {
return x; return x;
@@ -22,4 +25,13 @@ public class Location {
public void setY(double y) { public void setY(double y) {
this.y = y; this.y = y;
} }
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }
@@ -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;
}
}
@@ -11,7 +11,6 @@ public final class APIServicePaths {
private APIServicePaths() { private APIServicePaths() {
} }
@@ -28,6 +27,9 @@ public final class APIServicePaths {
public static final String PUT_LAMP = "/lamp"; public static final String PUT_LAMP = "/lamp";
public static final String DELETE_LAMP = "/lamp"; public static final String DELETE_LAMP = "/lamp";
public static final String GET_LAMP = "/lamp"; public static final String GET_LAMP = "/lamp";
public static final String GET_POLYGON = "polygon";
//SENSORS //SENSORS
public static final String SENSORS_CONTROLLER = "/sensors"; public static final String SENSORS_CONTROLLER = "/sensors";
@@ -57,7 +59,7 @@ public final class APIServicePaths {
public static final String DELETE_CONFIGURATION = "/configuration"; public static final String DELETE_CONFIGURATION = "/configuration";
public static final String DELETE_PROJECT = "/project"; public static final String DELETE_PROJECT = "/project";
//PROFILE //LOGI
public static final String LOGS_CONTROLLER = "/logs"; public static final String LOGS_CONTROLLER = "/logs";
public static final String PUT_LOG = "/log"; public static final String PUT_LOG = "/log";
public static final String GET_LOGS = "/logs"; public static final String GET_LOGS = "/logs";
@@ -7,6 +7,7 @@ public class GisPoint {
private long id; private long id;
private double x; private double x;
private double y; private double y;
private String description;
public long getId() { public long getId() {
return id; return id;
@@ -42,4 +43,12 @@ public class GisPoint {
public void setY(double y) { public void setY(double y) {
this.y = y; this.y = y;
} }
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }
@@ -1,8 +1,10 @@
package hello.agh.edu.bazy.row.mappers.gis; 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.GisPoint;
import org.postgis.Geometry;
import org.postgis.PGgeometry; import org.postgis.PGgeometry;
import org.postgis.Point; import org.postgresql.geometric.PGpolygon;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet; import java.sql.ResultSet;
@@ -13,18 +15,19 @@ import java.util.List;
/** /**
* Created by lukasgol on 26.01.15. * Created by lukasgol on 26.01.15.
*/ */
public class PolygonRowMapper implements RowMapper<List<GisPoint>> { public class PolygonRowMapper implements RowMapper<List<Point>> {
@Override @Override
public List<GisPoint> mapRow(ResultSet rs, int rowNum) throws SQLException { public List<Point> mapRow(ResultSet rs, int rowNum) throws SQLException {
PGgeometry geometry = (PGgeometry) rs.getObject("polygon"); PGgeometry geometry = (PGgeometry) rs.getObject("polygon");
//PGPolygon //PGPolygon
int size = geometry.getGeometry().numPoints(); int size = geometry.getGeometry().numPoints();
List<GisPoint> list = new ArrayList<>(); List<hello.agh.edu.bazy.model.Point> list = new ArrayList<>();
Geometry geo = null;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Point p = geometry.getGeometry().getPoint(i); org.postgis.Point p = geometry.getGeometry().getPoint(i);
GisPoint gp = new GisPoint(p.getX(), p.getY()); Point point = new Point(p.getX(),p.getY());
list.add(gp); list.add(point);
} }
return list; return list;
} }
@@ -1,9 +1,8 @@
package hello.agh.edu.bazy.row.mappers.gis; 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.GisPoint;
import hello.agh.edu.bazy.row.gis.GisPolygon;
import org.postgis.PGgeometry; import org.postgis.PGgeometry;
import org.postgis.Point;
import org.postgis.Polygon; import org.postgis.Polygon;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
@@ -15,17 +14,16 @@ import java.util.List;
/** /**
* Created by lukasgol on 26.01.15. * Created by lukasgol on 26.01.15.
*/ */
public class PolygonsRowMapper implements RowMapper<List<GisPoint>> { public class PolygonsRowMapper implements RowMapper<List<Point>> {
@Override @Override
public List<GisPoint> mapRow(ResultSet rs, int rowNum) throws SQLException { public List<Point> mapRow(ResultSet rs, int rowNum) throws SQLException {
PGgeometry geometry = (PGgeometry) rs.getObject("polygon"); PGgeometry geometry = (PGgeometry) rs.getObject("polygon");
int size = geometry.getGeometry().numPoints(); int size = geometry.getGeometry().numPoints();
List<GisPoint> list = new ArrayList<>(); List<Point> list = new ArrayList<>();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Point p = geometry.getGeometry().getPoint(i); Point p = new Point(geometry.getGeometry().getPoint(i).x,geometry.getGeometry().getPoint(i).y);
GisPoint gp = new GisPoint(p.getX(), p.getY()); list.add(p);
list.add(gp);
} }
return list; return list;
} }
@@ -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<Location> {
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;
}
}