1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.Subscription;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.model.impl.SubscriptionImpl;
27  import com.liferay.portal.service.base.SubscriptionLocalServiceBaseImpl;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.util.Date;
31  import java.util.List;
32  
33  /**
34   * <a href="SubscriptionLocalServiceImpl.java.html"><b><i>View Source</i></b>
35   * </a>
36   *
37   * @author Charles May
38   *
39   */
40  public class SubscriptionLocalServiceImpl
41      extends SubscriptionLocalServiceBaseImpl {
42  
43      public Subscription addSubscription(
44              long userId, String className, long classPK)
45          throws PortalException, SystemException {
46  
47          return addSubscription(
48              userId, className, classPK, SubscriptionImpl.FREQUENCY_INSTANT);
49      }
50  
51      public Subscription addSubscription(
52              long userId, String className, long classPK, String frequency)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57          Date now = new Date();
58  
59          long subscriptionId = counterLocalService.increment();
60  
61          Subscription subscription = subscriptionPersistence.create(
62              subscriptionId);
63  
64          subscription.setCompanyId(user.getCompanyId());
65          subscription.setUserId(user.getUserId());
66          subscription.setUserName(user.getFullName());
67          subscription.setCreateDate(now);
68          subscription.setModifiedDate(now);
69          subscription.setClassNameId(classNameId);
70          subscription.setClassPK(classPK);
71          subscription.setFrequency(frequency);
72  
73          subscriptionPersistence.update(subscription, false);
74  
75          return subscription;
76      }
77  
78      public void deleteSubscription(long subscriptionId)
79          throws PortalException, SystemException {
80  
81          subscriptionPersistence.remove(subscriptionId);
82      }
83  
84      public void deleteSubscription(
85              long userId, String className, long classPK)
86          throws PortalException, SystemException {
87  
88          User user = userPersistence.findByPrimaryKey(userId);
89          long classNameId = PortalUtil.getClassNameId(className);
90  
91          subscriptionPersistence.removeByC_U_C_C(
92              user.getCompanyId(), userId, classNameId, classPK);
93      }
94  
95      public void deleteSubscriptions(long userId) throws SystemException {
96          subscriptionPersistence.removeByUserId(userId);
97      }
98  
99      public void deleteSubscriptions(
100             long companyId, String className, long classPK)
101         throws SystemException {
102 
103         long classNameId = PortalUtil.getClassNameId(className);
104 
105         subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
106     }
107 
108     public Subscription getSubscription(
109             long companyId, long userId, String className, long classPK)
110         throws PortalException, SystemException {
111 
112         long classNameId = PortalUtil.getClassNameId(className);
113 
114         return subscriptionPersistence.findByC_U_C_C(
115             companyId, userId, classNameId, classPK);
116     }
117 
118     public List<Subscription> getSubscriptions(
119             long companyId, String className, long classPK)
120         throws SystemException {
121 
122         long classNameId = PortalUtil.getClassNameId(className);
123 
124         return subscriptionPersistence.findByC_C_C(
125             companyId, classNameId, classPK);
126     }
127 
128     public List<Subscription> getUserSubscriptions(
129             long userId, String className)
130         throws SystemException {
131 
132         long classNameId = PortalUtil.getClassNameId(className);
133 
134         return subscriptionPersistence.findByU_C(userId, classNameId);
135     }
136 
137     public boolean isSubscribed(
138             long companyId, long userId, String className, long classPK)
139         throws SystemException {
140 
141         long classNameId = PortalUtil.getClassNameId(className);
142 
143         Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
144             companyId, userId, classNameId, classPK);
145 
146         if (subscription != null) {
147             return true;
148         }
149         else {
150             return false;
151         }
152     }
153 
154 }