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