diff --git a/.classpath b/.classpath
index ebde520..8c947c2 100644
--- a/.classpath
+++ b/.classpath
@@ -8,9 +8,9 @@
+
-
@@ -23,5 +23,10 @@
+
+
+
+
+
diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs
index f897a7f..ff7698f 100644
--- a/.settings/org.eclipse.m2e.core.prefs
+++ b/.settings/org.eclipse.m2e.core.prefs
@@ -1,4 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
-resolveWorkspaceProjects=true
+resolveWorkspaceProjects=false
version=1
diff --git a/src/main/java/org/gcube/application/geoportal/PostgisDBManager.java b/src/main/java/org/gcube/application/geoportal/PostgisDBManager.java
index 33aa97b..b278ed2 100644
--- a/src/main/java/org/gcube/application/geoportal/PostgisDBManager.java
+++ b/src/main/java/org/gcube/application/geoportal/PostgisDBManager.java
@@ -1,14 +1,22 @@
package org.gcube.application.geoportal;
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
-import java.util.HashMap;
+import java.sql.Statement;
import org.gcube.application.geoportal.model.CentroidRecord;
+import lombok.extern.slf4j.Slf4j;
+@Slf4j
public class PostgisDBManager {
private static final String CENTROIDS_TABLE="centroids";
@@ -28,7 +36,7 @@ public class PostgisDBManager {
private static String user;
private static String pwd;
- public static void init(String url,String user, String pwd) {
+ public static void init(String url,String user, String pwd) throws SQLException, IOException {
PostgisDBManager.url=url;
PostgisDBManager.user=user;
PostgisDBManager.pwd=pwd;
@@ -38,19 +46,52 @@ public class PostgisDBManager {
}catch(Exception e){
throw new RuntimeException(e);
}
-
- System.out.println("Driver version: " + org.postgresql.Driver.getVersion());
-
- System.err.println("GOING TO USE DB "+url);
- System.err.println("STOP THIS IF THE ENVIRONMENT IS INCORRECT");
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- System.err.println("CONNECTING..");
+
+ log.info("Initializing Database..");
+
+ Connection conn=null;
+
+
+ File sqlFolder=getFileFromResources("sql");
+
+ if(!sqlFolder.isDirectory()) throw new RuntimeException(sqlFolder.getAbsolutePath()+" is not a folder.");
+ conn=DriverManager.getConnection(url,user,pwd);
+ conn.setAutoCommit(false);
+
+ Statement stmt=conn.createStatement();
+ for(File f:sqlFolder.listFiles()) {
+ log.info("Executing "+f.getName());
+ String sql=readFile(f.getAbsolutePath(),Charset.forName("UTF-8"));
+
+ stmt.execute(sql);
}
+
+ log.info("Committing..");
+ conn.commit();
+
}
+ private static String readFile(String path, Charset encoding)
+ throws IOException
+ {
+ byte[] encoded = Files.readAllBytes(Paths.get(path));
+ return new String(encoded, encoding);
+ }
+
+
+ private static File getFileFromResources(String fileName) {
+
+ ClassLoader classLoader =PostgisDBManager.class.getClassLoader();
+
+ URL resource = classLoader.getResource(fileName);
+ if (resource == null) {
+ throw new IllegalArgumentException("file is not found!");
+ } else {
+ return new File(resource.getFile());
+ }
+
+ }
private Connection conn=null;
@@ -62,8 +103,10 @@ public class PostgisDBManager {
conn=DriverManager.getConnection(url,user,pwd);
conn.setAutoCommit(false);
- insertCentroids=conn.prepareStatement("Insert into "+CENTROIDS_TABLE+" (datasetUUID,nome,anno,regione,the_geom) values(?,?,?,ST_GeomFromText(?, 4326))");
+
+ insertCentroids=conn.prepareStatement("Insert into "+CENTROIDS_TABLE+" (datasetUUID,nome,anno,regione,geom) values(?,?,?,ST_GeomFromText(?, 4326))");
}
+
public void commit() throws SQLException {
conn.commit();
diff --git a/src/main/resources/sql/centroids.sql b/src/main/resources/sql/centroids.sql
new file mode 100644
index 0000000..bd28edf
--- /dev/null
+++ b/src/main/resources/sql/centroids.sql
@@ -0,0 +1,11 @@
+CREATE TABLE IF NOT EXISTS ceontroids
+(
+ fid BIGSERIAL PRIMARY KEY,
+ datasetuuid character varying(40),
+ nome text,
+ anno int,
+ regione character varying(100),
+ geom geometry(POINT,4326)
+)WITH (
+ OIDS = FALSE
+)