Refs #11749: Add 'With Similarities' - 'No Similarities' tag to GRSF Records
Task-Url: https://support.d4science.org/issues/11749 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@167394 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
06d369db23
commit
3a451b5ac7
|
@ -24,7 +24,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Information that both Stock and Fishery records must contain.
|
* Information that both Stock and Fishery records must contain.
|
||||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
* @author Costantino Perciante (ISTI - CNR)
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
public abstract class Common extends Base{
|
public abstract class Common extends Base{
|
||||||
|
|
||||||
|
@ -119,6 +120,11 @@ public abstract class Common extends Base{
|
||||||
@CustomField(key=Constants.ANNOTATION_PUBLISHER_CUSTOM_KEY)
|
@CustomField(key=Constants.ANNOTATION_PUBLISHER_CUSTOM_KEY)
|
||||||
private List<AnnotationBean> annotations;
|
private List<AnnotationBean> annotations;
|
||||||
|
|
||||||
|
@JsonProperty(Constants.SIMILARITIES_INDICATOR)
|
||||||
|
@Tag
|
||||||
|
private String similaritiesIndicator;
|
||||||
|
|
||||||
|
|
||||||
public Common() {
|
public Common() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
@ -342,6 +348,14 @@ public abstract class Common extends Base{
|
||||||
this.sdgFlag = sdgFlag;
|
this.sdgFlag = sdgFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSimilaritiesIndicator() {
|
||||||
|
return similaritiesIndicator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSimilaritiesIndicator(String similaritiesIndicator) {
|
||||||
|
this.similaritiesIndicator = similaritiesIndicator;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Common [dataOwner=" + dataOwner + ", databaseSources="
|
return "Common [dataOwner=" + dataOwner + ", databaseSources="
|
||||||
|
|
|
@ -48,13 +48,15 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fishery web service methods.
|
* Fishery web service methods.
|
||||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
* @author Costantino Perciante (ISTI - CNR)
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
@Path("{source:firms|FIRMS|grsf|GRSF|FishSource|fishsource}/fishery/")
|
@Path("{source:firms|FIRMS|grsf|GRSF|FishSource|fishsource}/fishery/")
|
||||||
public class GrsfPublisherFisheryService {
|
public class GrsfPublisherFisheryService {
|
||||||
|
|
||||||
// the context
|
// the context
|
||||||
@Context ServletContext contextServlet;
|
@Context
|
||||||
|
ServletContext contextServlet;
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class);
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class);
|
||||||
|
@ -62,27 +64,28 @@ public class GrsfPublisherFisheryService {
|
||||||
@GET
|
@GET
|
||||||
@Path("hello")
|
@Path("hello")
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
public Response hello(){
|
public Response hello() {
|
||||||
return Response.ok("Hello.. Fishery service is here").build();
|
return Response.ok("Hello.. Fishery service is here").build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("get-licenses")
|
@Path("get-licenses")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getLicenses(){
|
public Response getLicenses() {
|
||||||
Status status = Status.OK;
|
Status status = Status.OK;
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
DataCatalogue catalogue;
|
DataCatalogue catalogue;
|
||||||
try {
|
try {
|
||||||
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
Map<String, String> licenses = CommonServiceUtils.getLicenses(catalogue);
|
Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
|
||||||
if(licenses == null)
|
if(licenses == null)
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
return Response.status(status).entity(licenses).build();
|
return Response.status(status).entity(licenses).build();
|
||||||
} catch (Exception e) {
|
} catch(Exception e) {
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
return Response.status(status).entity(new ResponseBean(false,
|
return Response.status(status)
|
||||||
"Unable to retrieve license list " + e.getLocalizedMessage(), null)).build();
|
.entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,45 +93,44 @@ public class GrsfPublisherFisheryService {
|
||||||
@Path("publish-product")
|
@Path("publish-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response publishFishery(
|
public Response publishFishery(@NotNull(message = "record cannot be null") @Valid FisheryRecord record,
|
||||||
@NotNull(message="record cannot be null")
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@Valid FisheryRecord record,
|
|
||||||
@PathParam("source") String source)
|
|
||||||
throws ValidationException{
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
String token = SecurityTokenProvider.instance.get();
|
String token = SecurityTokenProvider.instance.get();
|
||||||
|
|
||||||
logger.info("Incoming request for creating a fishery record = " + record + ".\nRequest comes from user " + username + " in context " + context);
|
logger.info("Incoming request for creating a fishery record = " + record + ".\nRequest comes from user "
|
||||||
|
+ username + " in context " + context);
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
String id = ""; // id of the created record, if everything went ok
|
String id = ""; // id of the created record, if everything went ok
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
// Cast the source to the accepted ones
|
// Cast the source to the accepted ones
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
||||||
}else{
|
} else {
|
||||||
|
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
String organization = HelperMethods.retrieveOrgNameFromScope(context);
|
String organization = HelperMethods.retrieveOrgNameFromScope(context);
|
||||||
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
||||||
|
|
||||||
// extend this role to the other organizations in this context
|
// extend this role to the other organizations in this context
|
||||||
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization, RolesCkanGroupOrOrg.ADMIN);
|
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
|
||||||
|
RolesCkanGroupOrOrg.ADMIN);
|
||||||
|
|
||||||
// retrieve the user's email and fullname
|
// retrieve the user's email and fullname
|
||||||
String authorMail = HelperMethods.getUserEmail(context, token);
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
||||||
String authorFullname = HelperMethods.getUserFullname(context, token);
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
||||||
|
|
||||||
if(authorMail == null || authorFullname == null){
|
if(authorMail == null || authorFullname == null) {
|
||||||
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,92 +142,72 @@ public class GrsfPublisherFisheryService {
|
||||||
// check name
|
// check name
|
||||||
CommonServiceUtils.checkName(futureName, catalogue);
|
CommonServiceUtils.checkName(futureName, catalogue);
|
||||||
|
|
||||||
Map<String, List<String>> customFields = record.getExtrasFields();
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
||||||
Set<String> tags = new HashSet<String>();
|
Set<String> tags = new HashSet<String>();
|
||||||
Set<String> groups = new HashSet<String>();
|
Set<String> groups = new HashSet<String>();
|
||||||
List<ResourceBean> resources = record.getExtrasResources();
|
List<ResourceBean> resources = record.getExtrasResources();
|
||||||
|
|
||||||
// validate end set sources
|
// validate end set sources
|
||||||
CommonServiceUtils.validateRecordAndMapFields(
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
||||||
apiKey,
|
Product_Type.FISHERY, tags, customFields, groups, resources, username, futureTitle);
|
||||||
context,
|
|
||||||
contextServlet,
|
|
||||||
sourceInPath,
|
|
||||||
record,
|
|
||||||
Product_Type.FISHERY,
|
|
||||||
tags,
|
|
||||||
customFields,
|
|
||||||
groups,
|
|
||||||
resources,
|
|
||||||
username,
|
|
||||||
futureTitle);
|
|
||||||
|
|
||||||
// check the license id
|
// check the license id
|
||||||
String license = null;
|
String license = null;
|
||||||
if(record.getLicense() == null || record.getLicense().isEmpty())
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
||||||
license = Constants.DEFAULT_LICENSE;
|
license = Constants.DEFAULT_LICENSE;
|
||||||
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
||||||
|
license = record.getLicense();
|
||||||
else
|
else
|
||||||
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
throw new Exception("Please check the license id!");
|
||||||
license = record.getLicense();
|
|
||||||
else
|
|
||||||
throw new Exception("Please check the license id!");
|
|
||||||
|
|
||||||
long version = record.getVersion() == null ? 1 : record.getVersion();
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
||||||
|
|
||||||
// set the visibility of the datatest according the context
|
// set the visibility of the datatest according the context
|
||||||
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
boolean publicDataset = context
|
||||||
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
||||||
|
|
||||||
// convert extras' keys to keys with namespace
|
// convert extras' keys to keys with namespace
|
||||||
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
Map<String,String> namespaces = HelperMethods
|
||||||
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
||||||
|
|
||||||
if(namespaces == null)
|
if(namespaces == null)
|
||||||
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
||||||
|
|
||||||
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF));
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces,
|
||||||
|
!sourceInPath.equals(Sources.GRSF));
|
||||||
|
|
||||||
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
||||||
|
|
||||||
logger.info("Invoking create method..");
|
logger.info("Invoking create method..");
|
||||||
|
|
||||||
// create the product
|
// create the product
|
||||||
id = catalogue.createCKanDatasetMultipleCustomFields(
|
id = catalogue.createCKanDatasetMultipleCustomFields(apiKey, futureTitle, futureName,
|
||||||
apiKey,
|
publishInOrganization, authorFullname, authorMail,
|
||||||
futureTitle,
|
record.getMaintainer() == null ? authorFullname : record.getMaintainer(),
|
||||||
futureName,
|
record.getMaintainerContact() == null ? authorMail : record.getMaintainerContact(), version,
|
||||||
publishInOrganization,
|
null, license, new ArrayList<String>(tags), customFields, resources, publicDataset);
|
||||||
authorFullname,
|
|
||||||
authorMail,
|
|
||||||
record.getMaintainer() == null? authorFullname : record.getMaintainer(),
|
|
||||||
record.getMaintainerContact() == null? authorMail : record.getMaintainerContact(),
|
|
||||||
version,
|
|
||||||
null,
|
|
||||||
license,
|
|
||||||
new ArrayList<String>(tags),
|
|
||||||
customFields,
|
|
||||||
resources,
|
|
||||||
publicDataset);
|
|
||||||
|
|
||||||
// post actions
|
// post actions
|
||||||
if(id != null){
|
if(id != null) {
|
||||||
|
|
||||||
logger.info("Created record with identifier " + id);
|
logger.info("Created record with identifier " + id);
|
||||||
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
||||||
|
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getFisheryId() + "\n";
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getFisheryId()
|
||||||
|
+ "\n";
|
||||||
|
|
||||||
CommonServiceUtils.actionsPostCreateOrUpdate(
|
CommonServiceUtils.actionsPostCreateOrUpdate(id, futureName, record, apiKey, username, organization,
|
||||||
id, futureName, record, apiKey, username, organization,
|
null, responseBean, catalogue, namespaces, groups, context, token, futureTitle,
|
||||||
null, responseBean, catalogue, namespaces, groups, context, token, futureTitle, authorFullname,
|
authorFullname, contextServlet, false, description);
|
||||||
contextServlet, false, description);
|
|
||||||
|
|
||||||
status = Status.CREATED;
|
status = Status.CREATED;
|
||||||
|
|
||||||
}else{
|
} else {
|
||||||
throw new Exception("There was an error during the record generation, sorry");
|
throw new Exception("There was an error during the record generation, sorry");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to create fishery record" + e);
|
logger.error("Failed to create fishery record" + e);
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
|
@ -238,10 +220,8 @@ public class GrsfPublisherFisheryService {
|
||||||
@Path("delete-product")
|
@Path("delete-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response deleteFishery(
|
public Response deleteFishery(@NotNull(message = "input value is missing") @Valid DeleteRecord recordToDelete,
|
||||||
@NotNull(message="input value is missing")
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@Valid DeleteRecord recordToDelete,
|
|
||||||
@PathParam("source") String source) throws ValidationException{
|
|
||||||
|
|
||||||
// retrieve context and username
|
// retrieve context and username
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
|
@ -252,8 +232,9 @@ public class GrsfPublisherFisheryService {
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
// check it is a fishery ...
|
// check it is a fishery ...
|
||||||
logger.info("Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a fishery");
|
logger.info(
|
||||||
try{
|
"Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a fishery");
|
||||||
|
try {
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
|
@ -265,7 +246,7 @@ public class GrsfPublisherFisheryService {
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
CkanDataset fisheryInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
CkanDataset fisheryInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
||||||
|
|
||||||
if(fisheryInCkan == null){
|
if(fisheryInCkan == null) {
|
||||||
status = Status.NOT_FOUND;
|
status = Status.NOT_FOUND;
|
||||||
throw new Exception("There was a problem while serving your request. This item was not found");
|
throw new Exception("There was a problem while serving your request. This item was not found");
|
||||||
}
|
}
|
||||||
|
@ -273,22 +254,24 @@ public class GrsfPublisherFisheryService {
|
||||||
// check it is in the right source and it is a fishery
|
// check it is in the right source and it is a fishery
|
||||||
String type = fisheryInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
String type = fisheryInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
||||||
|
|
||||||
if((fisheryInCkan.getOrganization().getName().equalsIgnoreCase(source) || fisheryInCkan.getOrganization().getName().toLowerCase().contains(source))&&
|
if((fisheryInCkan.getOrganization().getName().equalsIgnoreCase(source)
|
||||||
Product_Type.FISHERY.getOrigName().equals(type)){
|
|| fisheryInCkan.getOrganization().getName().toLowerCase().contains(source))
|
||||||
|
&& Product_Type.FISHERY.getOrigName().equals(type)) {
|
||||||
|
|
||||||
logger.debug("Ok, this is a fishery of the right source, removing it");
|
logger.debug("Ok, this is a fishery of the right source, removing it");
|
||||||
boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), apiKey, true);
|
boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), apiKey, true);
|
||||||
|
|
||||||
if(deleted){
|
if(deleted) {
|
||||||
logger.info("Fishery DELETED AND PURGED!");
|
logger.info("Fishery DELETED AND PURGED!");
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
responseBean.setId(fisheryInCkan.getId());
|
responseBean.setId(fisheryInCkan.getId());
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
status = Status.BAD_REQUEST;
|
status = Status.BAD_REQUEST;
|
||||||
throw new Exception("The id you are using doesn't belong to a Fishery item having source " + source + "!");
|
throw new Exception(
|
||||||
|
"The id you are using doesn't belong to a Fishery item having source " + source + "!");
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to delete this", e);
|
logger.error("Failed to delete this", e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -299,8 +282,7 @@ public class GrsfPublisherFisheryService {
|
||||||
@GET
|
@GET
|
||||||
@Path("get-fisheries-ids")
|
@Path("get-fisheries-ids")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getFisheriesIds(
|
public Response getFisheriesIds(@PathParam("source") String source) {
|
||||||
@PathParam("source") String source){
|
|
||||||
|
|
||||||
logger.info("Received call to get fisheries with source " + source);
|
logger.info("Received call to get fisheries with source " + source);
|
||||||
|
|
||||||
|
@ -311,13 +293,13 @@ public class GrsfPublisherFisheryService {
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
List<String> datasetsIds = new ArrayList<String>();
|
List<String> datasetsIds = new ArrayList<String>();
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
// Cast the source to the accepted ones
|
// Cast the source to the accepted ones
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request");
|
throw new Exception("There was a problem while serving your request");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,12 +307,12 @@ public class GrsfPublisherFisheryService {
|
||||||
// For other cases, records needs to be parsed
|
// For other cases, records needs to be parsed
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "fishery", catalogue);
|
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "fishery", catalogue);
|
||||||
else{
|
else {
|
||||||
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
for (String id : fullGroupListIds) {
|
for(String id : fullGroupListIds) {
|
||||||
CkanDataset dataset = catalogue.getDataset(id, apiKey);
|
CkanDataset dataset = catalogue.getDataset(id, apiKey);
|
||||||
if(dataset != null){
|
if(dataset != null) {
|
||||||
String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
||||||
if(Product_Type.FISHERY.getOrigName().equals(type))
|
if(Product_Type.FISHERY.getOrigName().equals(type))
|
||||||
datasetsIds.add(id);
|
datasetsIds.add(id);
|
||||||
|
@ -341,7 +323,7 @@ public class GrsfPublisherFisheryService {
|
||||||
responseBean.setResult(datasetsIds);
|
responseBean.setResult(datasetsIds);
|
||||||
responseBean.setSuccess(true);
|
responseBean.setSuccess(true);
|
||||||
|
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to fetch this list of ids ", e);
|
logger.error("Failed to fetch this list of ids ", e);
|
||||||
responseBean.setMessage(e.getMessage());
|
responseBean.setMessage(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -352,8 +334,7 @@ public class GrsfPublisherFisheryService {
|
||||||
@GET
|
@GET
|
||||||
@Path("get-catalogue-id-and-url-from-name")
|
@Path("get-catalogue-id-and-url-from-name")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getCatalogueIdAndUrlFromKBID(
|
public Response getCatalogueIdAndUrlFromKBID(@QueryParam("name") String name) {
|
||||||
@QueryParam("name") String name){
|
|
||||||
|
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
|
@ -363,23 +344,23 @@ public class GrsfPublisherFisheryService {
|
||||||
|
|
||||||
logger.info("Received call to get the catalogue identifier for the product with name " + name);
|
logger.info("Received call to get the catalogue identifier for the product with name " + name);
|
||||||
|
|
||||||
try{
|
try {
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request");
|
throw new Exception("There was a problem while serving your request");
|
||||||
}
|
}
|
||||||
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
||||||
if(dataset != null){
|
if(dataset != null) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String,String> result = new HashMap<String,String>();
|
||||||
result.put("id", dataset.getId());
|
result.put("id", dataset.getId());
|
||||||
result.put("url", catalogue.getUnencryptedUrlFromDatasetIdOrName(dataset.getId()));
|
result.put("url", catalogue.getUnencryptedUrlFromDatasetIdOrName(dataset.getId()));
|
||||||
responseBean.setResult(result);
|
responseBean.setResult(result);
|
||||||
responseBean.setSuccess(true);
|
responseBean.setSuccess(true);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
}else{
|
} else {
|
||||||
responseBean.setMessage("Unable to retrieve a catalogue item with name " + name);
|
responseBean.setMessage("Unable to retrieve a catalogue item with name " + name);
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to retrieve this product", e);
|
logger.error("Failed to retrieve this product", e);
|
||||||
responseBean.setMessage(e.getMessage());
|
responseBean.setMessage(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -390,18 +371,16 @@ public class GrsfPublisherFisheryService {
|
||||||
@Path("update-product")
|
@Path("update-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response updateFishery(
|
public Response updateFishery(@NotNull(message = "record cannot be null") @Valid FisheryRecord record,
|
||||||
@NotNull(message="record cannot be null")
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@Valid FisheryRecord record,
|
|
||||||
@PathParam("source") String source)
|
|
||||||
throws ValidationException{
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
String token = SecurityTokenProvider.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);
|
logger.info("Incoming request for updating a fishery record = " + record + ". Request comes from user "
|
||||||
|
+ username + " in context " + context);
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
@ -409,18 +388,19 @@ public class GrsfPublisherFisheryService {
|
||||||
// catalog id must be reported
|
// catalog id must be reported
|
||||||
String catalogId = record.getCatalogId();
|
String catalogId = record.getCatalogId();
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
if(catalogId == null || catalogId.isEmpty()){
|
if(catalogId == null || catalogId.isEmpty()) {
|
||||||
status = Status.BAD_REQUEST;
|
status = Status.BAD_REQUEST;
|
||||||
throw new Exception("Please specify the '" + Constants.CATALOG_ID + "' property");
|
throw new Exception("Please specify the '" + Constants.CATALOG_ID + "' property");
|
||||||
}
|
}
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
|
throw new Exception(
|
||||||
}else{
|
"There was a problem while serving your request. No catalogue instance was found in this context!");
|
||||||
|
} else {
|
||||||
|
|
||||||
// get already published record and modify it
|
// get already published record and modify it
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
|
@ -433,12 +413,12 @@ public class GrsfPublisherFisheryService {
|
||||||
String authorMail = HelperMethods.getUserEmail(context, token);
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
||||||
String authorFullname = HelperMethods.getUserFullname(context, token);
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
||||||
|
|
||||||
if(authorMail == null || authorFullname == null){
|
if(authorMail == null || authorFullname == null) {
|
||||||
logger.debug("Author fullname or mail missing, cannot continue");
|
logger.debug("Author fullname or mail missing, cannot continue");
|
||||||
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
||||||
}
|
}
|
||||||
|
|
||||||
String organization = HelperMethods.retrieveOrgNameFromScope(context);
|
String organization = HelperMethods.retrieveOrgNameFromScope(context);
|
||||||
|
|
||||||
// check he/she has admin role
|
// check he/she has admin role
|
||||||
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
||||||
|
@ -451,97 +431,76 @@ public class GrsfPublisherFisheryService {
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
|
|
||||||
// load infos
|
// load infos
|
||||||
Map<String, List<String>> customFields = record.getExtrasFields();
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
||||||
Set<String> tags = new HashSet<String>();
|
Set<String> tags = new HashSet<String>();
|
||||||
Set<String> groups = new HashSet<String>();
|
Set<String> groups = new HashSet<String>();
|
||||||
List<ResourceBean> resources = record.getExtrasResources();
|
List<ResourceBean> resources = record.getExtrasResources();
|
||||||
|
|
||||||
// validate end set sources
|
// validate end set sources
|
||||||
CommonServiceUtils.validateRecordAndMapFields(
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
||||||
apiKey,
|
Product_Type.FISHERY, tags, customFields, groups, resources, username, title);
|
||||||
context,
|
|
||||||
contextServlet,
|
|
||||||
sourceInPath,
|
|
||||||
record,
|
|
||||||
Product_Type.FISHERY,
|
|
||||||
tags,
|
|
||||||
customFields,
|
|
||||||
groups,
|
|
||||||
resources,
|
|
||||||
username,
|
|
||||||
title);
|
|
||||||
|
|
||||||
// check the license id
|
// check the license id
|
||||||
String license = null;
|
String license = null;
|
||||||
if(record.getLicense() == null || record.getLicense().isEmpty())
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
||||||
license = Constants.DEFAULT_LICENSE;
|
license = Constants.DEFAULT_LICENSE;
|
||||||
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
||||||
|
license = record.getLicense();
|
||||||
else
|
else
|
||||||
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
throw new Exception("Please check the license id!");
|
||||||
license = record.getLicense();
|
|
||||||
else throw new Exception("Please check the license id!");
|
|
||||||
|
|
||||||
long version = record.getVersion() == null ? 1 : record.getVersion();
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
||||||
|
|
||||||
// set the visibility of the datatest according the context
|
// set the visibility of the datatest according the context
|
||||||
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
boolean publicDataset = context
|
||||||
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
||||||
|
|
||||||
// convert extras' keys to keys with namespace
|
// convert extras' keys to keys with namespace
|
||||||
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
Map<String,String> namespaces = HelperMethods
|
||||||
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY);
|
||||||
|
|
||||||
if(namespaces == null)
|
if(namespaces == null)
|
||||||
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
||||||
|
|
||||||
// retrieve the already generated url
|
// retrieve the already generated url
|
||||||
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD) ?
|
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD)
|
||||||
namespaces.get(Constants.ITEM_URL_FIELD) :
|
? namespaces.get(Constants.ITEM_URL_FIELD)
|
||||||
Constants.ITEM_URL_FIELD;
|
: Constants.ITEM_URL_FIELD;
|
||||||
String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey);
|
String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey);
|
||||||
customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl));
|
customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl));
|
||||||
|
|
||||||
// replace fields
|
// replace fields
|
||||||
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF));
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces,
|
||||||
|
!sourceInPath.equals(Sources.GRSF));
|
||||||
|
|
||||||
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
||||||
|
|
||||||
logger.info("Invoking update method..");
|
logger.info("Invoking update method..");
|
||||||
|
|
||||||
// update the product
|
// update the product
|
||||||
String id = catalogue.updateCKanDataset(
|
String id = catalogue.updateCKanDataset(apiKey, catalogId, title, name, publishInOrganization,
|
||||||
apiKey,
|
authorFullname, authorMail, record.getMaintainer(), record.getMaintainerContact(), version,
|
||||||
catalogId,
|
HelperMethods.removeHTML(record.getDescription()), license, new ArrayList<String>(tags), null, // remove any previous group
|
||||||
title, name,
|
customFields, resources, publicDataset);
|
||||||
publishInOrganization,
|
|
||||||
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){
|
if(id != null) {
|
||||||
logger.info("Item updated!");
|
logger.info("Item updated!");
|
||||||
|
|
||||||
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
||||||
|
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getFisheryId() + "\n";
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getFisheryId()
|
||||||
|
+ "\n";
|
||||||
|
|
||||||
CommonServiceUtils.actionsPostCreateOrUpdate(
|
CommonServiceUtils.actionsPostCreateOrUpdate(id, name, record, apiKey, username, organization,
|
||||||
id, name, record, apiKey, username, organization,
|
itemUrl, responseBean, catalogue, namespaces, groups, context, token, title, authorFullname,
|
||||||
itemUrl, responseBean, catalogue, namespaces, groups, context, token, title, authorFullname,
|
contextServlet, true, description);
|
||||||
contextServlet, true, description);
|
status = Status.OK;
|
||||||
status = Status.OK;
|
} else {
|
||||||
}else{
|
throw new Exception("There was an error during the item updated, sorry");
|
||||||
throw new Exception("There was an error during the item updated, sorry");
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to update fishery record" + e);
|
logger.error("Failed to update fishery record" + e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -552,28 +511,27 @@ public class GrsfPublisherFisheryService {
|
||||||
@Path("update-status")
|
@Path("update-status")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response updateStatusStock(
|
public Response updateStatusStock(@Valid UpdateRecordStatus bean, @PathParam("source") String source)
|
||||||
@Valid UpdateRecordStatus bean,
|
throws ValidationException {
|
||||||
@PathParam("source") String source)
|
|
||||||
throws ValidationException{
|
|
||||||
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
|
|
||||||
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user " + username + " in context " + context);
|
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user "
|
||||||
|
+ username + " in context " + context);
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
|
throw new Exception(
|
||||||
}else{
|
"There was a problem while serving your request. No catalogue instance was found in this context!");
|
||||||
|
} else {
|
||||||
|
|
||||||
// catalog id must be reported
|
// catalog id must be reported
|
||||||
String uuid = bean.getUuid();
|
String uuid = bean.getUuid();
|
||||||
|
@ -587,18 +545,20 @@ public class GrsfPublisherFisheryService {
|
||||||
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
|
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
|
||||||
|
|
||||||
// check system type
|
// check system type
|
||||||
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY).equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
|
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY)
|
||||||
|
.equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
|
||||||
|
|
||||||
if(!isGRSF)
|
if(!isGRSF)
|
||||||
throw new Exception("You are trying to modify a Legacy record!");
|
throw new Exception("You are trying to modify a Legacy record!");
|
||||||
|
|
||||||
boolean rightDomain = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY).equalsIgnoreCase(Product_Type.FISHERY.getOrigName());
|
boolean rightDomain = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY)
|
||||||
|
.equalsIgnoreCase(Product_Type.FISHERY.getOrigName());
|
||||||
|
|
||||||
if(!rightDomain)
|
if(!rightDomain)
|
||||||
throw new Exception("This is not a Fishery record!");
|
throw new Exception("This is not a Fishery record!");
|
||||||
|
|
||||||
// update it
|
// update it
|
||||||
Map<String, List<String>> updateStatus = new HashMap<String, List<String>>(1);
|
Map<String,List<String>> updateStatus = new HashMap<String,List<String>>(1);
|
||||||
updateStatus.put(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY, Arrays.asList(newStatus));
|
updateStatus.put(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY, Arrays.asList(newStatus));
|
||||||
catalogue.patchProductCustomFields(uuid, apiKeyUser, updateStatus, true);
|
catalogue.patchProductCustomFields(uuid, apiKeyUser, updateStatus, true);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
|
@ -608,8 +568,8 @@ public class GrsfPublisherFisheryService {
|
||||||
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
|
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
|
||||||
|
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to update fishery record's status", e);
|
logger.error("Failed to update fishery record's status", e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,13 +48,15 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stock web service methods.
|
* Stock web service methods.
|
||||||
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
* @author Costantino Perciante (ISTI - CNR)
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
@Path("{source:firms|FIRMS|ram|RAM|grsf|GRSF|FishSource|fishsource}/stock/")
|
@Path("{source:firms|FIRMS|ram|RAM|grsf|GRSF|FishSource|fishsource}/stock/")
|
||||||
public class GrsfPublisherStockService {
|
public class GrsfPublisherStockService {
|
||||||
|
|
||||||
// the context
|
// the context
|
||||||
@Context ServletContext contextServlet;
|
@Context
|
||||||
|
ServletContext contextServlet;
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
|
||||||
|
@ -62,27 +64,28 @@ public class GrsfPublisherStockService {
|
||||||
@GET
|
@GET
|
||||||
@Path("hello")
|
@Path("hello")
|
||||||
@Produces(MediaType.TEXT_PLAIN)
|
@Produces(MediaType.TEXT_PLAIN)
|
||||||
public Response hello(){
|
public Response hello() {
|
||||||
return Response.ok("Hello.. Stock service is here").build();
|
return Response.ok("Hello.. Stock service is here").build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("get-licenses")
|
@Path("get-licenses")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getLicenses(){
|
public Response getLicenses() {
|
||||||
Status status = Status.OK;
|
Status status = Status.OK;
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
DataCatalogue catalogue;
|
DataCatalogue catalogue;
|
||||||
try {
|
try {
|
||||||
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
Map<String, String> licenses = CommonServiceUtils.getLicenses(catalogue);
|
Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
|
||||||
if(licenses == null)
|
if(licenses == null)
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
return Response.status(status).entity(licenses).build();
|
return Response.status(status).entity(licenses).build();
|
||||||
} catch (Exception e) {
|
} catch(Exception e) {
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
return Response.status(status).entity(new ResponseBean(false,
|
return Response.status(status)
|
||||||
"Unable to retrieve license list " + e.getLocalizedMessage(), null)).build();
|
.entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,45 +93,46 @@ public class GrsfPublisherStockService {
|
||||||
@Path("publish-product")
|
@Path("publish-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response publishStock(
|
public Response publishStock(@NotNull(message = "record cannot be null") @Valid StockRecord record,
|
||||||
@NotNull(message="record cannot be null") @Valid StockRecord record,
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@PathParam("source") String source) throws ValidationException{
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
String token = SecurityTokenProvider.instance.get();
|
String token = SecurityTokenProvider.instance.get();
|
||||||
|
|
||||||
logger.info("Incoming request for creating a stock record = " + record + ".\nRequest coming from user " + username + " in context " + context);
|
logger.info("Incoming request for creating a stock record = " + record + ".\nRequest coming from user "
|
||||||
|
+ username + " in context " + context);
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
String id = ""; // id of the created record, if everything went ok
|
String id = ""; // id of the created record, if everything went ok
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
// Cast the source to the accepted ones
|
// Cast the source to the accepted ones
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
|
||||||
}else{
|
} else {
|
||||||
|
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
|
||||||
|
|
||||||
// check it has admin role or throw exception
|
// check it has admin role or throw exception
|
||||||
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
|
||||||
|
|
||||||
// extend this role to the other organizations in this context
|
// extend this role to the other organizations in this context
|
||||||
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization, RolesCkanGroupOrOrg.ADMIN);
|
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
|
||||||
|
RolesCkanGroupOrOrg.ADMIN);
|
||||||
|
|
||||||
// retrieve the user's email and fullname
|
// retrieve the user's email and fullname
|
||||||
String authorMail = HelperMethods.getUserEmail(context, token);
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
||||||
String authorFullname = HelperMethods.getUserFullname(context, token);
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
||||||
|
|
||||||
if(authorMail == null || authorFullname == null){
|
if(authorMail == null || authorFullname == null) {
|
||||||
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,90 +144,73 @@ public class GrsfPublisherStockService {
|
||||||
CommonServiceUtils.checkName(futureName, catalogue);
|
CommonServiceUtils.checkName(futureName, catalogue);
|
||||||
|
|
||||||
// load other information
|
// load other information
|
||||||
Map<String, List<String>> customFields = record.getExtrasFields();
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
||||||
Set<String> tags = new HashSet<String>();
|
Set<String> tags = new HashSet<String>();
|
||||||
Set<String> groups = new HashSet<String>();
|
Set<String> groups = new HashSet<String>();
|
||||||
List<ResourceBean> resources = record.getExtrasResources();
|
List<ResourceBean> resources = record.getExtrasResources();
|
||||||
|
|
||||||
// validate end set sources, tags, etc
|
// validate end set sources, tags, etc
|
||||||
CommonServiceUtils.validateRecordAndMapFields(
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
||||||
apiKey,
|
Product_Type.STOCK, tags, customFields, groups, resources, username, futureTitle);
|
||||||
context,
|
|
||||||
contextServlet,
|
|
||||||
sourceInPath,
|
|
||||||
record,
|
|
||||||
Product_Type.STOCK,
|
|
||||||
tags,
|
|
||||||
customFields,
|
|
||||||
groups,
|
|
||||||
resources,
|
|
||||||
username,
|
|
||||||
futureTitle);
|
|
||||||
|
|
||||||
// check the license id
|
// check the license id
|
||||||
String license = null;
|
String license = null;
|
||||||
if(record.getLicense() == null || record.getLicense().isEmpty())
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
||||||
license = Constants.DEFAULT_LICENSE;
|
license = Constants.DEFAULT_LICENSE;
|
||||||
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
||||||
|
license = record.getLicense();
|
||||||
else
|
else
|
||||||
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
throw new Exception("Please check the license id!");
|
||||||
license = record.getLicense();
|
|
||||||
else throw new Exception("Please check the license id!");
|
|
||||||
|
|
||||||
// check the version
|
// check the version
|
||||||
long version = record.getVersion() == null ? 1 : record.getVersion();
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
||||||
|
|
||||||
// set the visibility of the datatest according the context
|
// set the visibility of the datatest according the context
|
||||||
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
boolean publicDataset = context
|
||||||
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
||||||
|
|
||||||
// convert extras' keys to keys with namespace
|
// convert extras' keys to keys with namespace
|
||||||
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
Map<String,String> namespaces = HelperMethods
|
||||||
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
||||||
|
|
||||||
if(namespaces == null)
|
if(namespaces == null)
|
||||||
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
||||||
|
|
||||||
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF));
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces,
|
||||||
|
!sourceInPath.equals(Sources.GRSF));
|
||||||
|
|
||||||
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
||||||
|
|
||||||
logger.info("Invoking create method..");
|
logger.info("Invoking create method..");
|
||||||
|
|
||||||
// create the product
|
// create the product
|
||||||
id = catalogue.createCKanDatasetMultipleCustomFields(
|
id = catalogue.createCKanDatasetMultipleCustomFields(apiKey, futureTitle, futureName,
|
||||||
apiKey,
|
publishInOrganization, authorFullname, authorMail,
|
||||||
futureTitle,
|
record.getMaintainer() == null ? authorFullname : record.getMaintainer(),
|
||||||
futureName,
|
record.getMaintainerContact() == null ? authorMail : record.getMaintainerContact(), version,
|
||||||
publishInOrganization,
|
HelperMethods.removeHTML(record.getDescription()), license, new ArrayList<String>(tags),
|
||||||
authorFullname,
|
customFields, resources, publicDataset);
|
||||||
authorMail,
|
|
||||||
record.getMaintainer() == null? authorFullname : record.getMaintainer(),
|
|
||||||
record.getMaintainerContact() == null? authorMail : record.getMaintainerContact(),
|
|
||||||
version,
|
|
||||||
HelperMethods.removeHTML(record.getDescription()),
|
|
||||||
license,
|
|
||||||
new ArrayList<String>(tags),
|
|
||||||
customFields,
|
|
||||||
resources,
|
|
||||||
publicDataset);
|
|
||||||
|
|
||||||
if(id != null){
|
if(id != null) {
|
||||||
|
|
||||||
logger.info("Product created! Id is " + id);
|
logger.info("Product created! Id is " + id);
|
||||||
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
||||||
|
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId() + "\n";
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId()
|
||||||
|
+ "\n";
|
||||||
|
|
||||||
CommonServiceUtils.actionsPostCreateOrUpdate(
|
CommonServiceUtils.actionsPostCreateOrUpdate(id, futureName, record, apiKey, username, organization,
|
||||||
id, futureName, record, apiKey, username, organization, null,
|
null, responseBean, catalogue, namespaces, groups, context, token, futureTitle,
|
||||||
responseBean, catalogue, namespaces, groups, context, token,
|
authorFullname, contextServlet, false, description);
|
||||||
futureTitle, authorFullname, contextServlet, false, description);
|
|
||||||
|
|
||||||
status = Status.CREATED;
|
status = Status.CREATED;
|
||||||
|
|
||||||
}else
|
} else
|
||||||
throw new Exception("There was an error during the product generation, sorry! Unable to create the dataset");
|
throw new Exception(
|
||||||
|
"There was an error during the product generation, sorry! Unable to create the dataset");
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to create stock record", e);
|
logger.error("Failed to create stock record", e);
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
|
@ -235,10 +222,8 @@ public class GrsfPublisherStockService {
|
||||||
@Path("delete-product")
|
@Path("delete-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response deleteStock(
|
public Response deleteStock(@NotNull(message = "missing input value") @Valid DeleteRecord recordToDelete,
|
||||||
@NotNull(message="missing input value")
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@Valid DeleteRecord recordToDelete,
|
|
||||||
@PathParam("source") String source) throws ValidationException{
|
|
||||||
|
|
||||||
// retrieve context and username
|
// retrieve context and username
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
|
@ -249,11 +234,12 @@ public class GrsfPublisherStockService {
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
// check it is a stock ...
|
// check it is a stock ...
|
||||||
logger.info("Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a stock");
|
logger.info(
|
||||||
try{
|
"Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a stock");
|
||||||
|
try {
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
throw new Exception("There was a problem while serving your request");
|
throw new Exception("There was a problem while serving your request");
|
||||||
}
|
}
|
||||||
|
@ -266,32 +252,33 @@ public class GrsfPublisherStockService {
|
||||||
// retrieve the catalogue instance
|
// retrieve the catalogue instance
|
||||||
CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), apiKey);
|
||||||
|
|
||||||
if(stockInCkan == null){
|
if(stockInCkan == null) {
|
||||||
status = Status.NOT_FOUND;
|
status = Status.NOT_FOUND;
|
||||||
throw new Exception("There was a problem while serving your request. This product was not found");
|
throw new Exception("There was a problem while serving your request. This product was not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check it is in the right source and it is a stock
|
// check it is in the right source and it is a stock
|
||||||
String type = stockInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
String type = stockInCkan.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
||||||
if((stockInCkan.getOrganization().getName().equalsIgnoreCase(source) || stockInCkan.getOrganization().getName().toLowerCase().contains(source))
|
if((stockInCkan.getOrganization().getName().equalsIgnoreCase(source)
|
||||||
&& Product_Type.STOCK.getOrigName().equals(type)){
|
|| stockInCkan.getOrganization().getName().toLowerCase().contains(source))
|
||||||
|
&& Product_Type.STOCK.getOrigName().equals(type)) {
|
||||||
|
|
||||||
logger.debug("Ok, this is a stock of the right type, removing it");
|
logger.debug("Ok, this is a stock of the right type, removing it");
|
||||||
boolean deleted = catalogue.deleteProduct(stockInCkan.getId(), apiKey, true);
|
boolean deleted = catalogue.deleteProduct(stockInCkan.getId(), apiKey, true);
|
||||||
if(deleted){
|
if(deleted) {
|
||||||
logger.info("Stock DELETED AND PURGED!");
|
logger.info("Stock DELETED AND PURGED!");
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
responseBean.setId(stockInCkan.getId());
|
responseBean.setId(stockInCkan.getId());
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
throw new Exception("Request failed, sorry. Unable to delete/purge the stock");
|
throw new Exception("Request failed, sorry. Unable to delete/purge the stock");
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
status = Status.BAD_REQUEST;
|
status = Status.BAD_REQUEST;
|
||||||
throw new Exception("The id you are using doesn't belong to a Stock product having source " + source + "!");
|
throw new Exception(
|
||||||
|
"The id you are using doesn't belong to a Stock product having source " + source + "!");
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to delete this ", e);
|
logger.error("Failed to delete this ", e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -301,8 +288,7 @@ public class GrsfPublisherStockService {
|
||||||
@GET
|
@GET
|
||||||
@Path("get-stocks-ids")
|
@Path("get-stocks-ids")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getStocksIds(
|
public Response getStocksIds(@PathParam("source") String source) {
|
||||||
@PathParam("source") String source){
|
|
||||||
|
|
||||||
// retrieve context and username
|
// retrieve context and username
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
|
@ -314,12 +300,12 @@ public class GrsfPublisherStockService {
|
||||||
// check it is a stock ...
|
// check it is a stock ...
|
||||||
logger.info("Received call to get stocks with source " + source);
|
logger.info("Received call to get stocks with source " + source);
|
||||||
List<String> datasetsIds = new ArrayList<String>();
|
List<String> datasetsIds = new ArrayList<String>();
|
||||||
try{
|
try {
|
||||||
|
|
||||||
// Cast the source to the accepted ones
|
// Cast the source to the accepted ones
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
status = Status.INTERNAL_SERVER_ERROR;
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
throw new Exception("There was a problem while serving your request");
|
throw new Exception("There was a problem while serving your request");
|
||||||
}
|
}
|
||||||
|
@ -328,11 +314,11 @@ public class GrsfPublisherStockService {
|
||||||
// For other cases, records needs to be parsed
|
// For other cases, records needs to be parsed
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "stock", catalogue);
|
datasetsIds = HelperMethods.getProductsInGroup(source + "-" + "stock", catalogue);
|
||||||
else{
|
else {
|
||||||
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
List<String> fullGroupListIds = HelperMethods.getProductsInOrganization(source, catalogue);
|
||||||
for (String id : fullGroupListIds) {
|
for(String id : fullGroupListIds) {
|
||||||
CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username));
|
CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username));
|
||||||
if(dataset != null){
|
if(dataset != null) {
|
||||||
String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY);
|
||||||
if(Product_Type.STOCK.getOrigName().equals(type))
|
if(Product_Type.STOCK.getOrigName().equals(type))
|
||||||
datasetsIds.add(id);
|
datasetsIds.add(id);
|
||||||
|
@ -342,7 +328,7 @@ public class GrsfPublisherStockService {
|
||||||
responseBean.setResult(datasetsIds);
|
responseBean.setResult(datasetsIds);
|
||||||
responseBean.setSuccess(true);
|
responseBean.setSuccess(true);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to fetch this list of ids " + source, e);
|
logger.error("Failed to fetch this list of ids " + source, e);
|
||||||
responseBean.setMessage(e.getMessage());
|
responseBean.setMessage(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -352,8 +338,7 @@ public class GrsfPublisherStockService {
|
||||||
@GET
|
@GET
|
||||||
@Path("get-catalogue-id-and-url-from-name")
|
@Path("get-catalogue-id-and-url-from-name")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response getCatalogueIdAndUrlFromKBID(
|
public Response getCatalogueIdAndUrlFromKBID(@QueryParam("name") String name) {
|
||||||
@QueryParam("name") String name){
|
|
||||||
|
|
||||||
// retrieve context and username
|
// retrieve context and username
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
|
@ -362,23 +347,23 @@ public class GrsfPublisherStockService {
|
||||||
ResponseBean responseBean = new ResponseBean();
|
ResponseBean responseBean = new ResponseBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
logger.info("Received call to get the catalogue identifier for the product with name " + name);
|
logger.info("Received call to get the catalogue identifier for the product with name " + name);
|
||||||
try{
|
try {
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request");
|
throw new Exception("There was a problem while serving your request");
|
||||||
}
|
}
|
||||||
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
|
||||||
if(dataset != null){
|
if(dataset != null) {
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String,String> result = new HashMap<String,String>();
|
||||||
result.put("id", dataset.getId());
|
result.put("id", dataset.getId());
|
||||||
result.put("url", catalogue.getUnencryptedUrlFromDatasetIdOrName(dataset.getId()));
|
result.put("url", catalogue.getUnencryptedUrlFromDatasetIdOrName(dataset.getId()));
|
||||||
responseBean.setResult(result);
|
responseBean.setResult(result);
|
||||||
responseBean.setSuccess(true);
|
responseBean.setSuccess(true);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
}else{
|
} else {
|
||||||
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
|
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to retrieve this product", e);
|
logger.error("Failed to retrieve this product", e);
|
||||||
responseBean.setMessage(e.getMessage());
|
responseBean.setMessage(e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -389,18 +374,16 @@ public class GrsfPublisherStockService {
|
||||||
@Path("update-product")
|
@Path("update-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response updateStock(
|
public Response updateStock(@NotNull(message = "record cannot be null") @Valid StockRecord record,
|
||||||
@NotNull(message="record cannot be null")
|
@PathParam("source") String source) throws ValidationException {
|
||||||
@Valid StockRecord record,
|
|
||||||
@PathParam("source") String source)
|
|
||||||
throws ValidationException{
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
String token = SecurityTokenProvider.instance.get();
|
String token = SecurityTokenProvider.instance.get();
|
||||||
|
|
||||||
logger.info("Incoming request for updating a stock record = " + record + ". Request comes from user " + username + " in context " + context);
|
logger.info("Incoming request for updating a stock record = " + record + ". Request comes from user " + username
|
||||||
|
+ " in context " + context);
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
@ -408,18 +391,19 @@ public class GrsfPublisherStockService {
|
||||||
// catalog id must be reported
|
// catalog id must be reported
|
||||||
String catalogId = record.getCatalogId();
|
String catalogId = record.getCatalogId();
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
if(catalogId == null || catalogId.isEmpty()){
|
if(catalogId == null || catalogId.isEmpty()) {
|
||||||
status = Status.BAD_REQUEST;
|
status = Status.BAD_REQUEST;
|
||||||
throw new Exception("Please specify the 'catalog_id' property");
|
throw new Exception("Please specify the 'catalog_id' property");
|
||||||
}
|
}
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
|
throw new Exception(
|
||||||
}else{
|
"There was a problem while serving your request. No catalogue instance was found in this context!");
|
||||||
|
} else {
|
||||||
|
|
||||||
// get already published record and modify it
|
// get already published record and modify it
|
||||||
String apiKey = catalogue.getApiKeyFromUsername(username);
|
String apiKey = catalogue.getApiKeyFromUsername(username);
|
||||||
|
@ -432,7 +416,7 @@ public class GrsfPublisherStockService {
|
||||||
String authorMail = HelperMethods.getUserEmail(context, token);
|
String authorMail = HelperMethods.getUserEmail(context, token);
|
||||||
String authorFullname = HelperMethods.getUserFullname(context, token);
|
String authorFullname = HelperMethods.getUserFullname(context, token);
|
||||||
|
|
||||||
if(authorMail == null || authorFullname == null){
|
if(authorMail == null || authorFullname == null) {
|
||||||
logger.debug("Author fullname or mail missing, cannot continue");
|
logger.debug("Author fullname or mail missing, cannot continue");
|
||||||
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
|
||||||
}
|
}
|
||||||
|
@ -449,97 +433,78 @@ public class GrsfPublisherStockService {
|
||||||
Sources sourceInPath = Sources.onDeserialize(source);
|
Sources sourceInPath = Sources.onDeserialize(source);
|
||||||
|
|
||||||
// load infos
|
// load infos
|
||||||
Map<String, List<String>> customFields = record.getExtrasFields();
|
Map<String,List<String>> customFields = record.getExtrasFields();
|
||||||
Set<String> tags = new HashSet<String>();
|
Set<String> tags = new HashSet<String>();
|
||||||
Set<String> groups = new HashSet<String>();
|
Set<String> groups = new HashSet<String>();
|
||||||
List<ResourceBean> resources = record.getExtrasResources();
|
List<ResourceBean> resources = record.getExtrasResources();
|
||||||
|
|
||||||
// validate end set sources
|
// validate end set sources
|
||||||
CommonServiceUtils.validateRecordAndMapFields(
|
CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record,
|
||||||
apiKey,
|
Product_Type.STOCK, tags, customFields, groups, resources, username, title);
|
||||||
context,
|
|
||||||
contextServlet,
|
|
||||||
sourceInPath,
|
|
||||||
record,
|
|
||||||
Product_Type.STOCK,
|
|
||||||
tags,
|
|
||||||
customFields,
|
|
||||||
groups,
|
|
||||||
resources,
|
|
||||||
username,
|
|
||||||
title);
|
|
||||||
|
|
||||||
// check the license id
|
// check the license id
|
||||||
String license = null;
|
String license = null;
|
||||||
if(record.getLicense() == null || record.getLicense().isEmpty())
|
if(record.getLicense() == null || record.getLicense().isEmpty())
|
||||||
license = Constants.DEFAULT_LICENSE;
|
license = Constants.DEFAULT_LICENSE;
|
||||||
|
else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
||||||
|
license = record.getLicense();
|
||||||
else
|
else
|
||||||
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
|
throw new Exception("Please check the license id!");
|
||||||
license = record.getLicense();
|
|
||||||
else throw new Exception("Please check the license id!");
|
|
||||||
|
|
||||||
long version = record.getVersion() == null ? 1 : record.getVersion();
|
long version = record.getVersion() == null ? 1 : record.getVersion();
|
||||||
|
|
||||||
// set the visibility of the datatest according the context
|
// set the visibility of the datatest according the context
|
||||||
boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
boolean publicDataset = context
|
||||||
|
.equals((String) contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY));
|
||||||
|
|
||||||
// convert extras' keys to keys with namespace
|
// convert extras' keys to keys with namespace
|
||||||
Map<String, String> namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
Map<String,String> namespaces = HelperMethods
|
||||||
|
.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK);
|
||||||
|
|
||||||
if(namespaces == null)
|
if(namespaces == null)
|
||||||
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
throw new Exception("Failed to retrieve the namespaces for the key fields!");
|
||||||
|
|
||||||
// retrieve the url
|
// retrieve the url
|
||||||
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD) ? namespaces.get(Constants.ITEM_URL_FIELD) : Constants.ITEM_URL_FIELD;
|
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD)
|
||||||
|
? namespaces.get(Constants.ITEM_URL_FIELD)
|
||||||
|
: Constants.ITEM_URL_FIELD;
|
||||||
String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey);
|
String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey);
|
||||||
customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl));
|
customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl));
|
||||||
|
|
||||||
// replace fields
|
// replace fields
|
||||||
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces,!sourceInPath.equals(Sources.GRSF));
|
customFields = HelperMethods.replaceFieldsKey(customFields, namespaces,
|
||||||
|
!sourceInPath.equals(Sources.GRSF));
|
||||||
|
|
||||||
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath);
|
||||||
|
|
||||||
logger.info("Invoking update method..");
|
logger.info("Invoking update method..");
|
||||||
|
|
||||||
// update the product
|
// update the product
|
||||||
String id = catalogue.updateCKanDataset(
|
String id = catalogue.updateCKanDataset(apiKey, catalogId, title, name, publishInOrganization,
|
||||||
apiKey,
|
authorFullname, authorMail, record.getMaintainer(), record.getMaintainerContact(), version,
|
||||||
catalogId,
|
HelperMethods.removeHTML(record.getDescription()), license, new ArrayList<String>(tags), null, // remove any previous group
|
||||||
title, name,
|
customFields, resources, publicDataset);
|
||||||
publishInOrganization,
|
|
||||||
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){
|
if(id != null) {
|
||||||
|
|
||||||
logger.info("Item updated!");
|
logger.info("Item updated!");
|
||||||
|
|
||||||
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n";
|
||||||
|
|
||||||
if(sourceInPath.equals(Sources.GRSF))
|
if(sourceInPath.equals(Sources.GRSF))
|
||||||
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId() + "\n";
|
description += Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId()
|
||||||
CommonServiceUtils.actionsPostCreateOrUpdate(
|
+ "\n";
|
||||||
recordPublished.getId(), name, record, apiKey, username, organization, itemUrl,
|
CommonServiceUtils.actionsPostCreateOrUpdate(recordPublished.getId(), name, record, apiKey,
|
||||||
responseBean, catalogue, namespaces, groups, context, token,
|
username, organization, itemUrl, responseBean, catalogue, namespaces, groups, context,
|
||||||
title, authorFullname, contextServlet, true, description);
|
token, title, authorFullname, contextServlet, true, description);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
|
|
||||||
}else{
|
} else {
|
||||||
throw new Exception("There was an error during the item updated, sorry");
|
throw new Exception("There was an error during the item updated, sorry");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to update stock record", e);
|
logger.error("Failed to update stock record", e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
return Response.status(status).entity(responseBean).build();
|
return Response.status(status).entity(responseBean).build();
|
||||||
|
@ -549,29 +514,28 @@ public class GrsfPublisherStockService {
|
||||||
@Path("update-status")
|
@Path("update-status")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response updateStatusStock(
|
public Response updateStatusStock(@Valid UpdateRecordStatus bean, @PathParam("source") String source)
|
||||||
@Valid UpdateRecordStatus bean,
|
throws ValidationException {
|
||||||
@PathParam("source") String source)
|
|
||||||
throws ValidationException{
|
|
||||||
|
|
||||||
|
|
||||||
Caller caller = AuthorizationProvider.instance.get();
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
String username = caller.getClient().getId();
|
String username = caller.getClient().getId();
|
||||||
String context = ScopeProvider.instance.get();
|
String context = ScopeProvider.instance.get();
|
||||||
|
|
||||||
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user " + username + " in context " + context);
|
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user "
|
||||||
|
+ username + " in context " + context);
|
||||||
|
|
||||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
ResponseCreationBean responseBean = new ResponseCreationBean();
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
|
|
||||||
try{
|
try {
|
||||||
|
|
||||||
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
if(catalogue == null){
|
if(catalogue == null) {
|
||||||
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
|
throw new Exception(
|
||||||
}else{
|
"There was a problem while serving your request. No catalogue instance was found in this context!");
|
||||||
|
} else {
|
||||||
|
|
||||||
// catalog id must be reported
|
// catalog id must be reported
|
||||||
String uuid = bean.getUuid();
|
String uuid = bean.getUuid();
|
||||||
|
@ -585,18 +549,20 @@ public class GrsfPublisherStockService {
|
||||||
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
|
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
|
||||||
|
|
||||||
// check system type
|
// check system type
|
||||||
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY).equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
|
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY)
|
||||||
|
.equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
|
||||||
|
|
||||||
if(!isGRSF)
|
if(!isGRSF)
|
||||||
throw new Exception("You are trying to modify a Legacy record!");
|
throw new Exception("You are trying to modify a Legacy record!");
|
||||||
|
|
||||||
boolean rightDomain = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY).equals(Product_Type.STOCK.getOrigName());
|
boolean rightDomain = record.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY)
|
||||||
|
.equals(Product_Type.STOCK.getOrigName());
|
||||||
|
|
||||||
if(!rightDomain)
|
if(!rightDomain)
|
||||||
throw new Exception("This is not a Stock record!");
|
throw new Exception("This is not a Stock record!");
|
||||||
|
|
||||||
// update it
|
// update it
|
||||||
Map<String, List<String>> updateStatus = new HashMap<String, List<String>>(1);
|
Map<String,List<String>> updateStatus = new HashMap<String,List<String>>(1);
|
||||||
updateStatus.put(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY, Arrays.asList(newStatus));
|
updateStatus.put(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY, Arrays.asList(newStatus));
|
||||||
catalogue.patchProductCustomFields(uuid, apiKeyUser, updateStatus, true);
|
catalogue.patchProductCustomFields(uuid, apiKeyUser, updateStatus, true);
|
||||||
status = Status.OK;
|
status = Status.OK;
|
||||||
|
@ -605,8 +571,8 @@ public class GrsfPublisherStockService {
|
||||||
responseBean.setId(record.getId());
|
responseBean.setId(record.getId());
|
||||||
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
|
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
|
||||||
}
|
}
|
||||||
}catch(Exception e){
|
} catch(Exception e) {
|
||||||
logger.error("Failed to update stock record's status", e);
|
logger.error("Failed to update stock record's status", e);
|
||||||
responseBean.setError(e.getMessage());
|
responseBean.setError(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue