33import java .sql .*;
44import java .util .ArrayList ;
55
6- import com .sun .xml .internal .ws .wsdl .writer .document .Import ;
76import de .htwb .model .imported .ImportedShape ;
87import de .htwb .model .ohdm .*;
98import org .postgis .*;
109import org .postgresql .PGConnection ;
1110
11+ import static de .htwb .utils .Config .*;
12+
1213public class DatabaseRepository {
1314
1415 private Connection connection ;
1516
16- private final String DB_USER = "geoserver" ;
17- private final String DB_PASS = "ohdm4ever!" ;
18- private final String DB_NAME = "ohdm_test" ;
19- private final String DB_HOST = "ohm.f4.htw-berlin.de" ;
20-
21- public final String TEMP_SCHEME = "temp" ;
22- public final String TEST_SCHEME = "ohm_test" ;
23- public final String PRODUCTIVE_SCHEME = "ohdm" ;
24-
2517 public void connect () throws SQLException
2618 {
2719 if (connection != null )
@@ -61,7 +53,7 @@ public ArrayList<ImportedShape> getImportedShapesFromCache(String tableName) thr
6153 if (connection == null )
6254 connect ();
6355
64- final String sql = String .format ("SELECT gid FROM \" %s\" .\" %s\" " , "importedCache" , tableName );
56+ final String sql = String .format ("SELECT gid FROM \" %s\" .\" %s\" " , SCHEME_CACHE , tableName );
6557 ArrayList <ImportedShape > importedShapes = new ArrayList <>();
6658 PreparedStatement stmtGetImportedShapes = connection .prepareStatement (sql );
6759
@@ -83,7 +75,7 @@ public ArrayList<ImportedShape> getImportedShapesFromTable(String tableName) thr
8375 if (connection == null )
8476 connect ();
8577
86- final String sql = String .format ("SELECT gid FROM \" %s\" .\" %s\" " , TEMP_SCHEME , tableName );
78+ final String sql = String .format ("SELECT gid FROM \" %s\" .\" %s\" " , SCHEME_TEMP , tableName );
8779 ArrayList <ImportedShape > importedShapes = new ArrayList <>();
8880 PreparedStatement stmtGetImportedShapes = connection .prepareStatement (sql );
8981
@@ -102,7 +94,7 @@ public ArrayList<ImportedShape> getImportedShapesFromTable(String tableName) thr
10294
10395 private ImportedShape getImportedShapeForId (String tableName , int id ) throws SQLException
10496 {
105- final String sql = String .format ("SELECT name, start_date, end_date FROM \" %s\" .\" %s\" WHERE gid = ?" , TEMP_SCHEME , tableName );
97+ final String sql = String .format ("SELECT name, start_date, end_date FROM \" %s\" .\" %s\" WHERE gid = ?" , SCHEME_TEMP , tableName );
10698 PreparedStatement ps = connection .prepareStatement (sql );
10799 ps .setInt (1 , id );
108100
@@ -122,7 +114,7 @@ private ImportedShape getImportedShapeForId(String tableName, int id) throws SQL
122114
123115 private ImportedShape getImportedShapeFromCache (String tableName , int id ) throws SQLException
124116 {
125- final String sql = String .format ("SELECT \" name\" , \" validSince\" , \" validUntil\" , \" geom\" , ST_ASGEOJSON(geom) FROM \" %s\" .\" %s\" WHERE gid = ?" , "importedCache" , tableName );
117+ final String sql = String .format ("SELECT \" name\" , \" validSince\" , \" validUntil\" , \" geom\" , ST_ASGEOJSON(geom) FROM \" %s\" .\" %s\" WHERE gid = ?" , SCHEME_CACHE , tableName );
126118 PreparedStatement ps = connection .prepareStatement (sql );
127119 ps .setInt (1 , id );
128120
@@ -146,7 +138,7 @@ private ImportedShape getImportedShapeFromCache(String tableName, int id) throws
146138 private ArrayList <Polygon > getPolygonsForId (String tableName , int id ) throws SQLException
147139 {
148140 ArrayList <Polygon > polygons = new ArrayList <>();
149- final String sql = String .format ("SELECT ST_GeometryN(geom, generate_series(1, ST_NumGeometries(geom))) FROM %s.\" %s\" WHERE gid = ?" , TEMP_SCHEME , tableName );
141+ final String sql = String .format ("SELECT ST_GeometryN(geom, generate_series(1, ST_NumGeometries(geom))) FROM %s.\" %s\" WHERE gid = ?" , SCHEME_TEMP , tableName );
150142 PreparedStatement stmtPolygons = connection .prepareStatement (sql );
151143 stmtPolygons .setInt (1 , id );
152144
@@ -225,7 +217,7 @@ private void insertOHDMPolygons(ArrayList<OHDMPolygon> polygons) throws SQLExcep
225217
226218 private void insertOHDMPolygon (OHDMPolygon polygon ) throws SQLException
227219 {
228- final String sql = String .format ("INSERT INTO %s.\" polygons\" (polygon, source_user_id) VALUES (?, ?) RETURNING id" , TEST_SCHEME );
220+ final String sql = String .format ("INSERT INTO %s.\" polygons\" (polygon, source_user_id) VALUES (?, ?) RETURNING id" , SCHEME_TEST );
229221 PreparedStatement stmt = connection .prepareStatement (sql );
230222 stmt .setObject (1 , new PGgeometry (polygon .getPolygon ()));
231223 stmt .setLong (2 , polygon .getIdSourceUser ());
@@ -242,7 +234,7 @@ private void insertOHDMPolygon(OHDMPolygon polygon) throws SQLException
242234
243235 private void insertGeoobject (Geoobject geoobject ) throws SQLException
244236 {
245- final String sql = String .format ("INSERT INTO %s.\" geoobject\" (name, source_user_id) VALUES (?, ?) RETURNING id" , TEST_SCHEME );
237+ final String sql = String .format ("INSERT INTO %s.\" geoobject\" (name, source_user_id) VALUES (?, ?) RETURNING id" , SCHEME_TEST );
246238 PreparedStatement stmt = connection .prepareStatement (sql );
247239 stmt .setString (1 , geoobject .getName ());
248240 stmt .setLong (2 , geoobject .getIdSourceUser ());
@@ -265,7 +257,7 @@ private void insertGeoobjectGeometries(ArrayList<GeoobjectGeometry> geoobjectGeo
265257
266258 private void insertGeoobjectGeometry (GeoobjectGeometry geoobjectGeometry ) throws SQLException
267259 {
268- final String sql = String .format ("INSERT INTO %s.\" geoobject_geometry\" (id_target, type_target, id_geoobject_source, classification_id, valid_since, valid_until, source_user_id) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id" , TEST_SCHEME );
260+ final String sql = String .format ("INSERT INTO %s.\" geoobject_geometry\" (id_target, type_target, id_geoobject_source, classification_id, valid_since, valid_until, source_user_id) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id" , SCHEME_TEST );
269261 PreparedStatement stmt = connection .prepareStatement (sql );
270262 stmt .setLong (1 , geoobjectGeometry .getIdTarget ());
271263 stmt .setLong (2 , geoobjectGeometry .getIdTargetType ());
@@ -295,8 +287,8 @@ private void insertGeoobjectGeometry(GeoobjectGeometry geoobjectGeometry) throws
295287
296288 public void dropTable (String tableName ) throws SQLException
297289 {
298- final String dropCommand = String .format ("DROP TABLE %s.\" %s\" " , TEMP_SCHEME , tableName );
299- PreparedStatement stmt = connection .prepareStatement (dropCommand );
290+ final String sql = String .format ("DROP TABLE %s.\" %s\" " , SCHEME_TEMP , tableName );
291+ PreparedStatement stmt = connection .prepareStatement (sql );
300292
301293 if (!stmt .execute ())
302294 {
@@ -306,7 +298,7 @@ public void dropTable(String tableName) throws SQLException
306298
307299 public ArrayList <OHDMClassification > getClassifications () throws SQLException
308300 {
309- final String sql = String .format ("SELECT * FROM ohm_test .\" classification\" " );
301+ final String sql = String .format ("SELECT * FROM " + SCHEME_TEST + " .\" classification\" " );
310302 PreparedStatement stmt = connection .prepareStatement (sql );
311303
312304 ResultSet rs = stmt .executeQuery ();
@@ -344,7 +336,7 @@ public void transferShapesToCache(String tableName, String userName) throws Exce
344336
345337 private boolean createCacheTable (String tableName ) throws Exception
346338 {
347- final String sql = String .format ("CREATE TABLE \" importedCache \" .\" %s\" \n " +
339+ final String sql = String .format ("CREATE TABLE \" " + SCHEME_CACHE + " \" .\" %s\" \n " +
348340 "(\n " +
349341 " gid SERIAL,\n " +
350342 " name character varying(254),\n " +
@@ -359,7 +351,7 @@ private boolean createCacheTable(String tableName) throws Exception
359351 " OIDS = FALSE\n " +
360352 ");\n " +
361353 "\n " +
362- "ALTER TABLE \" importedCache \" .\" %s\" \n " +
354+ "ALTER TABLE \" " + SCHEME_CACHE + " \" .\" %s\" \n " +
363355 " OWNER to geoserver;" , tableName , tableName );
364356
365357 PreparedStatement ps = connection .prepareStatement (sql );
@@ -369,7 +361,7 @@ private boolean createCacheTable(String tableName) throws Exception
369361
370362 private void insertShapeIntoCache (String tableName , ImportedShape shape , String userName ) throws Exception
371363 {
372- final String sql = String .format ("INSERT INTO \" importedCache \" .\" %s\" (\" name\" , \" validSince\" , \" validUntil\" , \" classId\" , \" username\" , \" geom\" ) VALUES(?, ?, ?, ?, ?, ?)" , tableName );
364+ final String sql = String .format ("INSERT INTO \" " + SCHEME_CACHE + " \" .\" %s\" (\" name\" , \" validSince\" , \" validUntil\" , \" classId\" , \" username\" , \" geom\" ) VALUES(?, ?, ?, ?, ?, ?)" , tableName );
373365 PreparedStatement ps = connection .prepareStatement (sql );
374366
375367 ps .setString (1 , shape .getName ());
@@ -392,7 +384,7 @@ private void insertShapeIntoCache(String tableName, ImportedShape shape, String
392384
393385 public void updateImportedShape (ImportedShape updatedImportedShape , String table ) throws Exception
394386 {
395- final String sql = String .format ("UPDATE \" importedCache \" .\" %s\" SET \" name\" = ?, \" validSince\" = ?, \" validUntil\" = ?, \" classId\" = ? WHERE gid = ?" , table );
387+ final String sql = String .format ("UPDATE \" " + SCHEME_CACHE + " \" .\" %s\" SET \" name\" = ?, \" validSince\" = ?, \" validUntil\" = ?, \" classId\" = ? WHERE gid = ?" , table );
396388
397389 PreparedStatement ps = connection .prepareStatement (sql );
398390
0 commit comments