2016-10-08 20:46:17 +02:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws.services;
|
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
import java.util.ArrayList;
|
2016-12-07 18:41:05 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
2016-12-01 19:19:22 +01:00
|
|
|
import java.util.HashSet;
|
2016-10-10 18:50:11 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-12-01 19:19:22 +01:00
|
|
|
import java.util.Set;
|
2016-10-09 16:06:45 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
import javax.servlet.ServletContext;
|
2016-10-14 18:44:34 +02:00
|
|
|
import javax.validation.Valid;
|
2016-10-15 22:34:57 +02:00
|
|
|
import javax.validation.ValidationException;
|
|
|
|
import javax.validation.constraints.NotNull;
|
2016-10-08 20:46:17 +02:00
|
|
|
import javax.ws.rs.Consumes;
|
2016-10-14 18:44:34 +02:00
|
|
|
import javax.ws.rs.DELETE;
|
2016-10-10 18:50:11 +02:00
|
|
|
import javax.ws.rs.GET;
|
2016-10-08 20:46:17 +02:00
|
|
|
import javax.ws.rs.POST;
|
|
|
|
import javax.ws.rs.Path;
|
2016-12-01 19:19:22 +01:00
|
|
|
import javax.ws.rs.PathParam;
|
2016-10-08 20:46:17 +02:00
|
|
|
import javax.ws.rs.Produces;
|
2016-12-09 16:56:52 +01:00
|
|
|
import javax.ws.rs.QueryParam;
|
2016-10-10 18:50:11 +02:00
|
|
|
import javax.ws.rs.core.Context;
|
2016-10-08 20:46:17 +02:00
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.Response;
|
2016-10-09 16:06:45 +02:00
|
|
|
import javax.ws.rs.core.Response.Status;
|
2016-10-08 20:46:17 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-10-14 18:44:34 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.DeleteProductBean;
|
2016-10-08 20:46:17 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
|
2016-12-03 11:50:38 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.RefersToBean;
|
2016-12-06 17:06:51 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean;
|
2016-10-09 16:06:45 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean;
|
2016-10-10 18:50:11 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
|
2016-12-03 11:50:38 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Product_Type;
|
2016-12-01 19:19:22 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Record_Type;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
|
2016-11-24 17:53:50 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.ManageTimeSeriesThread;
|
2016-12-09 14:06:19 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.WritePostCatalogueManagerThread;
|
2016-10-10 18:50:11 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
|
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
|
2016-10-08 20:46:17 +02:00
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanDataset;
|
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
/**
|
|
|
|
* Fishery web service methods
|
|
|
|
* @author Costantino Perciante at ISTI-CNR
|
|
|
|
*/
|
2016-12-01 19:19:22 +01:00
|
|
|
@Path("{source}/fishery/")
|
2016-10-08 20:46:17 +02:00
|
|
|
public class GrsfPublisherFisheryService {
|
2016-10-11 11:39:25 +02:00
|
|
|
|
|
|
|
private static final String DEFAULT_FISHERY_LICENSE = "CC-BY-SA-4.0";
|
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
// the context
|
2016-12-01 19:19:22 +01:00
|
|
|
@Context ServletContext contextServlet;
|
2016-10-09 16:06:45 +02:00
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
// Logger
|
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class);
|
2016-10-11 11:39:25 +02:00
|
|
|
|
2016-12-16 16:02:03 +01:00
|
|
|
private static final String PRODUCT_URL_FIELD_KEY = "Product URL";
|
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
@GET
|
|
|
|
@Path("hello")
|
|
|
|
@Produces(MediaType.TEXT_PLAIN)
|
|
|
|
public Response hello(){
|
|
|
|
return Response.ok("Hello.. Fishery service is here").build();
|
|
|
|
}
|
|
|
|
|
2016-10-11 11:39:25 +02:00
|
|
|
@GET
|
|
|
|
@Path("get-licenses")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public Response getLicenses(){
|
|
|
|
|
2016-12-01 22:20:46 +01:00
|
|
|
Status status = Status.OK;
|
2016-12-03 11:50:38 +01:00
|
|
|
Map<String, String> licenses = CommonServiceUtils.getLicenses();
|
2016-12-01 22:20:46 +01:00
|
|
|
if(licenses == null)
|
2016-10-11 11:39:25 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
return Response.status(status).entity(licenses).build();
|
|
|
|
|
|
|
|
}
|
2016-10-09 16:06:45 +02:00
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
@POST
|
|
|
|
@Path("publish-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2016-10-15 22:34:57 +02:00
|
|
|
public Response publishFishery(
|
2016-11-06 21:21:11 +01:00
|
|
|
@NotNull(message="record cannot be null")
|
2016-12-01 19:19:22 +01:00
|
|
|
@Valid FisheryRecord record,
|
|
|
|
@PathParam("source") String source)
|
2016-11-06 21:21:11 +01:00
|
|
|
throws ValidationException{
|
2016-10-09 16:06:45 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
// retrieve context and username
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
String context = ScopeProvider.instance.get();
|
2016-11-04 16:26:19 +01:00
|
|
|
String token = SecurityTokenProvider.instance.get();
|
2016-10-10 18:50:11 +02:00
|
|
|
|
|
|
|
logger.info("Incoming request for creating a fishery record = " + record);
|
2016-11-06 21:21:11 +01:00
|
|
|
logger.info("Request comes from user " + username + " in context " + context);
|
2016-10-10 18:50:11 +02:00
|
|
|
|
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
String id = "";
|
2016-11-06 21:21:11 +01:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
try{
|
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// Cast the source to the accepted ones
|
2016-12-01 19:25:30 +01:00
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-10 11:24:47 +01:00
|
|
|
if(sourceInPath == null){
|
|
|
|
status = Status.BAD_REQUEST;
|
2016-12-03 11:50:38 +01:00
|
|
|
throw new Exception("The specified source in the path is unrecognized. Values accepted are " + Sources.getAsList());
|
2016-12-10 11:24:47 +01:00
|
|
|
}
|
2016-12-01 19:19:22 +01:00
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
2016-10-10 18:50:11 +02:00
|
|
|
}else{
|
|
|
|
|
2016-12-07 18:41:05 +01:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// check the user has editor/admin role into the org
|
|
|
|
String organization = HelperMethods.retrieveOrgNameFromScope(context);
|
2016-12-07 18:41:05 +01:00
|
|
|
String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey);
|
2016-12-03 11:50:38 +01:00
|
|
|
logger.info("Role of the user " + username + " is " + role);
|
2016-12-01 19:19:22 +01:00
|
|
|
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
|
|
|
|
status = Status.FORBIDDEN;
|
|
|
|
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-admin role!");
|
|
|
|
}
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// The name of the product will be the uuid of the kb. The title will be the fishery's fishery_name. Fishery has also the constraint that
|
|
|
|
// fishing area and jurisdiction area cannot be empty at the same time
|
|
|
|
String futureName = record.getUuid();
|
|
|
|
String futureTitle = record.getFisheryName();
|
|
|
|
if(!HelperMethods.isNameValid(futureName)){
|
|
|
|
status = Status.BAD_REQUEST;
|
2016-12-07 18:41:05 +01:00
|
|
|
throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'");
|
2016-12-01 19:19:22 +01:00
|
|
|
}else{
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet...");
|
2016-12-03 11:50:38 +01:00
|
|
|
boolean alreadyExists = catalogue.existProductWithNameOrId(futureName);
|
2016-10-13 11:46:42 +02:00
|
|
|
|
2016-12-03 11:50:38 +01:00
|
|
|
if(alreadyExists){
|
2016-12-01 19:19:22 +01:00
|
|
|
|
2016-12-07 18:41:05 +01:00
|
|
|
logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists");
|
2016-12-01 19:19:22 +01:00
|
|
|
status = Status.CONFLICT;
|
2016-12-07 18:41:05 +01:00
|
|
|
throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists");
|
2016-10-13 11:46:42 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
}else{
|
2016-12-03 11:50:38 +01:00
|
|
|
|
2016-12-06 11:16:31 +01:00
|
|
|
// validate the record if it is a GRSF one and set the record type and in manage context
|
2016-12-07 18:41:05 +01:00
|
|
|
// Status field is needed only in the Manage context for GRSF records
|
2016-12-09 14:06:19 +01:00
|
|
|
if(context.equals((String)contextServlet.getInitParameter(HelperMethods.MANAGE_CONTEX_KEY))){
|
2016-12-06 11:16:31 +01:00
|
|
|
if(sourceInPath.equals(Sources.GRSF)){
|
|
|
|
record.setRecordType(Record_Type.AGGREGATED);
|
|
|
|
CommonServiceUtils.validateAggregatedRecord(record);
|
|
|
|
}else
|
2016-12-07 14:45:22 +01:00
|
|
|
record.setRecordType(Record_Type.SOURCE);
|
2016-12-06 11:16:31 +01:00
|
|
|
}
|
2016-10-10 18:50:11 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// set the type
|
2016-12-03 11:50:38 +01:00
|
|
|
record.setProductType(Product_Type.FISHERY.getOrigName());
|
2016-12-09 16:56:52 +01:00
|
|
|
|
2016-12-09 14:06:19 +01:00
|
|
|
// product system type is a list of values for sources records, so remove it (so that no group is generated)
|
2016-12-09 14:54:48 +01:00
|
|
|
if(!sourceInPath.equals(Sources.GRSF))
|
2016-12-09 14:06:19 +01:00
|
|
|
record.setProductionSystemType(null);
|
2016-12-01 19:19:22 +01:00
|
|
|
|
2016-12-03 11:50:38 +01:00
|
|
|
// evaluate the custom fields/tags, resources and groups
|
|
|
|
Map<String, List<String>> customFields = record.getExtrasFields();
|
2016-12-01 22:41:24 +01:00
|
|
|
Set<String> tags = new HashSet<String>();
|
2016-12-01 19:19:22 +01:00
|
|
|
Set<String> groups = new HashSet<String>();
|
2016-12-03 11:50:38 +01:00
|
|
|
List<ResourceBean> resources = record.getExtrasResources();
|
|
|
|
boolean skipTags = !sourceInPath.equals(Sources.GRSF); // no tags for the Original records
|
2016-12-07 18:41:05 +01:00
|
|
|
CommonServiceUtils.getTagsGroupsResourcesExtrasByRecord(tags, skipTags, groups, resources, customFields, record, username, sourceInPath);
|
2016-12-01 19:19:22 +01:00
|
|
|
|
2016-12-03 11:50:38 +01:00
|
|
|
// manage the refers to
|
|
|
|
if(sourceInPath.equals(Sources.GRSF)){
|
|
|
|
List<RefersToBean> refersTo = record.getRefersTo();
|
|
|
|
for (RefersToBean refersToBean : refersTo) {
|
|
|
|
resources.add(new ResourceBean(refersToBean.getUrl(), "Source of product " + futureTitle + " in the catalogue has id: "
|
|
|
|
+ refersToBean.getId(), "Information of a source of the product " + futureTitle, null, username, null, null));
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 19:19:22 +01:00
|
|
|
|
|
|
|
// retrieve the user's email and fullname
|
|
|
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
|
|
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
|
|
|
|
|
|
|
if(authorMail == null || authorFullname == null){
|
|
|
|
|
|
|
|
logger.debug("Author fullname or mail missing, cannot continue");
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
2016-10-10 18:50:11 +02:00
|
|
|
|
|
|
|
}else{
|
2016-11-06 21:21:11 +01:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// check the license id
|
|
|
|
String license = null;
|
|
|
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
|
|
|
license = DEFAULT_FISHERY_LICENSE;
|
|
|
|
else
|
|
|
|
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
|
|
|
license = record.getLicense();
|
|
|
|
else throw new Exception("Please check the license id!");
|
|
|
|
|
|
|
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
|
|
|
|
2016-12-01 19:27:40 +01:00
|
|
|
logger.info("Invoking creation method..");
|
2016-12-03 11:50:38 +01:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// create the product
|
|
|
|
id = catalogue.createCKanDatasetMultipleCustomFields(
|
2016-12-07 18:41:05 +01:00
|
|
|
apiKey,
|
2016-12-01 19:19:22 +01:00
|
|
|
futureTitle,
|
|
|
|
futureName,
|
2016-12-09 14:54:48 +01:00
|
|
|
organization,
|
2016-12-01 19:19:22 +01:00
|
|
|
authorFullname,
|
|
|
|
authorMail,
|
|
|
|
record.getMaintainer(),
|
|
|
|
record.getMaintainerContact(),
|
|
|
|
version,
|
|
|
|
HelperMethods.removeHTML(record.getDescription()),
|
|
|
|
license,
|
|
|
|
new ArrayList<String>(tags),
|
|
|
|
customFields,
|
|
|
|
resources,
|
2016-12-04 12:24:37 +01:00
|
|
|
true);
|
2016-12-01 19:19:22 +01:00
|
|
|
|
|
|
|
if(id != null){
|
|
|
|
|
|
|
|
logger.info("Product created! Id is " + id);
|
|
|
|
responseBean.setId(id);
|
|
|
|
status = Status.CREATED;
|
2016-12-19 17:18:37 +01:00
|
|
|
String productUrl = catalogue.getUnencryptedUrlFromDatasetIdOrName(futureName);
|
2016-12-07 18:41:05 +01:00
|
|
|
responseBean.setProductUrl(productUrl);
|
2016-12-01 19:19:22 +01:00
|
|
|
responseBean.setKbUuid(record.getUuid());
|
|
|
|
|
2016-12-07 18:41:05 +01:00
|
|
|
// add the "Product URL" to the field
|
|
|
|
Map<String, List<String>> addField = new HashMap<String, List<String>>();
|
2016-12-16 16:02:03 +01:00
|
|
|
addField.put(PRODUCT_URL_FIELD_KEY, Arrays.asList(productUrl));
|
2016-12-07 18:41:05 +01:00
|
|
|
catalogue.patchProductCustomFields(id, apiKey, addField);
|
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
if(!groups.isEmpty()){
|
|
|
|
logger.info("Launching thread for association to the list of groups " + groups);
|
|
|
|
AssociationToGroupThread threadGroups = new AssociationToGroupThread(new ArrayList<String>(groups), id, organization, username, catalogue);
|
|
|
|
threadGroups.start();
|
|
|
|
logger.info("Waiting association thread to die..");
|
|
|
|
threadGroups.join();
|
|
|
|
logger.debug("Ok, it died");
|
|
|
|
}
|
|
|
|
|
|
|
|
// manage time series
|
|
|
|
logger.info("Launching thread for time series handling");
|
|
|
|
new ManageTimeSeriesThread(record, futureName, username, catalogue, context, token).start();
|
|
|
|
|
2016-12-09 14:06:19 +01:00
|
|
|
// write a post if the product has been published in grsf context
|
|
|
|
if(context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY))){
|
|
|
|
new WritePostCatalogueManagerThread(
|
|
|
|
context,
|
|
|
|
token,
|
|
|
|
futureTitle,
|
|
|
|
productUrl,
|
|
|
|
false,
|
2016-12-09 16:12:51 +01:00
|
|
|
null,
|
2016-12-09 14:06:19 +01:00
|
|
|
authorFullname).start();
|
|
|
|
logger.info("Thread to write a post about the new product has been launched");
|
|
|
|
}
|
|
|
|
|
2016-12-10 11:24:47 +01:00
|
|
|
}else{
|
2016-12-01 19:19:22 +01:00
|
|
|
throw new Exception("There was an error during the product generation, sorry");
|
2016-12-10 11:24:47 +01:00
|
|
|
}
|
2016-10-10 18:50:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 19:19:22 +01:00
|
|
|
// }
|
2016-10-10 18:50:11 +02:00
|
|
|
}catch(Exception e){
|
2016-12-01 19:27:40 +01:00
|
|
|
logger.error("Failed to create fishery record" + e);
|
2016-10-10 18:50:11 +02:00
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
2016-10-09 16:06:45 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
return Response.status(status).entity(responseBean).build();
|
2016-10-08 20:46:17 +02:00
|
|
|
}
|
2016-10-15 22:34:57 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
@DELETE
|
|
|
|
@Path("delete-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2016-11-06 21:21:11 +01:00
|
|
|
public Response deleteFishery(
|
|
|
|
@NotNull(message="input value is missing")
|
2016-12-03 11:50:38 +01:00
|
|
|
@Valid DeleteProductBean recordToDelete,
|
|
|
|
@PathParam("source") String source) throws ValidationException{
|
2016-10-14 18:44:34 +02:00
|
|
|
|
|
|
|
// retrieve context and username
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
|
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
|
2016-10-15 22:34:57 +02:00
|
|
|
// check it is a fishery ...
|
2016-10-14 18:44:34 +02:00
|
|
|
logger.info("Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a fishery");
|
|
|
|
try{
|
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
|
|
|
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-03 11:50:38 +01:00
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
|
|
|
|
|
|
|
if(sourceInPath == null)
|
2016-12-06 17:06:51 +01:00
|
|
|
throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]");
|
2016-12-03 11:50:38 +01:00
|
|
|
|
2016-12-09 14:30:28 +01:00
|
|
|
logger.info("The request is to delete a stock object of source " + sourceInPath);
|
2016-12-03 11:50:38 +01:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
// retrieve the catalogue instance
|
|
|
|
CkanDataset fisheryInCkan = catalogue.getDataset(recordToDelete.getId(), catalogue.getApiKeyFromUsername(username));
|
|
|
|
|
|
|
|
if(fisheryInCkan == null){
|
|
|
|
|
|
|
|
status = Status.NOT_FOUND;
|
|
|
|
throw new Exception("There was a problem while serving your request. This product was not found");
|
|
|
|
|
|
|
|
}
|
2016-11-29 12:44:36 +01:00
|
|
|
|
2016-11-27 12:37:44 +01:00
|
|
|
// get extras and check there is the product type
|
2016-12-06 17:06:51 +01:00
|
|
|
if(catalogue.isDatasetInGroup(source + "-" + "fishery", recordToDelete.getId())){
|
2016-10-14 18:44:34 +02:00
|
|
|
logger.warn("Ok, this is a fishery, removing it");
|
|
|
|
boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), catalogue.getApiKeyFromUsername(username), true);
|
|
|
|
if(deleted){
|
|
|
|
logger.info("Stock DELETED AND PURGED!");
|
|
|
|
status = Status.OK;
|
|
|
|
responseBean.setId(fisheryInCkan.getId());
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("Request failed, sorry");
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
status = Status.BAD_REQUEST;
|
2016-12-06 17:06:51 +01:00
|
|
|
throw new Exception("The id you are using doesn't belong to a Fishery product having source " + source + "!");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed to delete this ");
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2016-10-08 20:46:17 +02:00
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
@GET
|
|
|
|
@Path("get-fisheries-ids")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public Response getFisheriesIds(
|
|
|
|
@PathParam("source") String source){
|
|
|
|
|
|
|
|
// retrieve context and username
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
2016-12-07 18:41:05 +01:00
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
logger.info("Received call to get fisheries with source " + source);
|
|
|
|
|
|
|
|
List<String> datasetsIds = new ArrayList<String>();
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
|
|
|
|
|
|
|
if(sourceInPath == null)
|
|
|
|
throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]");
|
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
|
|
|
}
|
|
|
|
|
2016-12-19 17:18:37 +01:00
|
|
|
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "fishery", catalogue);
|
2016-12-06 17:06:51 +01:00
|
|
|
responseBean.setResult(datasetsIds);
|
|
|
|
responseBean.setSuccess(true);
|
|
|
|
|
|
|
|
}catch(Exception e){
|
2016-12-19 17:18:37 +01:00
|
|
|
logger.error("Failed to fetch this list of ids ", e);
|
2016-12-06 17:06:51 +01:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
responseBean.setSuccess(false);
|
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
|
2016-12-09 16:56:52 +01:00
|
|
|
@GET
|
2016-12-16 16:02:03 +01:00
|
|
|
@Path("get-catalogue-id-and-url-from-name")
|
2016-12-09 16:56:52 +01:00
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2016-12-16 16:02:03 +01:00
|
|
|
public Response getCatalogueIdAndUrlFromKBID(
|
2016-12-09 16:56:52 +01:00
|
|
|
@QueryParam("name") String name){
|
|
|
|
|
|
|
|
// retrieve context and username
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
2016-12-10 11:24:47 +01:00
|
|
|
Status status = Status.OK;
|
2016-12-09 16:56:52 +01:00
|
|
|
|
|
|
|
logger.info("Received call to get the catalogue identifier for the product with name " + name);
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
|
|
|
}
|
|
|
|
|
|
|
|
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
|
|
|
if(dataset != null){
|
2016-12-16 16:02:03 +01:00
|
|
|
|
|
|
|
Map<String, String> result = new HashMap<String, String>();
|
|
|
|
result.put("id", dataset.getId());
|
|
|
|
|
|
|
|
// retrieve the product url
|
|
|
|
Map<String, String> customFields = dataset.getExtrasAsHashMap();
|
|
|
|
if(customFields.containsKey(PRODUCT_URL_FIELD_KEY))
|
|
|
|
result.put("url", customFields.get(PRODUCT_URL_FIELD_KEY));
|
|
|
|
else
|
2016-12-19 17:18:37 +01:00
|
|
|
result.put("url", catalogue.getUnencryptedUrlFromDatasetIdOrName(dataset.getId()));
|
2016-12-16 16:02:03 +01:00
|
|
|
|
|
|
|
responseBean.setResult(result);
|
2016-12-09 16:56:52 +01:00
|
|
|
responseBean.setSuccess(true);
|
|
|
|
}else{
|
|
|
|
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed to retrieve this product", e);
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
responseBean.setSuccess(false);
|
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
}
|