001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.subscription;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.messaging.BaseMessageListener;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.kernel.workflow.WorkflowInstance;
028    import com.liferay.portal.kernel.workflow.WorkflowInstanceManagerUtil;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.model.Subscription;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutLocalServiceUtil;
035    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
036    import com.liferay.portal.service.UserLocalServiceUtil;
037    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
038    import com.liferay.portlet.asset.model.AssetEntry;
039    import com.liferay.portlet.asset.model.AssetRendererFactory;
040    import com.liferay.portlet.messageboards.model.MBCategory;
041    import com.liferay.portlet.messageboards.model.MBThread;
042    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
043    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
044    import com.liferay.portlet.wiki.model.WikiNode;
045    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
046    
047    import java.io.Serializable;
048    
049    import java.util.List;
050    import java.util.Map;
051    
052    /**
053     * @author Raymond Aug??
054     */
055    public class CleanUpSubscriptionMessageListener extends BaseMessageListener {
056    
057            @Override
058            protected void doReceive(Message message) throws Exception {
059                    long groupId = (Long)message.get("groupId");
060                    long[] userIds = (long[])message.get("userIds");
061    
062                    for (long userId : userIds) {
063                            User user = UserLocalServiceUtil.getUser(userId);
064    
065                            processUser(user, groupId);
066                    }
067            }
068    
069            protected long[] getGroupIds(List<Group> groups) {
070                    long[] groupIds = new long[groups.size()];
071    
072                    for (int i = 0; i < groups.size(); i++) {
073                            Group group = groups.get(i);
074    
075                            groupIds[i] = group.getGroupId();
076                    }
077    
078                    return groupIds;
079            }
080    
081            protected void processAssetEntry(
082                            Subscription subscription, long groupId, long[] groupIds)
083                    throws PortalException {
084    
085                    String className = subscription.getClassName();
086    
087                    AssetRendererFactory assetRendererFactory =
088                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
089                                    className);
090    
091                    if (assetRendererFactory != null) {
092                            AssetEntry assetEntry = assetRendererFactory.getAssetEntry(
093                                    className, subscription.getClassPK());
094    
095                            if ((assetEntry.getGroupId() == groupId) ||
096                                    !ArrayUtil.contains(
097                                            groupIds, assetEntry.getGroupId())) {
098    
099                                    SubscriptionLocalServiceUtil.deleteSubscription(
100                                            subscription.getSubscriptionId());
101                            }
102    
103                            return;
104                    }
105            }
106    
107            protected void processLayout(
108                            Subscription subscription, long groupId, long[] groupIds)
109                    throws PortalException {
110    
111                    Layout layout = LayoutLocalServiceUtil.fetchLayout(
112                            subscription.getClassPK());
113    
114                    if ((layout != null) &&
115                            ((layout.getGroupId() == groupId) ||
116                             !ArrayUtil.contains(groupIds, layout.getGroupId()))) {
117    
118                            SubscriptionLocalServiceUtil.deleteSubscription(
119                                    subscription.getSubscriptionId());
120                    }
121            }
122    
123            protected void processMBCategory(
124                            Subscription subscription, long groupId, long[] groupIds)
125                    throws PortalException {
126    
127                    MBCategory mbCategory = MBCategoryLocalServiceUtil.fetchMBCategory(
128                            subscription.getClassPK());
129    
130                    if ((mbCategory != null) &&
131                            ((mbCategory.getGroupId() == groupId) ||
132                             !ArrayUtil.contains(groupIds, mbCategory.getGroupId()))) {
133    
134                            SubscriptionLocalServiceUtil.deleteSubscription(
135                                    subscription.getSubscriptionId());
136    
137                            return;
138                    }
139    
140                    Group group = GroupLocalServiceUtil.fetchGroup(
141                            subscription.getClassPK());
142    
143                    if ((group != null) &&
144                            ((group.getGroupId() == groupId) ||
145                             !ArrayUtil.contains(groupIds, group.getGroupId()))) {
146    
147                            SubscriptionLocalServiceUtil.deleteSubscription(
148                                    subscription.getSubscriptionId());
149                    }
150            }
151    
152            protected void processMBThread(
153                            Subscription subscription, long groupId, long[] groupIds)
154                    throws PortalException {
155    
156                    MBThread mbThread = MBThreadLocalServiceUtil.fetchThread(
157                            subscription.getClassPK());
158    
159                    if ((mbThread != null) &&
160                            ((mbThread.getGroupId() == groupId) ||
161                             !ArrayUtil.contains(groupIds, mbThread.getGroupId()))) {
162    
163                            SubscriptionLocalServiceUtil.deleteSubscription(
164                                    subscription.getSubscriptionId());
165                    }
166            }
167    
168            protected void processSubscription(
169                            Subscription subscription, long groupId, long[] groupIds)
170                    throws PortalException {
171    
172                    String className = subscription.getClassName();
173    
174                    if (className.equals(Layout.class.getName())) {
175                            processLayout(subscription, groupId, groupIds);
176                    }
177                    else if (className.equals(MBCategory.class.getName())) {
178                            processMBCategory(subscription, groupId, groupIds);
179                    }
180                    else if (className.equals(MBThread.class.getName())) {
181                            processMBThread(subscription, groupId, groupIds);
182                    }
183                    else if (className.equals(WikiNode.class.getName())) {
184                            processWikiNode(subscription, groupId, groupIds);
185                    }
186                    else if (className.equals(WorkflowInstance.class.getName())) {
187                            processWorkflowInstance(subscription, groupId, groupIds);
188                    }
189                    else {
190                            processAssetEntry(subscription, groupId, groupIds);
191                    }
192    
193                    throw new PortalException();
194            }
195    
196            protected void processUser(User user, long groupId) throws PortalException {
197    
198                    // Get the list of groups the current user is still a member of and
199                    // verify that subscriptions outside those groups are automatically
200                    // removed as well
201    
202                    List<Group> groups = user.getMySiteGroups(true, QueryUtil.ALL_POS);
203    
204                    long[] groupIds = getGroupIds(groups);
205    
206                    List<Subscription> subscriptions =
207                            SubscriptionLocalServiceUtil.getUserSubscriptions(
208                                    user.getUserId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
209    
210                    for (Subscription subscription : subscriptions) {
211                            try {
212                                    processSubscription(subscription, groupId, groupIds);
213                            }
214                            catch (Exception e) {
215                                    if (_log.isWarnEnabled()) {
216                                            StringBundler sb = new StringBundler(8);
217    
218                                            sb.append("Subscription was not removed for class name ");
219                                            sb.append(subscription.getClassName());
220                                            sb.append(" with class PK ");
221                                            sb.append(subscription.getClassPK());
222                                            sb.append(" in group ");
223                                            sb.append(groupId);
224                                            sb.append(" for user ");
225                                            sb.append(subscription.getUserId());
226    
227                                            _log.warn(sb.toString());
228                                    }
229                            }
230                    }
231            }
232    
233            protected void processWikiNode(
234                            Subscription subscription, long groupId, long[] groupIds)
235                    throws PortalException {
236    
237                    WikiNode wikiNode = WikiNodeLocalServiceUtil.fetchWikiNode(
238                            subscription.getClassPK());
239    
240                    if ((wikiNode != null) &&
241                            ((wikiNode.getGroupId() == groupId) ||
242                             !ArrayUtil.contains(groupIds, wikiNode.getGroupId()))) {
243    
244                            SubscriptionLocalServiceUtil.deleteSubscription(
245                                    subscription.getSubscriptionId());
246                    }
247            }
248    
249            protected void processWorkflowInstance(
250                            Subscription subscription, long groupId, long[] groupIds)
251                    throws PortalException {
252    
253                    WorkflowInstance workflowInstance =
254                            WorkflowInstanceManagerUtil.getWorkflowInstance(
255                                    subscription.getCompanyId(), subscription.getClassPK());
256    
257                    Map<String, Serializable> workflowContext =
258                            workflowInstance.getWorkflowContext();
259    
260                    long workflowInstanceGroupId = GetterUtil.getLong(
261                            (String)workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
262    
263                    if ((workflowInstanceGroupId > 0) &&
264                            ((workflowInstanceGroupId == groupId) ||
265                             !ArrayUtil.contains(groupIds, workflowInstanceGroupId))) {
266    
267                            SubscriptionLocalServiceUtil.deleteSubscription(
268                                    subscription.getSubscriptionId());
269                    }
270            }
271    
272            private static Log _log = LogFactoryUtil.getLog(
273                    CleanUpSubscriptionMessageListener.class);
274    
275    }