2015-12-09 14:34:41 +01:00
package org.gcube.portlet.user.userstatisticsportlet.server ;
2016-05-10 18:40:55 +02:00
import java.io.Serializable ;
2015-12-09 14:34:41 +01:00
import java.text.DecimalFormat ;
2016-05-10 18:40:55 +02:00
import java.util.Calendar ;
2015-12-09 14:34:41 +01:00
import java.util.List ;
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
import org.gcube.common.homelibrary.home.HomeLibrary ;
import org.gcube.common.homelibrary.home.workspace.Workspace ;
2016-05-20 15:12:01 +02:00
import org.gcube.common.portal.PortalContext ;
2016-03-31 18:22:06 +02:00
import org.gcube.portal.custom.communitymanager.SiteManagerUtil ;
2015-12-09 14:34:41 +01:00
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
import org.gcube.portal.databook.server.DatabookStore ;
import org.gcube.portal.databook.shared.Feed ;
import org.gcube.portlet.user.userstatisticsportlet.client.UserStatisticsService ;
2016-05-23 17:45:17 +02:00
import org.gcube.portlet.user.userstatisticsportlet.server.cache.UserInfrastructureSpaceCache ;
2015-12-09 14:34:41 +01:00
import org.gcube.portlet.user.userstatisticsportlet.shared.PostsStatsBean ;
import org.gcube.portlet.user.userstatisticsportlet.shared.UserInformation ;
2016-04-27 12:50:56 +02:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
2016-05-10 18:40:55 +02:00
import org.gcube.vomanagement.usermanagement.UserManager ;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault ;
2016-04-27 12:50:56 +02:00
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
2016-05-10 18:40:55 +02:00
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.util.ManagementUtils ;
2015-12-09 14:34:41 +01:00
import org.slf4j.LoggerFactory ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2016-07-11 16:02:37 +02:00
import com.liferay.portal.kernel.cache.CacheRegistryUtil ;
2015-12-09 14:34:41 +01:00
import com.liferay.portal.model.Contact ;
import com.liferay.portal.model.User ;
import com.liferay.portal.model.Website ;
2016-05-10 18:40:55 +02:00
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 ;
2015-12-09 14:34:41 +01:00
import com.liferay.portal.service.UserLocalServiceUtil ;
import com.liferay.portal.service.WebsiteLocalServiceUtil ;
2016-05-10 18:40:55 +02:00
import com.liferay.portlet.expando.model.ExpandoColumnConstants ;
2015-12-09 14:34:41 +01:00
/ * *
* The server side implementation of the RPC service .
* /
@SuppressWarnings ( " serial " )
public class UserStatisticsServiceImpl extends RemoteServiceServlet implements UserStatisticsService {
// Logger
private static final org . slf4j . Logger _log = LoggerFactory . getLogger ( UserStatisticsServiceImpl . class ) ;
//dev user
2016-05-10 18:40:55 +02:00
public static final String defaultUserId = " test.user " ;
2015-12-09 14:34:41 +01:00
//dev vre
private static final String vreID = " /gcube/devsec/devVRE " ;
// Cassandra connection
private DatabookStore store ;
2016-05-10 18:40:55 +02:00
// 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 " ;
2015-12-09 14:34:41 +01:00
@Override
public void init ( ) {
// get connection to Cassandra
2016-02-29 12:11:23 +01:00
_log . debug ( " Getting connection to Cassandra.. " ) ;
2015-12-09 14:34:41 +01:00
store = new DBCassandraAstyanaxImpl ( ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
// add statistics option for profile pages and set to true
createUserCustomField ( CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY , true ) ;
2015-12-09 14:34:41 +01:00
}
@Override
public void destroy ( ) {
// shutting down connection to Cassandra
2016-07-11 10:54:38 +02:00
_log . info ( " Closing connection to Cassandra " ) ;
2015-12-09 14:34:41 +01:00
store . closeConnection ( ) ;
}
/ * *
* the current ASLSession
* @return the session
* /
private ASLSession getASLSession ( ) {
String sessionID = this . getThreadLocalRequest ( ) . getSession ( ) . getId ( ) ;
String user = ( String ) this . getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( user = = null ) {
2016-05-10 18:40:55 +02:00
_log . warn ( " USER IS NULL setting " + defaultUserId + " and Running OUTSIDE PORTAL " ) ;
2015-12-09 14:34:41 +01:00
user = getDevelopmentUser ( ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setScope ( vreID ) ;
}
return SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) ;
}
/ * *
* Indicates whether the scope is the whole infrastructure .
2016-05-12 10:29:44 +02:00
* @param userid in case userid is not null , the user is visiting a profile page and
* the statistics to return are the ones available in the whole infrastructure
2015-12-09 14:34:41 +01:00
* @return < code > true < / code > if it is , < code > false < / code > otherwise .
* /
2016-05-12 10:29:44 +02:00
private boolean isInfrastructureScope ( String userid ) {
2015-12-09 14:34:41 +01:00
boolean toReturn = false ;
try {
2016-04-27 12:50:56 +02:00
GroupManager manager = new LiferayGroupManager ( ) ;
long groupId = manager . getGroupIdFromInfrastructureScope ( getASLSession ( ) . getScope ( ) ) ;
2016-05-12 10:29:44 +02:00
toReturn = ! manager . isVRE ( groupId ) | | userid ! = null ;
2015-12-09 14:34:41 +01:00
return toReturn ;
}
2016-04-27 12:50:56 +02:00
catch ( Exception e ) {
2015-12-09 14:34:41 +01:00
_log . error ( " NullPointerException in isInfrastructureScope returning false " ) ;
return false ;
}
}
/ * *
* Online or in development mode ?
* @return true if you ' re running into the portal , false if in development
* /
private boolean isWithinPortal ( ) {
try {
UserLocalServiceUtil . getService ( ) ;
return true ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
_log . trace ( " Development Mode ON " ) ;
return false ;
}
}
/ * *
* when packaging test will fail if the user is not set to test . user
* @return .
* /
public String getDevelopmentUser ( ) {
2016-05-10 18:40:55 +02:00
String user = defaultUserId ;
2016-04-11 11:41:33 +02:00
// user = "costantino.perciante";
2015-12-09 14:34:41 +01:00
return user ;
}
@Override
2016-05-10 18:40:55 +02:00
public String getTotalSpaceInUse ( String userid ) {
2015-12-09 14:34:41 +01:00
String storageInUse = null ;
// get the session
ASLSession session = getASLSession ( ) ;
2016-05-10 18:40:55 +02:00
// username in the session
2015-12-09 14:34:41 +01:00
String userName = session . getUsername ( ) ;
2016-05-10 18:40:55 +02:00
// retrieve statistics of...
String statisticsOfUsername = userName ;
if ( userid ! = null & & ! userid . equals ( userName ) )
statisticsOfUsername = userid ;
2015-12-09 14:34:41 +01:00
//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
2016-05-10 18:40:55 +02:00
if ( userName . compareTo ( defaultUserId ) = = 0 ) {
2015-12-09 14:34:41 +01:00
_log . debug ( " Found " + userName + " returning nothing " ) ;
return null ;
2016-04-06 15:42:41 +02:00
2015-12-09 14:34:41 +01:00
} else {
2016-05-10 18:40:55 +02:00
_log . debug ( " Getting " + statisticsOfUsername + " amount of workspace in use. " ) ;
2015-12-09 14:34:41 +01:00
try {
2016-04-06 15:42:41 +02:00
2015-12-09 14:34:41 +01:00
long init = System . currentTimeMillis ( ) ;
2016-04-06 15:42:41 +02:00
2016-05-23 17:45:17 +02:00
// retrieve the cache
UserInfrastructureSpaceCache cacheWorkspace = UserInfrastructureSpaceCache . getCacheInstance ( ) ;
// check if information is present
Long storageInUseLong = ( Long ) cacheWorkspace . get ( statisticsOfUsername ) ;
// if not, ask the workspace
if ( storageInUseLong = = null ) {
_log . debug ( " Information not available in the cache, asking workspace " ) ;
Workspace workspace = HomeLibrary . getUserWorkspace ( statisticsOfUsername ) ;
storageInUseLong = workspace . getDiskUsage ( ) ;
_log . debug ( " Put information in the cache " ) ;
cacheWorkspace . insert ( statisticsOfUsername , storageInUseLong ) ;
}
storageInUse = formatFileSize ( storageInUseLong ) ;
2016-04-06 15:42:41 +02:00
2015-12-09 14:34:41 +01:00
long end = System . currentTimeMillis ( ) ;
2016-02-29 12:11:23 +01:00
_log . debug ( " [USER-STATISTICS] time taken to retrieve user space is " + ( end - init ) + " ms " ) ;
2015-12-09 14:34:41 +01:00
} catch ( Exception e ) {
_log . error ( " Unable to retrieve workspace information! " ) ;
}
}
return storageInUse ;
}
@Override
2016-05-10 18:40:55 +02:00
public int getProfileStrength ( String userid ) {
2015-12-09 14:34:41 +01:00
int profileStrenght = - 1 ;
// get the session
ASLSession session = getASLSession ( ) ;
2016-05-10 18:40:55 +02:00
// username
2015-12-09 14:34:41 +01:00
String userName = session . getUsername ( ) ;
2016-05-10 18:40:55 +02:00
// retrieve statistics of...
String statisticsOfUsername = userName ;
if ( userid ! = null & & ! userid . equals ( userName ) )
statisticsOfUsername = userid ;
2015-12-09 14:34:41 +01:00
//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
2016-05-10 18:40:55 +02:00
if ( userName . compareTo ( defaultUserId ) = = 0 ) {
2015-12-09 14:34:41 +01:00
_log . debug ( " Found " + userName + " returning nothing " ) ;
return profileStrenght ;
} else {
// valuate profile strength
if ( isWithinPortal ( ) ) {
try {
2016-05-23 17:45:17 +02:00
long init = System . currentTimeMillis ( ) ;
2015-12-09 14:34:41 +01:00
// check if the avatar is present
2016-05-10 18:40:55 +02:00
boolean avatarPresent = ( new LiferayUserManager ( ) . getUserAvatarBytes ( statisticsOfUsername ) ! = null ) ;
2015-12-09 14:34:41 +01:00
2016-05-10 18:40:55 +02:00
User user = UserLocalServiceUtil . getUserByScreenName ( SiteManagerUtil . getCompany ( ) . getCompanyId ( ) , statisticsOfUsername ) ;
2015-12-09 14:34:41 +01:00
profileStrenght = evaluateProfileStrenght ( user , avatarPresent ) ;
2016-04-06 15:42:41 +02:00
2015-12-09 14:34:41 +01:00
long end = System . currentTimeMillis ( ) ;
2016-02-29 12:11:23 +01:00
_log . debug ( " [USER-STATISTICS] time taken to evaluate user profile strenght is " + ( end - init ) + " ms " ) ;
2015-12-09 14:34:41 +01:00
} catch ( Exception e ) {
2016-05-23 17:45:17 +02:00
_log . error ( " Profile strenght evaluation failed!! " + e . toString ( ) , e ) ;
2015-12-09 14:34:41 +01:00
}
}
}
return profileStrenght ;
}
@Override
2016-05-10 18:40:55 +02:00
public UserInformation getUserSettings ( String userid ) {
2015-12-09 14:34:41 +01:00
// get the session
ASLSession session = getASLSession ( ) ;
2016-05-10 18:40:55 +02:00
//username of the asl session
2015-12-09 14:34:41 +01:00
String userName = session . getUsername ( ) ;
2016-05-10 18:40:55 +02:00
// 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 ) ;
2016-07-11 16:02:37 +02:00
_log . info ( " Is profile showable for user " + userid + " " + isProfileShowable ) ;
2016-05-10 18:40:55 +02:00
}
2015-12-09 14:34:41 +01:00
//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
2016-05-10 18:40:55 +02:00
if ( userName . compareTo ( defaultUserId ) = = 0 ) {
2015-12-09 14:34:41 +01:00
_log . debug ( " Found " + userName + " returning nothing " ) ;
return null ;
}
if ( isWithinPortal ( ) ) {
2016-05-12 10:29:44 +02:00
// If the user is in the root panel (or is visiting a profile page), we have to send him the overall number of posts made, comments/likes(received) and the space in use.
2015-12-09 14:34:41 +01:00
// Otherwise we have to filter on the vre.
2016-05-12 10:29:44 +02:00
boolean isInfrastructure = isInfrastructureScope ( userid ) ;
2016-02-29 12:11:23 +01:00
_log . debug ( " User scope is " + ( isInfrastructure ? " the whole infrastucture " : " a VRE " ) ) ;
2015-12-09 14:34:41 +01:00
// get path of the avatar
2016-05-10 18:40:55 +02:00
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 ) ;
2015-12-09 14:34:41 +01:00
// get the vre (if not in the infrastructure)
String actualVre = null ;
if ( ! isInfrastructure ) {
String [ ] temp = session . getScope ( ) . split ( " / " ) ;
actualVre = temp [ temp . length - 1 ] ;
}
2016-05-23 17:45:17 +02:00
2016-05-20 15:12:01 +02:00
// page landing
String pageLanding = PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( getThreadLocalRequest ( ) ) ;
2015-12-09 14:34:41 +01:00
2016-05-20 15:12:01 +02:00
UserInformation bean = new UserInformation ( isInfrastructure , thumbnailURL , userName , actualVre , isOwner , isProfileShowable ) ;
bean . setCurrentPageLanding ( pageLanding ) ;
return bean ;
2016-05-10 18:40:55 +02:00
}
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 ( ) ;
2016-07-11 16:02:37 +02:00
//needed to avoid cache use by liferay API
CacheRegistryUtil . clear ( ) ;
2016-05-10 18:40:55 +02:00
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 ;
2015-12-09 14:34:41 +01:00
}
}
@Override
2016-05-10 18:40:55 +02:00
public PostsStatsBean getPostsStats ( String userid ) {
2016-04-06 15:42:41 +02:00
2015-12-09 14:34:41 +01:00
// get the session
ASLSession session = getASLSession ( ) ;
//username
String userName = session . getUsername ( ) ;
2016-05-10 18:40:55 +02:00
// retrieve statistics of...
String statisticsOfUsername = userName ;
if ( userid ! = null & & ! userid . equals ( userName ) )
statisticsOfUsername = userid ;
2015-12-09 14:34:41 +01:00
//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
2016-05-10 18:40:55 +02:00
if ( userName . compareTo ( defaultUserId ) = = 0 ) {
2015-12-09 14:34:41 +01:00
_log . debug ( " Found " + userName + " returning nothing " ) ;
return null ;
}
long totalFeeds = 0 , totalLikes = 0 , totalComments = 0 ;
// check if the user is or not in a VRE
2016-05-12 10:29:44 +02:00
boolean isInfrastructure = isInfrastructureScope ( userid ) ;
2015-12-09 14:34:41 +01:00
// date corresponding to one year ago
2016-05-10 18:40:55 +02:00
Calendar oneYearAgo = Calendar . getInstance ( ) ;
oneYearAgo . set ( Calendar . YEAR , oneYearAgo . get ( Calendar . YEAR ) - 1 ) ;
2015-12-09 14:34:41 +01:00
2016-05-10 18:40:55 +02:00
_log . debug ( " Reference time is " + oneYearAgo . getTime ( ) ) ;
2015-12-09 14:34:41 +01:00
try {
long init = System . currentTimeMillis ( ) ;
2016-05-10 18:40:55 +02:00
_log . debug ( " Getting " + statisticsOfUsername + " feeds in the last year. " ) ;
2015-12-09 14:34:41 +01:00
2016-05-10 18:40:55 +02:00
List < Feed > userFeeds = store . getRecentFeedsByUserAndDate ( statisticsOfUsername , oneYearAgo . getTime ( ) . getTime ( ) ) ;
2015-12-09 14:34:41 +01:00
2016-05-10 18:40:55 +02:00
_log . debug ( " Evaluating number of comments and likes of " + statisticsOfUsername + " 's feeds. " ) ;
2015-12-09 14:34:41 +01:00
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 ;
// increment feeds number
totalFeeds + + ;
//increment number of post replies and likes
totalComments + = Integer . parseInt ( feed . getCommentsNo ( ) ) ;
totalLikes + = Integer . parseInt ( feed . getLikesNo ( ) ) ;
} catch ( NumberFormatException e ) {
_log . error ( e . toString ( ) ) ;
}
}
long end = System . currentTimeMillis ( ) ;
2016-04-06 15:42:41 +02:00
2016-02-29 12:11:23 +01:00
_log . debug ( " [USER-STATISTICS] time taken to retrieve and filter user feeds, get likes and replies got is " + ( end - init ) + " ms " ) ;
2016-05-10 18:40:55 +02:00
_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 ) ;
2015-12-09 14:34:41 +01:00
} catch ( Exception e ) {
_log . error ( e . toString ( ) ) ;
return null ;
}
// return the object
return new PostsStatsBean ( totalFeeds , totalLikes , totalComments ) ;
}
2016-05-10 18:40:55 +02:00
@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 ( ) ;
2016-07-11 16:02:37 +02:00
//needed to avoid cache use by liferay API
CacheRegistryUtil . clear ( ) ;
2016-05-10 18:40:55 +02:00
_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 ) ;
}
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
} catch ( Exception e ) {
_log . error ( " Unable to check user's privacy for his statistics " , e ) ;
}
}
}
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
/ * *
* On servlet instanciation , create the custom field and set it to startingValue
* @param customFieldNameUserStatisticsVisibility
* @param b
* /
private void createUserCustomField (
String customFieldNameUserStatisticsVisibility , boolean startingValue ) {
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
// set permission checker
setPermissionChecker ( ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
try {
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
User defaultUser = UserLocalServiceUtil . getDefaultUser ( ManagementUtils . getCompany ( ) . getCompanyId ( ) ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
// check if it exists
boolean exists = defaultUser . getExpandoBridge ( ) . hasAttribute ( customFieldNameUserStatisticsVisibility ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
if ( exists ) {
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
_log . debug ( " Custom field already exists... There is no need to create it " ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
} else {
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
_log . debug ( " Creating custom field " + customFieldNameUserStatisticsVisibility +
" with starting value " + startingValue ) ;
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
// create
defaultUser . getExpandoBridge ( ) . addAttribute ( CUSTOM_FIELD_NAME_USER_STATISTICS_VISIBILITY , ExpandoColumnConstants . BOOLEAN , ( Serializable ) ( true ) ) ;
}
2016-05-23 17:45:17 +02:00
2016-05-10 18:40:55 +02:00
} catch ( Exception e ) {
_log . error ( " Unable to create custom field " + customFieldNameUserStatisticsVisibility ) ;
}
}
2015-12-09 14:34:41 +01:00
/ * *
* returns dynamically the formated size .
*
* @param size the size
* @return the string
* /
private static String formatFileSize ( long size ) {
String formattedSize = null ;
double b = size ;
double k = size / 1024 . 0 ;
double m = ( ( size / 1024 . 0 ) / 1024 . 0 ) ;
double g = ( ( ( size / 1024 . 0 ) / 1024 . 0 ) / 1024 . 0 ) ;
double t = ( ( ( ( size / 1024 . 0 ) / 1024 . 0 ) / 1024 . 0 ) / 1024 . 0 ) ;
DecimalFormat dec = new DecimalFormat ( " 0.00 " ) ;
if ( t > = 1 . 0 ) {
formattedSize = dec . format ( t ) . concat ( " TB " ) ;
} else if ( g > = 1 . 0 ) {
formattedSize = dec . format ( g ) . concat ( " GB " ) ;
} else if ( m > = 1 . 0 ) {
formattedSize = dec . format ( m ) . concat ( " MB " ) ;
} else if ( k > = 1 . 0 ) {
formattedSize = dec . format ( k ) . concat ( " KB " ) ;
} else {
formattedSize = dec . format ( b ) . concat ( " Bytes " ) ;
}
return formattedSize ;
}
/ * *
* Evaluates the profile strenght of the user
* @param user
* @return a int in [ 0 , 100 ]
* /
private static int evaluateProfileStrenght ( User user , boolean imageIsPresent ) {
int score = evaluateContactScore ( user ) ;
score + = evaluateInformationScore ( user , imageIsPresent ) ;
return score ;
}
/ * *
* Evaluates a score according to the information of the user such as job , organization , comments
* @param user
* @return a score in [ 0 , 65 ]
* /
private static int evaluateInformationScore ( User user , boolean imageIsPresent ) {
int score = 0 ;
2016-05-23 17:45:17 +02:00
if ( user . getJobTitle ( ) ! = null )
score + = ! user . getJobTitle ( ) . isEmpty ( ) ? 20 : 0 ;
if ( user . getOpenId ( ) ! = null )
score + = ! user . getOpenId ( ) . isEmpty ( ) ? 20 : 0 ;
2015-12-09 14:34:41 +01:00
String summary = getSummary ( user ) ;
2016-05-23 17:45:17 +02:00
if ( summary ! = null ) {
int lenght = summary . replace ( " " , " " ) . length ( ) ;
float partialScore = ( ( float ) lenght / 10 . 0f ) ;
score + = partialScore > 20f ? 20 : ( int ) partialScore ;
}
2015-12-09 14:34:41 +01:00
if ( imageIsPresent )
score + = 5 ;
return score ;
}
/ * *
* get the user ' s comment
* @param user
* @return
* /
private static String getSummary ( User user ) {
2016-05-23 17:45:17 +02:00
if ( user . getComments ( ) ! = null ) {
String toReturn = escapeHtml ( user . getComments ( ) ) ;
// replace all the line breaks by <br/>
toReturn = toReturn . replaceAll ( " ( \ r \ n| \ n) " , " <br/> " ) ;
// then replace all the double spaces by the html version
toReturn = toReturn . replaceAll ( " \\ s \\ s " , " " ) ;
return toReturn ;
} else
return null ;
2015-12-09 14:34:41 +01:00
}
/ * *
* Escape an html string . Escaping data received from the client helps to
* prevent cross - site script vulnerabilities .
*
* @param html the html string to escape
* @return the escaped string
* /
private static String escapeHtml ( String html ) {
if ( html = = null ) {
return null ;
}
return html . replaceAll ( " & " , " & " ) . replaceAll ( " < " , " < " )
. replaceAll ( " > " , " > " ) ;
}
/ * *
* Evaluates user ' s contact information
* @param user
* @return a value in [ 0 , 35 ]
* /
private static int evaluateContactScore ( User user ) {
int score = 0 ;
try {
Contact contact = user . getContact ( ) ;
2016-05-23 17:45:17 +02:00
if ( contact . getMySpaceSn ( ) ! = null )
score + = ! contact . getMySpaceSn ( ) . isEmpty ( ) ? 5 : 0 ;
if ( contact . getTwitterSn ( ) ! = null )
score + = ! contact . getTwitterSn ( ) . isEmpty ( ) ? 5 : 0 ;
if ( contact . getFacebookSn ( ) ! = null )
score + = ! contact . getFacebookSn ( ) . isEmpty ( ) ? 5 : 0 ;
if ( contact . getSkypeSn ( ) ! = null )
score + = ! contact . getSkypeSn ( ) . isEmpty ( ) ? 5 : 0 ;
if ( contact . getJabberSn ( ) ! = null )
score + = ! contact . getJabberSn ( ) . isEmpty ( ) ? 5 : 0 ;
if ( contact . getAimSn ( ) ! = null )
score + = ! contact . getAimSn ( ) . isEmpty ( ) ? 5 : 0 ;
2015-12-09 14:34:41 +01:00
List < Website > websites = WebsiteLocalServiceUtil . getWebsites ( user . getCompanyId ( ) , " com.liferay.portal.model.Contact " , contact . getContactId ( ) ) ;
score + = websites . size ( ) > 0 ? 5 : 0 ;
} catch ( Exception e ) {
_log . error ( " Contact profile score evaluation failed!! " ) ;
score = 0 ;
}
return score ;
}
2016-05-10 18:40:55 +02:00
/ * *
* 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... " ) ;
}
}
2015-12-09 14:34:41 +01:00
}