2016-10-08 20:46:17 +02:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws.services;
|
|
|
|
|
2016-10-08 23:10:25 +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-08 23:10:25 +02:00
|
|
|
import java.util.List;
|
2016-10-10 18:50:11 +02:00
|
|
|
import java.util.Map;
|
2016-12-01 19:19:22 +01:00
|
|
|
import java.util.Set;
|
2016-10-08 23:10:25 +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-08 23:10:25 +02:00
|
|
|
import javax.ws.rs.core.Response.Status;
|
2016-10-08 20:46:17 +02:00
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2016-10-10 18:50:11 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-10-08 23:10:25 +02:00
|
|
|
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;
|
2018-02-08 16:58:11 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.UpdateRecordStatus;
|
2017-08-04 15:04:35 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.record.StockRecord;
|
2016-12-06 17:06:51 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean;
|
2016-10-08 23:10:25 +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-08 23:10:25 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
|
2017-01-30 17:58:22 +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
|
|
|
* Stock web service methods.
|
2018-05-10 16:44:48 +02:00
|
|
|
* @author Costantino Perciante (ISTI - CNR)
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
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}/stock/")
|
2016-10-08 20:46:17 +02:00
|
|
|
public class GrsfPublisherStockService {
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
// the context
|
2018-05-10 16:44:48 +02:00
|
|
|
@Context
|
|
|
|
ServletContext contextServlet;
|
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
// Logger
|
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-10 18:50:11 +02:00
|
|
|
@GET
|
|
|
|
@Path("hello")
|
|
|
|
@Produces(MediaType.TEXT_PLAIN)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response hello() {
|
2016-10-10 18:50:11 +02:00
|
|
|
return Response.ok("Hello.. Stock service is here").build();
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-11 11:39:25 +02:00
|
|
|
@GET
|
|
|
|
@Path("get-licenses")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response getLicenses() {
|
2016-12-01 22:20:46 +01:00
|
|
|
Status status = Status.OK;
|
2017-11-09 09:28:22 +01:00
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
DataCatalogue catalogue;
|
|
|
|
try {
|
|
|
|
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
|
2017-11-09 09:28:22 +01:00
|
|
|
if(licenses == null)
|
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
return Response.status(status).entity(licenses).build();
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
2016-10-11 11:39:25 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
2018-05-10 16:44:48 +02:00
|
|
|
return Response.status(status)
|
|
|
|
.entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
|
|
|
|
.build();
|
2017-11-09 09:28:22 +01:00
|
|
|
}
|
2016-10-11 11:39:25 +02:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-08 20:46:17 +02:00
|
|
|
@POST
|
|
|
|
@Path("publish-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response publishStock(@NotNull(message = "record cannot be null") @Valid StockRecord record,
|
|
|
|
@PathParam("source") String source) throws ValidationException {
|
|
|
|
|
2016-10-08 23:10:25 +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();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
logger.info("Incoming request for creating a stock record = " + record + ".\nRequest coming from user "
|
|
|
|
+ username + " in context " + context);
|
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
2016-10-10 18:50:11 +02:00
|
|
|
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
|
2018-05-10 16:44:48 +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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
if(catalogue == null) {
|
2017-07-12 18:05:35 +02:00
|
|
|
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
2018-05-10 16:44:48 +02:00
|
|
|
} else {
|
|
|
|
|
2016-12-07 18:41:05 +01:00
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2018-05-10 16:44:48 +02:00
|
|
|
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// check it has admin role or throw exception
|
|
|
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-09-19 16:42:15 +02:00
|
|
|
// extend this role to the other organizations in this context
|
2018-05-10 16:44:48 +02:00
|
|
|
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
|
|
|
|
RolesCkanGroupOrOrg.ADMIN);
|
|
|
|
|
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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
if(authorMail == null || authorFullname == null) {
|
2017-07-12 18:05:35 +02:00
|
|
|
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
2016-12-01 19:19:22 +01:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-12-01 19:19:22 +01:00
|
|
|
// check the record has a name, at least
|
|
|
|
String futureName = record.getUuid();
|
|
|
|
String futureTitle = record.getStockName();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// check name and throws exception
|
|
|
|
CommonServiceUtils.checkName(futureName, catalogue);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// load other information
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
|
|
|
Set<String> tags = new HashSet<String>();
|
2017-07-12 18:05:35 +02:00
|
|
|
Set<String> groups = new HashSet<String>();
|
|
|
|
List<ResourceBean> resources = record.getExtrasResources();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// validate end set sources, tags, etc
|
2018-05-10 16:44:48 +02:00
|
|
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
|
|
|
Product_Type.STOCK, 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;
|
2018-05-10 16:44:48 +02:00
|
|
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
|
|
|
license = record.getLicense();
|
2017-07-12 18:05:35 +02:00
|
|
|
else
|
2018-05-10 16:44:48 +02:00
|
|
|
throw new Exception("Please check the license id!");
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// check the version
|
|
|
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// set the visibility of the datatest according the context
|
2018-05-10 16:44:48 +02:00
|
|
|
boolean publicDataset = context
|
|
|
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// convert extras' keys to keys with namespace
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,String> namespaces = HelperMethods
|
|
|
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
if(namespaces == null)
|
|
|
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
2018-05-10 16:44:48 +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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Invoking create method..");
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// create the product
|
2018-05-10 16:44:48 +02:00
|
|
|
id = catalogue.createCKanDatasetMultipleCustomFields(apiKey, futureTitle, futureName,
|
|
|
|
publishInOrganization, authorFullname, authorMail,
|
|
|
|
record.getMaintainer() == null ? authorFullname : record.getMaintainer(),
|
|
|
|
record.getMaintainerContact() == null ? authorMail : record.getMaintainerContact(), version,
|
2019-06-14 17:07:35 +02:00
|
|
|
HelperMethods.removeHTML(record.getDescription(), false), license, new ArrayList<String>(tags),
|
2018-05-10 16:44:48 +02:00
|
|
|
customFields, resources, publicDataset);
|
|
|
|
|
|
|
|
if(id != null) {
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Product created! Id is " + id);
|
2017-10-27 16:04:47 +02:00
|
|
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-08-02 12:33:25 +02:00
|
|
|
if(sourceInPath.equals(Sources.GRSF))
|
2018-05-10 16:44:48 +02:00
|
|
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId()
|
|
|
|
+ "\n";
|
|
|
|
|
|
|
|
CommonServiceUtils.actionsPostCreateOrUpdate(id, futureName, record, apiKey, username, organization,
|
|
|
|
null, responseBean, catalogue, namespaces, groups, context, token, futureTitle,
|
|
|
|
authorFullname, contextServlet, false, description);
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.CREATED;
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
} else
|
|
|
|
throw new Exception(
|
|
|
|
"There was an error during the product generation, sorry! Unable to create the dataset");
|
2016-10-10 18:50:11 +02:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
2016-10-08 23:10:25 +02:00
|
|
|
logger.error("Failed to create stock record", e);
|
2017-11-09 09:28:22 +01:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
2016-10-10 18:50:11 +02:00
|
|
|
responseBean.setError(e.getMessage());
|
2016-10-08 23:10:25 +02:00
|
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
2017-07-12 18:05:35 +02:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
@DELETE
|
|
|
|
@Path("delete-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response deleteStock(@NotNull(message = "missing input value") @Valid DeleteRecord 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();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
// check it is a stock ...
|
2018-05-10 16:44:48 +02:00
|
|
|
logger.info(
|
|
|
|
"Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a stock");
|
|
|
|
try {
|
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
if(catalogue == null) {
|
2016-10-14 18:44:34 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
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
|
|
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
2016-12-06 17:06:51 +01:00
|
|
|
logger.info("The request is to delete a stock object of source " + sourceInPath);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-10-14 18:44:34 +02:00
|
|
|
// retrieve the catalogue instance
|
2017-07-12 18:05:35 +02:00
|
|
|
CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
if(stockInCkan == null) {
|
2016-10-14 18:44:34 +02:00
|
|
|
status = Status.NOT_FOUND;
|
|
|
|
throw new Exception("There was a problem while serving your request. This product was not found");
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-27 16:45:18 +02:00
|
|
|
// check it is in the right source and it is a stock
|
2017-10-27 14:37:41 +02:00
|
|
|
String type = stockInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
2018-05-10 16:44:48 +02:00
|
|
|
if((stockInCkan.getOrganization().getName().equalsIgnoreCase(source)
|
|
|
|
|| stockInCkan.getOrganization().getName().toLowerCase().contains(source))
|
|
|
|
&& Product_Type.STOCK.getOrigName().equals(type)) {
|
|
|
|
|
2017-02-19 15:53:42 +01:00
|
|
|
logger.debug("Ok, this is a stock of the right type, removing it");
|
2017-07-12 18:05:35 +02:00
|
|
|
boolean deleted = catalogue.deleteProduct(stockInCkan.getId(), apiKey, true);
|
2018-05-10 16:44:48 +02:00
|
|
|
if(deleted) {
|
2016-10-14 18:44:34 +02:00
|
|
|
logger.info("Stock DELETED AND PURGED!");
|
|
|
|
status = Status.OK;
|
|
|
|
responseBean.setId(stockInCkan.getId());
|
2018-05-10 16:44:48 +02:00
|
|
|
} else {
|
2016-10-14 18:44:34 +02:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
2017-02-19 15:53:42 +01:00
|
|
|
throw new Exception("Request failed, sorry. Unable to delete/purge the stock");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} else {
|
2016-10-14 18:44:34 +02:00
|
|
|
status = Status.BAD_REQUEST;
|
2018-05-10 16:44:48 +02:00
|
|
|
throw new Exception(
|
|
|
|
"The id you are using doesn't belong to a Stock product having source " + source + "!");
|
2016-10-14 18:44:34 +02:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
2016-10-14 18:44:34 +02:00
|
|
|
logger.error("Failed to delete this ", e);
|
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
@GET
|
|
|
|
@Path("get-stocks-ids")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response getStocksIds(@PathParam("source") String source) {
|
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
// retrieve context and username
|
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();
|
2017-07-12 18:05:35 +02:00
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
2016-12-06 17:06:51 +01:00
|
|
|
ResponseBean responseBean = new ResponseBean();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
// check it is a stock ...
|
|
|
|
logger.info("Received call to get stocks with source " + source);
|
|
|
|
List<String> datasetsIds = new ArrayList<String>();
|
2018-05-10 16:44:48 +02:00
|
|
|
try {
|
|
|
|
|
2016-12-06 17:06:51 +01:00
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
if(catalogue == null) {
|
2016-12-06 17:06:51 +01:00
|
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
throw new Exception("There was a problem while serving your request");
|
2017-02-19 15:53:42 +01:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
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 + "-" + "stock", catalogue);
|
2018-05-10 16:44:48 +02:00
|
|
|
else {
|
2017-09-19 16:42:15 +02:00
|
|
|
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
2018-05-10 16:44:48 +02:00
|
|
|
for(String id : fullGroupListIds) {
|
2017-02-19 15:53:42 +01:00
|
|
|
CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username));
|
2018-05-10 16:44:48 +02: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.STOCK.getOrigName().equals(type))
|
2017-02-19 15:53:42 +01:00
|
|
|
datasetsIds.add(id);
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 17:06:51 +01:00
|
|
|
}
|
|
|
|
responseBean.setResult(datasetsIds);
|
|
|
|
responseBean.setSuccess(true);
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.OK;
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
2016-12-19 17:18:37 +01:00
|
|
|
logger.error("Failed to fetch this list of ids " + source, e);
|
2016-12-06 17:06:51 +01:00
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
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)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response getCatalogueIdAndUrlFromKBID(@QueryParam("name") String name) {
|
|
|
|
|
2016-12-09 16:56:52 +01:00
|
|
|
// retrieve context and username
|
|
|
|
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);
|
2018-05-10 16:44:48 +02:00
|
|
|
try {
|
2016-12-09 16:56:52 +01:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
if(catalogue == null) {
|
2016-12-09 16:56:52 +01:00
|
|
|
throw new Exception("There was a problem while serving your request");
|
|
|
|
}
|
|
|
|
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
2018-05-10 16:44:48 +02:00
|
|
|
if(dataset != null) {
|
|
|
|
Map<String,String> result = new HashMap<String,String>();
|
2016-12-16 16:02:03 +01:00
|
|
|
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;
|
2018-05-10 16:44:48 +02:00
|
|
|
} else {
|
2016-12-09 16:56:52 +01:00
|
|
|
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
2016-12-09 16:56:52 +01:00
|
|
|
logger.error("Failed to retrieve this product", e);
|
|
|
|
responseBean.setMessage(e.getMessage());
|
|
|
|
}
|
2017-07-12 18:05:35 +02:00
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
@POST
|
|
|
|
@Path("update-product")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response updateStock(@NotNull(message = "record cannot be null") @Valid StockRecord record,
|
|
|
|
@PathParam("source") String source) throws ValidationException {
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
String context = ScopeProvider.instance.get();
|
|
|
|
String token = SecurityTokenProvider.instance.get();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
logger.info("Incoming request for updating a stock record = " + record + ". Request comes from user " + username
|
|
|
|
+ " in context " + context);
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// catalog id must be reported
|
|
|
|
String catalogId = record.getCatalogId();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(catalogId == null || catalogId.isEmpty()) {
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.BAD_REQUEST;
|
|
|
|
throw new Exception("Please specify the 'catalog_id' property");
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
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-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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
if(recordPublished == null)
|
2018-02-08 16:58:11 +01:00
|
|
|
throw new Exception("A record with id " + catalogId + " does not exist!");
|
2018-05-10 16:44:48 +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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
if(authorMail == null || authorFullname == null) {
|
2017-07-12 18:05:35 +02:00
|
|
|
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-13 17:22:32 +02:00
|
|
|
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// check he/she has admin role
|
|
|
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2019-06-14 17:07:35 +02:00
|
|
|
// name, product url and are going to remain unchanged (so we keep them from the publisher record);
|
2017-07-12 18:05:35 +02:00
|
|
|
String name = recordPublished.getName();
|
2019-06-14 17:07:35 +02:00
|
|
|
|
|
|
|
// The title must change if Stock Name change
|
|
|
|
//String title = recordPublished.getTitle();
|
|
|
|
String title = record.getStockName();
|
|
|
|
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// Cast the source to the accepted ones
|
|
|
|
Sources sourceInPath = Sources.onDeserialize(source);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// load infos
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
|
|
|
Set<String> tags = new HashSet<String>();
|
2017-07-12 18:05:35 +02:00
|
|
|
Set<String> groups = new HashSet<String>();
|
|
|
|
List<ResourceBean> resources = record.getExtrasResources();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// validate end set sources
|
2018-05-10 16:44:48 +02:00
|
|
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
|
|
|
Product_Type.STOCK, tags, customFields, groups, resources, username, title);
|
|
|
|
|
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;
|
2018-05-10 16:44:48 +02:00
|
|
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
|
|
|
license = record.getLicense();
|
2017-07-12 18:05:35 +02:00
|
|
|
else
|
2018-05-10 16:44:48 +02:00
|
|
|
throw new Exception("Please check the license id!");
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// set the visibility of the datatest according the context
|
2018-05-10 16:44:48 +02:00
|
|
|
boolean publicDataset = context
|
|
|
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// convert extras' keys to keys with namespace
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,String> namespaces = HelperMethods
|
|
|
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
if(namespaces == null)
|
|
|
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// retrieve the url
|
2018-05-10 16:44:48 +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));
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// replace fields
|
2018-05-10 16:44:48 +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);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Invoking update method..");
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
// update the product
|
2018-05-10 16:44:48 +02:00
|
|
|
String id = catalogue.updateCKanDataset(apiKey, catalogId, title, name, publishInOrganization,
|
|
|
|
authorFullname, authorMail, record.getMaintainer(), record.getMaintainerContact(), version,
|
2019-06-14 17:07:35 +02:00
|
|
|
HelperMethods.removeHTML(record.getDescription(), false), license, new ArrayList<String>(tags), null, // remove any previous group
|
2018-05-10 16:44:48 +02:00
|
|
|
customFields, resources, publicDataset);
|
|
|
|
|
|
|
|
if(id != null) {
|
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
logger.info("Item updated!");
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-10-31 12:31:52 +01:00
|
|
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-10-31 12:31:52 +01:00
|
|
|
if(sourceInPath.equals(Sources.GRSF))
|
2018-05-10 16:44:48 +02:00
|
|
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId()
|
|
|
|
+ "\n";
|
|
|
|
CommonServiceUtils.actionsPostCreateOrUpdate(recordPublished.getId(), name, record, apiKey,
|
|
|
|
username, organization, itemUrl, responseBean, catalogue, namespaces, groups, context,
|
|
|
|
token, title, authorFullname, contextServlet, true, description);
|
2017-07-12 18:05:35 +02:00
|
|
|
status = Status.OK;
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
} else {
|
2017-07-12 18:05:35 +02:00
|
|
|
throw new Exception("There was an error during the item updated, sorry");
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Failed to update stock record", e);
|
2017-07-12 18:05:35 +02:00
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
2016-12-09 16:56:52 +01:00
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
@POST
|
|
|
|
@Path("update-status")
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2018-05-10 16:44:48 +02:00
|
|
|
public Response updateStatusStock(@Valid UpdateRecordStatus bean, @PathParam("source") String source)
|
|
|
|
throws ValidationException {
|
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
|
|
|
String username = caller.getClient().getId();
|
|
|
|
String context = ScopeProvider.instance.get();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user "
|
|
|
|
+ username + " in context " + context);
|
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
|
|
|
|
|
|
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
|
|
|
if(catalogue == null) {
|
|
|
|
throw new Exception(
|
|
|
|
"There was a problem while serving your request. No catalogue instance was found in this context!");
|
|
|
|
} else {
|
2018-02-08 16:58:11 +01:00
|
|
|
|
|
|
|
// catalog id must be reported
|
|
|
|
String uuid = bean.getUuid();
|
|
|
|
String newStatus = bean.getNewStatus().getOrigName();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
// get the dataset
|
|
|
|
String apiKeyUser = catalogue.getApiKeyFromUsername(username);
|
|
|
|
CkanDataset record = catalogue.getDataset(uuid, apiKeyUser);
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
if(record == null)
|
|
|
|
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
// check system type
|
2018-05-10 16:44:48 +02:00
|
|
|
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY)
|
|
|
|
.equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
|
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
if(!isGRSF)
|
|
|
|
throw new Exception("You are trying to modify a Legacy record!");
|
|
|
|
|
2018-05-10 16:44:48 +02:00
|
|
|
boolean rightDomain = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY)
|
|
|
|
.equals(Product_Type.STOCK.getOrigName());
|
2018-02-08 16:58:11 +01:00
|
|
|
|
|
|
|
if(!rightDomain)
|
|
|
|
throw new Exception("This is not a Stock record!");
|
|
|
|
|
|
|
|
// update it
|
2018-05-10 16:44:48 +02:00
|
|
|
Map<String,List<String>> updateStatus = new HashMap<String,List<String>>(1);
|
2018-02-08 16:58:11 +01:00
|
|
|
updateStatus.put(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY, Arrays.asList(newStatus));
|
|
|
|
catalogue.patchProductCustomFields(uuid, apiKeyUser, updateStatus, true);
|
|
|
|
status = Status.OK;
|
|
|
|
responseBean.setError(null);
|
|
|
|
responseBean.setKbUuid(uuid);
|
|
|
|
responseBean.setId(record.getId());
|
|
|
|
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
} catch(Exception e) {
|
|
|
|
logger.error("Failed to update stock record's status", e);
|
2018-02-08 16:58:11 +01:00
|
|
|
responseBean.setError(e.getMessage());
|
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
return Response.status(status).entity(responseBean).build();
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2018-02-08 16:58:11 +01:00
|
|
|
}
|
2018-05-10 16:44:48 +02:00
|
|
|
|
2017-07-12 18:05:35 +02:00
|
|
|
}
|