package org.gcube.application.geoportal.service.rest; import java.util.Collection; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import org.gcube.application.geoportal.common.rest.InterfaceConstants; import org.gcube.application.geoportal.managers.ConcessioneManager; import org.gcube.application.geoportal.managers.ManagerFactory; import org.gcube.application.geoportal.model.concessioni.Concessione; import org.gcube.application.geoportal.model.report.PublicationReport; import org.gcube.application.geoportal.utils.Serialization; import org.json.JSONArray; import org.json.JSONObject; import lombok.extern.slf4j.Slf4j; @Path(InterfaceConstants.Methods.CONCESSIONI) @Slf4j public class Concessioni { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public String getById(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { try { log.info("Loading Concessione by id {} ",id); Concessione toReturn=(Concessione) ManagerFactory.getByRecordID(Long.parseLong(id)).getRecord(); log.debug("Loaded object {} ",toReturn); return toReturn.asJson(); }catch(WebApplicationException e){ log.warn("Unable to serve request",e); throw e; }catch(Throwable e){ log.warn("Unable to serve request",e); throw new WebApplicationException("Unable to serve request", e); } } @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public String registerNew(Concessione toRegister) { try { log.info("Registering new Concessione "+toRegister); ConcessioneManager manager=ManagerFactory.registerNew(toRegister); PublicationReport report=manager.commitSafely(false); return report.prettyPrint(); }catch(WebApplicationException e){ log.warn("Unable to serve request",e); throw e; }catch(Throwable e){ log.warn("Unable to serve request",e); throw new WebApplicationException("Unable to serve request", e); } } @PUT @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @Path("{"+InterfaceConstants.Parameters.PROJECT_ID+"}") public String modify(@PathParam(InterfaceConstants.Parameters.PROJECT_ID) String id) { try { log.info("Add Section"); throw new RuntimeException("Feature not yet available"); }catch(WebApplicationException e){ log.warn("Unable to serve request",e); throw e; }catch(Throwable e){ log.warn("Unable to serve request",e); throw new WebApplicationException("Unable to serve request", e); } } @GET @Produces(MediaType.APPLICATION_JSON) public String getList(){ try { Collection toReturn=ManagerFactory.getList(Concessione.class); log.debug("Found "+toReturn.size()+" elements.."); JSONArray array=new JSONArray(); for(Concessione found:toReturn) { array.put(found.asJson()); } return array.toString(); }catch(WebApplicationException e){ log.warn("Unable to serve request",e); throw e; }catch(Throwable e){ log.warn("Unable to serve request",e); throw new WebApplicationException("Unable to serve request", e); } } }