001    /**
002     * Copyright (c) 2000-2010 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.model.Subscription;
020    import com.liferay.portal.model.SubscriptionConstants;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
023    import com.liferay.portal.util.PortalUtil;
024    
025    import java.util.Date;
026    import java.util.List;
027    
028    /**
029     * @author Charles May
030     */
031    public class SubscriptionLocalServiceImpl
032            extends SubscriptionLocalServiceBaseImpl {
033    
034            public Subscription addSubscription(
035                            long userId, String className, long classPK)
036                    throws PortalException, SystemException {
037    
038                    return addSubscription(
039                            userId, className, classPK,
040                            SubscriptionConstants.FREQUENCY_INSTANT);
041            }
042    
043            public Subscription addSubscription(
044                            long userId, String className, long classPK, String frequency)
045                    throws PortalException, SystemException {
046    
047                    User user = userPersistence.findByPrimaryKey(userId);
048                    long classNameId = PortalUtil.getClassNameId(className);
049                    Date now = new Date();
050    
051                    long subscriptionId = counterLocalService.increment();
052    
053                    Subscription subscription = subscriptionPersistence.create(
054                            subscriptionId);
055    
056                    subscription.setCompanyId(user.getCompanyId());
057                    subscription.setUserId(user.getUserId());
058                    subscription.setUserName(user.getFullName());
059                    subscription.setCreateDate(now);
060                    subscription.setModifiedDate(now);
061                    subscription.setClassNameId(classNameId);
062                    subscription.setClassPK(classPK);
063                    subscription.setFrequency(frequency);
064    
065                    subscriptionPersistence.update(subscription, false);
066    
067                    return subscription;
068            }
069    
070            public void deleteSubscription(long subscriptionId)
071                    throws PortalException, SystemException {
072    
073                    subscriptionPersistence.remove(subscriptionId);
074            }
075    
076            public void deleteSubscription(
077                            long userId, String className, long classPK)
078                    throws PortalException, SystemException {
079    
080                    User user = userPersistence.findByPrimaryKey(userId);
081                    long classNameId = PortalUtil.getClassNameId(className);
082    
083                    subscriptionPersistence.removeByC_U_C_C(
084                            user.getCompanyId(), userId, classNameId, classPK);
085            }
086    
087            public void deleteSubscriptions(long userId) throws SystemException {
088                    subscriptionPersistence.removeByUserId(userId);
089            }
090    
091            public void deleteSubscriptions(
092                            long companyId, String className, long classPK)
093                    throws SystemException {
094    
095                    long classNameId = PortalUtil.getClassNameId(className);
096    
097                    subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
098            }
099    
100            public Subscription getSubscription(
101                            long companyId, long userId, String className, long classPK)
102                    throws PortalException, SystemException {
103    
104                    long classNameId = PortalUtil.getClassNameId(className);
105    
106                    return subscriptionPersistence.findByC_U_C_C(
107                            companyId, userId, classNameId, classPK);
108            }
109    
110            public List<Subscription> getSubscriptions(
111                            long companyId, String className, long classPK)
112                    throws SystemException {
113    
114                    long classNameId = PortalUtil.getClassNameId(className);
115    
116                    return subscriptionPersistence.findByC_C_C(
117                            companyId, classNameId, classPK);
118            }
119    
120            public List<Subscription> getUserSubscriptions(
121                            long userId, String className)
122                    throws SystemException {
123    
124                    long classNameId = PortalUtil.getClassNameId(className);
125    
126                    return subscriptionPersistence.findByU_C(userId, classNameId);
127            }
128    
129            public boolean isSubscribed(
130                            long companyId, long userId, String className, long classPK)
131                    throws SystemException {
132    
133                    long classNameId = PortalUtil.getClassNameId(className);
134    
135                    Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
136                            companyId, userId, classNameId, classPK);
137    
138                    if (subscription != null) {
139                            return true;
140                    }
141                    else {
142                            return false;
143                    }
144            }
145    
146    }