1   /**
2    * Copyright (c) 2000-2008 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.NoSuchSubscriptionException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.Subscription;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.impl.SubscriptionImpl;
31  import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  /**
38   * <a href="SubscriptionLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Charles May
42   *
43   */
44  public class SubscriptionLocalServiceImpl
45      extends SubscriptionLocalServiceBaseImpl {
46  
47      public Subscription addSubscription(
48              long userId, String className, long classPK)
49          throws PortalException, SystemException {
50  
51          return addSubscription(
52              userId, className, classPK, SubscriptionImpl.FREQUENCY_INSTANT);
53      }
54  
55      public Subscription addSubscription(
56              long userId, String className, long classPK, String frequency)
57          throws PortalException, SystemException {
58  
59          User user = userPersistence.findByPrimaryKey(userId);
60          long classNameId = PortalUtil.getClassNameId(className);
61          Date now = new Date();
62  
63          long subscriptionId = counterLocalService.increment();
64  
65          Subscription subscription = subscriptionPersistence.create(
66              subscriptionId);
67  
68          subscription.setCompanyId(user.getCompanyId());
69          subscription.setUserId(user.getUserId());
70          subscription.setUserName(user.getFullName());
71          subscription.setCreateDate(now);
72          subscription.setModifiedDate(now);
73          subscription.setClassNameId(classNameId);
74          subscription.setClassPK(classPK);
75          subscription.setFrequency(frequency);
76  
77          subscriptionPersistence.update(subscription, false);
78  
79          return subscription;
80      }
81  
82      public void deleteSubscription(long subscriptionId)
83          throws PortalException, SystemException {
84  
85          subscriptionPersistence.remove(subscriptionId);
86      }
87  
88      public void deleteSubscription(
89              long userId, String className, long classPK)
90          throws PortalException, SystemException {
91  
92          User user = userPersistence.findByPrimaryKey(userId);
93          long classNameId = PortalUtil.getClassNameId(className);
94  
95          subscriptionPersistence.removeByC_U_C_C(
96              user.getCompanyId(), userId, classNameId, classPK);
97      }
98  
99      public void deleteSubscriptions(long userId) throws SystemException {
100         subscriptionPersistence.removeByUserId(userId);
101     }
102 
103     public void deleteSubscriptions(
104             long companyId, String className, long classPK)
105         throws SystemException {
106 
107         long classNameId = PortalUtil.getClassNameId(className);
108 
109         subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
110     }
111 
112     public Subscription getSubscription(
113             long companyId, long userId, String className, long classPK)
114         throws PortalException, SystemException {
115 
116         long classNameId = PortalUtil.getClassNameId(className);
117 
118         return subscriptionPersistence.findByC_U_C_C(
119             companyId, userId, classNameId, classPK);
120     }
121 
122     public List<Subscription> getSubscriptions(
123             long companyId, String className, long classPK)
124         throws SystemException {
125 
126         long classNameId = PortalUtil.getClassNameId(className);
127 
128         return subscriptionPersistence.findByC_C_C(
129             companyId, classNameId, classPK);
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         try {
139             subscriptionPersistence.findByC_U_C_C(
140                 companyId, userId, classNameId, classPK);
141 
142             return true;
143         }
144         catch (NoSuchSubscriptionException nsse) {
145             return false;
146         }
147     }
148 
149 }