Implementing new Solution refs #13207

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-util-library@176749 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-23 14:51:28 +00:00
parent 6ad6e357e4
commit 26b718bc09
5 changed files with 59 additions and 6 deletions

View File

@ -5,13 +5,23 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.gcube.common.portal.GCubePortalConstants;
import org.gcube.portal.databook.client.GCubeSocialNetworking;
import org.gcube.portlets.widgets.pickitem.shared.ItemBean;
import org.gcube.socialnetworking.token.ReplaceableToken; import org.gcube.socialnetworking.token.ReplaceableToken;
import org.gcube.socialnetworking.token.TagToken; import org.gcube.socialnetworking.token.TagToken;
import org.gcube.socialnetworking.token.Token; import org.gcube.socialnetworking.token.Token;
import org.gcube.socialnetworking.token.URLToken; import org.gcube.socialnetworking.token.URLToken;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
import org.gcube.vomanagement.usermanagement.model.GCubeTeam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MessageParser { public class MessageParser {
private static final Logger logger = LoggerFactory.getLogger(MessageParser.class);
private final String originalMessage; private final String originalMessage;
private StringWriter stringWriter; private StringWriter stringWriter;
@ -58,4 +68,38 @@ public class MessageParser {
return stringWriter.toString(); return stringWriter.toString();
} }
public String getParsedMessage(List<ItemBean> taggedPeople, String siteLandingPagePath) {
String parsedMessage = getParsedMessage();
for (ItemBean tagged : taggedPeople) {
String baseURL;
String hrefAttributeName;
String hrefAttributeValue;
if (! tagged.isItemGroup()) {
baseURL = siteLandingPagePath+GCubePortalConstants.USER_PROFILE_FRIENDLY_URL;
hrefAttributeName = GCubeSocialNetworking.USER_PROFILE_OID;
hrefAttributeValue = tagged.getName();
} else {
try {
long teamId = Long.parseLong(tagged.getId());
GCubeTeam theTeam = new LiferayRoleManager().getTeam(teamId);
//returns the VRE url e.g. /devVRE
String vreURL = new LiferayGroupManager().getGroup(theTeam.getGroupId()).getFriendlyURL();
//append the members url
baseURL= GCubePortalConstants.PREFIX_GROUP_URL + vreURL + GCubePortalConstants.GROUP_MEMBERS_FRIENDLY_URL;
hrefAttributeName = GCubeSocialNetworking.GROUP_MEMBERS_OID;
hrefAttributeValue = tagged.getId();
} catch (Exception e) {
logger.error("Error while retrieving team {}", tagged.getAlternativeName(), e);
continue;
}
}
String linkTarget = ReplaceableToken.createHref(baseURL, hrefAttributeName, hrefAttributeValue);
String replacement = ReplaceableToken.createLink(linkTarget, tagged.getAlternativeName(), null);
parsedMessage = parsedMessage.replace(tagged.getAlternativeName(), replacement);
}
return parsedMessage;
}
} }

View File

@ -9,7 +9,7 @@ public class ReplaceableToken extends Token {
protected String tokenReplacement; protected String tokenReplacement;
protected String createHref(String baseURL, String attributeName, String attributeValue) { public static String createHref(String baseURL, String attributeName, String attributeValue) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(baseURL); stringBuilder.append(baseURL);
stringBuilder.append("?"); stringBuilder.append("?");
@ -20,7 +20,7 @@ public class ReplaceableToken extends Token {
} }
protected String createLink(String linkTarget, String linkValue, Map<String, String> additionalAttributes) { public static String createLink(String linkTarget, String linkValue, Map<String, String> additionalAttributes) {
Map<String, String> attributes = new HashMap<>(); Map<String, String> attributes = new HashMap<>();
if(additionalAttributes!=null) { if(additionalAttributes!=null) {
attributes.putAll(additionalAttributes); attributes.putAll(additionalAttributes);

View File

@ -13,8 +13,8 @@ public class TagToken extends ReplaceableToken {
public String getTokenReplacement() { public String getTokenReplacement() {
if(!replaced) { if(!replaced) {
String linkTarget = createHref("", GCubeSocialNetworking.HASHTAG_OID, token); String linkTarget = ReplaceableToken.createHref("", GCubeSocialNetworking.HASHTAG_OID, token);
tokenReplacement = createLink(linkTarget, token, null); tokenReplacement = ReplaceableToken.createLink(linkTarget, token, null);
replaced = true; replaced = true;
} }
return super.getTokenReplacement(); return super.getTokenReplacement();

View File

@ -32,7 +32,7 @@ public class URLToken extends ReplaceableToken {
if(!replaced) { if(!replaced) {
Map<String, String> anchorAttibutes = new HashMap<>(1); Map<String, String> anchorAttibutes = new HashMap<>(1);
anchorAttibutes.put("target", "_blank"); anchorAttibutes.put("target", "_blank");
tokenReplacement = createLink(token, token, anchorAttibutes); tokenReplacement = ReplaceableToken.createLink(token, token, anchorAttibutes);
replaced = true; replaced = true;
} }
return super.getTokenReplacement(); return super.getTokenReplacement();

View File

@ -1,10 +1,12 @@
package org.gcube.social_networking.socialutillibrary; package org.gcube.social_networking.socialutillibrary;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.gcube.portal.databook.client.GCubeSocialNetworking; import org.gcube.portal.databook.client.GCubeSocialNetworking;
import org.gcube.portlets.widgets.pickitem.shared.ItemBean;
import org.gcube.socialnetworking.token.Token; import org.gcube.socialnetworking.token.Token;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -32,7 +34,7 @@ public class MessageParserTest {
public static final String TEST_LUCA = "Dear members,\n" + public static final String TEST_LUCA = "Dear members,\n" +
"The item 'just a test with time fields' has been just published by Francesco Mangiacrapa .\n" + "The item 'just a test with time fields' has been just published by Francesco Mangiacrapa.\n" +
"You can find it here: http://data-d.d4science.org/ctlg/NextNext/just_a_test_with_time_fields\n" + "You can find it here: http://data-d.d4science.org/ctlg/NextNext/just_a_test_with_time_fields\n" +
"#Text_mining #Field_1 #B3"; "#Text_mining #Field_1 #B3";
@ -51,6 +53,13 @@ public class MessageParserTest {
messageParser = new MessageParser(TEST_LUCA); messageParser = new MessageParser(TEST_LUCA);
logger.debug(messageParser.getParsedMessage()); logger.debug(messageParser.getParsedMessage());
List<ItemBean> mentionedUsers = new ArrayList<>();
ItemBean itemBean = new ItemBean("21150", "francesco.mangiacrapa", "Francesco Mangiacrapa", "");
itemBean.setItemGroup(false);
mentionedUsers.add(itemBean);
logger.debug(messageParser.getParsedMessage(mentionedUsers, "/group/nextnext"));
} }
@Test @Test