diff --git a/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java b/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java index 851ce39..9fbb717 100644 --- a/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/topics/server/TopicServiceImpl.java @@ -11,6 +11,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.apache.commons.codec.binary.Base64; import org.gcube.application.framework.core.session.ASLSession; @@ -39,9 +40,10 @@ import com.google.gwt.user.server.rpc.RemoteServiceServlet; */ @SuppressWarnings("serial") public class TopicServiceImpl extends RemoteServiceServlet implements TopicService { - private static final Logger _log = LoggerFactory.getLogger(TopicServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(TopicServiceImpl.class); public static final String TEST_USER = "test.user"; + private static final String TEST_SCOPE = "/gcube/devsec/devVRE"; private static final int WINDOW_SIZE_IN_MONTHS = 6; // it must not exceed 12 /** @@ -70,12 +72,13 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi String sessionID = this.getThreadLocalRequest().getSession().getId(); String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE); if (user == null) { - _log.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL"); + logger.warn("USER IS NULL setting test.user and Running OUTSIDE PORTAL"); user = getDevelopmentUser(); - SessionManager.getInstance().getASLSession(sessionID, user).setScope("/gcube/devsec/devVRE"); + SessionManager.getInstance().getASLSession(sessionID, user).setScope(TEST_SCOPE); } return SessionManager.getInstance().getASLSession(sessionID, user); } + /** * when packaging test will fail if the user is not set to test.user * @return . @@ -93,10 +96,16 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi public HashtagsWrapper getHashtags() { ArrayList hashtagsChart = new ArrayList(); ASLSession session = getASLSession(); - String userName = session.getUsername(); - boolean isInfrastructure = isInfrastructureScope(); String currentScope = session.getScope(); + boolean isInfrastructure = isInfrastructureScope(); + + //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) + //this check just return nothing if that happens + if (userName.compareTo(TEST_USER) == 0) { + logger.debug("Found " + userName + " returning nothing"); + return null; + } long timestampStart = System.currentTimeMillis(); @@ -107,20 +116,14 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi // print it SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - _log.debug("Reference time for trending topics is " + format.format(referenceTime.getTime())); + logger.debug("Reference time for trending topics is " + format.format(referenceTime.getTime())); try { - //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info) - //this check just return nothing if that happens - if (userName.compareTo("test.user") == 0) { - _log.debug("Found " + userName + " returning nothing"); - return null; - } - ArrayList toSort = new ArrayList(); + if (isInfrastructure) { - _log.debug("****** retrieving hashtags for user VREs"); + logger.debug("****** retrieving hashtags for user VREs and site"); // different vres could have a same hashtag, we need to merge them Map hashtags = new HashMap(); @@ -134,39 +137,44 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi UserManager um = new LiferayUserManager(); GCubeUser user = um.getUserByUsername(userName); - List groups = gm.listGroupsByUser(user.getUserId()); - for (GCubeGroup group : groups) { - if (gm.isVRE(group.getGroupId())) { - String vreid = gm.getInfrastructureScope(group.getGroupId()); //get the scope - Map map = store.getVREHashtagsWithOccurrenceFilteredByTime(vreid, referenceTime.getTimeInMillis()); + Set vresInPortal = gm.listGroupsByUserAndSite(user.getUserId(), getThreadLocalRequest().getServerName()); + logger.debug("Contexts in this site are per user " + vresInPortal); - // merge the values if needed - for (String hashtag : map.keySet()) { + List contexts = new ArrayList(); - if(hashtags.containsKey(hashtag)){ + // get the scopes associated with such groups + for (GCubeGroup gCubeGroup : vresInPortal) { + contexts.add(gm.getInfrastructureScope(gCubeGroup.getGroupId())); + } - int currentValue = hashtags.get(hashtag); - int newValue = currentValue + map.get(hashtag); + for (String context : contexts) { + Map map = store.getVREHashtagsWithOccurrenceFilteredByTime(context, referenceTime.getTimeInMillis()); - // remove and re-add - hashtags.remove(hashtag); - hashtags.put(hashtag, newValue); + // merge the values if needed + for (String hashtag : map.keySet()) { - // get the current list of vres in which the hashtag is present and add this new one - List vres = hashtagsInVres.get(hashtag); - vres.add(vreid); - hashtagsInVres.remove(hashtag); - hashtagsInVres.put(hashtag, vres); + if(hashtags.containsKey(hashtag)){ - }else{ + int currentValue = hashtags.get(hashtag); + int newValue = currentValue + map.get(hashtag); - hashtags.put(hashtag, map.get(hashtag)); + // remove and re-add + hashtags.remove(hashtag); + hashtags.put(hashtag, newValue); - // put in the hashmap hashtagsInVres too - List vres = new ArrayList(); - vres.add(vreid); - hashtagsInVres.put(hashtag, vres); - } + // get the current list of vres in which the hashtag is present and add this new one + List vres = hashtagsInVres.get(hashtag); + vres.add(context); + hashtagsInVres.put(hashtag, vres); + + }else{ + + hashtags.put(hashtag, map.get(hashtag)); + + // put in the hashmap hashtagsInVres too + List vres = new ArrayList(); + vres.add(context); + hashtagsInVres.put(hashtag, vres); } } } @@ -181,24 +189,22 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi } else { - //else must be in a VRE scope - String scope = session.getScope(); - _log.debug("****** retrieving hashtags for scope " + scope); - Map hashtags = store.getVREHashtagsWithOccurrenceFilteredByTime(scope, referenceTime.getTimeInMillis()); + logger.debug("****** retrieving hashtags for scope " + currentScope); + Map hashtags = store.getVREHashtagsWithOccurrenceFilteredByTime(currentScope, referenceTime.getTimeInMillis()); // now we need to evaluate the weight for each element - Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, scope, null); + Map weights = evaluateWeight(hashtags, WINDOW_SIZE_IN_MONTHS, currentMonth, referenceTime, currentScope, null); for (String hashtag : hashtags.keySet()) { toSort.add(new HashTagAndOccurrence(hashtag, hashtags.get(hashtag), weights.get(hashtag))); } } - _log.debug("Number of topics retrieved is " + toSort.size()); + logger.debug("Number of topics retrieved is " + toSort.size()); Collections.sort(toSort); // sort for weight for (HashTagAndOccurrence wrapper : toSort) { - _log.debug("Entry is " + wrapper.toString() + " with weight " + wrapper.getWeight()); + logger.debug("Entry is " + wrapper.toString() + " with weight " + wrapper.getWeight()); String hashtag = wrapper.getHashtag(); @@ -210,12 +216,12 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi } } catch (Exception e) { - e.printStackTrace(); + logger.error("Error while retrieving hashtags ", e); return null; } long timestampEnd = System.currentTimeMillis() - timestampStart; - _log.debug("Overall time to retrieve hastags is " + timestampEnd + "ms"); + logger.debug("Overall time to retrieve hastags is " + timestampEnd + "ms"); return new HashtagsWrapper(isInfrastructure, hashtagsChart); } @@ -273,7 +279,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi }catch(Exception e){ - _log.error("Unable to retrieve the most recent feeds for hashtag " + entry.getKey() + " in " + vreId); + logger.error("Unable to retrieve the most recent feeds for hashtag " + entry.getKey() + " in " + vreId); // put a weight of zero for this hashtag weights.put(entry.getKey(), 0.0); @@ -293,7 +299,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi try{ feedsForVre = store.getVREFeedsByHashtag(vre, entry.getKey()); }catch(Exception e){ - _log.error("Unable to retrieve the most recent feeds for hashtag " + entry.getKey() + " in " + vreId); + logger.error("Unable to retrieve the most recent feeds for hashtag " + entry.getKey() + " in " + vreId); continue; } @@ -319,7 +325,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi int sub = currentMonth - monstRecentFeedForHashTagTime.get(Calendar.MONTH); int value = sub >= 0? sub : 12 - Math.abs(sub); double freshness = 1.0 - (double)(value) / (double)(windowSize); - _log.debug("freshness is " + freshness + " for hashtag " + entry.getKey() + + logger.debug("freshness is " + freshness + " for hashtag " + entry.getKey() + " because the last feed has month " + monstRecentFeedForHashTagTime.get(Calendar.MONTH)); // update the weight @@ -333,7 +339,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi Map scoredListSorted = sortByWeight(weights); for(Entry entry : scoredListSorted.entrySet()){ - _log.debug("[hashtag=" + entry.getKey() + " , weight=" + entry.getValue() + "]"); + logger.debug("[hashtag=" + entry.getKey() + " , weight=" + entry.getValue() + "]"); } return weights; @@ -378,7 +384,7 @@ public class TopicServiceImpl extends RemoteServiceServlet implements TopicServi return toReturn; } catch (Exception e) { - _log.error("NullPointerException in isInfrastructureScope returning false"); + logger.error("NullPointerException in isInfrastructureScope returning false"); return false; } } diff --git a/src/main/java/org/gcube/portlets/user/topics/shared/HashTagAndOccurrence.java b/src/main/java/org/gcube/portlets/user/topics/shared/HashTagAndOccurrence.java index 05f8003..79049d4 100644 --- a/src/main/java/org/gcube/portlets/user/topics/shared/HashTagAndOccurrence.java +++ b/src/main/java/org/gcube/portlets/user/topics/shared/HashTagAndOccurrence.java @@ -1,5 +1,9 @@ package org.gcube.portlets.user.topics.shared; +/** + * Bean class for hashtag and its occurrence number + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ public class HashTagAndOccurrence implements Comparable{ private String hashtag; private Integer occurrence;