Subscribe to Add / Update / Remove Folder in Liferay 6.2

Hi, I want to implement a folder subscription feature in the Liferay documents and media portlet .

The current functionality provided by liferay is such that if you subscribe to any folder / parent folder, you will receive email to add / update files, but not folders.

So, if you add subfolders to the signed folder, you won't receive any emails.

Any help on this is appreciated.

+3


source to share


1 answer


The hasPermission method on SubscriptionPermissionImpl allows validation only for MBDiscussion, BlogsEntry, BookmarksEntry, DLFileEntry, JournalArticle, MBCategory, MBThread, WikiPage

Check following SubscriptionPermissionImpl.java method

protected Boolean hasPermission(
            PermissionChecker permissionChecker, String className, long classPK,
            String actionId)
        throws PortalException, SystemException {...}

      

So when implementing a custom solution , when we say subscriptionSender.flushNotificationsAsync () will not send email for folders as hasPermisson returns false .

And since Liferay doesn't support email subscriptions on add / update / delete folders, we had to implement our own solution.

So, follow these steps to achieve this functionality.

1) Create a binding and override DLAppService for addFolder , updateFolder and moveFolderToTrash methods . Here I will show you how to do this for adding a folder subscription, the rest is the same.



@Override
    public Folder addFolder(long repositoryId, long parentFolderId,
        String name, String description, ServiceContext serviceContext)
        throws PortalException, SystemException {

    Folder folder = null;

    folder =
        super
            .addFolder(
                repositoryId, parentFolderId, name, description,
                serviceContext);

    MySubscriptionUtil.notifySubscribersForFolders(
        folder, serviceContext, MyDLConstants.EVENT_TYPE_ADD);

    return folder;
}

      

2) Here MySubcriptionUtil is a utility class generated by static methods.

Now in notifySubscribersForFolders use the folder object and then create a subscriptionSender object setting the required details. eg:

SubscriptionSender subscriptionSender = new SubscriptionSender();

subscriptionSender.setCompanyId(folder.getCompanyId());
subscriptionSender.setContextAttributes(
    "[$FOLDER_NAME$]", folder.getName(), "[$PARENT_FOLDER_NAME$]",
    parentFolderName, "[$SITE$]", group.getDescriptiveName());
subscriptionSender.setContextUserPrefix("FOLDER");
subscriptionSender.setFrom(fromAddress, fromName);
subscriptionSender.setHtmlFormat(true);
subscriptionSender.setBody(body);
subscriptionSender.setSubject(subject);
subscriptionSender.setMailId("folder", folder.getFolderId());
subscriptionSender.setPortletId(PortletKeys.DOCUMENT_LIBRARY);
subscriptionSender.setReplyToAddress(fromAddress);
subscriptionSender.setScopeGroupId(folder.getGroupId());
subscriptionSender.setServiceContext(serviceContext);
subscriptionSender.setUserId(folder.getUserId());

      

3) Now instead of addPersistedSubscribers () use addRuntimeSubscribers () , the reason is that this value is higher than ie due to a problem with checking permissions. Before doing this, select the folder subscription from the subscription table using Dynamic Query.

    List<Long> folderIds //set this list with all parents folder ids of the folder

    // adding group id for parent folder
    /*
    * This is the default parent of any folder.
    * So if user is subscribed to home folder then 
    * he will have entry in subscription table with classPK as groupId
    */
    folderIds.add(folder.getGroupId());

    long classNameId = PortalUtil.getClassNameId(Folder.class.getName());

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(Subscription.class);
    dynamicQuery.add(RestrictionsFactoryUtil.in("classPK", folderIds));
    dynamicQuery
        .add(RestrictionsFactoryUtil.eq("classNameId", classNameId));

    List<Subscription> subscriptionList =
        SubscriptionLocalServiceUtil.dynamicQuery(dynamicQuery);

    for(Subscription subscription : subscriptionList) {
        try {
            User user =
                UserLocalServiceUtil.getUser(subscription.getUserId());

            if(user.isActive()) {

                if(_log.isDebugEnabled()) {
                    _log.debug("User added to subscription list : "
                        + user.getEmailAddress());
                }

                /*
                * This is the key method call as this adds all the 
                *users to whom we need to send an email.
                */
                subscriptionSender.addRuntimeSubscribers(
                    user.getEmailAddress(), user.getFirstName()
                        + StringPool.SPACE + user.getLastName());
            }
        }
        catch(Exception e) {
            _log.error("Exception occured while fetching user @_notifySubscribersForFolders : "
                + e.getMessage());
        }
    }

    //This is the last call which will trigger the send email event
    subscriptionSender.flushNotificationsAsync();

      

0


source







All Articles