From 288c2c4208fb7c316637fc4fc4cbbf8e201e418a Mon Sep 17 00:00:00 2001 From: "fabio.sinibaldi" Date: Wed, 19 Dec 2018 18:13:06 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application/perform-service@176021 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 1 + .project | 8 + .settings/.jsdtscope | 13 ++ .settings/org.eclipse.jdt.core.prefs | 3 + .settings/org.eclipse.wst.common.component | 10 ++ ...se.wst.common.project.facet.core.prefs.xml | 7 + ....eclipse.wst.common.project.facet.core.xml | 8 + ...rg.eclipse.wst.jsdt.ui.superType.container | 1 + .../org.eclipse.wst.jsdt.ui.superType.name | 1 + .settings/org.eclipse.wst.validation.prefs | 2 + distro/LICENSE | 1 + distro/README | 65 +++++++++ distro/changelog.xml | 5 + distro/descriptor.xml | 30 ++++ distro/gcube-app.xml | 7 + distro/profile.xml | 27 ++++ pom.xml | 27 ++++ .../perform/service/LocalConfiguration.java | 38 ++++- .../service/PerformServiceManager.java | 18 ++- .../perform/service/ServiceConstants.java | 12 +- .../service/engine/AnagraphicManagerImpl.java | 106 -------------- ...raphicManager.java => MappingManager.java} | 2 +- .../service/engine/MappingManagerImpl.java | 137 ++++++++++++++++++ .../perform/service/engine/model/DBField.java | 29 +++- .../engine/model/DBQueryDescriptor.java | 18 +++ .../perform/service/rest/Anagraphic.java | 20 --- .../service/rest/InterfaceCommons.java | 14 ++ .../perform/service/rest/Mappings.java | 95 ++++++++++++ src/main/webapp/WEB-INF/config.properties | 0 .../perform/service/AnagraphicTests.java | 5 + .../perform/service/MiscTests.java | 18 +++ 31 files changed, 592 insertions(+), 136 deletions(-) create mode 100644 .settings/.jsdtscope create mode 100644 .settings/org.eclipse.wst.common.component create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.prefs.xml create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.container create mode 100644 .settings/org.eclipse.wst.jsdt.ui.superType.name create mode 100644 .settings/org.eclipse.wst.validation.prefs create mode 100644 distro/LICENSE create mode 100644 distro/README create mode 100644 distro/changelog.xml create mode 100644 distro/descriptor.xml create mode 100644 distro/gcube-app.xml create mode 100644 distro/profile.xml delete mode 100644 src/main/java/org/gcube/application/perform/service/engine/AnagraphicManagerImpl.java rename src/main/java/org/gcube/application/perform/service/engine/{AnagraphicManager.java => MappingManager.java} (93%) create mode 100644 src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java delete mode 100644 src/main/java/org/gcube/application/perform/service/rest/Anagraphic.java create mode 100644 src/main/java/org/gcube/application/perform/service/rest/InterfaceCommons.java create mode 100644 src/main/java/org/gcube/application/perform/service/rest/Mappings.java create mode 100644 src/main/webapp/WEB-INF/config.properties create mode 100644 src/test/java/org/gcube/application/perform/service/AnagraphicTests.java create mode 100644 src/test/java/org/gcube/application/perform/service/MiscTests.java diff --git a/.classpath b/.classpath index fae1a2b..5c3ac53 100644 --- a/.classpath +++ b/.classpath @@ -30,6 +30,7 @@ + diff --git a/.project b/.project index bac53d6..4a1ea22 100644 --- a/.project +++ b/.project @@ -20,10 +20,18 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature diff --git a/.settings/.jsdtscope b/.settings/.jsdtscope new file mode 100644 index 0000000..f179e11 --- /dev/null +++ b/.settings/.jsdtscope @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 714351a..6e80039 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,8 @@ eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..4bd632b --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml b/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml new file mode 100644 index 0000000..cc81385 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.prefs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..7a104c1 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.container b/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..3bd5d0a --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/.settings/org.eclipse.wst.jsdt.ui.superType.name b/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..05bd71b --- /dev/null +++ b/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/.settings/org.eclipse.wst.validation.prefs b/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000..04cad8c --- /dev/null +++ b/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,2 @@ +disabled=06target +eclipse.preferences.version=1 diff --git a/distro/LICENSE b/distro/LICENSE new file mode 100644 index 0000000..2d9616a --- /dev/null +++ b/distro/LICENSE @@ -0,0 +1 @@ +${gcube.license} \ No newline at end of file diff --git a/distro/README b/distro/README new file mode 100644 index 0000000..8d176eb --- /dev/null +++ b/distro/README @@ -0,0 +1,65 @@ +The gCube System - ${name} +-------------------------------------------------- + +${description} + + +${gcube.description} + +${gcube.funding} + + +Version +-------------------------------------------------- + +${version} (${buildDate}) + +Please see the file named "changelog.xml" in this directory for the release notes. + + + +Authors +-------------------------------------------------- + +* Fabio Sinibaldi (fabio.sinibaldi-AT-isti.cnr.it) Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). + +Maintainers +----------- + +* Fabio Sinibaldi (fabio.sinibaldi-AT-isti.cnr.it) Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" - CNR, Pisa (Italy). + + +Download information +-------------------------------------------------- + +Source code is available from SVN: + ${scm.url} + +Binaries can be downloaded from the gCube website: + ${gcube.website} + +Installation +-------------------------------------------------- + +Installation documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot} + +Documentation +-------------------------------------------------- + +Documentation is available on-line in the gCube Wiki: + ${gcube.wikiRoot} + + + +Support +-------------------------------------------------- + +Bugs and support requests can be reported in the gCube issue tracking tool: + ${gcube.issueTracking} + + +Licensing +-------------------------------------------------- + +This software is licensed under the terms you may find in the file named "LICENSE" in this directory. \ No newline at end of file diff --git a/distro/changelog.xml b/distro/changelog.xml new file mode 100644 index 0000000..b63515c --- /dev/null +++ b/distro/changelog.xml @@ -0,0 +1,5 @@ + + + First Release + + \ No newline at end of file diff --git a/distro/descriptor.xml b/distro/descriptor.xml new file mode 100644 index 0000000..dc46ade --- /dev/null +++ b/distro/descriptor.xml @@ -0,0 +1,30 @@ + + servicearchive + + tar.gz + + / + + + ${distroDirectory} + / + true + + README + LICENSE + changelog.xml + + 755 + true + + + + + target/${build.finalName}.${project.packaging} + /${artifactId} + + + \ No newline at end of file diff --git a/distro/gcube-app.xml b/distro/gcube-app.xml new file mode 100644 index 0000000..cf2c2f9 --- /dev/null +++ b/distro/gcube-app.xml @@ -0,0 +1,7 @@ + + ${artifactId} + Application + ${version} + ${description} + + \ No newline at end of file diff --git a/distro/profile.xml b/distro/profile.xml new file mode 100644 index 0000000..a67f4f9 --- /dev/null +++ b/distro/profile.xml @@ -0,0 +1,27 @@ + + + + Service + + ${description} + Application + ${artifactId} + 1.0.0 + + + ${artifactId} + ${version} + + ${groupId} + ${artifactId} + ${version} + + + ${build.finalName}.jar + + + + + + + diff --git a/pom.xml b/pom.xml index 905b677..4daac6b 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,13 @@ Service for Farm Perform Data Management war + + ${project.basedir}/src/main/webapp/WEB-INF + ${project.basedir}/distro + 2.14 + 2.14 + 2.2.4.Final + @@ -43,6 +50,12 @@ + + javax.servlet + javax.servlet-api + 3.0.1 + provided + javax.ws.rs javax.ws.rs-api @@ -56,6 +69,20 @@ 1.14.8 + + + + + javax.enterprise + cdi-api + 1.2 + + + org.jboss.weld.servlet + weld-servlet + ${weld-version} + + \ No newline at end of file diff --git a/src/main/java/org/gcube/application/perform/service/LocalConfiguration.java b/src/main/java/org/gcube/application/perform/service/LocalConfiguration.java index b2b5dd5..c27bf59 100644 --- a/src/main/java/org/gcube/application/perform/service/LocalConfiguration.java +++ b/src/main/java/org/gcube/application/perform/service/LocalConfiguration.java @@ -1,5 +1,13 @@ package org.gcube.application.perform.service; +import java.net.URL; +import java.util.Properties; + + +import lombok.Synchronized; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class LocalConfiguration { public static final String POOL_MAX_IDLE="db.pools.max_idle"; @@ -7,10 +15,38 @@ public class LocalConfiguration { public static final String POOL_MIN_IDLE="db.pools.min_total"; + public static final String MAPPING_DB_ENDPOINT_NAME="mapping-db.ep.name"; + public static final String MAPPING_DB_ENDPOINT_CATEGORY="mapping-db.ep.name"; + + + static LocalConfiguration instance=null; + + + @Synchronized + public static LocalConfiguration init(URL propertiesURL){ + if(instance==null) + instance=new LocalConfiguration(propertiesURL); + return instance; + } + + private Properties props=new Properties(); + + private LocalConfiguration(URL propertiesURL) { + try{ + log.debug("Loading {} ",propertiesURL); + props.load(propertiesURL.openStream()); + }catch(Exception e){ + throw new RuntimeException(e); + } + } + public static final String getProperty(String propertyName) { - throw new RuntimeException("Need to implement this"); + return instance.props.getProperty(propertyName); } + + + } diff --git a/src/main/java/org/gcube/application/perform/service/PerformServiceManager.java b/src/main/java/org/gcube/application/perform/service/PerformServiceManager.java index 6cc4c51..a641984 100644 --- a/src/main/java/org/gcube/application/perform/service/PerformServiceManager.java +++ b/src/main/java/org/gcube/application/perform/service/PerformServiceManager.java @@ -1,14 +1,26 @@ package org.gcube.application.perform.service; -import org.gcube.smartgears.ApplicationManager; +import java.net.URL; +import org.gcube.smartgears.ApplicationManager; +import org.gcube.smartgears.ContextProvider; +import org.gcube.smartgears.context.application.ApplicationContext; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class PerformServiceManager implements ApplicationManager{ @Override public void onInit() { - // TODO Auto-generated method stub - + try { + ApplicationContext context=ContextProvider.get(); + URL resourceUrl = context.application().getResource("/WEB-INF/config.properties"); + LocalConfiguration.init(resourceUrl); + }catch(Throwable t) { + log.error("UNABLE TO INIT SERVICE ", t); + } } diff --git a/src/main/java/org/gcube/application/perform/service/ServiceConstants.java b/src/main/java/org/gcube/application/perform/service/ServiceConstants.java index 1f2aef8..ee94370 100644 --- a/src/main/java/org/gcube/application/perform/service/ServiceConstants.java +++ b/src/main/java/org/gcube/application/perform/service/ServiceConstants.java @@ -2,7 +2,15 @@ package org.gcube.application.perform.service; public interface ServiceConstants { - - public static final String ANAGRAPHIC_INTERFACE="anagraphic"; + public static interface Mappings{ + public static final String PATH="mappings"; + public static final String BATCHES_METHOD="batch"; + public static final String FARM_ID_PARAMETER="farmid"; + public static final String FARM_UUID_PARAMETER="farmuuid"; + public static final String BATCH_NAME_PARAMETER="name"; + public static final String BATCH_TYPE_PARAMETER="type"; + + public static final String FARM_METHOD="farm"; + } } diff --git a/src/main/java/org/gcube/application/perform/service/engine/AnagraphicManagerImpl.java b/src/main/java/org/gcube/application/perform/service/engine/AnagraphicManagerImpl.java deleted file mode 100644 index 7683865..0000000 --- a/src/main/java/org/gcube/application/perform/service/engine/AnagraphicManagerImpl.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.gcube.application.perform.service.engine; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import org.gcube.application.perform.service.engine.model.BeanNotFound; -import org.gcube.application.perform.service.engine.model.DBField; -import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; -import org.gcube.application.perform.service.engine.model.ISQueryDescriptor; -import org.gcube.application.perform.service.engine.model.InternalException; -import org.gcube.application.perform.service.engine.model.anagraphic.Batch; -import org.gcube.application.perform.service.engine.model.anagraphic.Farm; - -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class AnagraphicManagerImpl implements AnagraphicManager { - - - private static final String INSERT_BATCH=""; - private static final String GET_BATCH=""; - private static final String INSERT_FARM=""; - private static final String GET_FARM=""; - - - private ISQueryDescriptor getISQueryDescriptor() { - throw new RuntimeException("Implment this"); - } - - - @Override - public Batch getBatch(DBQueryDescriptor desc) throws BeanNotFound,SQLException{ - DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); - - - Connection conn=db.getConnection(); - PreparedStatement psInsert=conn.prepareStatement(INSERT_BATCH); - // TODO set values - boolean inserted=psInsert.executeUpdate()==1; - if(inserted) - conn.commit(); - - PreparedStatement psGet=conn.prepareStatement(GET_BATCH); - // TODO set values - ResultSet rs=psGet.executeQuery(); - if(!rs.next()) - throw new BeanNotFound("Batch not found. Condition was "+desc); - - Batch toReturn=rowToBatch(rs); - if(inserted) - log.trace("Inserted Batch {} ",toReturn); - - return toReturn; - - } - - @Override - public Farm getFarm(DBQueryDescriptor desc) throws BeanNotFound,SQLException{ - DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); - - - Connection conn=db.getConnection(); - PreparedStatement psInsert=conn.prepareStatement(INSERT_FARM); - // TODO set values - boolean inserted=psInsert.executeUpdate()==1; - if(inserted) - conn.commit(); - - PreparedStatement psGet=conn.prepareStatement(GET_FARM); - // TODO set values - ResultSet rs=psGet.executeQuery(); - if(!rs.next()) - throw new BeanNotFound("Farm not found. Condition was "+desc); - - Farm toReturn=rowToFarm(rs); - if(inserted) - log.trace("Inserted Batch {} ",toReturn); - - return toReturn; - } - - - - private static Batch rowToBatch(ResultSet rs) throws SQLException { - Batch toReturn=new Batch(); - toReturn.setFarmId(rs.getLong(DBField.Batch.FARM_ID)); - toReturn.setId(rs.getLong(DBField.Batch.BATCH_ID)); - toReturn.setName(rs.getString(DBField.Batch.BATCH_NAME)); - toReturn.setType(rs.getString(DBField.Batch.BATCH_TYPE)); - return toReturn; - } - - - private 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.setCompanyId(rs.getLong(DBField.Farm.COMPANY_ID)); - toReturn.setCompanyUUID(rs.getString(DBField.Farm.COMPANY_UUID)); - toReturn.setId(rs.getLong(DBField.Farm.FARM_ID)); - toReturn.setUUID(rs.getString(DBField.Farm.UUID)); - return toReturn; - } -} diff --git a/src/main/java/org/gcube/application/perform/service/engine/AnagraphicManager.java b/src/main/java/org/gcube/application/perform/service/engine/MappingManager.java similarity index 93% rename from src/main/java/org/gcube/application/perform/service/engine/AnagraphicManager.java rename to src/main/java/org/gcube/application/perform/service/engine/MappingManager.java index cbddcc1..787c582 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/AnagraphicManager.java +++ b/src/main/java/org/gcube/application/perform/service/engine/MappingManager.java @@ -7,7 +7,7 @@ import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; import org.gcube.application.perform.service.engine.model.anagraphic.Batch; import org.gcube.application.perform.service.engine.model.anagraphic.Farm; -public interface AnagraphicManager { +public interface MappingManager { public Batch getBatch(DBQueryDescriptor desc) throws BeanNotFound, SQLException; public Farm getFarm(DBQueryDescriptor desc)throws BeanNotFound, SQLException; 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 new file mode 100644 index 0000000..a39e8bd --- /dev/null +++ b/src/main/java/org/gcube/application/perform/service/engine/MappingManagerImpl.java @@ -0,0 +1,137 @@ +package org.gcube.application.perform.service.engine; + +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; +import org.gcube.application.perform.service.engine.model.DBField; +import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; +import org.gcube.application.perform.service.engine.model.DBType; +import org.gcube.application.perform.service.engine.model.ISQueryDescriptor; +import org.gcube.application.perform.service.engine.model.anagraphic.Batch; +import org.gcube.application.perform.service.engine.model.anagraphic.Farm; + +import lombok.Synchronized; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class MappingManagerImpl implements MappingManager { + + + private static final String INSERT_BATCH="INSERT INTO batches (id,uuid,farmid,type,name) VALUES (SELECT (max(id)+1 from batches),?,?,?,?) ON CONFLICT DO NOTHING"; + private static final String GET_BATCH_BY_ID="Select * from batches where id = ?"; + private static final String GET_BATCH_BY_DESCRIPTIVE_KEY="Select * from batches where farmid=? AND type=? AND name= ?"; + + + private static final String GET_FARM_BY_ID=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); + + + private static ISQueryDescriptor isQueryDescriptor=null; + + + public MappingManagerImpl() { + + } + + @Synchronized + private ISQueryDescriptor getISQueryDescriptor() { + if(isQueryDescriptor==null) { + isQueryDescriptor= + new ISQueryDescriptor(DBType.Anagraphic, + LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_NAME), null, + LocalConfiguration.getProperty(LocalConfiguration.MAPPING_DB_ENDPOINT_CATEGORY)); + } + return isQueryDescriptor; + } + + + @Override + public Batch getBatch(DBQueryDescriptor desc) throws BeanNotFound,SQLException{ + DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); + + String type=desc.getCondition().get(DBField.Batch.fields.get(DBField.Batch.BATCH_TYPE)); + String name=desc.getCondition().get(DBField.Batch.fields.get(DBField.Batch.BATCH_NAME)); + Long farmid=Long.parseLong(desc.getCondition().get(DBField.Batch.fields.get(DBField.Batch.FARM_ID))); + + Connection conn=db.getConnection(); + PreparedStatement psSearch=conn.prepareStatement(GET_BATCH_BY_DESCRIPTIVE_KEY); + psSearch.setLong(1, farmid); + psSearch.setString(2, type); + psSearch.setString(3, name); + ResultSet rs=psSearch.executeQuery(); + + if(rs.next()) + return rowToBatch(rs); + + // ID NOT FOUND, TRY TO REGISTER IT + + log.trace("Registering new Batch [name {} , type {}, farmid {} ] ",name,type,farmid); + PreparedStatement psInsert=conn.prepareStatement(INSERT_BATCH); + psInsert.setString(1, UUID.randomUUID().toString()); + psInsert.setLong(2,farmid); + psInsert.setString(3, type); + psInsert.setString(4, name); + + boolean inserted=psInsert.executeUpdate()==1; + if(inserted) { + conn.commit(); + log.trace("Committed Batch."); + } + rs=psSearch.executeQuery(); + + if(rs.next()) + return rowToBatch(rs); + else throw new BeanNotFound(String.format("Unable to find Bean [farmid=%1$d, type=%2$s,name=%3$s]",farmid,type,name)); + + } + + @Override + public Farm getFarm(DBQueryDescriptor desc) throws BeanNotFound,SQLException{ + DataBaseManager db=DataBaseManager.get(getISQueryDescriptor()); + + + Connection conn=db.getConnection(); + PreparedStatement psGet=null; + DBField IDField=DBField.Farm.fields.get(DBField.Farm.FARM_ID); + if(desc.getCondition().containsKey(IDField)) { + // get By ID + psGet=conn.prepareStatement(GET_FARM_BY_ID); + psGet.setLong(1,Long.parseLong(desc.getCondition().get(IDField))); + } + + ResultSet rs=psGet.executeQuery(); + if(!rs.next()) + throw new BeanNotFound("Farm not found. Condition was "+desc); + + return rowToFarm(rs); + } + + + + private static Batch rowToBatch(ResultSet rs) throws SQLException { + Batch toReturn=new Batch(); + toReturn.setFarmId(rs.getLong(DBField.Batch.FARM_ID)); + toReturn.setId(rs.getLong(DBField.Batch.BATCH_ID)); + toReturn.setName(rs.getString(DBField.Batch.BATCH_NAME)); + toReturn.setType(rs.getString(DBField.Batch.BATCH_TYPE)); + return toReturn; + } + + + private 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.setCompanyId(rs.getLong(DBField.Farm.COMPANY_ID)); + toReturn.setCompanyUUID(rs.getString(DBField.Farm.COMPANY_UUID)); + toReturn.setId(rs.getLong(DBField.Farm.FARM_ID)); + toReturn.setUUID(rs.getString(DBField.Farm.UUID)); + return toReturn; + } +} 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 4500133..cf46fe2 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 @@ -50,10 +50,33 @@ public class DBField { } } - - - private int type; + + private int type; private String fieldName; + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((fieldName == null) ? 0 : fieldName.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DBField other = (DBField) obj; + if (fieldName == null) { + if (other.fieldName != null) + return false; + } else if (!fieldName.equals(other.fieldName)) + return false; + return true; + } + } diff --git a/src/main/java/org/gcube/application/perform/service/engine/model/DBQueryDescriptor.java b/src/main/java/org/gcube/application/perform/service/engine/model/DBQueryDescriptor.java index 1b89483..d28b530 100644 --- a/src/main/java/org/gcube/application/perform/service/engine/model/DBQueryDescriptor.java +++ b/src/main/java/org/gcube/application/perform/service/engine/model/DBQueryDescriptor.java @@ -1,5 +1,23 @@ package org.gcube.application.perform.service.engine.model; +import java.util.Map; +import java.util.Map.Entry; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor public class DBQueryDescriptor { + private Map condition; + + + public String toString() { + StringBuilder builder=new StringBuilder(); + for(Entry entry : condition.entrySet()) { + builder.append(String.format("%1$s = %2$s AND ", entry.getKey().getFieldName(),entry.getValue())); + } + return builder.substring(0,builder.lastIndexOf(" AND ")).toString(); + } } diff --git a/src/main/java/org/gcube/application/perform/service/rest/Anagraphic.java b/src/main/java/org/gcube/application/perform/service/rest/Anagraphic.java deleted file mode 100644 index 976c2ca..0000000 --- a/src/main/java/org/gcube/application/perform/service/rest/Anagraphic.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.gcube.application.perform.service.rest; - -import javax.ws.rs.Path; - -import org.gcube.application.perform.service.PerformServiceManager; -import org.gcube.smartgears.annotations.ManagedBy; -import org.gcube.application.perform.service.ServiceConstants; - - -@Path(ServiceConstants.ANAGRAPHIC_INTERFACE) - -@ManagedBy(PerformServiceManager.class) - -public class Anagraphic { - - - - - -} diff --git a/src/main/java/org/gcube/application/perform/service/rest/InterfaceCommons.java b/src/main/java/org/gcube/application/perform/service/rest/InterfaceCommons.java new file mode 100644 index 0000000..61f9084 --- /dev/null +++ b/src/main/java/org/gcube/application/perform/service/rest/InterfaceCommons.java @@ -0,0 +1,14 @@ +package org.gcube.application.perform.service.rest; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; + +public class InterfaceCommons { + + public static final void checkMandatory(Object toCheck, String name) throws WebApplicationException{ + if(toCheck==null) + throw new WebApplicationException(String.format("Parameter %1$s is mandatory",name),Response.Status.BAD_REQUEST); + } + + +} 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 new file mode 100644 index 0000000..5e53cc7 --- /dev/null +++ b/src/main/java/org/gcube/application/perform/service/rest/Mappings.java @@ -0,0 +1,95 @@ +package org.gcube.application.perform.service.rest; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.HashMap; + +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.gcube.application.perform.service.PerformServiceManager; +import org.gcube.application.perform.service.ServiceConstants; +import org.gcube.application.perform.service.engine.MappingManager; +import org.gcube.application.perform.service.engine.model.BeanNotFound; +import org.gcube.application.perform.service.engine.model.DBField; +import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; +import org.gcube.application.perform.service.engine.model.anagraphic.Batch; +import org.gcube.application.perform.service.engine.model.anagraphic.Farm; +import org.gcube.smartgears.annotations.ManagedBy; + + +@Path(ServiceConstants.Mappings.PATH) + +@ManagedBy(PerformServiceManager.class) + +public class Mappings { + + + @Inject + private MappingManager mappings; + + @GET + @Path(ServiceConstants.Mappings.BATCHES_METHOD) + @Produces(MediaType.APPLICATION_JSON) + public Batch getBatch(@QueryParam(ServiceConstants.Mappings.BATCH_NAME_PARAMETER) String name, + @QueryParam(ServiceConstants.Mappings.BATCH_TYPE_PARAMETER) String type, + @QueryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER) Long farmid) { + + InterfaceCommons.checkMandatory(name, ServiceConstants.Mappings.BATCH_NAME_PARAMETER); + InterfaceCommons.checkMandatory(type, ServiceConstants.Mappings.BATCH_TYPE_PARAMETER); + InterfaceCommons.checkMandatory(farmid, ServiceConstants.Mappings.FARM_ID_PARAMETER); + + HashMap condition=new HashMap(); + + condition.put(DBField.Batch.fields.get(DBField.Batch.BATCH_NAME), name); + condition.put(DBField.Batch.fields.get(DBField.Batch.BATCH_TYPE), type); + condition.put(DBField.Batch.fields.get(DBField.Batch.FARM_ID), farmid.toString()); + + DBQueryDescriptor desc=new DBQueryDescriptor(condition); + + try{ + return mappings.getBatch(desc); + }catch(BeanNotFound e) { + throw new WebApplicationException("Unable to find Farm with condition "+desc,Response.Status.BAD_REQUEST); + }catch(SQLException e) { + throw new WebApplicationException("Unexpected Exception occurred while dealing with database.", e,Response.Status.INTERNAL_SERVER_ERROR); + } + } + + + + @GET + @Path(ServiceConstants.Mappings.FARM_METHOD) + @Produces(MediaType.APPLICATION_JSON) + public Farm getFarm(@QueryParam(ServiceConstants.Mappings.FARM_ID_PARAMETER) Long farmid, + @QueryParam(ServiceConstants.Mappings.FARM_UUID_PARAMETER) String farmuuid){ + + DBQueryDescriptor desc=null; + + try { + InterfaceCommons.checkMandatory(farmid, ServiceConstants.Mappings.FARM_ID_PARAMETER); + desc=new DBQueryDescriptor(Collections.singletonMap(DBField.Farm.fields.get(DBField.Farm.FARM_ID), farmid.toString())); + }catch(WebApplicationException e) { + try { + InterfaceCommons.checkMandatory(farmuuid, ServiceConstants.Mappings.FARM_UUID_PARAMETER); + desc=new DBQueryDescriptor(Collections.singletonMap(DBField.Farm.fields.get(DBField.Farm.UUID), farmuuid)); + }catch(WebApplicationException e1) { + throw new WebApplicationException("Specify either "+ServiceConstants.Mappings.FARM_UUID_PARAMETER+" or "+ServiceConstants.Mappings.FARM_ID_PARAMETER,Response.Status.BAD_REQUEST); + } + } + + try{ + return mappings.getFarm(desc); + }catch(BeanNotFound e) { + throw new WebApplicationException("Unable to find Farm with condition "+desc,Response.Status.BAD_REQUEST); + }catch(SQLException e) { + throw new WebApplicationException("Unexpected Exception occurred while dealing with database.", e,Response.Status.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/src/main/webapp/WEB-INF/config.properties b/src/main/webapp/WEB-INF/config.properties new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java b/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java new file mode 100644 index 0000000..4d3c112 --- /dev/null +++ b/src/test/java/org/gcube/application/perform/service/AnagraphicTests.java @@ -0,0 +1,5 @@ +package org.gcube.application.perform.service; + +public class AnagraphicTests { + +} diff --git a/src/test/java/org/gcube/application/perform/service/MiscTests.java b/src/test/java/org/gcube/application/perform/service/MiscTests.java new file mode 100644 index 0000000..175a512 --- /dev/null +++ b/src/test/java/org/gcube/application/perform/service/MiscTests.java @@ -0,0 +1,18 @@ +package org.gcube.application.perform.service; + +import java.util.HashMap; + +import org.gcube.application.perform.service.engine.model.DBField; +import org.gcube.application.perform.service.engine.model.DBQueryDescriptor; + +public class MiscTests { + + public static void main(String[] args) { + HashMap condition=new HashMap(); + + condition.put(DBField.Farm.fields.get(DBField.Farm.FARM_ID), "suca"); + condition.put(DBField.Farm.fields.get(DBField.Farm.UUID), "boh"); + System.out.println(new DBQueryDescriptor(condition)); + } + +}