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