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