From 2d3a14f777cb2ee45602c19c308697b8142dda50 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Fri, 30 Sep 2016 10:35:37 +0000 Subject: [PATCH] Feature #4792, filter per site added git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/user-statistics@132171 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../server/UserStatisticsServiceImpl.java | 107 ++++++++++-------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/server/UserStatisticsServiceImpl.java b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/server/UserStatisticsServiceImpl.java index 1bbf8c4..86edeca 100644 --- a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/server/UserStatisticsServiceImpl.java +++ b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/server/UserStatisticsServiceImpl.java @@ -1,9 +1,11 @@ package org.gcube.portlet.user.userstatisticsportlet.server; +import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.homelibrary.home.HomeLibrary; @@ -18,10 +20,13 @@ import org.gcube.portlet.user.userstatisticsportlet.client.UserStatisticsService import org.gcube.portlet.user.userstatisticsportlet.server.cache.UserInfrastructureSpaceCache; import org.gcube.portlet.user.userstatisticsportlet.shared.PostsStatsBean; import org.gcube.portlet.user.userstatisticsportlet.shared.UserInformation; +import org.gcube.vomanagement.usermanagement.GroupManager; import org.gcube.vomanagement.usermanagement.UserManager; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; +import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; +import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.util.ManagementUtils; import org.slf4j.LoggerFactory; @@ -51,6 +56,10 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U // custom field' name to remember action to take for the portlet deployed in the user profile TODO (make it a resource?) private static final String CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY = "show_user_statistics_other_people"; + // User manager + private UserManager userManager = new LiferayUserManager(); + private GroupManager groupManager = new LiferayGroupManager(); + @Override public void init() { @@ -167,7 +176,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U long init = System.currentTimeMillis(); // check if the avatar is present - boolean avatarPresent = (new LiferayUserManager().getUserAvatarBytes(statisticsOfUsername) != null); + boolean avatarPresent = (userManager.getUserAvatarBytes(statisticsOfUsername) != null); User user = UserLocalServiceUtil.getUserByScreenName(SiteManagerUtil.getCompany().getCompanyId(), statisticsOfUsername); profileStrenght = ServerUtils.evaluateProfileStrenght(user, avatarPresent); @@ -230,12 +239,9 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U boolean isInfrastructure = ServerUtils.isInfrastructureScope(userid, this.getThreadLocalRequest().getSession()); logger.debug("User scope is " + (isInfrastructure ? " the whole infrastucture " : " a VRE")); - // get path of the avatar - UserManager um = new LiferayUserManager(); - String thumbnailURL = null; try { - thumbnailURL = um.getUserByUsername(statisticsOfUsername).getUserAvatarURL(); + thumbnailURL = userManager.getUserByUsername(statisticsOfUsername).getUserAvatarURL(); } catch (UserManagementSystemException e) { logger.error("Unable to retrieve avatar url for user " + statisticsOfUsername, e); } catch (UserRetrievalFault e) { @@ -301,12 +307,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U //username String userName = session.getUsername(); - - // retrieve statistics of... - String statisticsOfUsername = userName; - - if(userid != null && !userid.equals(userName)) - statisticsOfUsername = userid; + String scope = session.getScope(); //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 @@ -315,6 +316,12 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U return null; } + // retrieve statistics of... + String statisticsOfUsername = userName; + + if(userid != null && !userid.equals(userName)) + statisticsOfUsername = userid; + long feedsMade = 0, likesGot = 0, commentsGot = 0, commentsMade = 0, likesMade = 0; // check if the user is or not in a VRE @@ -329,21 +336,46 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U try { long init = System.currentTimeMillis(); + long userId = userManager.getUserId(statisticsOfUsername); - logger.debug("Getting " + statisticsOfUsername + " feeds in the last year."); + logger.debug("Retrieving " + statisticsOfUsername + "'s statistics in the last year."); // retrieve the most recent user's feeds List userFeeds = store.getRecentFeedsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); - logger.debug("Evaluating number of comments and likes of " + statisticsOfUsername + "'s feeds."); + // retrieve the most recent user's liked feeds + List recentLikedFeeds = store.getRecentLikedFeedsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); + + // retrieve the most recent user's comments + List recentComments = store.getRecentCommentsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); + Map parentFeeds = new HashMap(); + + // Evaluate the contexts to use + List contexts = new ArrayList(); + + if(isInfrastructure){ + + // filter for site + Set vresInPortal = groupManager.listGroupsByUserAndSite(userId, getThreadLocalRequest().getServerName()); + logger.debug("Contexts in this site are " + vresInPortal); + + // get the scopes associated with such groups + for (GCubeGroup gCubeGroup : vresInPortal) { + contexts.add(groupManager.getInfrastructureScope(gCubeGroup.getGroupId())); + } + + }else{ + + // just the current scope + contexts.add(scope); + + } + + logger.debug("Context(s) that are going to be used " + contexts); for (Feed feed : userFeeds) { - try{ - - // check if the user is in the root, if not check if the VRE of the feed is the current one - if(!isInfrastructure && !feed.getVreid().equals(session.getScope())) - continue; + if(contexts.contains(feed.getVreid())){ // increment feeds number feedsMade ++; @@ -352,27 +384,16 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U commentsGot += Integer.parseInt(feed.getCommentsNo()); likesGot += Integer.parseInt(feed.getLikesNo()); - }catch(NumberFormatException e){ - logger.error(e.toString()); } - } - // retrieve the most recent user's liked feeds - List recentLikedFeeds = store.getRecentLikedFeedsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); + } for (Feed feed : recentLikedFeeds) { - // check if the user is in the root, if not check if the VRE of the feed is the current one - if(!isInfrastructure && !feed.getVreid().equals(session.getScope())) - continue; - - likesMade ++; // no further check is needed since the user can do like just one time per feed + if(contexts.contains(feed.getVreid())) + likesMade ++; // no further check is needed since the user can do like just one time per feed } - // retrieve the most recent user's comments - List recentComments = store.getRecentCommentsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); - Map parentFeeds = new HashMap(); - for (Comment comment : recentComments) { Feed parentFeed = null; @@ -380,25 +401,22 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U if(!parentFeeds.containsKey(comment.getFeedid())){ parentFeed = store.readFeed(comment.getFeedid()); parentFeeds.put(comment.getFeedid(), parentFeed); - } + }else + parentFeed = parentFeeds.get(comment.getFeedid()); - parentFeed = parentFeeds.get(comment.getFeedid()); - - // check if the user is in the root, if not check if the VRE of the feed is the current one - if(!isInfrastructure && !parentFeed.getVreid().equals(session.getScope())) - continue; - - commentsMade ++; + if((contexts.contains(parentFeed.getVreid()))) + commentsMade ++; } + long end = System.currentTimeMillis(); - logger.debug("[USER-STATISTICS] time taken to retrieve statistics is " + (end - init) + " ms"); - logger.debug("Total number of feeds made (after time filtering) of " + statisticsOfUsername + " is " + feedsMade); + logger.debug("time taken to retrieve statistics is " + (end - init) + " ms"); + logger.debug("Total number of feeds done (after time filtering) of " + statisticsOfUsername + " is " + feedsMade); logger.debug("Total number of likes got (after time filtering) for " + statisticsOfUsername + " is " + likesGot); logger.debug("Total number of comments got (after time filtering) for " + statisticsOfUsername + " is " + commentsGot); - logger.debug("Total number of likes made (after time filtering) for " + statisticsOfUsername + " is " + likesMade); - logger.debug("Total number of comments made (after time filtering) for " + statisticsOfUsername + " is " + commentsMade); + logger.debug("Total number of likes done (after time filtering) for " + statisticsOfUsername + " is " + likesMade); + logger.debug("Total number of comments done (after time filtering) for " + statisticsOfUsername + " is " + commentsMade); }catch(Exception e){ logger.error(e.toString()); @@ -415,7 +433,6 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U if(ServerUtils.isWithinPortal()){ ASLSession session = ServerUtils.getASLSession(this.getThreadLocalRequest().getSession()); - String username = session.getUsername(); //in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)