001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portlet.social.model.SocialActivity;
020    import com.liferay.portlet.social.model.SocialActivitySet;
021    import com.liferay.portlet.social.service.base.SocialActivitySetLocalServiceBaseImpl;
022    import com.liferay.portlet.social.util.comparator.SocialActivitySetModifiedDateComparator;
023    
024    import java.util.List;
025    
026    /**
027     * @author Matthew Kong
028     */
029    public class SocialActivitySetLocalServiceImpl
030            extends SocialActivitySetLocalServiceBaseImpl {
031    
032            @Override
033            public SocialActivitySet addActivitySet(long activityId)
034                    throws PortalException, SystemException {
035    
036                    // Activity set
037    
038                    SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
039                            activityId);
040    
041                    long activitySetId = counterLocalService.increment();
042    
043                    SocialActivitySet activitySet = socialActivitySetPersistence.create(
044                            activitySetId);
045    
046                    activitySet.setGroupId(activity.getGroupId());
047                    activitySet.setCompanyId(activity.getCompanyId());
048                    activitySet.setUserId(activity.getUserId());
049                    activitySet.setCreateDate(activity.getCreateDate());
050                    activitySet.setModifiedDate(activity.getCreateDate());
051                    activitySet.setClassName(activity.getClassName());
052                    activitySet.setClassPK(activity.getClassPK());
053                    activitySet.setType(activity.getType());
054                    activitySet.setActivityCount(1);
055    
056                    socialActivitySetPersistence.update(activitySet);
057    
058                    // Activity
059    
060                    activity.setActivitySetId(activitySetId);
061    
062                    socialActivityPersistence.update(activity);
063    
064                    return activitySet;
065            }
066    
067            @Override
068            public void decrementActivityCount(long activitySetId)
069                    throws PortalException, SystemException {
070    
071                    if (activitySetId == 0) {
072                            return;
073                    }
074    
075                    SocialActivitySet activitySet =
076                            socialActivitySetPersistence.findByPrimaryKey(activitySetId);
077    
078                    if (activitySet.getActivityCount() == 1) {
079                            socialActivitySetPersistence.remove(activitySetId);
080    
081                            return;
082                    }
083    
084                    activitySet.setActivityCount(activitySet.getActivityCount() - 1);
085    
086                    socialActivitySetPersistence.update(activitySet);
087            }
088    
089            @Override
090            public void decrementActivityCount(long classNameId, long classPK)
091                    throws PortalException, SystemException {
092    
093                    List<SocialActivity> activities = socialActivityPersistence.findByC_C(
094                            classNameId, classPK);
095    
096                    for (SocialActivity activity : activities) {
097                            decrementActivityCount(activity.getActivitySetId());
098                    }
099            }
100    
101            @Override
102            public SocialActivitySet getClassActivitySet(
103                            long classNameId, long classPK, int type)
104                    throws SystemException {
105    
106                    return socialActivitySetPersistence.fetchByC_C_T_First(
107                            classNameId, classPK, type,
108                            new SocialActivitySetModifiedDateComparator());
109            }
110    
111            @Override
112            public SocialActivitySet getClassActivitySet(
113                            long userId, long classNameId, long classPK, int type)
114                    throws SystemException {
115    
116                    return socialActivitySetPersistence.fetchByU_C_C_T_First(
117                            userId, classNameId, classPK, type,
118                            new SocialActivitySetModifiedDateComparator());
119            }
120    
121            @Override
122            public List<SocialActivitySet> getGroupActivitySets(
123                            long groupId, int start, int end)
124                    throws SystemException {
125    
126                    return socialActivitySetPersistence.findByGroupId(
127                            groupId, start, end, new SocialActivitySetModifiedDateComparator());
128            }
129    
130            @Override
131            public int getGroupActivitySetsCount(long groupId) throws SystemException {
132                    return socialActivitySetPersistence.countByGroupId(groupId);
133            }
134    
135            @Override
136            public List<SocialActivitySet> getRelationActivitySets(
137                            long userId, int start, int end)
138                    throws SystemException {
139    
140                    return socialActivitySetFinder.findByRelation(userId, start, end);
141            }
142    
143            @Override
144            public List<SocialActivitySet> getRelationActivitySets(
145                            long userId, int type, int start, int end)
146                    throws SystemException {
147    
148                    return socialActivitySetFinder.findByRelationType(
149                            userId, type, start, end);
150            }
151    
152            @Override
153            public int getRelationActivitySetsCount(long userId)
154                    throws SystemException {
155    
156                    return socialActivitySetFinder.countByRelation(userId);
157            }
158    
159            @Override
160            public int getRelationActivitySetsCount(long userId, int type)
161                    throws SystemException {
162    
163                    return socialActivitySetFinder.countByRelationType(userId, type);
164            }
165    
166            @Override
167            public SocialActivitySet getUserActivitySet(
168                            long groupId, long userId, int type)
169                    throws SystemException {
170    
171                    return socialActivitySetPersistence.fetchByG_U_T_First(
172                            groupId, userId, type,
173                            new SocialActivitySetModifiedDateComparator());
174            }
175    
176            @Override
177            public SocialActivitySet getUserActivitySet(
178                            long groupId, long userId, long classNameId, int type)
179                    throws SystemException {
180    
181                    return socialActivitySetPersistence.fetchByG_U_C_T_First(
182                            groupId, userId, classNameId, type,
183                            new SocialActivitySetModifiedDateComparator());
184            }
185    
186            @Override
187            public List<SocialActivitySet> getUserActivitySets(
188                            long userId, int start, int end)
189                    throws SystemException {
190    
191                    return socialActivitySetPersistence.findByUserId(userId, start, end);
192            }
193    
194            @Override
195            public int getUserActivitySetsCount(long userId) throws SystemException {
196                    return socialActivitySetPersistence.countByUserId(userId);
197            }
198    
199            @Override
200            public List<SocialActivitySet> getUserGroupsActivitySets(
201                            long userId, int start, int end)
202                    throws SystemException {
203    
204                    return socialActivitySetFinder.findByUserGroups(userId, start, end);
205            }
206    
207            @Override
208            public int getUserGroupsActivitySetsCount(long userId)
209                    throws SystemException {
210    
211                    return socialActivitySetFinder.countByUserGroups(userId);
212            }
213    
214            @Override
215            public List<SocialActivitySet> getUserViewableActivitySets(
216                            long userId, int start, int end)
217                    throws SystemException {
218    
219                    return socialActivitySetFinder.findByUser(userId, start, end);
220            }
221    
222            @Override
223            public int getUserViewableActivitySetsCount(long userId)
224                    throws SystemException {
225    
226                    return socialActivitySetFinder.countByUser(userId);
227            }
228    
229            @Override
230            public void incrementActivityCount(long activitySetId, long activityId)
231                    throws PortalException, SystemException {
232    
233                    // Activity set
234    
235                    SocialActivitySet activitySet =
236                            socialActivitySetPersistence.findByPrimaryKey(activitySetId);
237    
238                    SocialActivity activity = socialActivityPersistence.findByPrimaryKey(
239                            activityId);
240    
241                    activitySet.setModifiedDate(activity.getCreateDate());
242                    activitySet.setUserId(activity.getUserId());
243    
244                    activitySet.setActivityCount(activitySet.getActivityCount() + 1);
245    
246                    socialActivitySetPersistence.update(activitySet);
247    
248                    // Activity
249    
250                    activity.setActivitySetId(activitySetId);
251    
252                    socialActivityPersistence.update(activity);
253            }
254    
255    }