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