diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java index b4fdf20..9814a84 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/CKANPublisherServicesImpl.java @@ -370,7 +370,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C datasetUrl, false, // send notification to other people toCreate.getTags(), - GenericUtils.getCurrentUser(getThreadLocalRequest()).getFullname() + GenericUtils.getCurrentUser(getThreadLocalRequest()).getFullname(), + GenericUtils.getCurrentClientUrl(getThreadLocalRequest()) ); threadWritePost.start(); diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/AssociationToGroupAndNotifyThread.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/AssociationToGroupAndNotifyThread.java index 997b933..9e110f8 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/AssociationToGroupAndNotifyThread.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/AssociationToGroupAndNotifyThread.java @@ -72,45 +72,6 @@ public class AssociationToGroupAndNotifyThread extends Thread { logger.info("Association thread started to put the dataset with id = "+ datasetId + " into group with title " + groupTitle + " for user " + username); - // if(groupTitle != null){ - // try{ - // - // // create the group - // CkanGroup group = catalogue.createGroup(groupTitle, groupTitle, ""); - // - // if(group == null){ - // - // logger.warn("The group doesn't exist! Unable to perform such association"); - // - // }else{ - // - // logger.debug("Group exists, going to add the user " + username + " as its admin..."); - // - // // retrieve the role to be assigned according the one the user has into the organization of the dataset - // RolesCkanGroupOrOrg role = RolesCkanGroupOrOrg.valueOf(catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).toUpperCase()); - // - // if(!role.equals(RolesCkanGroupOrOrg.ADMIN)) - // role = RolesCkanGroupOrOrg.MEMBER; // decrease the role to member if it is not an admin - // - // boolean assigned = catalogue.checkRoleIntoGroup(username, groupTitle, role); - // - // if(assigned){ - // - // logger.debug("Admin/editor role was assigned for this group, going to associate the product to the group"); - // boolean putIntoGroup = catalogue.assignDatasetToGroup(groupTitle, datasetId, catalogue.getApiKeyFromUsername(username)); - // logger.info("Was product put into group? " + putIntoGroup); - // - // if(putIntoGroup) - // notifyGroupAdmins(catalogue, group.getName(), group.getTitle(), username); - // - // } - // } - // - // }catch(Exception e){ - // logger.warn("Something went wrong when tried to add the group " + groupTitle, e); - // } - // } - // force creation of groups if needed if(groupsForceCreation != null){ logger.info("Groups that must be created before association are " + groupsForceCreation); diff --git a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/WritePostCatalogueManagerThread.java b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/WritePostCatalogueManagerThread.java index 5d6de4a..4dae2d8 100644 --- a/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/WritePostCatalogueManagerThread.java +++ b/src/main/java/org/gcube/portlets/widgets/ckandatapublisherwidget/server/threads/WritePostCatalogueManagerThread.java @@ -2,10 +2,13 @@ package org.gcube.portlets.widgets.ckandatapublisherwidget.server.threads; import java.io.IOException; import java.net.HttpURLConnection; +import java.util.Base64; import java.util.List; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.portal.GCubePortalConstants; import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.portal.databook.client.GCubeSocialNetworking; import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.GCoreEndPointReaderSocial; import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.GenericUtils; import org.json.simple.JSONObject; @@ -34,8 +37,8 @@ public class WritePostCatalogueManagerThread extends Thread { private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.ProductCatalogue"; private static final String NOTIFICATION_MESSAGE = "Dear members,
The item '$PRODUCT_TITLE' has been just published by $USER_FULLNAME.
You can find it here: $PRODUCT_URL
"; - 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 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"; private static final Log logger = LogFactoryUtil.getLog(WritePostCatalogueManagerThread.class); // private static Logger logger = LoggerFactory.getLogger(WritePostCatalogueManagerThread.class); @@ -46,6 +49,7 @@ public class WritePostCatalogueManagerThread extends Thread { private boolean enableNotification; private List hashtags; private String userFullName; + private String userCurrentUrl; /** * @param token @@ -59,7 +63,7 @@ public class WritePostCatalogueManagerThread extends Thread { public WritePostCatalogueManagerThread( String username, String scope, String productTitle, String productUrl, boolean enableNotification, - List hashtags, String userFullName) { + List hashtags, String userFullName, String userCurrentUrl) { super(); this.username = username; this.scope = scope; @@ -68,6 +72,7 @@ public class WritePostCatalogueManagerThread extends Thread { this.enableNotification = enableNotification; this.hashtags = hashtags; this.userFullName = userFullName; + this.userCurrentUrl = userCurrentUrl; } @Override @@ -90,6 +95,13 @@ public class WritePostCatalogueManagerThread extends Thread { ScopeProvider.instance.set(scope); SecurityTokenProvider.instance.set(token); + final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL + extractOrgFriendlyURL(userCurrentUrl) + GCubePortalConstants.USER_PROFILE_FRIENDLY_URL; + + userFullName = ""+userFullName+ + " "; + // write writeProductPost( productTitle, @@ -107,6 +119,21 @@ public class WritePostCatalogueManagerThread extends Thread { } } + public static String extractOrgFriendlyURL(String portalURL) { + String groupRegEx = "/group/"; + if (portalURL.contains(groupRegEx)) { + String[] splits = portalURL.split(groupRegEx); + String friendlyURL = splits[1]; + if (friendlyURL.contains("/")) { + friendlyURL = friendlyURL.split("/")[0]; + } else { + friendlyURL = friendlyURL.split("\\?")[0].split("\\#")[0]; + } + return "/"+friendlyURL; + } + return null; + } + /** * Send notification to vre members about the created product by writing a post. * @param productName the title of the product @@ -158,7 +185,9 @@ public class WritePostCatalogueManagerThread extends Thread { String token = null; try{ - HttpResponse response = performRequest(client, path, "{\"app_id\":\"" + APPLICATION_ID_CATALOGUE_MANAGER + "\"}"); + JSONObject request = new JSONObject(); + request.put("app_id", APPLICATION_ID_CATALOGUE_MANAGER); + HttpResponse response = performRequest(client, path, request.toJSONString()); int statusTokenGenerate = response.getStatusLine().getStatusCode(); @@ -218,9 +247,11 @@ public class WritePostCatalogueManagerThread extends Thread { message += " #" + modifiedHashtag; // ckan accepts tag with empty spaces, we don't } - logger.info("The post that is going to be written is -> " + message); - - HttpResponse response = performRequest(client, path, "{\"text\":\"" + message + "\", \"enable_notification\" : "+ enablePostNotification+ "}"); + JSONObject request = new JSONObject(); + request.put("text", message); + request.put("enable_notification", enablePostNotification); + logger.info("The post that is going to be written is ->\n" + request.toJSONString()); + HttpResponse response = performRequest(client, path, request.toJSONString()); int statusWritePost = response.getStatusLine().getStatusCode(); if(statusWritePost == HttpURLConnection.HTTP_CREATED){ @@ -244,14 +275,14 @@ public class WritePostCatalogueManagerThread extends Thread { writePost(client, realLocation, productName, productUrl, userFullname, hashtags, enablePostNotification); }else - throw new RuntimeException("Failed to write the post"); + throw new RuntimeException("Failed to write the post "); }catch(Exception e){ - logger.error("Failed to retrieve application token", e); + logger.error("Failed to write the post ", e); } } - + /** * Convert the json response to a map * @param response