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.portlet.social.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.social.NoSuchActivityException;
30  import com.liferay.portlet.social.model.SocialActivity;
31  import com.liferay.portlet.social.service.base.SocialActivityLocalServiceBaseImpl;
32  import com.liferay.portlet.social.util.SocialActivityThreadLocal;
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          if (!SocialActivityThreadLocal.isEnabled()) {
63              return null;
64          }
65  
66          User user = userPersistence.findByPrimaryKey(userId);
67          long classNameId = PortalUtil.getClassNameId(className);
68  
69          if (groupId > 0) {
70              Group group = groupLocalService.getGroup(groupId);
71  
72              if (group.isLayout()) {
73                  Layout layout = layoutLocalService.getLayout(
74                      group.getClassPK());
75  
76                  groupId = layout.getGroupId();
77              }
78          }
79  
80          long activityId = counterLocalService.increment(
81              SocialActivity.class.getName());
82  
83          SocialActivity activity = socialActivityPersistence.create(
84              activityId);
85  
86          activity.setGroupId(groupId);
87          activity.setCompanyId(user.getCompanyId());
88          activity.setUserId(user.getUserId());
89          activity.setCreateDate(createDate);
90          activity.setMirrorActivityId(0);
91          activity.setClassNameId(classNameId);
92          activity.setClassPK(classPK);
93          activity.setType(type);
94          activity.setExtraData(extraData);
95          activity.setReceiverUserId(receiverUserId);
96  
97          socialActivityPersistence.update(activity, false);
98  
99          if ((receiverUserId > 0) && (userId != receiverUserId)) {
100             long mirrorActivityId = counterLocalService.increment(
101                 SocialActivity.class.getName());
102 
103             SocialActivity mirrorActivity = socialActivityPersistence.create(
104                 mirrorActivityId);
105 
106             mirrorActivity.setGroupId(groupId);
107             mirrorActivity.setCompanyId(user.getCompanyId());
108             mirrorActivity.setUserId(receiverUserId);
109             mirrorActivity.setCreateDate(createDate);
110             mirrorActivity.setMirrorActivityId(activityId);
111             mirrorActivity.setClassNameId(classNameId);
112             mirrorActivity.setClassPK(classPK);
113             mirrorActivity.setType(type);
114             mirrorActivity.setExtraData(extraData);
115             mirrorActivity.setReceiverUserId(user.getUserId());
116 
117             socialActivityPersistence.update(mirrorActivity, false);
118         }
119 
120         return activity;
121     }
122 
123     public SocialActivity addUniqueActivity(
124             long userId, long groupId, String className, long classPK, int type,
125             String extraData, long receiverUserId)
126         throws PortalException, SystemException {
127 
128         return addUniqueActivity(
129             userId, groupId, new Date(), className, classPK, type, extraData,
130             receiverUserId);
131     }
132 
133     public SocialActivity addUniqueActivity(
134             long userId, long groupId, Date createDate, String className,
135             long classPK, int type, String extraData, long receiverUserId)
136         throws PortalException, SystemException {
137 
138         long classNameId = PortalUtil.getClassNameId(className);
139 
140         SocialActivity socialActivity =
141             socialActivityPersistence.fetchByG_U_CD_C_C_T_R(
142                 groupId, userId, createDate, classNameId, classPK, type,
143                 receiverUserId);
144 
145         if (socialActivity != null) {
146             return socialActivity;
147         }
148 
149         return addActivity(
150             userId, groupId, createDate, className, classPK, type, extraData,
151             receiverUserId);
152     }
153 
154     public void deleteActivities(String className, long classPK)
155         throws SystemException {
156 
157         long classNameId = PortalUtil.getClassNameId(className);
158 
159         deleteActivities(classNameId, classPK);
160     }
161 
162     public void deleteActivities(long classNameId, long classPK)
163         throws SystemException {
164 
165         socialActivityPersistence.removeByC_C(classNameId, classPK);
166     }
167 
168     public void deleteActivity(long activityId)
169         throws PortalException, SystemException {
170 
171         SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
172             activityId);
173 
174         try {
175             socialActivityPersistence.removeByMirrorActivityId(activityId);
176         }
177         catch (NoSuchActivityException nsae) {
178         }
179 
180         socialActivityPersistence.remove(activity);
181     }
182 
183     public void deleteUserActivities(long userId) throws SystemException {
184         List<SocialActivity> activities =
185             socialActivityPersistence.findByUserId(
186                 userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
187 
188         for (SocialActivity activity : activities) {
189             socialActivityPersistence.remove(activity);
190         }
191 
192         activities = socialActivityPersistence.findByReceiverUserId(
193             userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
194 
195         for (SocialActivity activity : activities) {
196             socialActivityPersistence.remove(activity);
197         }
198     }
199 
200     public List<SocialActivity> getActivities(
201             String className, int start, int end)
202         throws SystemException {
203 
204         long classNameId = PortalUtil.getClassNameId(className);
205 
206         return getActivities(classNameId, start, end);
207     }
208 
209     public List<SocialActivity> getActivities(
210             long classNameId, int start, int end)
211         throws SystemException {
212 
213         return socialActivityPersistence.findByClassNameId(
214             classNameId, start, end);
215     }
216 
217     public List<SocialActivity> getActivities(
218             long mirrorActivityId, String className, long classPK, int start,
219             int end)
220         throws SystemException {
221 
222         long classNameId = PortalUtil.getClassNameId(className);
223 
224         return getActivities(
225             mirrorActivityId, classNameId, classPK, start, end);
226     }
227 
228     public List<SocialActivity> getActivities(
229             long mirrorActivityId, long classNameId, long classPK, int start,
230             int end)
231         throws SystemException {
232 
233         return socialActivityPersistence.findByM_C_C(
234             mirrorActivityId, classNameId, classPK, start, end);
235     }
236 
237     public int getActivitiesCount(String className) throws SystemException {
238         long classNameId = PortalUtil.getClassNameId(className);
239 
240         return getActivitiesCount(classNameId);
241     }
242 
243     public int getActivitiesCount(long classNameId) throws SystemException {
244         return socialActivityPersistence.countByClassNameId(classNameId);
245     }
246 
247     public int getActivitiesCount(
248             long mirrorActivityId, String className, long classPK)
249         throws SystemException {
250 
251         long classNameId = PortalUtil.getClassNameId(className);
252 
253         return getActivitiesCount(mirrorActivityId, classNameId, classPK);
254     }
255 
256     public int getActivitiesCount(
257             long mirrorActivityId, long classNameId, long classPK)
258         throws SystemException {
259 
260         return socialActivityPersistence.countByM_C_C(
261             mirrorActivityId, classNameId, classPK);
262     }
263 
264     public SocialActivity getActivity(long activityId)
265         throws PortalException, SystemException {
266 
267         return socialActivityPersistence.findByPrimaryKey(activityId);
268     }
269 
270     public List<SocialActivity> getGroupActivities(
271             long groupId, int start, int end)
272         throws SystemException {
273 
274         return socialActivityFinder.findByGroupId(groupId, start, end);
275     }
276 
277     public int getGroupActivitiesCount(long groupId) throws SystemException {
278         return socialActivityFinder.countByGroupId(groupId);
279     }
280 
281     public List<SocialActivity> getGroupUsersActivities(
282             long groupId, int start, int end)
283         throws SystemException {
284 
285         return socialActivityFinder.findByGroupUsers(groupId, start, end);
286     }
287 
288     public int getGroupUsersActivitiesCount(long groupId)
289         throws SystemException {
290 
291         return socialActivityFinder.countByGroupUsers(groupId);
292     }
293 
294     public SocialActivity getMirrorActivity(long mirrorActivityId)
295         throws PortalException, SystemException {
296 
297         return socialActivityPersistence.findByMirrorActivityId(
298             mirrorActivityId);
299     }
300 
301     public List<SocialActivity> getOrganizationActivities(
302             long organizationId, int start, int end)
303         throws SystemException {
304 
305         return socialActivityFinder.findByOrganizationId(
306             organizationId, start, end);
307     }
308 
309     public int getOrganizationActivitiesCount(long organizationId)
310         throws SystemException {
311 
312         return socialActivityFinder.countByOrganizationId(organizationId);
313     }
314 
315     public List<SocialActivity> getOrganizationUsersActivities(
316             long organizationId, int start, int end)
317         throws SystemException {
318 
319         return socialActivityFinder.findByOrganizationUsers(
320             organizationId, start, end);
321     }
322 
323     public int getOrganizationUsersActivitiesCount(long organizationId)
324         throws SystemException {
325 
326         return socialActivityFinder.countByOrganizationUsers(organizationId);
327     }
328 
329     public List<SocialActivity> getRelationActivities(
330             long userId, int start, int end)
331         throws SystemException {
332 
333         return socialActivityFinder.findByRelation(userId, start, end);
334     }
335 
336     public List<SocialActivity> getRelationActivities(
337             long userId, int type, int start, int end)
338         throws SystemException {
339 
340         return socialActivityFinder.findByRelationType(
341             userId, type, start, end);
342     }
343 
344     public int getRelationActivitiesCount(long userId) throws SystemException {
345         return socialActivityFinder.countByRelation(userId);
346     }
347 
348     public int getRelationActivitiesCount(long userId, int type)
349         throws SystemException {
350 
351         return socialActivityFinder.countByRelationType(userId, type);
352     }
353 
354     public List<SocialActivity> getUserActivities(
355             long userId, int start, int end)
356         throws SystemException {
357 
358         return socialActivityPersistence.findByUserId(userId, start, end);
359     }
360 
361     public int getUserActivitiesCount(long userId) throws SystemException {
362         return socialActivityPersistence.countByUserId(userId);
363     }
364 
365     public List<SocialActivity> getUserGroupsActivities(
366             long userId, int start, int end)
367         throws SystemException {
368 
369         return socialActivityFinder.findByUserGroups(userId, start, end);
370     }
371 
372     public int getUserGroupsActivitiesCount(long userId)
373         throws SystemException {
374 
375         return socialActivityFinder.countByUserGroups(userId);
376     }
377 
378     public List<SocialActivity> getUserGroupsAndOrganizationsActivities(
379             long userId, int start, int end)
380         throws SystemException {
381 
382         return socialActivityFinder.findByUserGroupsAndOrganizations(
383             userId, start, end);
384     }
385 
386     public int getUserGroupsAndOrganizationsActivitiesCount(long userId)
387         throws SystemException {
388 
389         return socialActivityFinder.countByUserGroupsAndOrganizations(userId);
390     }
391 
392     public List<SocialActivity> getUserOrganizationsActivities(
393             long userId, int start, int end)
394         throws SystemException {
395 
396         return socialActivityFinder.findByUserOrganizations(userId, start, end);
397     }
398 
399     public int getUserOrganizationsActivitiesCount(long userId)
400         throws SystemException {
401 
402         return socialActivityFinder.countByUserOrganizations(userId);
403     }
404 
405 }