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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.social.NoSuchActivityException;
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  /**
38   * <a href="SocialActivityLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class SocialActivityLocalServiceImpl
45      extends SocialActivityLocalServiceBaseImpl {
46  
47      public SocialActivity addActivity(
48              long userId, long groupId, String className, long classPK, int type,
49              String extraData, long receiverUserId)
50          throws PortalException, SystemException {
51  
52          return addActivity(
53              userId, groupId, new Date(), className, classPK, type, extraData,
54              receiverUserId);
55      }
56  
57      public SocialActivity addActivity(
58              long userId, long groupId, Date createDate, String className,
59              long classPK, int type, String extraData, long receiverUserId)
60          throws PortalException, SystemException {
61  
62          User user = userPersistence.findByPrimaryKey(userId);
63          long classNameId = PortalUtil.getClassNameId(className);
64  
65          long activityId = counterLocalService.increment(
66              SocialActivity.class.getName());
67  
68          SocialActivity activity = socialActivityPersistence.create(
69              activityId);
70  
71          activity.setGroupId(groupId);
72          activity.setCompanyId(user.getCompanyId());
73          activity.setUserId(user.getUserId());
74          activity.setCreateDate(createDate);
75          activity.setMirrorActivityId(0);
76          activity.setClassNameId(classNameId);
77          activity.setClassPK(classPK);
78          activity.setType(type);
79          activity.setExtraData(extraData);
80          activity.setReceiverUserId(receiverUserId);
81  
82          socialActivityPersistence.update(activity, false);
83  
84          if ((receiverUserId > 0) && (userId != receiverUserId)) {
85              long mirrorActivityId = counterLocalService.increment(
86                  SocialActivity.class.getName());
87  
88              SocialActivity mirrorActivity = socialActivityPersistence.create(
89                  mirrorActivityId);
90  
91              mirrorActivity.setGroupId(groupId);
92              mirrorActivity.setCompanyId(user.getCompanyId());
93              mirrorActivity.setUserId(receiverUserId);
94              mirrorActivity.setCreateDate(createDate);
95              mirrorActivity.setMirrorActivityId(activityId);
96              mirrorActivity.setClassNameId(classNameId);
97              mirrorActivity.setClassPK(classPK);
98              mirrorActivity.setType(type);
99              mirrorActivity.setExtraData(extraData);
100             mirrorActivity.setReceiverUserId(user.getUserId());
101 
102             socialActivityPersistence.update(mirrorActivity, false);
103         }
104 
105         return activity;
106     }
107 
108     public SocialActivity addUniqueActivity(
109             long userId, long groupId, String className, long classPK, int type,
110             String extraData, long receiverUserId)
111         throws PortalException, SystemException {
112 
113         return addUniqueActivity(
114             userId, groupId, new Date(), className, classPK, type, extraData,
115             receiverUserId);
116     }
117 
118     public SocialActivity addUniqueActivity(
119             long userId, long groupId, Date createDate, String className,
120             long classPK, int type, String extraData, long receiverUserId)
121         throws PortalException, SystemException {
122 
123         long classNameId = PortalUtil.getClassNameId(className);
124 
125         SocialActivity socialActivity =
126             socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
127                 groupId, userId, createDate, classNameId, classPK, type,
128                 receiverUserId);
129 
130         if (socialActivity != null) {
131             return socialActivity;
132         }
133 
134         return addActivity(
135             userId, groupId, createDate, className, classPK, type, extraData,
136             receiverUserId);
137     }
138 
139     public void deleteActivities(String className, long classPK)
140         throws SystemException {
141 
142         long classNameId = PortalUtil.getClassNameId(className);
143 
144         deleteActivities(classNameId, classPK);
145     }
146 
147     public void deleteActivities(long classNameId, long classPK)
148         throws SystemException {
149 
150         socialActivityPersistence.removeByC_C(classNameId, classPK);
151     }
152 
153     public void deleteActivity(long activityId)
154         throws PortalException, SystemException {
155 
156         SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
157             activityId);
158 
159         try {
160             socialActivityPersistence.removeByMirrorActivityId(activityId);
161         }
162         catch (NoSuchActivityException nsae) {
163         }
164 
165         socialActivityPersistence.remove(activity);
166     }
167 
168     public void deleteUserActivities(long userId) throws SystemException {
169         List<SocialActivity> activities =
170             socialActivityPersistence.findByUserId(
171                 userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
172 
173         for (SocialActivity activity : activities) {
174             socialActivityPersistence.remove(activity);
175         }
176 
177         activities = socialActivityPersistence.findByReceiverUserId(
178             userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
179 
180         for (SocialActivity activity : activities) {
181             socialActivityPersistence.remove(activity);
182         }
183     }
184 
185     public List<SocialActivity> getActivities(
186             String className, int start, int end)
187         throws SystemException {
188 
189         long classNameId = PortalUtil.getClassNameId(className);
190 
191         return getActivities(classNameId, start, end);
192     }
193 
194     public List<SocialActivity> getActivities(
195             long classNameId, int start, int end)
196         throws SystemException {
197 
198         return socialActivityPersistence.findByClassNameId(
199             classNameId, start, end);
200     }
201 
202     public List<SocialActivity> getActivities(
203             long mirrorActivityId, String className, long classPK, int start,
204             int end)
205         throws SystemException {
206 
207         long classNameId = PortalUtil.getClassNameId(className);
208 
209         return getActivities(
210             mirrorActivityId, classNameId, classPK, start, end);
211     }
212 
213     public List<SocialActivity> getActivities(
214             long mirrorActivityId, long classNameId, long classPK, int start,
215             int end)
216         throws SystemException {
217 
218         return socialActivityPersistence.findByM_C_C(
219             mirrorActivityId, classNameId, classPK, start, end);
220     }
221 
222     public int getActivitiesCount(String className) throws SystemException {
223         long classNameId = PortalUtil.getClassNameId(className);
224 
225         return getActivitiesCount(classNameId);
226     }
227 
228     public int getActivitiesCount(long classNameId) throws SystemException {
229         return socialActivityPersistence.countByClassNameId(classNameId);
230     }
231 
232     public int getActivitiesCount(
233             long mirrorActivityId, String className, long classPK)
234         throws SystemException {
235 
236         long classNameId = PortalUtil.getClassNameId(className);
237 
238         return getActivitiesCount(mirrorActivityId, classNameId, classPK);
239     }
240 
241     public int getActivitiesCount(
242             long mirrorActivityId, long classNameId, long classPK)
243         throws SystemException {
244 
245         return socialActivityPersistence.countByM_C_C(
246             mirrorActivityId, classNameId, classPK);
247     }
248 
249     public SocialActivity getActivity(long activityId)
250         throws PortalException, SystemException {
251 
252         return socialActivityPersistence.findByPrimaryKey(activityId);
253     }
254 
255     public List<SocialActivity> getGroupActivities(
256             long groupId, int start, int end)
257         throws SystemException {
258 
259         return socialActivityFinder.findByGroupId(groupId, start, end);
260     }
261 
262     public int getGroupActivitiesCount(long groupId) throws SystemException {
263         return socialActivityFinder.countByGroupId(groupId);
264     }
265 
266     public SocialActivity getMirrorActivity(long mirrorActivityId)
267         throws PortalException, SystemException {
268 
269         return socialActivityPersistence.findByMirrorActivityId(
270             mirrorActivityId);
271     }
272 
273     public List<SocialActivity> getOrganizationActivities(
274             long organizationId, int start, int end)
275         throws SystemException {
276 
277         return socialActivityFinder.findByOrganizationId(
278             organizationId, start, end);
279     }
280 
281     public int getOrganizationActivitiesCount(long organizationId)
282         throws SystemException {
283 
284         return socialActivityFinder.countByOrganizationId(organizationId);
285     }
286 
287     public List<SocialActivity> getRelationActivities(
288             long userId, int start, int end)
289         throws SystemException {
290 
291         return socialActivityFinder.findByRelation(userId, start, end);
292     }
293 
294     public List<SocialActivity> getRelationActivities(
295             long userId, int type, int start, int end)
296         throws SystemException {
297 
298         return socialActivityFinder.findByRelationType(
299             userId, type, start, end);
300     }
301 
302     public int getRelationActivitiesCount(long userId) throws SystemException {
303         return socialActivityFinder.countByRelation(userId);
304     }
305 
306     public int getRelationActivitiesCount(long userId, int type)
307         throws SystemException {
308 
309         return socialActivityFinder.countByRelationType(userId, type);
310     }
311 
312     public List<SocialActivity> getUserActivities(
313             long userId, int start, int end)
314         throws SystemException {
315 
316         return socialActivityPersistence.findByUserId(userId, start, end);
317     }
318 
319     public int getUserActivitiesCount(long userId) throws SystemException {
320         return socialActivityPersistence.countByUserId(userId);
321     }
322 
323 }