Improved the Social Post: removed the <br> char.

This commit is contained in:
Francesco Mangiacrapa 2022-06-24 11:02:53 +02:00
parent 12c1a42138
commit 8d9e63b560
2 changed files with 176 additions and 143 deletions

View File

@ -673,7 +673,7 @@ public class ManageProductWidget extends Composite{
for(SimilarGRSFRecord sR: bean.getSimilarGrsfRecords()){
if(sR.isSuggestedMerge()){
bean.setMergesInvolved(true);
report += "\n\t - merge the current record with record '" + sR.getTitle() + " ;";
report += "\n\t - merge the current record with record '" + sR.getTitle() + "' ;";
report += "\n\t \t- GRSF Name '" + sR.getTitle() + "' ;";
report += "\n\t \t- Short Name '" + sR.getShortName() + "' ;";
report += "\n\t \t- URL '" + sR.getUrl() + "' ;";

View File

@ -41,9 +41,10 @@ import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* For managing the different interactions with social channels (posts and mails)
* For managing the different interactions with social channels (posts and
* mails)
*
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class SocialCommunications {
@ -66,7 +67,8 @@ public class SocialCommunications {
// for writing a post in the GRSF admin context
private static final String APPLICATION_ID_CATALOGUE_MANAGER = "org.gcube.datacatalogue.GRSFNotifier";
// emails to be sent to editors and reviewers and post to be written into the grsf admin vre
// emails to be sent to editors and reviewers and post to be written into the
// grsf admin vre
private static final String POST_MESSAGE = "Dear members,"
+ "\nThe record 'PRODUCT_TITLE' has been just updated by USER_FULLNAME."
+ "\nYou can inspect it here: LINK_RECORD.";
@ -117,17 +119,20 @@ public class SocialCommunications {
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()");
query.setResult(
"$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""
+ resource + "\"]/text()");
DiscoveryClient<String> client = client();
List<String> endpoints = client.submit(query);
if (endpoints == null || endpoints.isEmpty())
throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context);
throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: " + serviceName
+ ", serviceClass: " + serviceClass + ", in scope: " + context);
basePath = endpoints.get(0);
if (basePath == null)
throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context);
throw new Exception("Endpoint:" + resource + ", is null for serviceName: " + serviceName
+ ", serviceClass: " + serviceClass + ", in scope: " + context);
httpServletRequest.getSession().setAttribute(keyPerContext, basePath);
@ -141,6 +146,7 @@ public class SocialCommunications {
/**
* Require a proper application token for writing a post and send messages.
*
* @return
* @throws Exception
*/
@ -150,7 +156,8 @@ public class SocialCommunications {
String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for requireApplicationToken is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
logger.info("Current scope for requireApplicationToken is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
String basePath = serviceUrl;
if (basePath == null) {
@ -162,10 +169,12 @@ public class SocialCommunications {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();) {
// ask token application
HttpPost postRequest = new HttpPost(basePath + SOCIAL_SERVICE_APPLICATION_TOKEN + "?gcube-token=" + tokenUser);
HttpPost postRequest = new HttpPost(
basePath + SOCIAL_SERVICE_APPLICATION_TOKEN + "?gcube-token=" + tokenUser);
JSONObject requestToken = new JSONObject();
requestToken.put("app_id", APPLICATION_ID_CATALOGUE_MANAGER);
StringEntity input = new StringEntity(requestToken.toJSONString());
@ -201,6 +210,7 @@ public class SocialCommunications {
/**
* Notify the users about the required changes.
*
* @param bean
* @param url
* @param username
@ -210,13 +220,15 @@ public class SocialCommunications {
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void writePostOnRevert(String serviceUrl, RevertableOperationInfo rInfo, boolean enablePostNotification, String userCurrentUrl) throws Exception{
public static void writePostOnRevert(String serviceUrl, RevertableOperationInfo rInfo,
boolean enablePostNotification, String userCurrentUrl) throws Exception {
// discover service endpoint for the social networking library
String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writePostOnRevert is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
logger.info("Current scope for writePostOnRevert is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
String basePath = serviceUrl;
if (basePath == null) {
@ -228,36 +240,46 @@ public class SocialCommunications {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();) {
// require url
String applicationToken = requireApplicationToken(serviceUrl);
// see Feature #17576 updated by Francesco
/*final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL + extractOrgFriendlyURL(userCurrentUrl) + GCubePortalConstants.USER_PROFILE_FRIENDLY_URL;
String userFullNameHighlightedCurrent = "<a class=\"link\" href=\"" + profilePageURL + "?"+
Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
Base64.getEncoder().encodeToString(rInfo.getUserNameCurrentAdmin().getBytes())+"\">"+rInfo.getFullNameCurrentAdmin()+
"</a> ";
/*
* final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL +
* extractOrgFriendlyURL(userCurrentUrl) +
* GCubePortalConstants.USER_PROFILE_FRIENDLY_URL; String
* userFullNameHighlightedCurrent = "<a class=\"link\" href=\"" + profilePageURL
* + "?"+ Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
* Base64.getEncoder().encodeToString(rInfo.getUserNameCurrentAdmin().getBytes()
* )+"\">"+rInfo.getFullNameCurrentAdmin()+ "</a> ";
*/
String userFullNameHighlightedCurrent = "@" + rInfo.getUserNameCurrentAdmin();
/*String userFullNameHighlightedOriginal = "<a class=\"link\" href=\"" + profilePageURL + "?"+
Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
Base64.getEncoder().encodeToString(rInfo.getUserNameOriginalAdmin().getBytes())+"\">"+rInfo.getFullNameOriginalAdmin()+
"</a> ";*/
/*
* String userFullNameHighlightedOriginal = "<a class=\"link\" href=\"" +
* profilePageURL + "?"+
* Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
* Base64.getEncoder().encodeToString(rInfo.getUserNameOriginalAdmin().getBytes(
* ))+"\">"+rInfo.getFullNameOriginalAdmin()+ "</a> ";
*/
String userFullNameHighlightedOriginal = "@" + rInfo.getUserNameOriginalAdmin();
// replace
String message = POST_ON_REVERT.replace("RECORD_URL", rInfo.getRecordUrl()).replace("ADMIN_WHO_CHANGED", userFullNameHighlightedCurrent).replace("ORIGINAL_USER",userFullNameHighlightedOriginal);
String message = POST_ON_REVERT.replace("RECORD_URL", rInfo.getRecordUrl())
.replace("ADMIN_WHO_CHANGED", userFullNameHighlightedCurrent)
.replace("ORIGINAL_USER", userFullNameHighlightedOriginal);
// add hashtag
message += "\n\n";
message += " #" + HashTagsOnUpdate.REVERTED_MERGE.getString();
logger.info("The post that is going to be written is -> " + message);
HttpPost postRequest = new HttpPost(basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token=" + applicationToken);
HttpPost postRequest = new HttpPost(
basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token=" + applicationToken);
JSONObject object = new JSONObject();
object.put("text", message);
object.put("enable_notification", enablePostNotification);
@ -280,6 +302,7 @@ public class SocialCommunications {
/**
* Notify the users about the required changes.
*
* @param bean
* @param url
* @param username
@ -289,13 +312,15 @@ public class SocialCommunications {
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static void writeProductPost(String serviceUrl, ManageProductBean bean, String username, String fullName, boolean enablePostNotification, String userCurrentUrl) throws Exception{
public static void writeProductPost(String serviceUrl, ManageProductBean bean, String username, String fullName,
boolean enablePostNotification, String userCurrentUrl) throws Exception {
// discover service endpoint for the social networking library
String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
logger.info("Current scope for writeProductPost is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
String basePath = serviceUrl;
if (basePath == null) {
@ -307,27 +332,31 @@ public class SocialCommunications {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();) {
// require url
String applicationToken = requireApplicationToken(serviceUrl);
// see Feature #17576 Updated by Francesco
/*final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL + extractOrgFriendlyURL(userCurrentUrl) + GCubePortalConstants.USER_PROFILE_FRIENDLY_URL;
String userFullNameHighlighted = "<a class=\"link\" href=\"" + profilePageURL + "?"+
Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
Base64.getEncoder().encodeToString(username.getBytes())+"\">"+fullName+
"</a> ";
/*
* final String profilePageURL = GCubePortalConstants.PREFIX_GROUP_URL +
* extractOrgFriendlyURL(userCurrentUrl) +
* GCubePortalConstants.USER_PROFILE_FRIENDLY_URL; String
* userFullNameHighlighted = "<a class=\"link\" href=\"" + profilePageURL + "?"+
* Base64.getEncoder().encodeToString(USER_PROFILE_OID.getBytes())+"="+
* Base64.getEncoder().encodeToString(username.getBytes())+"\">"+fullName+
* "</a> ";
*/
String userFullNameHighlighted = "@" + username;
// replace
String message = POST_MESSAGE.replace("PRODUCT_TITLE", bean.getTitle()).replace("LINK_RECORD", bean.getUrl()).
replace("USER_FULLNAME", userFullNameHighlighted);
String message = POST_MESSAGE.replace("PRODUCT_TITLE", bean.getTitle())
.replace("LINK_RECORD", bean.getUrl()).replace("USER_FULLNAME", userFullNameHighlighted);
if (bean.getReport() != null && !bean.getReport().isEmpty())
message += ADD_REPORT.replace("REPORT_UPDATE", bean.getReport());
message += ADD_REPORT.replace("<br>", "").replace("REPORT_UPDATE", bean.getReport());
Set<String> hashtags = bean.getHashtags();
logger.debug("Hashtags are " + hashtags);
@ -342,7 +371,8 @@ public class SocialCommunications {
}
logger.info("The post that is going to be written is -> " + message);
HttpPost postRequest = new HttpPost(basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token=" + applicationToken);
HttpPost postRequest = new HttpPost(
basePath + SOCIAL_SERVICE_WRITE_APPLICATION_POST + "?gcube-token=" + applicationToken);
JSONObject object = new JSONObject();
object.put("text", message);
object.put("enable_notification", enablePostNotification);
@ -379,6 +409,7 @@ public class SocialCommunications {
/**
* Send an email to the administrator as well as the
*
* @param bean
* @param catalogue
* @param username
@ -388,15 +419,9 @@ public class SocialCommunications {
* @throws Exceptio
*/
@SuppressWarnings("unchecked")
public static void sendEmailAdministrators(
String serviceUrl,
ManageProductBean bean,
DataCatalogue catalogue,
String username,
String fullName,
long groupId,
String clientCurrenturl,
boolean isMergeInvolved) throws Exception {
public static void sendEmailAdministrators(String serviceUrl, ManageProductBean bean, DataCatalogue catalogue,
String username, String fullName, long groupId, String clientCurrenturl, boolean isMergeInvolved)
throws Exception {
// get the list of GRSF Reviewers to alert them as well
RoleManager roleManager = new LiferayRoleManager();
@ -421,7 +446,8 @@ public class SocialCommunications {
String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
logger.info("Current scope for writeProductPost is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
String basePath = serviceUrl;
if (basePath == null) {
@ -433,22 +459,23 @@ public class SocialCommunications {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();) {
/// require url
String applicationToken = requireApplicationToken(serviceUrl);
String revertUrl = "";
if (isMergeInvolved)
revertUrl = getEncodedUrlManage(operation, username, System.currentTimeMillis(), bean.getKnowledgeBaseId(), clientCurrenturl);
String messageToEditor = (EMAIL_MESSAGE_EDITOR +
(isMergeInvolved? REVERT_LINK_PIECE : "")).replace("USER_FULLNAME", fullName).replace("PRODUCT_TITLE", bean.getTitle()).
replace("LINK_RECORD", bean.getUrl()).replace("LINK", revertUrl);
String messageToReviewer = (EMAIL_MESSAGE_REVIEWER+
(isMergeInvolved? REVERT_LINK_PIECE : "")).replace("USER_FULLNAME", fullName).replace("PRODUCT_TITLE", bean.getTitle()).
replace("LINK_RECORD", bean.getUrl()).replace("LINK", revertUrl);
revertUrl = getEncodedUrlManage(operation, username, System.currentTimeMillis(),
bean.getKnowledgeBaseId(), clientCurrenturl);
String messageToEditor = (EMAIL_MESSAGE_EDITOR + (isMergeInvolved ? REVERT_LINK_PIECE : ""))
.replace("USER_FULLNAME", fullName).replace("PRODUCT_TITLE", bean.getTitle())
.replace("LINK_RECORD", bean.getUrl()).replace("LINK", revertUrl);
String messageToReviewer = (EMAIL_MESSAGE_REVIEWER + (isMergeInvolved ? REVERT_LINK_PIECE : ""))
.replace("USER_FULLNAME", fullName).replace("PRODUCT_TITLE", bean.getTitle())
.replace("LINK_RECORD", bean.getUrl()).replace("LINK", revertUrl);
String subject = "Update request on GRSF Record";
@ -522,11 +549,8 @@ public class SocialCommunications {
}
@SuppressWarnings("unchecked")
public static void sendEmailAdministratorsOnOperationReverted(
String serviceUrl,
RevertableOperationInfo rInfo,
long groupId
) throws Exception {
public static void sendEmailAdministratorsOnOperationReverted(String serviceUrl, RevertableOperationInfo rInfo,
long groupId) throws Exception {
// get the list of GRSF Reviewers to alert them as well
RoleManager roleManager = new LiferayRoleManager();
@ -548,7 +572,8 @@ public class SocialCommunications {
String currentScope = ScopeProvider.instance.get();
String tokenUser = SecurityTokenProvider.instance.get();
logger.info("Current scope for writeProductPost is " + currentScope + " and token is " + tokenUser.substring(0, 10) + "***************");
logger.info("Current scope for writeProductPost is " + currentScope + " and token is "
+ tokenUser.substring(0, 10) + "***************");
String basePath = serviceUrl;
if (basePath == null) {
@ -560,14 +585,18 @@ public class SocialCommunications {
basePath = basePath.endsWith("/") ? basePath : basePath + "/";
try(CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();){
try (CloseableHttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
.build();) {
String applicationToken = requireApplicationToken(serviceUrl);
String messageToEditor = EMAIL_EDITOR_REVERT.replace("RECORD_URL", rInfo.getRecordUrl()).replace("ORIGINAL_USER", rInfo.getFullNameOriginalAdmin()).
replace("ADMIN_WHO_CHANGED", rInfo.getFullNameCurrentAdmin());
String messageToReviewer = EMAIL_REVIEWER_REVERT.replace("ADMIN_WHO_CHANGED", rInfo.getFullNameCurrentAdmin()).replace("RECORD_URL", rInfo.getRecordUrl()).
replace("ORIGINAL_USER", rInfo.getFullNameOriginalAdmin());
String messageToEditor = EMAIL_EDITOR_REVERT.replace("RECORD_URL", rInfo.getRecordUrl())
.replace("ORIGINAL_USER", rInfo.getFullNameOriginalAdmin())
.replace("ADMIN_WHO_CHANGED", rInfo.getFullNameCurrentAdmin());
String messageToReviewer = EMAIL_REVIEWER_REVERT
.replace("ADMIN_WHO_CHANGED", rInfo.getFullNameCurrentAdmin())
.replace("RECORD_URL", rInfo.getRecordUrl())
.replace("ORIGINAL_USER", rInfo.getFullNameOriginalAdmin());
String subject = "Revert merge request on GRSF Record";
messageToEditor = messageToEditor.replace("<br>", "\n");
@ -630,19 +659,23 @@ public class SocialCommunications {
/**
* Create the url to be send for reverting the operation
*
* @param httpSession
* @return
* @throws Exception
*/
public static String getEncodedUrlManage(RevertableOperations operation, String administrator, long timestamp, String uuid, String clientCurrenturl) throws Exception{
public static String getEncodedUrlManage(RevertableOperations operation, String administrator, long timestamp,
String uuid, String clientCurrenturl) throws Exception {
logger.info("Request for revert link. Client current url is " + clientCurrenturl);
RevertOperationUrl operationUrl = new RevertOperationUrl(clientCurrenturl, administrator, timestamp, uuid, operation);
RevertOperationUrl operationUrl = new RevertOperationUrl(clientCurrenturl, administrator, timestamp, uuid,
operation);
String shortUrl = operationUrl.getShortUrl();
return shortUrl;
}
/**
* Convert the json response to a map
*
* @param response
* @return
*/