From e0266ddbe05cfd5e0f6660fa56aeea6e72ab8753 Mon Sep 17 00:00:00 2001 From: "fabio.sinibaldi" Date: Mon, 14 Jan 2019 13:07:39 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application/perform-service@176527 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../service/engine/DataBaseManagerImpl.java | 4 +- .../service/engine/MappingManagerImpl.java | 9 +- .../perform/service/engine/Queries.java | 30 +++--- .../perform/service/engine/Query.java | 6 +- .../perform/service/engine/model/DBField.java | 21 +++- .../model/DatabaseConnectionDescriptor.java | 5 + .../engine/model/anagraphic/Batch.java | 10 +- .../service/engine/model/anagraphic/Farm.java | 47 ++++----- .../perform/service/rest/Mappings.java | 4 +- .../perform/service/AnagraphicTests.java | 4 +- .../perform/service/InitializeDataBase.java | 96 +++++++++++++++++++ 11 files changed, 182 insertions(+), 54 deletions(-) create mode 100644 src/test/java/org/gcube/application/perform/service/InitializeDataBase.java diff --git a/src/main/java/org/gcube/application/perform/service/engine/DataBaseManagerImpl.java b/src/main/java/org/gcube/application/perform/service/engine/DataBaseManagerImpl.java index 08a65b6..d60e688 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/DataBaseManagerImpl.java +++ b/src/main/java/org/gcube/application/perform/service/engine/DataBaseManagerImpl.java @@ -57,7 +57,9 @@ public class DataBaseManagerImpl implements DataBaseManager{ @Override public Connection getConnection() throws SQLException, InternalException { DataSource ds=getDataSource(); - return ds.getConnection(); + Connection conn=ds.getConnection(); + conn.setAutoCommit(false); + return conn; } diff --git a/src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java b/src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java index 1c73f92..8d23448 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java +++ b/src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java @@ -4,6 +4,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.UUID; import org.gcube.application.perform.service.LocalConfiguration; import org.gcube.application.perform.service.engine.model.BeanNotFound; @@ -40,6 +41,7 @@ public class MappingManagerImpl implements MappingManager { public Batch getBatch(DBQueryDescriptor desc) throws SQLException, InternalException{ DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); Connection conn=db.getConnection(); + conn.setAutoCommit(true); Query getQuery=Queries.GET_BATCH_BY_DESCRIPTIVE_KEY; PreparedStatement psSearch=getQuery.get(conn, desc); @@ -51,13 +53,10 @@ public class MappingManagerImpl implements MappingManager { // ID NOT FOUND, TRY TO REGISTER IT log.trace("Registering new Batch from condition {}",desc); + desc.add(DBField.Batch.fields.get(DBField.Batch.UUID), UUID.randomUUID()); PreparedStatement psInsert=Queries.INSERT_BATCH.get(conn, desc); - boolean inserted=psInsert.executeUpdate()==1; - if(inserted) { - conn.commit(); - log.trace("Committed Batch."); - } + psInsert.executeUpdate(); rs=psSearch.executeQuery(); if(rs.next()) diff --git a/src/main/java/org/gcube/application/perform/service/engine/Queries.java b/src/main/java/org/gcube/application/perform/service/engine/Queries.java index ddbd6d3..5b10fb8 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/Queries.java +++ b/src/main/java/org/gcube/application/perform/service/engine/Queries.java @@ -2,6 +2,7 @@ package org.gcube.application.perform.service.engine; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.UUID; import org.gcube.application.perform.service.engine.model.DBField; import org.gcube.application.perform.service.engine.model.DBField.ImportRoutine; @@ -17,23 +18,25 @@ public class Queries { DBField.Batch.fields.get(DBField.Batch.BATCH_TYPE), DBField.Batch.fields.get(DBField.Batch.BATCH_NAME)}); - public static final Query INSERT_BATCH=new Query("INSERT INTO batches (id,uuid,farmid,type,name) VALUES (SELECT (max(id)+1 from batches),?,?,?,?) ON CONFLICT DO NOTHING", - new DBField[] {}); + public static final Query INSERT_BATCH=new Query("INSERT INTO batches (uuid,farmid,type,name) VALUES (?,?,?,?) ON CONFLICT DO NOTHING", + new DBField[] {DBField.Batch.fields.get(DBField.Batch.UUID), + DBField.Batch.fields.get(DBField.Batch.FARM_ID), + DBField.Batch.fields.get(DBField.Batch.BATCH_TYPE), + DBField.Batch.fields.get(DBField.Batch.BATCH_NAME)}); - public static final Query GET_FARM_BY_ID=new Query(String.format("SELECT f.farmid as $1%s, f.uuid as %2$s, c.companyid as %3$s, c.uuid as %4$s, a.associationid as %5$s, a.uuid as %6$s FROM " - + "farms as f INNER JOIN companies as c ON f.companyid=c.companyid INNER JOIN associations as a ON c.associationid=a.associationid WHERE f.farmid = ?", - DBField.Farm.FARM_ID,DBField.Farm.UUID,DBField.Farm.COMPANY_ID,DBField.Farm.COMPANY_UUID,DBField.Farm.ASSOCIATION_ID,DBField.Farm.ASSOCIATION_UUID), - new DBField[] {}); + public static final Query GET_FARM_BY_ID=new Query("SELECT * from completefarms WHERE id = ?", + new DBField[] {DBField.Farm.fields.get(DBField.Farm.FARM_ID)}); public static final Query GET_BATCH_BY_ID=new Query("Select * from batches where id = ?", - new DBField[] {}); - + new DBField[] {DBField.Batch.fields.get(DBField.Batch.BATCH_ID)}); + + // Imports with lock = hostname or lock == null + public static final Query ORPHAN_IMPORTS=new Query("SELECT * from imports where ",new DBField[]{}); // "acquire" // set lock = hostname where ID =? and LOCK is null // Acquired = updated rows == 1 - public static final Query ORPHAN_IMPORTS=new Query("",new DBField[]{}); - public static final Query ACQUIRE_PS=new Query("",new DBField[]{}); + public static final Query ACQUIRE_PS=new Query("Update ",new DBField[]{}); public static final Query GET_BY_ID=new Query("",new DBField[]{}); public static final Query INSERT_ROUTINE=new Query("",new DBField[]{}); @@ -49,6 +52,7 @@ public class Queries { toReturn.setId(rs.getLong(DBField.Batch.BATCH_ID)); toReturn.setName(rs.getString(DBField.Batch.BATCH_NAME)); toReturn.setType(rs.getString(DBField.Batch.BATCH_TYPE)); + toReturn.setUuid((UUID)rs.getObject(DBField.Batch.UUID)); return toReturn; } @@ -56,11 +60,11 @@ public class Queries { public static Farm rowToFarm(ResultSet rs) throws SQLException { Farm toReturn=new Farm(); toReturn.setAssociationId(rs.getLong(DBField.Farm.ASSOCIATION_ID)); - toReturn.setAssociationUUID(rs.getString(DBField.Farm.ASSOCIATION_UUID)); + toReturn.setAssociationUUID((UUID)rs.getObject(DBField.Farm.ASSOCIATION_UUID)); toReturn.setCompanyId(rs.getLong(DBField.Farm.COMPANY_ID)); - toReturn.setCompanyUUID(rs.getString(DBField.Farm.COMPANY_UUID)); + toReturn.setCompanyUUID((UUID)rs.getObject(DBField.Farm.COMPANY_UUID)); toReturn.setId(rs.getLong(DBField.Farm.FARM_ID)); - toReturn.setUUID(rs.getString(DBField.Farm.UUID)); + toReturn.setUUID((UUID)rs.getObject(DBField.Farm.UUID)); return toReturn; } diff --git a/src/main/java/org/gcube/application/perform/service/engine/Query.java b/src/main/java/org/gcube/application/perform/service/engine/Query.java index 72a6180..30244ad 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/Query.java +++ b/src/main/java/org/gcube/application/perform/service/engine/Query.java @@ -47,7 +47,11 @@ public class Query { DBField field=fields.get(i); if(!desc.getCondition().containsKey(field)) throw new InvalidRequestException("Missing field "+field); - else ps.setObject(i+1, desc.getCondition().get(field), field.getType()); + else { + if(field.getType()==Integer.MIN_VALUE) // UUID EXCEPTION + ps.setObject(i+1, desc.getCondition().get(field)); + else ps.setObject(i+1, desc.getCondition().get(field), field.getType()); + } } return ps; } diff --git a/src/main/java/org/gcube/application/perform/service/engine/model/DBField.java b/src/main/java/org/gcube/application/perform/service/engine/model/DBField.java index f5eeaf6..d55ade9 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/model/DBField.java +++ b/src/main/java/org/gcube/application/perform/service/engine/model/DBField.java @@ -8,6 +8,9 @@ import java.util.Map; public class DBField { public static class Batch{ + + public static final String TABLE="batches"; + public static final Map fields=new HashMap<>(); public static final String BATCH_ID="id"; @@ -18,7 +21,7 @@ public class DBField { static { fields.put(BATCH_ID, new DBField(Types.BIGINT,BATCH_ID)); - fields.put(UUID, new DBField(Types.VARCHAR,UUID)); + fields.put(UUID, new DBField(Integer.MIN_VALUE,UUID)); fields.put(FARM_ID, new DBField(Types.BIGINT,FARM_ID)); fields.put(BATCH_TYPE, new DBField(Types.VARCHAR,BATCH_TYPE)); fields.put(BATCH_NAME, new DBField(Types.VARCHAR,BATCH_NAME)); @@ -26,6 +29,9 @@ public class DBField { } public static class Farm{ + + public static final String TABLE="completefarms"; + public static final Map fields=new HashMap<>(); public static final String FARM_ID="id"; @@ -39,7 +45,7 @@ public class DBField { fields.put(FARM_ID, new DBField(Types.BIGINT,FARM_ID)); fields.put(COMPANY_ID, new DBField(Types.BIGINT,COMPANY_ID)); fields.put(ASSOCIATION_ID, new DBField(Types.BIGINT,ASSOCIATION_ID)); - fields.put(UUID, new DBField(Types.NVARCHAR,UUID)); + fields.put(UUID, new DBField(Integer.MIN_VALUE,UUID)); fields.put(COMPANY_UUID, new DBField(Types.NVARCHAR,COMPANY_UUID)); fields.put(ASSOCIATION_UUID, new DBField(Types.NVARCHAR,ASSOCIATION_UUID)); @@ -47,6 +53,8 @@ public class DBField { } public static class ImportRoutine{ + public static final String TABLE="imports"; + public static final Map fields=new HashMap<>(); public static final String ID="id"; @@ -55,8 +63,8 @@ public class DBField { public static final String SOURCE_URL="sourceurl"; public static final String SOURCE_VERSION="sourceversion"; - public static final String START="start"; - public static final String END="end"; + public static final String START="start_time"; + public static final String END="end_time"; public static final String STATUS="status"; public static final String CALLER="caller"; public static final String COMPUTATION_ID="computation_id"; @@ -131,6 +139,11 @@ public class DBField { return false; return true; } + + @Override + public String toString() { + return "DBField ["+fieldName+"]"; + } diff --git a/src/main/java/org/gcube/application/perform/service/engine/model/DatabaseConnectionDescriptor.java b/src/main/java/org/gcube/application/perform/service/engine/model/DatabaseConnectionDescriptor.java index 71b034f..28e5e15 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/model/DatabaseConnectionDescriptor.java +++ b/src/main/java/org/gcube/application/perform/service/engine/model/DatabaseConnectionDescriptor.java @@ -8,6 +8,11 @@ public class DatabaseConnectionDescriptor { + @Override + public String toString() { + return "DatabaseConnectionDescriptor [username=" + username + ", url=" + url + "]"; + } + public DatabaseConnectionDescriptor(String username, String url, String password) { super(); this.username = username; diff --git a/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Batch.java b/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Batch.java index c4629d4..14b8c73 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Batch.java +++ b/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Batch.java @@ -1,5 +1,7 @@ package org.gcube.application.perform.service.engine.model.anagraphic; +import java.util.UUID; + public class Batch { @@ -11,10 +13,10 @@ public class Batch { public void setId(Long id) { this.id = id; } - public String getUuid() { + public UUID getUuid() { return uuid; } - public void setUuid(String uuid) { + public void setUuid(UUID uuid) { this.uuid = uuid; } public String getName() { @@ -39,7 +41,7 @@ public class Batch { public Batch() { // TODO Auto-generated constructor stub } - public Batch(Long id, String uuid, String name, String type, Long farmId) { + public Batch(Long id, UUID uuid, String name, String type, Long farmId) { super(); this.id = id; this.uuid = uuid; @@ -48,7 +50,7 @@ public class Batch { this.farmId = farmId; } private Long id; - private String uuid; + private UUID uuid; private String name; private String type; private Long farmId; diff --git a/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Farm.java b/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Farm.java index 69c24b6..64d50cd 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Farm.java +++ b/src/main/java/org/gcube/application/perform/service/engine/model/anagraphic/Farm.java @@ -1,12 +1,24 @@ package org.gcube.application.perform.service.engine.model.anagraphic; +import java.util.UUID; + public class Farm { - - public Farm() { - // TODO Auto-generated constructor stub - } + +public Farm(Long id, Long companyId, Long associationId, java.util.UUID uUID, java.util.UUID companyUUID, + java.util.UUID associationUUID) { + super(); + this.id = id; + this.companyId = companyId; + this.associationId = associationId; + UUID = uUID; + this.companyUUID = companyUUID; + this.associationUUID = associationUUID; + } +public Farm() { + // TODO Auto-generated constructor stub +} public Long getId() { return id; } @@ -25,37 +37,28 @@ public class Farm { public void setAssociationId(Long associationId) { this.associationId = associationId; } - public String getUUID() { + public UUID getUUID() { return UUID; } - public void setUUID(String uUID) { + public void setUUID(UUID uUID) { UUID = uUID; } - public String getCompanyUUID() { + public UUID getCompanyUUID() { return companyUUID; } - public void setCompanyUUID(String companyUUID) { + public void setCompanyUUID(UUID companyUUID) { this.companyUUID = companyUUID; } - public String getAssociationUUID() { + public UUID getAssociationUUID() { return associationUUID; } - public void setAssociationUUID(String associationUUID) { - this.associationUUID = associationUUID; - } - public Farm(Long id, Long companyId, Long associationId, String uUID, String companyUUID, String associationUUID) { - super(); - this.id = id; - this.companyId = companyId; - this.associationId = associationId; - UUID = uUID; - this.companyUUID = companyUUID; + public void setAssociationUUID(UUID associationUUID) { this.associationUUID = associationUUID; } private Long id; private Long companyId; private Long associationId; - private String UUID; - private String companyUUID; - private String associationUUID; + private UUID UUID; + private UUID companyUUID; + private UUID associationUUID; } diff --git a/src/main/java/org/gcube/application/perform/service/rest/Mappings.java b/src/main/java/org/gcube/application/perform/service/rest/Mappings.java index 3115a18..293e00b 100644 --- a/src/main/java/org/gcube/application/perform/service/rest/Mappings.java +++ b/src/main/java/org/gcube/application/perform/service/rest/Mappings.java @@ -80,7 +80,7 @@ public class Mappings { return mappings.getBatch(desc); }catch(BeanNotFound e) { log.debug("Exception while getting Batch",e); - throw new WebApplicationException("Unable to find Batch with condition "+desc,Response.Status.BAD_REQUEST); + throw new WebApplicationException("Unable to find Batch with condition "+desc,Response.Status.NOT_FOUND); }catch(SQLException e) { log.debug("Exception while getting Batch",e); throw new WebApplicationException("Unexpected Exception occurred while dealing with database.", e,Response.Status.INTERNAL_SERVER_ERROR); @@ -130,7 +130,7 @@ public class Mappings { return mappings.getFarm(desc); }catch(BeanNotFound e) { log.debug("Exception while getting Farm",e); - throw new WebApplicationException("Unable to find Farm with condition "+desc,Response.Status.BAD_REQUEST); + throw new WebApplicationException("Unable to find Farm with condition "+desc,Response.Status.NOT_FOUND); }catch(SQLException e) { log.debug("Exception while getting Farm",e); throw new WebApplicationException("Unexpected Exception occurred while dealing with database.", e,Response.Status.INTERNAL_SERVER_ERROR); diff --git a/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java b/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java index ddd8fce..7414861 100644 --- a/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java +++ b/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java @@ -44,7 +44,7 @@ public class AnagraphicTests extends JerseyTest{ path(ServiceConstants.Mappings.BATCHES_METHOD). queryParam(ServiceConstants.Mappings.BATCH_NAME_PARAMETER, "gino"). queryParam(ServiceConstants.Mappings.BATCH_TYPE_PARAMETER, "pino"). - queryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER, 1234); + queryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER, 12682549); System.out.println(target.getUri()); Response resp=target.request().get(); @@ -58,7 +58,7 @@ public class AnagraphicTests extends JerseyTest{ // path(ServiceConstants.APPLICATION_PATH). target(ServiceConstants.Mappings.PATH). path(ServiceConstants.Mappings.FARM_METHOD). - queryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER, 12334). + queryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER, 126825). queryParam(ServiceConstants.Mappings.FARM_UUID_PARAMETER, "pino"); System.out.println(target.getUri()); diff --git a/src/test/java/org/gcube/application/perform/service/InitializeDataBase.java b/src/test/java/org/gcube/application/perform/service/InitializeDataBase.java new file mode 100644 index 0000000..972ce37 --- /dev/null +++ b/src/test/java/org/gcube/application/perform/service/InitializeDataBase.java @@ -0,0 +1,96 @@ +package org.gcube.application.perform.service; + +import java.net.MalformedURLException; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.gcube.application.perform.service.engine.DataBaseManager; +import org.gcube.application.perform.service.engine.model.DBField.Batch; +import org.gcube.application.perform.service.engine.model.DBField.Farm; +import org.gcube.application.perform.service.engine.model.DBField.ImportRoutine; +import org.gcube.application.perform.service.engine.model.ISQueryDescriptor; +import org.gcube.application.perform.service.engine.model.InternalException; + +public class InitializeDataBase { + + public static void main(String[] args) throws MalformedURLException, SQLException, InternalException { + TokenSetter.set("/gcube/preprod/preVRE"); + LocalConfiguration.init(Paths.get("src/main/webapp/WEB-INF/config.properties").toUri().toURL()); + + ISQueryDescriptor desc= new ISQueryDescriptor( + LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_NAME), null, + LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_CATEGORY)); + + + DataBaseManager db=DataBaseManager.get(desc); + Connection conn=db.getConnection(); + + Statement stmt=conn.createStatement(); + + // CREATE BATCHES + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS "+Batch.TABLE+" (" + + Batch.BATCH_ID+" bigserial NOT NULL," + + Batch.UUID+" uuid NOT NULL," + + Batch.FARM_ID+" bigint NOT NULL," + + Batch.BATCH_TYPE+" varchar(100)," + + Batch.BATCH_NAME+" text," + + "PRIMARY KEY ("+Batch.BATCH_ID+")," + + "FOREIGN KEY ("+Batch.FARM_ID+") REFERENCES farms(farmid))" + ); + + // CREATE IMPORT ROUTINE + + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS "+ImportRoutine.TABLE+" (" + + ImportRoutine.ID+" bigserial NOT NULL," + + ImportRoutine.FARM_ID+" bigint NOT NULL," + + ImportRoutine.BATCH_TYPE+" varchar(100) NOT NULL," + + ImportRoutine.SOURCE_URL+" text," + + ImportRoutine.SOURCE_VERSION+" text," + + ImportRoutine.START+" timestamp with time zone," + + ImportRoutine.END+" timestamp with time zone," + + ImportRoutine.STATUS+" varchar(20)," + + ImportRoutine.CALLER+" text," + + ImportRoutine.COMPUTATION_ID+" text," + + ImportRoutine.COMPUTATION_URL+" text," + + ImportRoutine.COMPUTATION_OPID+" text," + + ImportRoutine.COMPUTATION_OPNAME+" text," + + ImportRoutine.COMPUTATION_REQ+" text," + + ImportRoutine.LOCK+" varchar(200)," + + "primary key ("+ImportRoutine.ID+"))"); + + + // CREATE FARM VIEW + try { + ResultSet rs=stmt.executeQuery("Select * from "+Farm.TABLE); + if(rs.next()) { + // table already present + } + }catch(SQLException e) { + // Expected error on table not found, trying to create it + stmt.executeUpdate("CREATE VIEW "+Farm.TABLE+" AS (" + + "Select f.farmid as "+Farm.FARM_ID+", f.uuid as "+Farm.UUID+", c.companyid as "+Farm.COMPANY_ID+", " + + "c.uuid as "+Farm.COMPANY_UUID+", a.associationid as "+Farm.ASSOCIATION_ID+", a.uuid as "+Farm.ASSOCIATION_UUID+" " + + "FROM farms as f INNER JOIN companies as c ON f.companyid=c.companyid " + + "INNER JOIN associations as a ON c.associationid = a. associationid)"); + } + +// stmt.executeQuery("CREATE VIEW suca as SELECT 1"); + + + // CREATE ANAGRAPHIC OUTPUT + + + + // CREATE PUBLIC OUTPUT VIEW + + + + // CREATE IMPORTED TABLES + + conn.commit(); + } + +}