package org.gcube.applicationsupportlayer.social; import java.util.Date; import java.util.UUID; import org.gcube.application.framework.core.session.ASLSession; import org.gcube.common.core.utils.logging.GCUBEClientLog; import org.gcube.portal.databook.shared.ApplicationProfile; import org.gcube.portal.databook.shared.Notification; import org.gcube.portal.databook.shared.NotificationType; import org.gcube.portal.databook.shared.RunningJob; import org.gcube.portlets.user.homelibrary.home.exceptions.InternalErrorException; import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceFolder; import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceItem; import org.gcube.vomanagement.usermanagement.UserManager; import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault; import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.UserModel; /** * * @author Massimiliano Assante, ISTI-CNR * @version 0.1 Dec 2012 * * use to notify users from within your application */ public class ApplicationNotificationsManager extends SocialPortalBridge implements NotificationsManager { static GCUBEClientLog _log = new GCUBEClientLog(ApplicationNotificationsManager.class); /** * Use this constructor if you do not need notifications to point back to your applications * @param aslSession the ASLSession instance */ public ApplicationNotificationsManager(ASLSession session) { super(session); } /** * Use this constructor if you do need notifications to point back to your applications, * make sure you create your application profile on the infrastructure. * * @see http://gcube.wiki.gcube-system.org/gcube/index.php/Social_Networking_Library#Create_Your_Application_Profile * * @param aslSession the ASLSession instance * @param portletClassName your portlet class name will be used ad unique identifier for your applicationProfile */ public ApplicationNotificationsManager(ASLSession session, String portletClassName) { super(session, portletClassName); } /** * actually save the notification to the store * @param notification2Save the notification instance to save * @return true if the notification was sent ok */ private boolean saveNotification(Notification notification2Save) { _log.trace("Trying to send notification to: " + notification2Save.getUserid() + " Type: " + notification2Save.getType()); boolean result = getStoreInstance().saveNotification(notification2Save); if (result) _log.trace("Notification Saved Successfully! "); else _log.error("While trying to save Notification"); return result; } /** * return the url of the application if exists in the profile * @return . */ private String getApplicationUrl() { if (applicationProfile != null && applicationProfile.getUrl() != null) return applicationProfile.getUrl(); else return ""; } /** * {@inheritDoc} * @throws InternalErrorException */ @Override public boolean notifyFolderSharing(String userIdToNotify, WorkspaceFolder sharedFolder) throws InternalErrorException { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_FOLDER_SHARE, userIdToNotify, //user no notify sharedFolder.getId(), //the new Date(), "?oid="+sharedFolder.getId(), "has shared a workspace folder ("+ sharedFolder.getName() +") with you", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} * @throws UserManagementPortalException * @throws UserRetrievalFault * @throws UserManagementSystemException */ @Override public boolean notifyFolderAddedUser(String userIdToNotify, WorkspaceFolder sharedFolder, String newAddedUserId) throws InternalErrorException, UserManagementSystemException, UserRetrievalFault, UserManagementPortalException { UserManager um = new LiferayUserManager(); UserModel user = um.getUserByScreenName(newAddedUserId); Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_FOLDER_ADDEDUSER, userIdToNotify, //user no notify sharedFolder.getId(), //the new Date(), "?oid="+sharedFolder.getId(), "has added a new user ("+ user.getFullname() +") on your workspace shared folder ("+ sharedFolder.getName() +")", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyFolderRemovedUser(String userIdToNotify, WorkspaceFolder sharedFolder, String removedUserId) throws InternalErrorException, UserManagementSystemException, UserRetrievalFault, UserManagementPortalException { UserManager um = new LiferayUserManager(); UserModel user = um.getUserByScreenName(removedUserId); Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_FOLDER_REMOVEDUSER, userIdToNotify, //user no notify sharedFolder.getId(), //the new Date(), "?oid="+sharedFolder.getId(), "has removed a user ("+ user.getFullname() +") from your workspace shared folder ("+ sharedFolder.getName() +")", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} * @throws InternalErrorException */ @Override public boolean notifyAddedItem(String userIdToNotify, WorkspaceItem item) throws InternalErrorException { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_ITEM_NEW, userIdToNotify, //user no notify item.getId(), //the new Date(), "?oid="+item.getId()+"&parentoid="+item.getParent().getId(), "has added a new item ("+ item.getName() +") on your workspace shared folder ("+ item.getParent().getName() +")", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} * @throws InternalErrorException */ @Override public boolean notifyRemovedItem(String userIdToNotify, WorkspaceItem item) throws InternalErrorException { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_ITEM_DELETE, userIdToNotify, //user no notify item.getId(), //the new Date(), "?oid="+item.getId()+"&parentoid="+item.getParent().getId(), "has removed an item ("+ item.getName() +") from your workspace shared folder ("+ item.getParent().getName() +")", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} * @throws InternalErrorException */ @Override public boolean notifyUpdatedItem(String userIdToNotify, WorkspaceItem item) throws InternalErrorException { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.WP_ITEM_UPDATED, userIdToNotify, //user no notify "?oid="+item.getId()+"&parentoid="+item.getParent().getId(), new Date(), "?folder="+item.getParent().getId()+"&item="+item.getId(), "has updated an item ("+ item.getName() +") on your workspace shared folder ("+ item.getParent().getName() +")", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyMessageReceived(String userIdToNotify, String subject) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.MESSAGE, userIdToNotify, //user no notify "messageid_not_provided", //the new Date(), "", "has sent you a message with subject: " + escapeHtml(subject), false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyOwnCommentReply(String userIdToNotify, String feedid, String feedText) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.OWN_COMMENT, userIdToNotify, //user no notify feedid, //the post new Date(), "?oid="+feedid, "commented on your post: " + escapeHtml(feedText), false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyCommentReply(String userIdToNotify, String feedid, String feedText) { //TODO: missing implementation return false; } /** * {@inheritDoc} */ @Override public boolean notifyLikedFeed(String userIdToNotify, String feedid, String feedText) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.LIKE, userIdToNotify, //user no notify feedid, //the post new Date(), "?oid="+feedid, "likes your post: " + escapeHtml(feedText), false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyJobStatus(String userIdToNotify, ApplicationProfile executingJobApId, RunningJob job) { //TODO: missing implementation return false; } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowView(String userIdToNotify, String documentWorkflowId, String documentName) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_VIEW, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, "has viewed a document workflow you created (" + escapeHtml(documentName) + ").", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowUpdate(String userIdToNotify, String documentWorkflowId, String documentName) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_EDIT, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, "has edited a document workflow you created (" + escapeHtml(documentName) + ").", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowTaskRequest(String userIdToNotify, String documentWorkflowId, String documentName, String assignedRoleName) { String notificationText = "You are requested to perform a new task in the Document Workflow (" + escapeHtml(documentName) + "). " + "Your role is: " + assignedRoleName +". On " + aslSession.getGroupName() + " Virtual Research Environment."; Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_STEP_REQUEST_TASK, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, notificationText, false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowUserForward(String userIdToNotify, String documentWorkflowId, String documentName, String fromStepName, String toStepName){ Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_USER_FORWARD_TO_OWNER, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, "has forwarded a document workflow you created (" + escapeHtml(documentName) + ") " + "from step " + fromStepName + " towards step " + toStepName +". On " + aslSession.getGroupName() + " Virtual Research Environment. ", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowStepForwardComplete(String userIdToNotify, String documentWorkflowId, String documentName, String fromStepName, String toStepName) { Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_FORWARD_STEP_COMPLETED_OWNER, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, "has performed the last needed forward on a document workflow you created (" + escapeHtml(documentName) + "). " + "Consequently, this Document Workflow moved from step " + fromStepName + " to step " + toStepName +". On " + aslSession.getGroupName() + " Virtual Research Environment. ", false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } /** * {@inheritDoc} */ @Override public boolean notifyDocumentWorkflowFirstStepRequest(String userIdToNotify, String documentWorkflowId, String documentName, String assignedRole) { String notificationText = "has involved you in a Document Workflow (" + escapeHtml(documentName) + ") " + "and has assigned you the role: " + assignedRole +". On " + aslSession.getGroupName() + " Virtual Research Environment."; Notification not = new Notification( UUID.randomUUID().toString(), NotificationType.DOCUMENT_WORKFLOW_FIRST_STEP_REQUEST_INVOLVMENT, userIdToNotify, //user no notify documentWorkflowId, //the workflowid new Date(), getApplicationUrl()+"?oid="+documentWorkflowId, notificationText, false, aslSession.getUsername(), aslSession.getUserFullName(), aslSession.getUserAvatarId()); return saveNotification(not); } }