From e589b6079fd844ef9d441acead7c3ede85ae481b Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Tue, 10 May 2016 16:40:55 +0000 Subject: [PATCH] The portlet can now be deployed in a profile page git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/user/user-statistics@128552 82a268e6-3cf1-43bd-a215-b396298e98cf --- .settings/org.eclipse.wst.common.component | 3 + pom.xml | 6 + .../client/StatisticsPanel.java | 136 +++++++--- .../client/UserStatisticsService.java | 14 +- .../client/UserStatisticsServiceAsync.java | 10 +- .../server/UserStatisticsServiceImpl.java | 253 +++++++++++++++--- .../shared/UserInformation.java | 57 ++-- src/main/webapp/Statistics.css | 4 + .../client/TestForDeploy.java | 2 +- 9 files changed, 394 insertions(+), 91 deletions(-) diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 65eedd1..d16397c 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,9 @@ + + uses + diff --git a/pom.xml b/pom.xml index 8eee0b3..9f4c32a 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,12 @@ provided 2.7.0 + + com.google.gwt + gwt-dev + provided + 2.7.0 + org.gcube.portlets.user gcube-widgets diff --git a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/StatisticsPanel.java b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/StatisticsPanel.java index ea72676..03009b3 100644 --- a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/StatisticsPanel.java +++ b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/StatisticsPanel.java @@ -5,6 +5,8 @@ import net.eliasbalasis.tibcopagebus4gwt.client.PageBusAdapterException; import net.eliasbalasis.tibcopagebus4gwt.client.PageBusEvent; import net.eliasbalasis.tibcopagebus4gwt.client.PageBusListener; +import org.gcube.portal.databook.client.GCubeSocialNetworking; +import org.gcube.portal.databook.client.util.Encoder; import org.gcube.portlet.user.userstatisticsportlet.client.resources.Images; import org.gcube.portlet.user.userstatisticsportlet.client.ui.CommentsAndLikesWidget; import org.gcube.portlet.user.userstatisticsportlet.client.ui.StatisticWidget; @@ -15,10 +17,13 @@ import org.gcube.portlets.widgets.widgettour.client.extendedclasses.GCubeTour; import com.ait.toolkit.hopscotch.client.Placement; import com.ait.toolkit.hopscotch.client.TourStep; import com.github.gwtbootstrap.client.ui.Button; +import com.github.gwtbootstrap.client.ui.CheckBox; import com.github.gwtbootstrap.client.ui.constants.ButtonType; import com.google.gwt.core.client.GWT; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.logical.shared.ValueChangeEvent; +import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; @@ -64,7 +69,7 @@ public class StatisticsPanel extends Composite { */ private final static String POSTS_LABEL = "Posts"; private final static String STORAGE_LABEL = "Total Space Used"; - private final static String LIKES_COMMENTS_LABEL = "You got"; + private final static String LIKES_COMMENTS_LABEL = "Got"; private final static String PROFILE_STRENGTH_LABEL = "Profile Strength"; /** @@ -146,8 +151,12 @@ public class StatisticsPanel extends Composite { //show loader, waiting for the answer coming by the server showLoader(); + // does the profile belong to someone? (when we are on a profile page of someone other, we need + // to retrieve the statistics of that person) + final String userid = getUserToShowId(); + // request user's information - statisticsService.getUserSettings(new AsyncCallback() { + statisticsService.getUserSettings(userid, new AsyncCallback() { @Override public void onFailure(Throwable arg0) { @@ -157,11 +166,24 @@ public class StatisticsPanel extends Composite { } @Override - public void onSuccess(UserInformation information) { + public void onSuccess(final UserInformation information) { // remove loading image mainPanel.remove(loadingImage); + // first of all check if the statistics can be shown to other people + if(userid != null && !userid.equals(information.getAslSessionUsername()) && !information.isProfileShowable()){ + + mainPanel.add(new HTML("Sorry but the user set his Statistics to private.")); + return; + + } + + // is a user profile page? + final boolean isProfilePage = + Window.Location.getHref().endsWith("profile") + || Window.Location.getHref().contains("profile?"); + // check which kind of information we have to show isRoot = information.isRoot(); @@ -211,7 +233,7 @@ public class StatisticsPanel extends Composite { tour.startTour(); } - if(!isRoot){ + if(!isRoot && !isProfilePage){ // add the border to the panel and the VRE name (check for VRE name lenght) mainPanel.addStyleName("user-stats-frame-border"); @@ -234,8 +256,14 @@ public class StatisticsPanel extends Composite { // check if the user has an avatar if(information.getUrlAvatar() == null) userImage.setResource(image.avatarDefaultImage()); - else + else{ + userImage.setUrl(information.getUrlAvatar()); + if(userid != null) + userImage.setTitle("User's avatar"); + else + userImage.setTitle("Your current avatar"); + } // set the style for the user image userImage.setStyleName("user-image"); @@ -247,10 +275,10 @@ public class StatisticsPanel extends Composite { final StatisticWidget feeds = new StatisticWidget(isRoot); feeds.setHeader(POSTS_LABEL); - if(isRoot) - feeds.setToolTip("Your posts during the last year."); + if(isRoot || isProfilePage) + feeds.setToolTip("Posts during the last year."); else - feeds.setToolTip("Your posts during the last year in this VRE."); + feeds.setToolTip("Posts during the last year in this VRE."); // add loading image that will be replaced by the incoming values @@ -265,10 +293,10 @@ public class StatisticsPanel extends Composite { final StatisticWidget likesAndComments = new StatisticWidget(isRoot); likesAndComments.setHeader(LIKES_COMMENTS_LABEL); - if(isRoot) - likesAndComments.setToolTip("Likes and post replies you got during the last year."); + if(isRoot || isProfilePage) + likesAndComments.setToolTip("Likes and post replies got during the last year."); else - likesAndComments.setToolTip("Likes and post replies you got during the last year in this VRE."); + likesAndComments.setToolTip("Likes and post replies got during the last year in this VRE."); // add loading image that will be replaced by the incoming values Image commentsLikesLoader = new Image(imagePath); @@ -282,7 +310,7 @@ public class StatisticsPanel extends Composite { final StatisticWidget storage = new StatisticWidget(isRoot); final StatisticWidget profileStrength = new StatisticWidget(isRoot); - if(isRoot){ + if(isRoot || isProfilePage){ storage.setHeader(STORAGE_LABEL); storage.setToolTip("Total amount of space used in the infrastructure."); @@ -295,7 +323,7 @@ public class StatisticsPanel extends Composite { mainPanel.add(storage); profileStrength.setHeader(PROFILE_STRENGTH_LABEL); - profileStrength.setToolTip("Your profile strength."); + profileStrength.setToolTip("Profile strength."); // add loading image that will be replaced by the incoming values Image profileStrengthLoader = new Image(imagePath); @@ -306,7 +334,7 @@ public class StatisticsPanel extends Composite { mainPanel.add(profileStrength); // async requests that must be performed in root context - statisticsService.getTotalSpaceInUse(new AsyncCallback() { + statisticsService.getTotalSpaceInUse(userid, new AsyncCallback() { @Override public void onFailure(Throwable arg0) { @@ -331,7 +359,7 @@ public class StatisticsPanel extends Composite { } }); - statisticsService.getProfileStrength(new AsyncCallback() { + statisticsService.getProfileStrength(userid, new AsyncCallback() { @Override public void onFailure(Throwable arg0) { @@ -358,7 +386,7 @@ public class StatisticsPanel extends Composite { profileStrength.appendToPanel(profileStrengthLabel); // in case too low information within the user profile - if(profileStrengthInt < profileImproveThreshold){ + if(profileStrengthInt < profileImproveThreshold && information.isOwner()){ final Button improveProfileButton = new Button(IMPROVE_BUTTON_LABEL); improveProfileButton.setType(ButtonType.WARNING); @@ -387,7 +415,7 @@ public class StatisticsPanel extends Composite { } // retrieve othe information about number of feeds and post replies/likes - statisticsService.getPostsStats(new AsyncCallback(){ + statisticsService.getPostsStats(userid, new AsyncCallback(){ public void onFailure(Throwable arg0) { @@ -410,10 +438,10 @@ public class StatisticsPanel extends Composite { // update feeds number feeds.clearPanelValues(); numberOfFeedsLabel = new Label(formattedNumbers(postsBean.getFeedsNumber())); - if(isRoot) - numberOfFeedsLabel.setTitle("Your posts during the last year (" + postsBean.getFeedsNumber() + ")."); + if(isRoot || isProfilePage) + numberOfFeedsLabel.setTitle("Posts during the last year (" + postsBean.getFeedsNumber() + ")."); else - numberOfFeedsLabel.setTitle("Your posts during the last year in this VRE (" + postsBean.getFeedsNumber() + ")."); + numberOfFeedsLabel.setTitle("Posts during the last year in this VRE (" + postsBean.getFeedsNumber() + ")."); numberOfWrittenFeeds = postsBean.getFeedsNumber(); numberOfFeedsLabel.setStyleName("statistic-value"); @@ -424,34 +452,34 @@ public class StatisticsPanel extends Composite { content = new CommentsAndLikesWidget(); String urlLikesIcon = GWT.getModuleBaseURL() + "../images/star_blue.png"; - if(isRoot) + if(isRoot || isProfilePage) content.setLikes( urlLikesIcon, formattedNumbers(postsBean.getLikesReceived()), - "Likes you got during the last year.", - "Likes you got during the last year (" + postsBean.getLikesReceived() + ")."); + "Likes got during the last year.", + "Likes got during the last year (" + postsBean.getLikesReceived() + ")."); else content.setLikes( urlLikesIcon, formattedNumbers(postsBean.getLikesReceived()), - "Likes you got during the last year in this VRE", - "Likes you got during the last year in this VRE (" + postsBean.getLikesReceived() + ")."); + "Likes got during the last year in this VRE", + "Likes got during the last year in this VRE (" + postsBean.getLikesReceived() + ")."); numberOfLikesGot = postsBean.getLikesReceived(); String urlCommentsIcon = GWT.getModuleBaseURL() + "../images/comment_edit.png"; - if(isRoot) + if(isRoot || isProfilePage) content.setComments( urlCommentsIcon, formattedNumbers(postsBean.getCommentsReceived()), - "Post replies you got during the last year.", - "Post replies you got during the last year (" + postsBean.getCommentsReceived() + ")."); + "Post replies got during the last year.", + "Post replies got during the last year (" + postsBean.getCommentsReceived() + ")."); else content.setComments( urlCommentsIcon, formattedNumbers(postsBean.getCommentsReceived()), - "Post replies you got during the last year in this VRE.", - "Post replies you got during the last year in this VRE (" + postsBean.getCommentsReceived() + ")."); + "Post replies got during the last year in this VRE.", + "Post replies got during the last year in this VRE (" + postsBean.getCommentsReceived() + ")."); numberOfCommentsGot = postsBean.getCommentsReceived(); likesAndComments.appendToPanel(content); @@ -459,11 +487,43 @@ public class StatisticsPanel extends Composite { }); + + // check if we need to show the checkbox to allow the user's profile owner to edit privacy options + if(information.isOwner() && (isProfilePage)){ + + // add a checkbox with the settable privacy option + CheckBox privacyOption = new CheckBox("Show my statistics to VRE Members"); + privacyOption.setTitle("Show Statistics to members viewing your profile"); + privacyOption.setValue(information.isProfileShowable()); + privacyOption.addStyleName("privacy-checkbox-statistics-style"); + + privacyOption.addValueChangeHandler(new ValueChangeHandler() { + + @Override + public void onValueChange(ValueChangeEvent event) { + + // set this privacy option + statisticsService.setShowMyOwnStatisticsToOtherPeople(event.getValue(), new AsyncCallback(){ + + @Override + public void onFailure(Throwable caught) { + } + + @Override + public void onSuccess(Void result) { + } + + }); + + } + }); + + mainPanel.add(privacyOption); + } } }); - } - + /** * Bind for events of increment/decrement of user's posts coming from the news-feed portlet */ @@ -754,4 +814,16 @@ public class StatisticsPanel extends Composite { return formattedString; } + + /** + * decode the userid from the location param + * @return the decoded (base64) userid + */ + public static String getUserToShowId() { + String encodedOid = Encoder.encode(GCubeSocialNetworking.USER_PROFILE_OID); + if (Window.Location.getParameter(encodedOid) == null) + return null; + String encodedUserId = Window.Location.getParameter(encodedOid); + return Encoder.decode(encodedUserId); + } } diff --git a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsService.java b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsService.java index 58fe8a3..d139f1d 100644 --- a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsService.java +++ b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsService.java @@ -17,21 +17,27 @@ public interface UserStatisticsService extends RemoteService { * get other user's information * @return an object with user's information (actual vre, his name, url of his image) */ - UserInformation getUserSettings(); + UserInformation getUserSettings(String userid); /** * get information relatives to feeds(posts), comment replies and likes received * @return an object with user's statistics (number of feeds(posts), comment replies, likes received) */ - PostsStatsBean getPostsStats(); + PostsStatsBean getPostsStats(String userid); /** * get the total space in use on the workspace */ - String getTotalSpaceInUse(); + String getTotalSpaceInUse(String userid); /** * get profile strenght */ - int getProfileStrength(); + int getProfileStrength(String userid); + + /** + * when the portlet is deployed on the user profile page, allows him to decide to show his statistics + * to other members or not + */ + void setShowMyOwnStatisticsToOtherPeople(boolean show); } diff --git a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsServiceAsync.java b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsServiceAsync.java index ec4009d..8d910e9 100644 --- a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsServiceAsync.java +++ b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/client/UserStatisticsServiceAsync.java @@ -12,12 +12,14 @@ import com.google.gwt.user.client.rpc.AsyncCallback; */ public interface UserStatisticsServiceAsync { - void getUserSettings(AsyncCallback callback); + void getUserSettings(String userid, AsyncCallback callback); - void getPostsStats(AsyncCallback callback); + void getPostsStats(String userid, AsyncCallback callback); - void getTotalSpaceInUse(AsyncCallback callback); + void getTotalSpaceInUse(String userid, AsyncCallback callback); - void getProfileStrength(AsyncCallback callback); + void getProfileStrength(String userid, AsyncCallback callback); + void setShowMyOwnStatisticsToOtherPeople(boolean show, + AsyncCallback callback); } 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 c05d830..3f7b852 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,7 +1,8 @@ package org.gcube.portlet.user.userstatisticsportlet.server; +import java.io.Serializable; import java.text.DecimalFormat; -import java.util.Date; +import java.util.Calendar; import java.util.List; import org.gcube.application.framework.core.session.ASLSession; @@ -17,15 +18,25 @@ import org.gcube.portlet.user.userstatisticsportlet.client.UserStatisticsService 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.util.ManagementUtils; import org.slf4j.LoggerFactory; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.liferay.portal.model.Contact; import com.liferay.portal.model.User; import com.liferay.portal.model.Website; +import com.liferay.portal.security.auth.PrincipalThreadLocal; +import com.liferay.portal.security.permission.PermissionChecker; +import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil; +import com.liferay.portal.security.permission.PermissionThreadLocal; import com.liferay.portal.service.UserLocalServiceUtil; import com.liferay.portal.service.WebsiteLocalServiceUtil; +import com.liferay.portlet.expando.model.ExpandoColumnConstants; /** * The server side implementation of the RPC service. @@ -37,7 +48,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U private static final org.slf4j.Logger _log = LoggerFactory.getLogger(UserStatisticsServiceImpl.class); //dev user - public static final String userid = "test.user"; + public static final String defaultUserId = "test.user"; //dev vre private static final String vreID = "/gcube/devsec/devVRE"; @@ -45,11 +56,17 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U // Cassandra connection private DatabookStore store; + // custom field' name to remember action to take for the portlet deployed in the user profile + private static final String CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY = "show_user_statistics_other_people"; + @Override public void init() { // get connection to Cassandra _log.debug("Getting connection to Cassandra.."); store = new DBCassandraAstyanaxImpl(); + + // add statistics option for profile pages and set to true + createUserCustomField(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, true); } @@ -71,7 +88,7 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U if (user == null) { - _log.warn("USER IS NULL setting " + userid + " and Running OUTSIDE PORTAL"); + _log.warn("USER IS NULL setting " + defaultUserId + " and Running OUTSIDE PORTAL"); user = getDevelopmentUser(); SessionManager.getInstance().getASLSession(sessionID, user).setScope(vreID); @@ -118,39 +135,45 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U * @return . */ public String getDevelopmentUser() { - String user = userid; + String user = defaultUserId; // user = "costantino.perciante"; return user; } @Override - public String getTotalSpaceInUse() { + public String getTotalSpaceInUse(String userid) { String storageInUse = null; // get the session ASLSession session = getASLSession(); - //username + // username in the session String userName = session.getUsername(); + // retrieve statistics of... + String statisticsOfUsername = userName; + + if(userid != null && !userid.equals(userName)) + statisticsOfUsername = userid; + //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(userid) == 0) { + if (userName.compareTo(defaultUserId) == 0) { _log.debug("Found " + userName + " returning nothing"); return null; }else{ - _log.debug("Getting " + userName + " amount of workspace in use."); + _log.debug("Getting " + statisticsOfUsername + " amount of workspace in use."); try{ long init = System.currentTimeMillis(); - Workspace workspace = HomeLibrary.getUserWorkspace(userName); + Workspace workspace = HomeLibrary.getUserWorkspace(statisticsOfUsername); long storage = workspace.getDiskUsage(); storageInUse = formatFileSize(storage); @@ -168,19 +191,25 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U } @Override - public int getProfileStrength() { + public int getProfileStrength(String userid) { int profileStrenght = -1; // get the session ASLSession session = getASLSession(); - //username + // username String userName = session.getUsername(); + // retrieve statistics of... + String statisticsOfUsername = userName; + + if(userid != null && !userid.equals(userName)) + statisticsOfUsername = userid; + //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(userid) == 0) { + if (userName.compareTo(defaultUserId) == 0) { _log.debug("Found " + userName + " returning nothing"); return profileStrenght; }else{ @@ -191,11 +220,11 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U try{ // check if the avatar is present - boolean avatarPresent = session.getUserAvatarId() != null; + boolean avatarPresent = (new LiferayUserManager().getUserAvatarBytes(statisticsOfUsername) != null); long init = System.currentTimeMillis(); - User user = UserLocalServiceUtil.getUserByScreenName(SiteManagerUtil.getCompany().getCompanyId(), session.getUsername()); + User user = UserLocalServiceUtil.getUserByScreenName(SiteManagerUtil.getCompany().getCompanyId(), statisticsOfUsername); profileStrenght = evaluateProfileStrenght(user, avatarPresent); long end = System.currentTimeMillis(); @@ -213,17 +242,37 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U } @Override - public UserInformation getUserSettings() { + public UserInformation getUserSettings(String userid) { // get the session ASLSession session = getASLSession(); - //username + //username of the asl session String userName = session.getUsername(); + // retrieve statistics of... + String statisticsOfUsername = userName; + + // is he the owner of the profile? + boolean isOwner = false; + + // can we show this profile to other people? + boolean isProfileShowable = true; + + if(userid == null || (userid !=null && userid.equals(userName))){ + isOwner = true; + isProfileShowable = checkUserPrivacyOption(userName); + } + + if(userid != null && !userid.equals(userName)){ + // the stastics to show will be of the userid + statisticsOfUsername = userid; + isProfileShowable = checkUserPrivacyOption(statisticsOfUsername); + } + //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(userid) == 0) { + if (userName.compareTo(defaultUserId) == 0) { _log.debug("Found " + userName + " returning nothing"); return null; } @@ -235,8 +284,18 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U _log.debug("User scope is " + (isInfrastructure ? " the whole infrastucture " : " a VRE")); // get path of the avatar - String thumbnailURL = session.getUserAvatarId(); - _log.debug(userName + " avatar has url " + thumbnailURL); + UserManager um = new LiferayUserManager(); + + String thumbnailURL = null; + try { + thumbnailURL = um.getUserByUsername(statisticsOfUsername).getUserAvatarURL(); + } catch (UserManagementSystemException e) { + _log.error("Unable to retrieve avatar url for user " + statisticsOfUsername, e); + } catch (UserRetrievalFault e) { + _log.error("Unable to retrieve avatar url for user " + statisticsOfUsername, e); + } + + _log.debug(statisticsOfUsername + " avatar has url " + thumbnailURL); // get the vre (if not in the infrastructure) String actualVre = null; @@ -248,14 +307,39 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U } - return new UserInformation(isInfrastructure, thumbnailURL, userName, actualVre); + return new UserInformation(isInfrastructure, thumbnailURL, userName, actualVre, isOwner, isProfileShowable); + } + else return new UserInformation(true, null, userName, vreID, true, true); + } + + /** + * Check privacy option for user's own statistics + * @param username + * @return + */ + private boolean checkUserPrivacyOption(String username) { + try{ + + // set permission checker + setPermissionChecker(); + + User user = UserLocalServiceUtil.getUserByScreenName(ManagementUtils.getCompany().getCompanyId(), username); + + // the user has not decided yet + if(!user.getExpandoBridge().hasAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY)) + return true; + + return (boolean)user.getExpandoBridge().getAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY); + + }catch(Exception e){ + + _log.error("Unable to retrieve user's privacy option for his statistics"); + return true; } - else return new UserInformation(true, null, userName, vreID); } - @SuppressWarnings("deprecation") @Override - public PostsStatsBean getPostsStats(){ + public PostsStatsBean getPostsStats(String userid){ // get the session ASLSession session = getASLSession(); @@ -263,9 +347,15 @@ 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; + //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(userid) == 0) { + if (userName.compareTo(defaultUserId) == 0) { _log.debug("Found " + userName + " returning nothing"); return null; } @@ -276,19 +366,19 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U boolean isInfrastructure = isInfrastructureScope(); // date corresponding to one year ago - Date oneYearAgo = new Date(); - oneYearAgo.setYear(oneYearAgo.getYear() - 1); + Calendar oneYearAgo = Calendar.getInstance(); + oneYearAgo.set(Calendar.YEAR, oneYearAgo.get(Calendar.YEAR) - 1); - _log.debug("Reference time is " + oneYearAgo.toString()); + _log.debug("Reference time is " + oneYearAgo.getTime()); try { long init = System.currentTimeMillis(); - _log.debug("Getting " + userName + " feeds in the last year."); + _log.debug("Getting " + statisticsOfUsername + " feeds in the last year."); - List userFeeds = store.getRecentFeedsByUserAndDate(userName, oneYearAgo.getTime()); + List userFeeds = store.getRecentFeedsByUserAndDate(statisticsOfUsername, oneYearAgo.getTime().getTime()); - _log.debug("Evaluating number of comments and likes of " + userName + "'s feeds."); + _log.debug("Evaluating number of comments and likes of " + statisticsOfUsername + "'s feeds."); for (Feed feed : userFeeds) { @@ -313,9 +403,9 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U long end = System.currentTimeMillis(); _log.debug("[USER-STATISTICS] time taken to retrieve and filter user feeds, get likes and replies got is " + (end - init) + "ms"); - _log.debug("Total number of feeds (after time filtering) of " + userName + " is " + totalFeeds); - _log.debug("Total number of likes (after time filtering) for " + userName + " is " + totalLikes); - _log.debug("Total number of comments (after time filtering) for " + userName + " is " + totalComments); + _log.debug("Total number of feeds (after time filtering) of " + statisticsOfUsername + " is " + totalFeeds); + _log.debug("Total number of likes (after time filtering) for " + statisticsOfUsername + " is " + totalLikes); + _log.debug("Total number of comments (after time filtering) for " + statisticsOfUsername + " is " + totalComments); }catch(Exception e){ _log.error(e.toString()); @@ -326,6 +416,86 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U return new PostsStatsBean(totalFeeds, totalLikes, totalComments); } + @Override + public void setShowMyOwnStatisticsToOtherPeople(boolean show) { + + if(isWithinPortal()){ + + ASLSession session = getASLSession(); + + 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) + //this check just return nothing if that happens + if (username.compareTo(defaultUserId) == 0) { + _log.debug("Found " + username + " returning nothing"); + return; + } + + try{ + + // set permission checker + setPermissionChecker(); + + _log.debug("User " + username + (show ? " want to show " : " doesn't want to show ") + " his statistics"); + + User user = UserLocalServiceUtil.getUserByScreenName(ManagementUtils.getCompany().getCompanyId(), username); + boolean hasAttribute = user.getExpandoBridge().hasAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY); + + if(hasAttribute){ + + // set the new value + _log.debug("Setting custom field value to " + show + " for user " + username); + + // set the current value + user.getExpandoBridge().setAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, show); + + } + + }catch(Exception e){ + _log.error("Unable to check user's privacy for his statistics", e); + } + } + + } + + /** + * On servlet instanciation, create the custom field and set it to startingValue + * @param customFieldNameUserStatisticsVisibility + * @param b + */ + private void createUserCustomField( + String customFieldNameUserStatisticsVisibility, boolean startingValue) { + + // set permission checker + setPermissionChecker(); + + try{ + + User defaultUser = UserLocalServiceUtil.getDefaultUser(ManagementUtils.getCompany().getCompanyId()); + + // check if it exists + boolean exists = defaultUser.getExpandoBridge().hasAttribute(customFieldNameUserStatisticsVisibility); + + if(exists){ + + _log.debug("Custom field already exists... There is no need to create it"); + + }else{ + + _log.debug("Creating custom field " + customFieldNameUserStatisticsVisibility + + " with starting value " + startingValue); + + // create + defaultUser.getExpandoBridge().addAttribute(CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY, ExpandoColumnConstants.BOOLEAN, (Serializable)(true)); + + } + + }catch(Exception e){ + _log.error("Unable to create custom field " + customFieldNameUserStatisticsVisibility); + } + } + /** * returns dynamically the formated size. * @@ -450,4 +620,21 @@ public class UserStatisticsServiceImpl extends RemoteServiceServlet implements U return score; } + + /** + * Set the permission checker to set/get custom fields into liferay + */ + private void setPermissionChecker(){ + + // set permission checker + try{ + long adminId = LiferayUserManager.getAdmin().getUserId(); + PrincipalThreadLocal.setName(adminId); + PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(UserLocalServiceUtil.getUser(adminId)); + PermissionThreadLocal.setPermissionChecker(permissionChecker); + }catch(Exception e){ + _log.error("Unable to set permission checker. Custom fields set/get operations are likely to fail..."); + } + + } } diff --git a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/shared/UserInformation.java b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/shared/UserInformation.java index c0a919a..57e432b 100644 --- a/src/main/java/org/gcube/portlet/user/userstatisticsportlet/shared/UserInformation.java +++ b/src/main/java/org/gcube/portlet/user/userstatisticsportlet/shared/UserInformation.java @@ -9,27 +9,37 @@ import java.io.Serializable; */ public class UserInformation implements Serializable { - /** - * - */ private static final long serialVersionUID = 6360825683813448487L; private boolean isRoot; private String urlAvatar; - private String displayName; + private String aslSessionUsername; private String actualVre; - + private boolean isOwner; + // this value will be set to true when the portlet is deployed in the user profile page + // and the user that visits the page is the owner of the profile + // if it is deployed in a vre or in the root infrastructure, it will be set to false + // of course, the same is true if the user that visits the profile is not the owner + + private boolean isProfileShowable = true; + // this field is used when a user visit a user profile page. + // If the visiting user is the owner of the page, there is no proble + // if the visiting user is not the owner and the real owner doesn't want + // to show the portlet to the other users, we need to hide the statistics. + public UserInformation(){ super(); } public UserInformation(boolean isRoot, String urlAvatar, - String displayName, String actualVre) { + String aslSessionUsername, String actualVre, boolean isOwner, boolean isProfileShowable) { super(); this.isRoot = isRoot; this.urlAvatar = urlAvatar; - this.displayName = displayName; + this.aslSessionUsername = aslSessionUsername; this.actualVre = actualVre; + this.isOwner = isOwner; + this.isProfileShowable = isProfileShowable; } @@ -52,17 +62,14 @@ public class UserInformation implements Serializable { this.urlAvatar = urlAvatar; } - - public String getDisplayName() { - return displayName; + public String getAslSessionUsername() { + return aslSessionUsername; } - - public void setDisplayName(String displayName) { - this.displayName = displayName; + public void setAslSessionUsername(String aslSessionUsername) { + this.aslSessionUsername = aslSessionUsername; } - public String getActualVre() { return actualVre; } @@ -72,11 +79,27 @@ public class UserInformation implements Serializable { this.actualVre = actualVre; } + public boolean isOwner() { + return isOwner; + } + + public void setOwner(boolean isOwner) { + this.isOwner = isOwner; + } + + public boolean isProfileShowable() { + return isProfileShowable; + } + + public void setProfileShowable(boolean isProfileShowable) { + this.isProfileShowable = isProfileShowable; + } + @Override public String toString() { return "UserInformation [isRoot=" + isRoot + ", urlAvatar=" + urlAvatar - + ", displayName=" + displayName + ", actualVre=" + actualVre - + "]"; + + ", aslSessionUsername=" + aslSessionUsername + ", actualVre=" + + actualVre + ", isOwner=" + isOwner + ", isProfileShowable=" + + isProfileShowable + "]"; } - } diff --git a/src/main/webapp/Statistics.css b/src/main/webapp/Statistics.css index ae94438..a934640 100644 --- a/src/main/webapp/Statistics.css +++ b/src/main/webapp/Statistics.css @@ -60,4 +60,8 @@ padding: 5px 15px !important; font-weight: normal !important; display: inline !important; +} + +.privacy-checkbox-statistics-style { + margin-top: 10px; } \ No newline at end of file diff --git a/src/test/java/org/gcube/portlet/user/userstatisticsportlet/client/TestForDeploy.java b/src/test/java/org/gcube/portlet/user/userstatisticsportlet/client/TestForDeploy.java index e7c1caa..6c3a8ed 100644 --- a/src/test/java/org/gcube/portlet/user/userstatisticsportlet/client/TestForDeploy.java +++ b/src/test/java/org/gcube/portlet/user/userstatisticsportlet/client/TestForDeploy.java @@ -12,7 +12,7 @@ public class TestForDeploy { UserStatisticsServiceImpl serviceImpl = new UserStatisticsServiceImpl(); String username = serviceImpl.getDevelopmentUser(); System.out.println("username for deploy is: " + username); - assertTrue(username.compareTo(UserStatisticsServiceImpl.userid) == 0); + assertTrue(username.compareTo(UserStatisticsServiceImpl.defaultUserId) == 0); System.out.println("Test OK!"); }