From 632e085e9e5aee94071971e67a7273947453b550 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Thu, 21 Mar 2019 16:48:54 +0000 Subject: [PATCH] Updated pom version at 1.4.0 Added Feature #16312 Fixed requirement "WARNING: The record is locked due to a merge request in progress." git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/grsf-manage-widget@178652 82a268e6-3cf1-43bd-a215-b396298e98cf --- distro/changelog.xml | 36 ++-- pom.xml | 2 +- .../client/view/ManageProductWidget.java | 5 +- .../manage/GRSFNotificationService.java | 3 +- .../server/manage/SocialCommunications.java | 5 +- ...SFRecordAlreadyManagedStatusException.java | 42 +++++ .../client/SendNotification.java | 160 ++++++++++++++++++ 7 files changed, 239 insertions(+), 14 deletions(-) create mode 100644 src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ex/GRSFRecordAlreadyManagedStatusException.java create mode 100644 src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/SendNotification.java diff --git a/distro/changelog.xml b/distro/changelog.xml index b449c08..ef606c1 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,34 +1,50 @@ - - [Feature #12255] Pop-up Text improvement for the locked records + + [Feature #16312] Report the name of the editor as tag in the + published feed - + [Feature #12255] Pop-up Text improvement for the locked + records + + + [Task #11124] Testing the GRSF management panel - Minor fixes - Some management improvements - Minor changes to reflect the new model of grsf records - Minor improvements - Minor improvements - First Release diff --git a/pom.xml b/pom.xml index 9f3f5db..211af1e 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portlets.widgets grsf-manage-widget - 1.3.3-SNAPSHOT + 1.4.0-SNAPSHOT gCube GRSF Manage widget diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java index e085774..fafac26 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/client/view/ManageProductWidget.java @@ -26,6 +26,7 @@ import org.gcube.datacatalogue.grsf_manage_widget.shared.ManageProductBean; import org.gcube.datacatalogue.grsf_manage_widget.shared.RevertableOperationInfo; import org.gcube.datacatalogue.grsf_manage_widget.shared.SimilarGRSFRecord; import org.gcube.datacatalogue.grsf_manage_widget.shared.SourceRecord; +import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.GRSFRecordAlreadyManagedStatusException; import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.NoGRSFRecordException; import com.github.gwtbootstrap.client.ui.AlertBlock; @@ -489,7 +490,9 @@ public class ManageProductWidget extends Composite{ if(caught instanceof NoGRSFRecordException) showInfo(NO_GRSF_RECORD_BEAN, AlertType.WARNING); - else + else if(caught instanceof GRSFRecordAlreadyManagedStatusException) { + showInfo("WARNING: "+caught.getMessage(), AlertType.WARNING); + }else showInfo("Error is " + caught.getMessage(), AlertType.ERROR); // hide the form and disable the send button diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java index 0523200..0d9c572 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/GRSFNotificationService.java @@ -25,6 +25,7 @@ import org.gcube.datacatalogue.grsf_manage_widget.shared.RevertableOperationInfo import org.gcube.datacatalogue.grsf_manage_widget.shared.RevertableOperations; import org.gcube.datacatalogue.grsf_manage_widget.shared.SimilarGRSFRecord; import org.gcube.datacatalogue.grsf_manage_widget.shared.SourceRecord; +import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.GRSFRecordAlreadyManagedStatusException; import org.gcube.datacatalogue.grsf_manage_widget.shared.ex.NoGRSFRecordException; import org.gcube.vomanagement.usermanagement.RoleManager; import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; @@ -221,7 +222,7 @@ public class GRSFNotificationService extends RemoteServiceServlet implements GRS Status status = Status.fromString(extrasWithoutNamespaces.get(Constants.STATUS_OF_THE_GRSF_RECORD_CUSTOM_KEY).get(0)); if(status.equals(Status.To_be_Merged) && !requestForRevertingMerge) - throw new Exception("The record is locked due to a merge request in progress!"); + throw new GRSFRecordAlreadyManagedStatusException(Status.To_be_Merged, "The record is locked due to a merge request in progress!"); String uuidKB = extrasWithoutNamespaces.get(Constants.UUID_KB_CUSTOM_KEY).get(0); String grsfDomain = extrasWithoutNamespaces.get(Constants.DOMAIN_CUSTOM_KEY).get(0); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/SocialCommunications.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/SocialCommunications.java index 2b0ae29..a4396a0 100644 --- a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/SocialCommunications.java +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/server/manage/SocialCommunications.java @@ -332,6 +332,9 @@ public class SocialCommunications { for (String hashtag : hashtags) { message += " #" + hashtag; } + //Added by Francesco Mangiacrapa see at Feature #16312 + String normalizedFullName = fullName.trim().replace(" ", "_"); + message += " #" + normalizedFullName; } logger.info("The post that is going to be written is -> " + message); @@ -640,7 +643,7 @@ public class SocialCommunications { * @return */ @SuppressWarnings("unchecked") - private static Map getResponseEntityAsJSON(HttpResponse response){ + public static Map getResponseEntityAsJSON(HttpResponse response){ Map toReturn = null; HttpEntity entity = response.getEntity(); diff --git a/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ex/GRSFRecordAlreadyManagedStatusException.java b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ex/GRSFRecordAlreadyManagedStatusException.java new file mode 100644 index 0000000..d281607 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/grsf_manage_widget/shared/ex/GRSFRecordAlreadyManagedStatusException.java @@ -0,0 +1,42 @@ +package org.gcube.datacatalogue.grsf_manage_widget.shared.ex; + +import org.gcube.datacatalogue.common.enums.Status; + + +/** + * The Class GRSFRecordAlreadyManagedStatusException. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * + * Mar 21, 2019 + */ +public class GRSFRecordAlreadyManagedStatusException extends Exception{ + + /** + * + */ + private static final long serialVersionUID = -2034802685606929315L; + private Status status; + + /** + * Instantiates a new GRSF record already managed status exception. + * + * @param theStatus the the status + * @param arg0 the arg 0 + */ + public GRSFRecordAlreadyManagedStatusException(Status theStatus, String arg0){ + super(arg0); + this.status = theStatus; + + } + + /** + * Gets the status. + * + * @return the status + */ + public Status getStatus() { + return status; + } + +} diff --git a/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/SendNotification.java b/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/SendNotification.java new file mode 100644 index 0000000..1b26d66 --- /dev/null +++ b/src/test/java/org/gcube/datacatalogue/grsf_manage_widget/client/SendNotification.java @@ -0,0 +1,160 @@ +package org.gcube.datacatalogue.grsf_manage_widget.client; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; +import static org.gcube.resources.discovery.icclient.ICFactory.queryFor; + +import java.util.List; +import java.util.Map; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; +import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory; +import org.gcube.datacatalogue.grsf_manage_widget.server.manage.GRSFNotificationService; +import org.gcube.datacatalogue.grsf_manage_widget.server.manage.SocialCommunications; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.CloseableHttpResponse; +import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpPost; +import eu.trentorise.opendata.jackan.internal.org.apache.http.entity.StringEntity; +import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; +import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; +import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.LaxRedirectStrategy; + +public class SendNotification { + + private static String context = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"; + + private static String token = "57b42a99-6239-44c4-9a68-591c18363222-843339462"; ///d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab + + //private static String token = "8e74a17c-92f1-405a-b591-3a6090066248-98187548"; //devVRE + + private static String username = "francesco.mangiacrapa"; + + private static final String MEDIATYPE_JSON = "application/json"; + + private static final Logger logger = LoggerFactory.getLogger(SendNotification.class); + + // for discovering social networking service + private static final String resource = "jersey-servlet"; + private static final String serviceName = "SocialNetworking"; + private static final String serviceClass = "Portal"; + + private static final String SOCIAL_SEND_EMAIL = "2/messages/write-message"; + + + public static void main(String[] args){ + + try { + ScopeProvider.instance.set(context); + SecurityTokenProvider.instance.set(token); + GRSFNotificationService grsfNotificationService = new GRSFNotificationService(); + + DataCatalogue catalogue = DataCatalogueFactory.getFactory().getUtilsPerScope(context); + logger.info("The Catalogue in the scope {} is {}", context, catalogue.getCatalogueUrl()); + + // require social networking url + final String baseUrlSocial = getBaseUrlSocialService(); + + logger.info("Base URL SOCIAL IS {}", baseUrlSocial); + + // and the user current browser url + final String currentBrowserUrl = "http://127.0.0.1"; // ignore other parameters + + // manage interactions through a separated thread but set there security token and context (and then reset them) + Thread t = new Thread(new Runnable() { + + + + @Override + public void run() { + try{ + JSONObject message = new JSONObject(); + message.put("hello", "world"); + sendEmail(baseUrlSocial, ""); + + // create a post about the operation + //SocialCommunications.writeProductPost(baseUrlSocial, bean, username, fullName, false, currentBrowserUrl); + + }catch(Exception e){ + logger.error("Something failed while alerting editors/reviewers", e); + }finally{ + ScopeProvider.instance.reset(); + SecurityTokenProvider.instance.reset(); + } + } + }); + t.start(); + }catch (Exception e) { + System.err.println("Error: "); + e.printStackTrace(); + } + + } + + private static String getBaseUrlSocialService() throws Exception{ + SimpleQuery query = queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass)); + query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); + query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName)); + query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+resource+"\"]/text()"); + + DiscoveryClient client = client(); + List endpoints = client.submit(query); + if (endpoints == null || endpoints.isEmpty()) + throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); + + + String basePath = endpoints.get(0); + if(basePath==null) + throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); + + return basePath; + } + + + private static void sendEmail(String basePath, String messageToEditor) { + + try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){ + String subject = "TEST: just a test of email notification"; + + // send email to the editor + logger.info("The message that is going to be send to the editor is\n" + messageToEditor); + String postRequestURL = basePath + SOCIAL_SEND_EMAIL + "?gcube-token=" + token; + logger.info("The post URL is {}",postRequestURL); + HttpPost postRequest = new HttpPost(postRequestURL); + JSONObject reqMessage = new JSONObject(); + reqMessage.put("subject", subject); + reqMessage.put("body", messageToEditor); + JSONArray recipients = new JSONArray(); + JSONObject recipient = new JSONObject(); + recipient.put("id", username); + recipients.add(recipient); + reqMessage.put("recipients", recipients); + StringEntity input = new StringEntity(reqMessage.toJSONString()); + input.setContentType(MEDIATYPE_JSON); + postRequest.setEntity(input); + + logger.debug("Whole editor message is going to be " + reqMessage.toJSONString()); + + CloseableHttpResponse response = client.execute(postRequest); + + logger.info("The response code is {} ",response.getStatusLine().getStatusCode()); + + Map mapResponseWritePost = SocialCommunications.getResponseEntityAsJSON(response); + + if (response.getStatusLine().getStatusCode() != 201){ + logger.error("Failed to send message to editor : HTTP error code : " + + response.getStatusLine().getStatusCode() + mapResponseWritePost.get("message")); + } + }catch(Exception e){ + logger.error("Failed to send messages", e); + } + } +}