Skip to content

Commit 0508be0

Browse files
committed
added constants for configuration
1 parent 6b20707 commit 0508be0

4 files changed

Lines changed: 46 additions & 30 deletions

File tree

.idea/OHDMImportTool.iml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/webapp/java/de/htwb/model/DatabaseRepository.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@
33
import java.sql.*;
44
import java.util.ArrayList;
55

6-
import com.sun.xml.internal.ws.wsdl.writer.document.Import;
76
import de.htwb.model.imported.ImportedShape;
87
import de.htwb.model.ohdm.*;
98
import org.postgis.*;
109
import org.postgresql.PGConnection;
1110

11+
import static de.htwb.utils.Config.*;
12+
1213
public 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

src/main/webapp/java/de/htwb/shpImport/ShapeImporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import de.htwb.model.imported.ImportedShape;
1515
import org.apache.commons.io.*;
1616

17+
import static de.htwb.utils.Config.*;
18+
1719
public class ShapeImporter
1820
{
19-
private final String SHP_TO_PGSQL_FILE_PATH = "C:\\Program Files\\PostgreSQL\\11\\bin\\shp2pgsql.exe";
20-
private final String PGSQL_FILE_PATH = "C:\\Program Files\\PostgreSQL\\11\\bin\\psql.exe";
2121
private final int BUFFER_SIZE = 8 * 1024;
2222

2323
private final File uploadedFilesDir = new File("uploaded");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
package de.htwb.utils;
22

33
public class Config {
4+
5+
/*
6+
required database settings
7+
*/
8+
9+
public static final String DB_USER = "";
10+
public static final String DB_PASS = "";
11+
public static final String DB_NAME = "";
12+
public static final String DB_HOST = "";
13+
14+
public static final String SCHEME_TEMP = "temp";
15+
public static final String SCHEME_TEST = "test";
16+
public static final String SCHEME_CACHE = "importedCache";
17+
public static final String SCHEME_PRODUKTIVE = "ohdm";
18+
19+
/*
20+
required Postgres application settings with examples
21+
*/
22+
23+
// for macOS
24+
public static final String SHP_TO_PGSQL_FILE_PATH = "/Applications/Postgres.app/Contents/Versions/12/bin/shp2pgsql";
25+
public static final String PGSQL_FILE_PATH = "/Applications/Postgres.app/Contents/Versions/12/bin/psql";
26+
27+
// for Windows
28+
//public static final String SHP_TO_PGSQL_FILE_PATH = "C:\\Program Files\\PostgreSQL\\12\\bin\\shp2pgsql.exe";
29+
//public static final String PGSQL_FILE_PATH = "C:\\Program Files\\PostgreSQL\\12\\bin\\psql.exe";
30+
431
}

0 commit comments

Comments
 (0)