minor fixes and added method to update only the status of a record

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@163067 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2018-02-08 15:58:11 +00:00
parent cc71c9cd3c
commit 71a78c9e54
6 changed files with 204 additions and 3 deletions

View File

@ -6,6 +6,9 @@
<dependent-module archiveName="grsf-common-library-1.0.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/grsf-common-library/grsf-common-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<dependent-module archiveName="ckan-util-library-2.4.1-SNAPSHOT.jar" deploy-path="/WEB-INF/lib" handle="module:/resource/ckan-util-library/ckan-util-library">
<dependency-type>uses</dependency-type>
</dependent-module>
<property name="context-root" value="grsf-publisher-ws"/>
<property name="java-output-path" value="/grsf-publisher-ws/target/classes"/>
</wb-module>

View File

@ -0,0 +1,58 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input.others;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.gcube.datacatalogue.common.Constants;
import org.gcube.datacatalogue.common.enums.Status;
import com.fasterxml.jackson.annotation.JsonProperty;
public class UpdateRecordStatus {
@JsonProperty(Constants.KB_ID)
@NotNull(message= Constants.KB_ID + " cannot be null")
@Size(min=1, message= Constants.KB_ID + " cannot be empty")
private String uuid;
@JsonProperty(Constants.NEW_STATUS)
private Status newStatus;
public UpdateRecordStatus() {
super();
}
/**
* @param uuid
* @param newStatus
*/
public UpdateRecordStatus(String uuid, Status newStatus) {
super();
this.uuid = uuid;
this.newStatus = newStatus;
}
public Status getNewStatus() {
return newStatus;
}
public void setNewStatus(Status newStatus) {
this.newStatus = newStatus;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
@Override
public String toString() {
return "UpdateRecordStatus [uuid=" + uuid + ", newStatus=" + newStatus
+ "]";
}
}

View File

@ -30,6 +30,7 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.DeleteRecord;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.UpdateRecordStatus;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.record.FisheryRecord;
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean;
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean;
@ -546,4 +547,73 @@ public class GrsfPublisherFisheryService {
}
return Response.status(status).entity(responseBean).build();
}
@POST
@Path("update-status")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStatusStock(
@Valid UpdateRecordStatus bean,
@PathParam("source") String source)
throws ValidationException{
Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId();
String context = ScopeProvider.instance.get();
logger.info("Incoming request for updating a status of record = " + bean + ". Request comes from user " + username + " in context " + context);
ResponseCreationBean responseBean = new ResponseCreationBean();
Status status = Status.INTERNAL_SERVER_ERROR;
try{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
}else{
// catalog id must be reported
String uuid = bean.getUuid();
String newStatus = bean.getNewStatus().getOrigName();
// get the dataset
String apiKeyUser = catalogue.getApiKeyFromUsername(username);
CkanDataset record = catalogue.getDataset(uuid, apiKeyUser);
if(record == null)
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
// check system type
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY).equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
if(!isGRSF)
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());
if(!rightDomain)
throw new Exception("This is not a Fishery record!");
// update it
Map<String, List<String>> updateStatus = new HashMap<String, List<String>>(1);
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));
}
}catch(Exception e){
logger.error("Failed to update fishery record's status", e);
responseBean.setError(e.getMessage());
}
return Response.status(status).entity(responseBean).build();
}
}

View File

@ -30,6 +30,7 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.DeleteRecord;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.others.UpdateRecordStatus;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.record.StockRecord;
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean;
import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean;
@ -425,7 +426,7 @@ public class GrsfPublisherStockService {
CkanDataset recordPublished = catalogue.getDataset(catalogId, apiKey);
if(recordPublished == null)
throw new Exception("A record with catalogue id " + catalogId + " does not exist!");
throw new Exception("A record with id " + catalogId + " does not exist!");
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
@ -544,4 +545,73 @@ public class GrsfPublisherStockService {
return Response.status(status).entity(responseBean).build();
}
@POST
@Path("update-status")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateStatusStock(
@Valid UpdateRecordStatus bean,
@PathParam("source") String source)
throws ValidationException{
Caller caller = AuthorizationProvider.instance.get();
String username = caller.getClient().getId();
String context = ScopeProvider.instance.get();
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;
ResponseCreationBean responseBean = new ResponseCreationBean();
try{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
throw new Exception("There was a problem while serving your request. No catalogue instance was found in this context!");
}else{
// catalog id must be reported
String uuid = bean.getUuid();
String newStatus = bean.getNewStatus().getOrigName();
// get the dataset
String apiKeyUser = catalogue.getApiKeyFromUsername(username);
CkanDataset record = catalogue.getDataset(uuid, apiKeyUser);
if(record == null)
throw new Exception("A record with knowledge_base_id id " + uuid + " does not exist!");
// check system type
boolean isGRSF = !record.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY).equals(Constants.SYSTEM_TYPE_FOR_SOURCES_VALUE);
if(!isGRSF)
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());
if(!rightDomain)
throw new Exception("This is not a Stock record!");
// update it
Map<String, List<String>> updateStatus = new HashMap<String, List<String>>(1);
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));
}
}catch(Exception e){
logger.error("Failed to update stock record's status", e);
responseBean.setError(e.getMessage());
}
return Response.status(status).entity(responseBean).build();
}
}

View File

@ -552,7 +552,7 @@ public class CommonServiceUtils {
Map<String, List<String>> addField = new HashMap<String, List<String>>();
String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD) ? namespaces.get(Constants.ITEM_URL_FIELD) : Constants.ITEM_URL_FIELD;
addField.put(modifiedUUIDKey, Arrays.asList(itemUrl));
catalogue.patchProductCustomFields(datasetId, apiKey, addField);
catalogue.patchProductCustomFields(datasetId, apiKey, addField, false);
}
// update description anyway

View File

@ -66,7 +66,7 @@ public abstract class HelperMethods {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HelperMethods.class);
private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.ProductCatalogue";
private static final String NOTIFICATION_MESSAGE = "Dear members,<br>The item 'PRODUCT_TITLE' has been just published by USER_FULLNAME.<br>You can find it here: PRODUCT_URL <br>";
private static final String NOTIFICATION_MESSAGE = "Dear members,<br>The record 'PRODUCT_TITLE' has been just published by USER_FULLNAME.<br>You can find it here: PRODUCT_URL <br>";
private static final String SOCIAL_SERVICE_APPLICATION_TOKEN = "/2/tokens/generate-application-token/";
private static final String SOCIAL_SERVICE_WRITE_APPLICATION_POST = "/2/posts/write-post-app/";
private static final String MEDIATYPE_JSON = "application/json";