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