1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.Subscription;
20  import com.liferay.portal.model.SubscriptionConstants;
21  import com.liferay.portal.model.User;
22  import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
23  import com.liferay.portal.util.PortalUtil;
24  
25  import java.util.Date;
26  import java.util.List;
27  
28  /**
29   * <a href="SubscriptionLocalServiceImpl.java.html"><b><i>View Source</i></b>
30   * </a>
31   *
32   * @author Charles May
33   */
34  public class SubscriptionLocalServiceImpl
35      extends SubscriptionLocalServiceBaseImpl {
36  
37      public Subscription addSubscription(
38              long userId, String className, long classPK)
39          throws PortalException, SystemException {
40  
41          return addSubscription(
42              userId, className, classPK,
43              SubscriptionConstants.FREQUENCY_INSTANT);
44      }
45  
46      public Subscription addSubscription(
47              long userId, String className, long classPK, String frequency)
48          throws PortalException, SystemException {
49  
50          User user = userPersistence.findByPrimaryKey(userId);
51          long classNameId = PortalUtil.getClassNameId(className);
52          Date now = new Date();
53  
54          long subscriptionId = counterLocalService.increment();
55  
56          Subscription subscription = subscriptionPersistence.create(
57              subscriptionId);
58  
59          subscription.setCompanyId(user.getCompanyId());
60          subscription.setUserId(user.getUserId());
61          subscription.setUserName(user.getFullName());
62          subscription.setCreateDate(now);
63          subscription.setModifiedDate(now);
64          subscription.setClassNameId(classNameId);
65          subscription.setClassPK(classPK);
66          subscription.setFrequency(frequency);
67  
68          subscriptionPersistence.update(subscription, false);
69  
70          return subscription;
71      }
72  
73      public void deleteSubscription(long subscriptionId)
74          throws PortalException, SystemException {
75  
76          subscriptionPersistence.remove(subscriptionId);
77      }
78  
79      public void deleteSubscription(
80              long userId, String className, long classPK)
81          throws PortalException, SystemException {
82  
83          User user = userPersistence.findByPrimaryKey(userId);
84          long classNameId = PortalUtil.getClassNameId(className);
85  
86          subscriptionPersistence.removeByC_U_C_C(
87              user.getCompanyId(), userId, classNameId, classPK);
88      }
89  
90      public void deleteSubscriptions(long userId) throws SystemException {
91          subscriptionPersistence.removeByUserId(userId);
92      }
93  
94      public void deleteSubscriptions(
95              long companyId, String className, long classPK)
96          throws SystemException {
97  
98          long classNameId = PortalUtil.getClassNameId(className);
99  
100         subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
101     }
102 
103     public Subscription getSubscription(
104             long companyId, long userId, String className, long classPK)
105         throws PortalException, SystemException {
106 
107         long classNameId = PortalUtil.getClassNameId(className);
108 
109         return subscriptionPersistence.findByC_U_C_C(
110             companyId, userId, classNameId, classPK);
111     }
112 
113     public List<Subscription> getSubscriptions(
114             long companyId, String className, long classPK)
115         throws SystemException {
116 
117         long classNameId = PortalUtil.getClassNameId(className);
118 
119         return subscriptionPersistence.findByC_C_C(
120             companyId, classNameId, classPK);
121     }
122 
123     public List<Subscription> getUserSubscriptions(
124             long userId, String className)
125         throws SystemException {
126 
127         long classNameId = PortalUtil.getClassNameId(className);
128 
129         return subscriptionPersistence.findByU_C(userId, classNameId);
130     }
131 
132     public boolean isSubscribed(
133             long companyId, long userId, String className, long classPK)
134         throws SystemException {
135 
136         long classNameId = PortalUtil.getClassNameId(className);
137 
138         Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
139             companyId, userId, classNameId, classPK);
140 
141         if (subscription != null) {
142             return true;
143         }
144         else {
145             return false;
146         }
147     }
148 
149 }