001    /**
002     * Copyright (c) 2000-2012 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.OrderByComparator;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Subscription;
024    import com.liferay.portal.model.SubscriptionConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.asset.model.AssetEntry;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.social.model.SocialActivityConstants;
032    
033    import java.util.Date;
034    import java.util.List;
035    
036    /**
037     * @author Charles May
038     * @author Zsolt Berentey
039     */
040    public class SubscriptionLocalServiceImpl
041            extends SubscriptionLocalServiceBaseImpl {
042    
043            public Subscription addSubscription(
044                            long userId, long groupId, String className, long classPK)
045                    throws PortalException, SystemException {
046    
047                    return addSubscription(
048                            userId, groupId, className, classPK,
049                            SubscriptionConstants.FREQUENCY_INSTANT);
050            }
051    
052            public Subscription addSubscription(
053                            long userId, long groupId, String className, long classPK,
054                            String frequency)
055                    throws PortalException, SystemException {
056    
057                    // Subscription
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    long classNameId = PortalUtil.getClassNameId(className);
061                    Date now = new Date();
062    
063                    long subscriptionId = counterLocalService.increment();
064    
065                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
066                            user.getCompanyId(), userId, classNameId, classPK);
067    
068                    if (subscription == null) {
069                            subscription = subscriptionPersistence.create(subscriptionId);
070    
071                            subscription.setCompanyId(user.getCompanyId());
072                            subscription.setUserId(user.getUserId());
073                            subscription.setUserName(user.getFullName());
074                            subscription.setCreateDate(now);
075                            subscription.setModifiedDate(now);
076                            subscription.setClassNameId(classNameId);
077                            subscription.setClassPK(classPK);
078                            subscription.setFrequency(frequency);
079    
080                            subscriptionPersistence.update(subscription);
081                    }
082    
083                    if (groupId > 0) {
084    
085                            // Asset
086    
087                            try {
088                                    assetEntryLocalService.getEntry(className, classPK);
089                            }
090                            catch (Exception e) {
091                                    assetEntryLocalService.updateEntry(
092                                            userId, groupId, subscription.getCreateDate(),
093                                            subscription.getModifiedDate(), className, classPK, null, 0,
094                                            null, null, false, null, null, null, null,
095                                            String.valueOf(groupId), null, null, null, null, 0, 0, null,
096                                            false);
097                            }
098    
099                            // Social
100    
101                            if (className.equals(MBThread.class.getName())) {
102                                    MBThread mbThread = mbThreadLocalService.getMBThread(classPK);
103    
104                                    JSONObject extraDataJSONObject =
105                                            JSONFactoryUtil.createJSONObject();
106    
107                                    extraDataJSONObject.put("threadId", classPK);
108    
109                                    socialActivityLocalService.addActivity(
110                                            userId, groupId, MBMessage.class.getName(),
111                                            mbThread.getRootMessageId(),
112                                            SocialActivityConstants.TYPE_SUBSCRIBE,
113                                            extraDataJSONObject.toString(), 0);
114                            }
115                            else {
116                                    socialActivityLocalService.addActivity(
117                                            userId, groupId, className, classPK,
118                                            SocialActivityConstants.TYPE_SUBSCRIBE, StringPool.BLANK,
119                                            0);
120                            }
121                    }
122    
123                    return subscription;
124            }
125    
126            @Override
127            public Subscription deleteSubscription(long subscriptionId)
128                    throws PortalException, SystemException {
129    
130                    Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
131                            subscriptionId);
132    
133                    return deleteSubscription(subscription);
134            }
135    
136            public void deleteSubscription(long userId, String className, long classPK)
137                    throws PortalException, SystemException {
138    
139                    User user = userPersistence.findByPrimaryKey(userId);
140                    long classNameId = PortalUtil.getClassNameId(className);
141    
142                    Subscription subscription = subscriptionPersistence.findByC_U_C_C(
143                            user.getCompanyId(), userId, classNameId, classPK);
144    
145                    deleteSubscription(subscription);
146            }
147    
148            @Override
149            public Subscription deleteSubscription(Subscription subscription)
150                    throws PortalException, SystemException {
151    
152                    // Subscription
153    
154                    subscriptionPersistence.remove(subscription);
155    
156                    // Social
157    
158                    AssetEntry assetEntry = assetEntryPersistence.fetchByC_C(
159                            subscription.getClassNameId(), subscription.getClassPK());
160    
161                    if (assetEntry != null) {
162                            String className = PortalUtil.getClassName(
163                                    subscription.getClassNameId());
164    
165                            socialActivityLocalService.addActivity(
166                                    subscription.getUserId(), assetEntry.getGroupId(), className,
167                                    subscription.getClassPK(),
168                                    SocialActivityConstants.TYPE_UNSUBSCRIBE, StringPool.BLANK, 0);
169                    }
170    
171                    return subscription;
172            }
173    
174            public void deleteSubscriptions(long userId)
175                    throws PortalException, SystemException {
176    
177                    List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
178                            userId);
179    
180                    for (Subscription subscription : subscriptions) {
181                            deleteSubscription(subscription);
182                    }
183            }
184    
185            public void deleteSubscriptions(
186                            long companyId, String className, long classPK)
187                    throws PortalException, SystemException {
188    
189                    long classNameId = PortalUtil.getClassNameId(className);
190    
191                    List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
192                            companyId, classNameId, classPK);
193    
194                    for (Subscription subscription : subscriptions) {
195                            deleteSubscription(subscription);
196                    }
197            }
198    
199            public Subscription getSubscription(
200                            long companyId, long userId, String className, long classPK)
201                    throws PortalException, SystemException {
202    
203                    long classNameId = PortalUtil.getClassNameId(className);
204    
205                    return subscriptionPersistence.findByC_U_C_C(
206                            companyId, userId, classNameId, classPK);
207            }
208    
209            public List<Subscription> getSubscriptions(
210                            long companyId, String className, long classPK)
211                    throws SystemException {
212    
213                    long classNameId = PortalUtil.getClassNameId(className);
214    
215                    return subscriptionPersistence.findByC_C_C(
216                            companyId, classNameId, classPK);
217            }
218    
219            public List<Subscription> getUserSubscriptions(
220                            long userId, int start, int end,
221                            OrderByComparator orderByComparator)
222                    throws SystemException {
223    
224                    return subscriptionPersistence.findByUserId(
225                            userId, start, end, orderByComparator);
226            }
227    
228            public List<Subscription> getUserSubscriptions(
229                            long userId, String className)
230                    throws SystemException {
231    
232                    long classNameId = PortalUtil.getClassNameId(className);
233    
234                    return subscriptionPersistence.findByU_C(userId, classNameId);
235            }
236    
237            public int getUserSubscriptionsCount(long userId) throws SystemException {
238                    return subscriptionPersistence.countByUserId(userId);
239            }
240    
241            public boolean isSubscribed(
242                            long companyId, long userId, String className, long classPK)
243                    throws SystemException {
244    
245                    long classNameId = PortalUtil.getClassNameId(className);
246    
247                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
248                            companyId, userId, classNameId, classPK);
249    
250                    if (subscription != null) {
251                            return true;
252                    }
253                    else {
254                            return false;
255                    }
256            }
257    
258    }