1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.Subscription;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.model.impl.SubscriptionImpl;
30  import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
31  import com.liferay.portal.util.PortalUtil;
32  
33  import java.util.Date;
34  import java.util.List;
35  
36  /**
37   * <a href="SubscriptionLocalServiceImpl.java.html"><b><i>View Source</i></b>
38   * </a>
39   *
40   * @author Charles May
41   *
42   */
43  public class SubscriptionLocalServiceImpl
44      extends SubscriptionLocalServiceBaseImpl {
45  
46      public Subscription addSubscription(
47              long userId, String className, long classPK)
48          throws PortalException, SystemException {
49  
50          return addSubscription(
51              userId, className, classPK, SubscriptionImpl.FREQUENCY_INSTANT);
52      }
53  
54      public Subscription addSubscription(
55              long userId, String className, long classPK, String frequency)
56          throws PortalException, SystemException {
57  
58          User user = userPersistence.findByPrimaryKey(userId);
59          long classNameId = PortalUtil.getClassNameId(className);
60          Date now = new Date();
61  
62          long subscriptionId = counterLocalService.increment();
63  
64          Subscription subscription = subscriptionPersistence.create(
65              subscriptionId);
66  
67          subscription.setCompanyId(user.getCompanyId());
68          subscription.setUserId(user.getUserId());
69          subscription.setUserName(user.getFullName());
70          subscription.setCreateDate(now);
71          subscription.setModifiedDate(now);
72          subscription.setClassNameId(classNameId);
73          subscription.setClassPK(classPK);
74          subscription.setFrequency(frequency);
75  
76          subscriptionPersistence.update(subscription, false);
77  
78          return subscription;
79      }
80  
81      public void deleteSubscription(long subscriptionId)
82          throws PortalException, SystemException {
83  
84          subscriptionPersistence.remove(subscriptionId);
85      }
86  
87      public void deleteSubscription(
88              long userId, String className, long classPK)
89          throws PortalException, SystemException {
90  
91          User user = userPersistence.findByPrimaryKey(userId);
92          long classNameId = PortalUtil.getClassNameId(className);
93  
94          subscriptionPersistence.removeByC_U_C_C(
95              user.getCompanyId(), userId, classNameId, classPK);
96      }
97  
98      public void deleteSubscriptions(long userId) throws SystemException {
99          subscriptionPersistence.removeByUserId(userId);
100     }
101 
102     public void deleteSubscriptions(
103             long companyId, String className, long classPK)
104         throws SystemException {
105 
106         long classNameId = PortalUtil.getClassNameId(className);
107 
108         subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
109     }
110 
111     public Subscription getSubscription(
112             long companyId, long userId, String className, long classPK)
113         throws PortalException, SystemException {
114 
115         long classNameId = PortalUtil.getClassNameId(className);
116 
117         return subscriptionPersistence.findByC_U_C_C(
118             companyId, userId, classNameId, classPK);
119     }
120 
121     public List<Subscription> getSubscriptions(
122             long companyId, String className, long classPK)
123         throws SystemException {
124 
125         long classNameId = PortalUtil.getClassNameId(className);
126 
127         return subscriptionPersistence.findByC_C_C(
128             companyId, classNameId, classPK);
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 }