1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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 List<Subscription> getUserSubscriptions(
132             long userId, String className)
133         throws SystemException {
134 
135         long classNameId = PortalUtil.getClassNameId(className);
136 
137         return subscriptionPersistence.findByU_C(userId, classNameId);
138     }
139 
140     public boolean isSubscribed(
141             long companyId, long userId, String className, long classPK)
142         throws SystemException {
143 
144         long classNameId = PortalUtil.getClassNameId(className);
145 
146         Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
147             companyId, userId, classNameId, classPK);
148 
149         if (subscription != null) {
150             return true;
151         }
152         else {
153             return false;
154         }
155     }
156 
157 }