perform-service_broken/src/main/java/org/gcube/application/perform/service/rest/Mappings.java

96 lines
3.8 KiB
Java

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