1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.model.Subscription;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.model.impl.SubscriptionImpl;
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, SubscriptionImpl.FREQUENCY_INSTANT);
43      }
44  
45      public Subscription addSubscription(
46              long userId, String className, long classPK, String frequency)
47          throws PortalException, SystemException {
48  
49          User user = userPersistence.findByPrimaryKey(userId);
50          long classNameId = PortalUtil.getClassNameId(className);
51          Date now = new Date();
52  
53          long subscriptionId = counterLocalService.increment();
54  
55          Subscription subscription = subscriptionPersistence.create(
56              subscriptionId);
57  
58          subscription.setCompanyId(user.getCompanyId());
59          subscription.setUserId(user.getUserId());
60          subscription.setUserName(user.getFullName());
61          subscription.setCreateDate(now);
62          subscription.setModifiedDate(now);
63          subscription.setClassNameId(classNameId);
64          subscription.setClassPK(classPK);
65          subscription.setFrequency(frequency);
66  
67          subscriptionPersistence.update(subscription, false);
68  
69          return subscription;
70      }
71  
72      public void deleteSubscription(long subscriptionId)
73          throws PortalException, SystemException {
74  
75          subscriptionPersistence.remove(subscriptionId);
76      }
77  
78      public void deleteSubscription(
79              long userId, String className, long classPK)
80          throws PortalException, SystemException {
81  
82          User user = userPersistence.findByPrimaryKey(userId);
83          long classNameId = PortalUtil.getClassNameId(className);
84  
85          subscriptionPersistence.removeByC_U_C_C(
86              user.getCompanyId(), userId, classNameId, classPK);
87      }
88  
89      public void deleteSubscriptions(long userId) throws SystemException {
90          subscriptionPersistence.removeByUserId(userId);
91      }
92  
93      public void deleteSubscriptions(
94              long companyId, String className, long classPK)
95          throws SystemException {
96  
97          long classNameId = PortalUtil.getClassNameId(className);
98  
99          subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
100     }
101 
102     public Subscription getSubscription(
103             long companyId, long userId, String className, long classPK)
104         throws PortalException, SystemException {
105 
106         long classNameId = PortalUtil.getClassNameId(className);
107 
108         return subscriptionPersistence.findByC_U_C_C(
109             companyId, userId, classNameId, classPK);
110     }
111 
112     public List<Subscription> getSubscriptions(
113             long companyId, String className, long classPK)
114         throws SystemException {
115 
116         long classNameId = PortalUtil.getClassNameId(className);
117 
118         return subscriptionPersistence.findByC_C_C(
119             companyId, classNameId, classPK);
120     }
121 
122     public List<Subscription> getUserSubscriptions(
123             long userId, String className)
124         throws SystemException {
125 
126         long classNameId = PortalUtil.getClassNameId(className);
127 
128         return subscriptionPersistence.findByU_C(userId, classNameId);
129     }
130 
131     public boolean isSubscribed(
132             long companyId, long userId, String className, long classPK)
133         throws SystemException {
134 
135         long classNameId = PortalUtil.getClassNameId(className);
136 
137         Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
138             companyId, userId, classNameId, classPK);
139 
140         if (subscription != null) {
141             return true;
142         }
143         else {
144             return false;
145         }
146     }
147 
148 }