001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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, false);
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, className, classPK, null, 0,
093                                            null, null, false, null, null, null, null, null,
094                                            String.valueOf(groupId), null, null, null, null, 0, 0, null,
095                                            false);
096                            }
097    
098                            // Social
099    
100                            if (className.equals(MBThread.class.getName())) {
101                                    MBThread mbThread = mbThreadLocalService.getMBThread(classPK);
102    
103                                    JSONObject extraDataJSONObject =
104                                            JSONFactoryUtil.createJSONObject();
105    
106                                    extraDataJSONObject.put("threadId", classPK);
107    
108                                    socialActivityLocalService.addActivity(
109                                            userId, groupId, MBMessage.class.getName(),
110                                            mbThread.getRootMessageId(),
111                                            SocialActivityConstants.TYPE_SUBSCRIBE,
112                                            extraDataJSONObject.toString(), 0);
113                            }
114                            else {
115                                    socialActivityLocalService.addActivity(
116                                            userId, groupId, className, classPK,
117                                            SocialActivityConstants.TYPE_SUBSCRIBE, StringPool.BLANK,
118                                            0);
119                            }
120                    }
121    
122                    return subscription;
123            }
124    
125            @Override
126            public void deleteSubscription(long subscriptionId)
127                    throws PortalException, SystemException {
128    
129                    Subscription subscription = subscriptionPersistence.fetchByPrimaryKey(
130                            subscriptionId);
131    
132                    deleteSubscription(subscription);
133            }
134    
135            public void deleteSubscription(
136                            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 void 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    
172            public void deleteSubscriptions(long userId)
173                    throws PortalException, SystemException {
174    
175                    List<Subscription> subscriptions = subscriptionPersistence.findByUserId(
176                            userId);
177    
178                    for (Subscription subscription : subscriptions) {
179                            deleteSubscription(subscription);
180                    }
181            }
182    
183            public void deleteSubscriptions(
184                            long companyId, String className, long classPK)
185                    throws PortalException, SystemException {
186    
187                    long classNameId = PortalUtil.getClassNameId(className);
188    
189                    List<Subscription> subscriptions = subscriptionPersistence.findByC_C_C(
190                            companyId, classNameId, classPK);
191    
192                    for (Subscription subscription : subscriptions) {
193                            deleteSubscription(subscription);
194                    }
195            }
196    
197            public Subscription getSubscription(
198                            long companyId, long userId, String className, long classPK)
199                    throws PortalException, SystemException {
200    
201                    long classNameId = PortalUtil.getClassNameId(className);
202    
203                    return subscriptionPersistence.findByC_U_C_C(
204                            companyId, userId, classNameId, classPK);
205            }
206    
207            public List<Subscription> getSubscriptions(
208                            long companyId, String className, long classPK)
209                    throws SystemException {
210    
211                    long classNameId = PortalUtil.getClassNameId(className);
212    
213                    return subscriptionPersistence.findByC_C_C(
214                            companyId, classNameId, classPK);
215            }
216    
217            public List<Subscription> getUserSubscriptions(
218                            long userId, int start, int end,
219                            OrderByComparator orderByComparator)
220                    throws SystemException {
221    
222                    return subscriptionPersistence.findByUserId(
223                            userId, start, end, orderByComparator);
224            }
225    
226            public List<Subscription> getUserSubscriptions(
227                            long userId, String className)
228                    throws SystemException {
229    
230                    long classNameId = PortalUtil.getClassNameId(className);
231    
232                    return subscriptionPersistence.findByU_C(userId, classNameId);
233            }
234    
235            public int getUserSubscriptionsCount(long userId) throws SystemException {
236                    return subscriptionPersistence.countByUserId(userId);
237            }
238    
239            public boolean isSubscribed(
240                            long companyId, long userId, String className, long classPK)
241                    throws SystemException {
242    
243                    long classNameId = PortalUtil.getClassNameId(className);
244    
245                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
246                            companyId, userId, classNameId, classPK);
247    
248                    if (subscription != null) {
249                            return true;
250                    }
251                    else {
252                            return false;
253                    }
254            }
255    
256    }