This commit is contained in:
Fabio Sinibaldi 2018-12-19 18:13:06 +00:00
parent 4a7150ba1b
commit 288c2c4208
31 changed files with 592 additions and 136 deletions

View File

@ -30,6 +30,7 @@
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>

View File

@ -20,10 +20,18 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
</projectDescription>

13
.settings/.jsdtscope Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/webapp"/>
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="target/m2e-wtp/web-resources"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>

View File

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

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="perform-service-1.0.0-SNAPSHOT">
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<property name="context-root" value="perform-service"/>
<property name="java-output-path" value="/perform-service/target/classes"/>
</wb-module>
</project-modules>

View File

@ -0,0 +1,7 @@
<root>
<facet id="jst.jaxrs">
<node name="libprov">
<attribute name="provider-id" value="jaxrs-no-op-library-provider"/>
</node>
</facet>
</root>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="java" version="1.8"/>
<installed facet="jst.web" version="2.5"/>
<installed facet="jst.jaxrs" version="2.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
</faceted-project>

View File

@ -0,0 +1 @@
org.eclipse.wst.jsdt.launching.baseBrowserLibrary

View File

@ -0,0 +1 @@
Window

View File

@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1

1
distro/LICENSE Normal file
View File

@ -0,0 +1 @@
${gcube.license}

65
distro/README Normal file
View File

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

5
distro/changelog.xml Normal file
View File

@ -0,0 +1,5 @@
<ReleaseNotes>
<Changeset component="perform-service-1.0.0" date="2018-12-22">
<Change>First Release</Change>
</Changeset>
</ReleaseNotes>

30
distro/descriptor.xml Normal file
View File

@ -0,0 +1,30 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>servicearchive</id>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/</baseDirectory>
<fileSets>
<fileSet>
<directory>${distroDirectory}</directory>
<outputDirectory>/</outputDirectory>
<useDefaultExcludes>true</useDefaultExcludes>
<includes>
<include>README</include>
<include>LICENSE</include>
<include>changelog.xml</include>
</includes>
<fileMode>755</fileMode>
<filtered>true</filtered>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${build.finalName}.${project.packaging}</source>
<outputDirectory>/${artifactId}</outputDirectory>
</file>
</files>
</assembly>

7
distro/gcube-app.xml Normal file
View File

@ -0,0 +1,7 @@
<application mode='online'>
<name>${artifactId}</name>
<group>Application</group>
<version>${version}</version>
<description>${description}</description>
<local-persistence location='target' />
</application>

27
distro/profile.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID />
<Type>Service</Type>
<Profile>
<Description>${description}</Description>
<Class>Application</Class>
<Name>${artifactId}</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Name>${artifactId}</Name>
<Version>${version}</Version>
<MavenCoordinates>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</MavenCoordinates>
<Files>
<File>${build.finalName}.jar</File>
</Files>
</Software>
</Packages>
</Profile>
</Resource>

27
pom.xml
View File

@ -13,6 +13,13 @@
<description>Service for Farm Perform Data Management</description>
<packaging>war</packaging>
<properties>
<webappDirectory>${project.basedir}/src/main/webapp/WEB-INF</webappDirectory>
<distroDirectory>${project.basedir}/distro</distroDirectory>
<jersey-version>2.14</jersey-version> <!-- 2.22 -->
<jersey-cdi-version>2.14</jersey-cdi-version>
<weld-version>2.2.4.Final</weld-version>
</properties>
<dependencyManagement>
<dependencies>
@ -43,6 +50,12 @@
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
@ -56,6 +69,20 @@
<version>1.14.8</version>
</dependency>
<!-- weld -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>${weld-version}</version> <!-- 2.0.4 doesn't find implementations. 2.2.4 doesn't find CDI... -->
</dependency>
</dependencies>
</project>

View File

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

View File

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

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

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

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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<DBField,String> condition;
public String toString() {
StringBuilder builder=new StringBuilder();
for(Entry<DBField,String> 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();
}
}

View File

@ -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 {
}

View File

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

View File

@ -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<DBField,String> condition=new HashMap<DBField,String>();
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);
}
}
}

View File

@ -0,0 +1,5 @@
package org.gcube.application.perform.service;
public class AnagraphicTests {
}

View File

@ -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<DBField,String> condition=new HashMap<DBField,String>();
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));
}
}