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  public class SubscriptionLocalServiceImpl
43      extends SubscriptionLocalServiceBaseImpl {
44  
45      public Subscription addSubscription(
46              long userId, String className, long classPK)
47          throws PortalException, SystemException {
48  
49          return addSubscription(
50              userId, className, classPK, SubscriptionImpl.FREQUENCY_INSTANT);
51      }
52  
53      public Subscription addSubscription(
54              long userId, String className, long classPK, String frequency)
55          throws PortalException, SystemException {
56  
57          User user = userPersistence.findByPrimaryKey(userId);
58          long classNameId = PortalUtil.getClassNameId(className);
59          Date now = new Date();
60  
61          long subscriptionId = counterLocalService.increment();
62  
63          Subscription subscription = subscriptionPersistence.create(
64              subscriptionId);
65  
66          subscription.setCompanyId(user.getCompanyId());
67          subscription.setUserId(user.getUserId());
68          subscription.setUserName(user.getFullName());
69          subscription.setCreateDate(now);
70          subscription.setModifiedDate(now);
71          subscription.setClassNameId(classNameId);
72          subscription.setClassPK(classPK);
73          subscription.setFrequency(frequency);
74  
75          subscriptionPersistence.update(subscription, false);
76  
77          return subscription;
78      }
79  
80      public void deleteSubscription(long subscriptionId)
81          throws PortalException, SystemException {
82  
83          subscriptionPersistence.remove(subscriptionId);
84      }
85  
86      public void deleteSubscription(
87              long userId, String className, long classPK)
88          throws PortalException, SystemException {
89  
90          User user = userPersistence.findByPrimaryKey(userId);
91          long classNameId = PortalUtil.getClassNameId(className);
92  
93          subscriptionPersistence.removeByC_U_C_C(
94              user.getCompanyId(), userId, classNameId, classPK);
95      }
96  
97      public void deleteSubscriptions(long userId) throws SystemException {
98          subscriptionPersistence.removeByUserId(userId);
99      }
100 
101     public void deleteSubscriptions(
102             long companyId, String className, long classPK)
103         throws SystemException {
104 
105         long classNameId = PortalUtil.getClassNameId(className);
106 
107         subscriptionPersistence.removeByC_C_C(companyId, classNameId, classPK);
108     }
109 
110     public Subscription getSubscription(
111             long companyId, long userId, String className, long classPK)
112         throws PortalException, SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return subscriptionPersistence.findByC_U_C_C(
117             companyId, userId, classNameId, classPK);
118     }
119 
120     public List<Subscription> getSubscriptions(
121             long companyId, String className, long classPK)
122         throws SystemException {
123 
124         long classNameId = PortalUtil.getClassNameId(className);
125 
126         return subscriptionPersistence.findByC_C_C(
127             companyId, classNameId, classPK);
128     }
129 
130     public List<Subscription> getUserSubscriptions(
131             long userId, String className)
132         throws SystemException {
133 
134         long classNameId = PortalUtil.getClassNameId(className);
135 
136         return subscriptionPersistence.findByU_C(userId, classNameId);
137     }
138 
139     public boolean isSubscribed(
140             long companyId, long userId, String className, long classPK)
141         throws SystemException {
142 
143         long classNameId = PortalUtil.getClassNameId(className);
144 
145         Subscription subscription = subscriptionPersistence.fetchByC_U_C_C(
146             companyId, userId, classNameId, classPK);
147 
148         if (subscription != null) {
149             return true;
150         }
151         else {
152             return false;
153         }
154     }
155 
156 }