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