Initialize DB

This commit is contained in:
FabioISTI 2020-03-24 17:24:12 +01:00
parent c4cd11b370
commit 8b512552f8
4 changed files with 73 additions and 14 deletions

View File

@ -8,9 +8,9 @@
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
@ -23,5 +23,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,4 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
resolveWorkspaceProjects=false
version=1

View File

@ -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();

View File

@ -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
)