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.portlet.social.service.impl;
24  
25  import com.liferay.portal.NoSuchUserException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.social.model.SocialActivity;
32  import com.liferay.portlet.social.service.base.SocialActivityLocalServiceBaseImpl;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="SocialActivityLocalServiceImpl.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class SocialActivityLocalServiceImpl
48      extends SocialActivityLocalServiceBaseImpl {
49  
50      public SocialActivity addActivity(
51              long userId, long groupId, String className, long classPK,
52              String type, String extraData, long receiverUserId)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57  
58          String receiverUserName = StringPool.BLANK;
59  
60          if (receiverUserId > 0) {
61              try {
62                  User receiverUser = userPersistence.findByPrimaryKey(
63                      receiverUserId);
64  
65                  receiverUserName = receiverUser.getFullName();
66              }
67              catch (NoSuchUserException nsue) {
68                  if (_log.isWarnEnabled()) {
69                      _log.warn(nsue);
70                  }
71              }
72          }
73  
74          long activityId = counterLocalService.increment(
75              SocialActivity.class.getName());
76  
77          SocialActivity activity = socialActivityPersistence.create(
78              activityId);
79  
80          activity.setGroupId(groupId);
81          activity.setCompanyId(user.getCompanyId());
82          activity.setUserId(user.getUserId());
83          activity.setUserName(user.getFullName());
84          activity.setCreateDate(new Date());
85          activity.setClassNameId(classNameId);
86          activity.setClassPK(classPK);
87          activity.setType(type);
88          activity.setExtraData(extraData);
89          activity.setReceiverUserId(receiverUserId);
90          activity.setReceiverUserName(receiverUserName);
91  
92          socialActivityPersistence.update(activity, false);
93  
94          return activity;
95      }
96  
97      public void deleteActivities(String className, long classPK)
98          throws SystemException {
99  
100         long classNameId = PortalUtil.getClassNameId(className);
101 
102         deleteActivities(classNameId, classPK);
103     }
104 
105     public void deleteActivities(long classNameId, long classPK)
106         throws SystemException {
107 
108         socialActivityPersistence.removeByC_C(classNameId, classPK);
109     }
110 
111     public List<SocialActivity> getActivities(String className, long classPK)
112         throws SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return getActivities(classNameId, classPK);
117     }
118 
119     public List<SocialActivity> getActivities(long classNameId, long classPK)
120         throws SystemException {
121 
122         return socialActivityPersistence.findByC_C(classNameId, classPK);
123     }
124 
125     public List<SocialActivity> getActivities(
126             String className, long classPK, int begin, int end)
127         throws SystemException {
128 
129         long classNameId = PortalUtil.getClassNameId(className);
130 
131         return getActivities(classNameId, classPK, begin, end);
132     }
133 
134     public List<SocialActivity> getActivities(
135             long classNameId, long classPK, int begin, int end)
136         throws SystemException {
137 
138         return socialActivityPersistence.findByC_C(
139             classNameId, classPK, begin, end);
140     }
141 
142     public List<SocialActivity> getCompanyActivities(
143             long companyId, int begin, int end)
144         throws SystemException {
145 
146         return socialActivityPersistence.findByCompanyId(companyId, begin, end);
147     }
148 
149     public int getCompanyActivitiesCount(long companyId)
150         throws SystemException {
151 
152         return socialActivityPersistence.countByCompanyId(companyId);
153     }
154 
155     public List<SocialActivity> getGroupActivities(
156             long groupId, int begin, int end)
157         throws SystemException {
158 
159         return socialActivityPersistence.findByGroupId(groupId, begin, end);
160     }
161 
162     public int getGroupActivitiesCount(long groupId) throws SystemException {
163         return socialActivityPersistence.countByGroupId(groupId);
164     }
165 
166     public List<SocialActivity> getUserActivities(
167             long userId, int begin, int end)
168         throws SystemException {
169 
170         return socialActivityFinder.findByU_R(userId, userId, begin, end);
171     }
172 
173     public int getUserActivitiesCount(long userId) throws SystemException {
174         return socialActivityFinder.countByU_R(userId, userId);
175     }
176 
177     private static Log _log =
178         LogFactory.getLog(SocialActivityLocalServiceImpl.class);
179 
180 }