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;
|
2017-10-25 16:45:34 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.DeleteRecord;
|
2017-08-04 15:04:35 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.record.FisheryRecord;
|
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;
|
2017-08-04 15:04:35 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.CommonServiceUtils;
|
2016-10-10 18:50:11 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
|
2017-02-20 09:43:46 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
|
2017-03-08 17:09:34 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean;
|
2017-09-19 16:42:15 +02:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg;
|
2017-10-27 14:37:41 +02:00
|
|
|
import org.gcube.datacatalogue.common.Constants;
|
|
|
|
import org.gcube.datacatalogue.common.enums.Product_Type;
|
|
|
|
import org.gcube.datacatalogue.common.enums.Sources;
|
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
|
|
|
/**
|
2017-07-12 18:05:35 +02:00
|
|
|
* Fishery web service methods.
|
|
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
2016-10-08 20:46:17 +02:00
|
|
|
*/
|
2017-02-19 15:53:42 +01:00
|
|
|
@Path("{source:firms|FIRMS|ram|RAM|grsf|GRSF|FishSource|fishsource}/fishery/")
|
2016-10-08 20:46:17 +02:00
|
|
|
public class GrsfPublisherFisheryService {
|
2016-10-11 11:39:25 +02:00
|
|
|
|
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-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
|
|
|
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
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Incoming request for creating a fishery record = " + record + ".\nRequest 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;
|
2017-07-12 18:05:35 +02:00
|
|
|
String id = ""; // id of the created record, if everything went ok
|
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-01 19:19:22 +01:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
2017-02-19 15:53:42 +01:00
|
|
|
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
2016-10-10 18:50:11 +02:00
|
|
|
}else{
|
|
|
|
|
2016-12-07 18:41:05 +01:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2017-02-20 18:44:26 +01:00
|
|
|
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
2017-07-12 18:05:35 +02:00
|
|
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
2017-09-19 16:42:15 +02:00
|
|
|
|
|
|
|
// extend this role to the other organizations in this context
|
|
|
|
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization, RolesCkanGroupOrOrg.ADMIN);
|
2017-06-07 11:04:44 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// retrieve the user's email and fullname
|
|
|
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
|
|
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
2017-02-19 15:53:42 +01:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
if(authorMail == null || authorFullname == null){
|
2017-09-19 16:42:15 +02:00
|
|
|
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
2016-12-01 19:19:22 +01:00
|
|
|
}
|
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();
|
2017-02-19 15:53:42 +01:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// check name
|
|
|
|
CommonServiceUtils.checkName(futureName, catalogue);
|
|
|
|
|
|
|
|
Map<String, List<String>> customFields = record.getExtrasFields();
|
|
|
|
Set<String> tags = new HashSet<String>();
|
|
|
|
Set<String> groups = new HashSet<String>();
|
|
|
|
List<ResourceBean> resources = record.getExtrasResources();
|
|
|
|
|
|
|
|
// validate end set sources
|
2017-07-27 16:45:18 +02:00
|
|
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record, Product_Type.FISHERY, tags, customFields, groups, resources, username, futureTitle);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
// check the license id
|
|
|
|
String license = null;
|
|
|
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
2017-10-27 14:37:41 +02:00
|
|
|
license = Constants.DEFAULT_LICENSE;
|
2017-07-12 18:05:35 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
// set the visibility of the datatest according the context
|
|
|
|
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
|
|
|
|
|
|
|
// convert extras' keys to keys with namespace
|
2017-10-27 14:51:24 +02:00
|
|
|
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
if(namespaces == null)
|
|
|
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
|
|
|
|
2017-10-04 17:30:07 +02:00
|
|
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF));
|
2017-09-19 16:42:15 +02:00
|
|
|
|
|
|
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
logger.info("Invoking create method..");
|
|
|
|
|
|
|
|
// create the product
|
|
|
|
id = catalogue.createCKanDatasetMultipleCustomFields(
|
|
|
|
apiKey,
|
|
|
|
futureTitle,
|
|
|
|
futureName,
|
2017-09-19 16:42:15 +02:00
|
|
|
publishInOrganization,
|
2017-07-12 18:05:35 +02:00
|
|
|
authorFullname,
|
|
|
|
authorMail,
|
|
|
|
record.getMaintainer() == null? authorFullname : record.getMaintainer(),
|
2017-07-27 16:45:18 +02:00
|
|
|
record.getMaintainerContact() == null? authorMail : record.getMaintainerContact(),
|
|
|
|
version,
|
2017-08-02 12:11:47 +02:00
|
|
|
null,
|
2017-07-27 16:45:18 +02:00
|
|
|
license,
|
|
|
|
new ArrayList<String>(tags),
|
|
|
|
customFields,
|
|
|
|
resources,
|
|
|
|
publicDataset);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
// post actions
|
|
|
|
if(id != null){
|
|
|
|
|
|
|
|
logger.info("Created record with identifier " + id);
|
2017-10-25 17:13:58 +02:00
|
|
|
String description = "Short Name: " + record.getShortName() + "\n";
|
2017-08-02 12:33:25 +02:00
|
|
|
|
|
|
|
if(sourceInPath.equals(Sources.GRSF))
|
2017-10-25 17:13:58 +02:00
|
|
|
description += ", GRSF Semantic Identifier: " + record.getFisheryId() + "\n";
|
2017-08-02 12:33:25 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
CommonServiceUtils.actionsPostCreateOrUpdate(
|
|
|
|
id, futureName, record, apiKey, username, organization,
|
2017-07-13 15:43:19 +02:00
|
|
|
null, responseBean, catalogue, namespaces, groups, context, token, futureTitle, authorFullname,
|
2017-08-02 12:11:47 +02:00
|
|
|
contextServlet, false, description);
|
2017-10-25 17:13:58 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.CREATED;
|
2017-07-27 16:45:18 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
}else{
|
2017-10-25 17:13:58 +02:00
|
|
|
throw new Exception("There was an error during the record generation, sorry");
|
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);
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
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")
|
2017-10-04 17:30:07 +02:00
|
|
|
@Valid DeleteRecord recordToDelete,
|
2016-12-03 11:50:38 +01:00
|
|
|
@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);
|
|
|
|
|
2016-12-03 11:50:38 +01:00
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.debug("The request is to delete a fishery object of source " + sourceInPath);
|
2016-12-03 11:50:38 +01:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
// retrieve the catalogue instance
|
2017-07-12 18:05:35 +02:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
CkanDataset fisheryInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
2016-10-14 18:44:34 +02:00
|
|
|
|
|
|
|
if(fisheryInCkan == null){
|
|
|
|
status = Status.NOT_FOUND;
|
2017-02-19 15:53:42 +01:00
|
|
|
throw new Exception("There was a problem while serving your request. This item was not found");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
2016-11-29 12:44:36 +01:00
|
|
|
|
2017-02-19 15:53:42 +01:00
|
|
|
// check it is in the right source and it is a fishery
|
2017-10-27 14:37:41 +02:00
|
|
|
String type = fisheryInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
2017-06-07 11:04:44 +02:00
|
|
|
|
2017-09-19 16:42:15 +02:00
|
|
|
if(fisheryInCkan.getOrganization().getName().equalsIgnoreCase(source) &&
|
2017-10-04 17:30:07 +02:00
|
|
|
Product_Type.FISHERY.getOrigName().equals(type)){
|
2017-06-07 11:04:44 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.debug("Ok, this is a fishery of the right source, removing it");
|
|
|
|
boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), apiKey, true);
|
2017-06-07 11:04:44 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
if(deleted){
|
2017-02-19 15:53:42 +01:00
|
|
|
logger.info("Fishery DELETED AND PURGED!");
|
2016-10-14 18:44:34 +02:00
|
|
|
status = Status.OK;
|
|
|
|
responseBean.setId(fisheryInCkan.getId());
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
2017-02-19 15:53:42 +01:00
|
|
|
throw new Exception("Request failed, sorry. Unable to delete/purge the fishery");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
|
|
|
}else{
|
|
|
|
status = Status.BAD_REQUEST;
|
2017-02-19 15:53:42 +01:00
|
|
|
throw new Exception("The id you are using doesn't belong to a Fishery item having source " + source + "!");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
|
|
|
}catch(Exception e){
|
2017-02-19 15:53:42 +01:00
|
|
|
logger.error("Failed to delete this", e);
|
2016-10-14 18:44:34 +02:00
|
|
|
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){
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Received call to get fisheries with source " + source);
|
|
|
|
|
2017-02-19 15:53:42 +01:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
2016-12-06 17:06:51 +01:00
|
|
|
String context = ScopeProvider.instance.get();
|
2017-02-19 15:53:42 +01:00
|
|
|
String username = caller.getClient().getId();
|
2016-12-06 17:06:51 +01:00
|
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
List<String> datasetsIds = new ArrayList<String>();
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
if(catalogue == null){
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
2017-02-19 15:53:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it is a request for GRSF records, we have Fishery - Stock groups, so it is easy.
|
|
|
|
// For other cases, records needs to be parsed
|
|
|
|
if(sourceInPath.equals(Sources.GRSF))
|
|
|
|
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "fishery", catalogue);
|
|
|
|
else{
|
2017-09-19 16:42:15 +02:00
|
|
|
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
2017-07-12 18:05:35 +02:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2017-02-19 15:53:42 +01:00
|
|
|
for (String id : fullGroupListIds) {
|
2017-07-12 18:05:35 +02:00
|
|
|
CkanDataset dataset = catalogue.getDataset(id, apiKey);
|
2017-02-19 15:53:42 +01:00
|
|
|
if(dataset != null){
|
2017-10-27 14:37:41 +02:00
|
|
|
String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
2017-10-04 17:30:07 +02:00
|
|
|
if(Product_Type.FISHERY.getOrigName().equals(type))
|
2017-02-19 15:53:42 +01:00
|
|
|
datasetsIds.add(id);
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 17:06:51 +01:00
|
|
|
}
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.OK;
|
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
|
|
|
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){
|
|
|
|
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
ResponseBean responseBean = new ResponseBean();
|
2017-07-12 18:05:35 +02:00
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
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());
|
2017-07-12 18:05:35 +02: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);
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.OK;
|
2016-12-09 16:56:52 +01:00
|
|
|
}else{
|
2017-02-19 15:53:42 +01:00
|
|
|
responseBean.setMessage("Unable to retrieve a catalogue item with name " + name);
|
2016-12-09 16:56:52 +01:00
|
|
|
}
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Failed to retrieve this product", e);
|
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
@POST
|
|
|
|
@Path("update-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public Response updateFishery(
|
|
|
|
@NotNull(message="record cannot be null")
|
|
|
|
@Valid FisheryRecord record,
|
|
|
|
@PathParam("source") String source)
|
|
|
|
throws ValidationException{
|
|
|
|
|
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
|
|
|
|
|
|
|
logger.info("Incoming request for updating a fishery record = " + record + ". Request comes from user " + username + " in context " + context);
|
|
|
|
|
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
|
|
|
|
// catalog id must be reported
|
|
|
|
String catalogId = record.getCatalogId();
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
if(catalogId == null || catalogId.isEmpty()){
|
|
|
|
status = Status.BAD_REQUEST;
|
|
|
|
throw new Exception("Please specify the 'catalog_id' property");
|
|
|
|
}
|
|
|
|
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
|
|
|
|
|
|
|
if(catalogue == null){
|
|
|
|
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
|
|
|
|
}else{
|
2017-07-27 16:45:18 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// get already published record and modify it
|
2017-07-13 17:22:32 +02:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
|
|
|
CkanDataset recordPublished = catalogue.getDataset(catalogId, apiKey);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
if(recordPublished == null)
|
|
|
|
throw new Exception("A record with catalogue id " + catalogId + " does not exist!");
|
|
|
|
|
|
|
|
// retrieve the user's email and fullname
|
|
|
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
|
|
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
2016-12-09 16:56:52 +01:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
if(authorMail == null || authorFullname == null){
|
|
|
|
logger.debug("Author fullname or mail missing, cannot continue");
|
|
|
|
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
|
|
|
}
|
|
|
|
|
2017-07-27 16:45:18 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
|
|
|
|
|
|
|
// check he/she has admin role
|
|
|
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
|
|
|
|
|
|
|
// name, title, product url and are going to remain unchanged (so we keep them from the publisher record);
|
|
|
|
String name = recordPublished.getName();
|
|
|
|
String title = recordPublished.getTitle();
|
|
|
|
|
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
|
|
|
|
|
|
|
// load infos
|
|
|
|
Map<String, List<String>> customFields = record.getExtrasFields();
|
|
|
|
Set<String> tags = new HashSet<String>();
|
|
|
|
Set<String> groups = new HashSet<String>();
|
|
|
|
List<ResourceBean> resources = record.getExtrasResources();
|
|
|
|
|
|
|
|
// validate end set sources
|
|
|
|
CommonServiceUtils.validateRecordAndMapFields(
|
2017-07-27 16:45:18 +02:00
|
|
|
apiKey,
|
2017-07-12 18:05:35 +02:00
|
|
|
context,
|
|
|
|
contextServlet,
|
|
|
|
sourceInPath,
|
|
|
|
record,
|
|
|
|
Product_Type.FISHERY,
|
|
|
|
tags,
|
|
|
|
customFields,
|
|
|
|
groups,
|
|
|
|
resources,
|
|
|
|
username,
|
|
|
|
title);
|
|
|
|
|
|
|
|
// check the license id
|
|
|
|
String license = null;
|
|
|
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
2017-10-27 14:37:41 +02:00
|
|
|
license = Constants.DEFAULT_LICENSE;
|
2017-07-12 18:05:35 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
// set the visibility of the datatest according the context
|
|
|
|
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
|
|
|
|
|
|
|
// add the SYSTEM_TYPE
|
2017-10-27 14:37:41 +02:00
|
|
|
customFields.put(Constants.SYSTEM_TYPE_CUSTOM_KEY, Arrays.asList(sourceInPath.getOrigName()));
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
// convert extras' keys to keys with namespace
|
2017-10-27 14:51:24 +02:00
|
|
|
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
if(namespaces == null)
|
|
|
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
|
|
|
|
|
|
|
// retrieve the url
|
2017-10-27 14:37:41 +02:00
|
|
|
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD) ?
|
|
|
|
namespaces.get(Constants.ITEM_URL_FIELD) :
|
|
|
|
Constants.ITEM_URL_FIELD;
|
2017-07-12 18:05:35 +02:00
|
|
|
String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey);
|
2017-10-27 14:37:41 +02:00
|
|
|
customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl));
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
// replace fields
|
2017-10-04 17:30:07 +02:00
|
|
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF));
|
2017-09-19 16:42:15 +02:00
|
|
|
|
|
|
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
2017-07-12 18:05:35 +02:00
|
|
|
|
|
|
|
logger.info("Invoking update method..");
|
|
|
|
|
|
|
|
// update the product
|
|
|
|
String id = catalogue.updateCKanDataset(
|
|
|
|
apiKey,
|
|
|
|
catalogId,
|
|
|
|
title, name,
|
2017-09-19 16:42:15 +02:00
|
|
|
publishInOrganization,
|
2017-07-12 18:05:35 +02:00
|
|
|
authorFullname,
|
|
|
|
authorMail,
|
|
|
|
record.getMaintainer(),
|
|
|
|
record.getMaintainerContact(),
|
|
|
|
version,
|
|
|
|
HelperMethods.removeHTML(record.getDescription()),
|
|
|
|
license,
|
|
|
|
new ArrayList<String>(tags),
|
|
|
|
null, // remove any previous group
|
|
|
|
customFields,
|
|
|
|
resources,
|
|
|
|
publicDataset);
|
|
|
|
|
|
|
|
if(id != null){
|
|
|
|
logger.info("Item updated!");
|
|
|
|
CommonServiceUtils.actionsPostCreateOrUpdate(
|
|
|
|
id, name, record, apiKey, username, organization,
|
|
|
|
itemUrl, responseBean, catalogue, namespaces, groups, context, token, title, authorFullname,
|
2017-08-02 12:11:47 +02:00
|
|
|
contextServlet, false, null);
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.OK;
|
|
|
|
}else{
|
|
|
|
throw new Exception("There was an error during the item updated, sorry");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}catch(Exception e){
|
2017-07-13 17:22:32 +02:00
|
|
|
logger.error("Failed to update fishery record" + e);
|
2017-07-13 11:50:05 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
2017-07-12 18:05:35 +02:00
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
}
|