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.portal.model.User;
020    import com.liferay.portlet.social.model.SocialAchievement;
021    import com.liferay.portlet.social.model.SocialActivityAchievement;
022    import com.liferay.portlet.social.service.base.SocialActivityAchievementLocalServiceBaseImpl;
023    
024    import java.util.List;
025    
026    /**
027     * @author Zsolt Berentey
028     * @author Brian Wing Shun Chan
029     */
030    public class SocialActivityAchievementLocalServiceImpl
031            extends SocialActivityAchievementLocalServiceBaseImpl {
032    
033            @Override
034            public void addActivityAchievement(
035                            long userId, long groupId, SocialAchievement achievement)
036                    throws PortalException, SystemException {
037    
038                    SocialActivityAchievement activityAchievement =
039                            socialActivityAchievementPersistence.fetchByG_U_N(
040                                    groupId, userId, achievement.getName());
041    
042                    if (activityAchievement != null) {
043                            return;
044                    }
045    
046                    User user = userPersistence.findByPrimaryKey(userId);
047    
048                    long activityAchievementId = counterLocalService.increment();
049    
050                    activityAchievement = socialActivityAchievementPersistence.create(
051                            activityAchievementId);
052    
053                    activityAchievement.setGroupId(groupId);
054                    activityAchievement.setCompanyId(user.getCompanyId());
055                    activityAchievement.setUserId(userId);
056                    activityAchievement.setCreateDate(System.currentTimeMillis());
057    
058                    int count = socialActivityAchievementPersistence.countByG_N(
059                            groupId, achievement.getName());
060    
061                    if (count == 0) {
062                            activityAchievement.setFirstInGroup(true);
063                    }
064    
065                    activityAchievement.setName(achievement.getName());
066    
067                    socialActivityAchievementPersistence.update(activityAchievement, false);
068    
069                    socialActivityCounterLocalService.incrementUserAchievementCounter(
070                            userId, groupId);
071            }
072    
073            @Override
074            public SocialActivityAchievement fetchUserAchievement(
075                            long userId, long groupId, String name)
076                    throws SystemException {
077    
078                    return socialActivityAchievementPersistence.fetchByG_U_N(
079                            groupId, userId, name);
080            }
081    
082            @Override
083            public List<SocialActivityAchievement> getGroupAchievements(long groupId)
084                    throws SystemException {
085    
086                    return socialActivityAchievementPersistence.findByGroupId(groupId);
087            }
088    
089            @Override
090            public List<SocialActivityAchievement> getGroupAchievements(
091                            long groupId, String name)
092                    throws SystemException {
093    
094                    return socialActivityAchievementPersistence.findByG_N(groupId, name);
095            }
096    
097            @Override
098            public int getGroupAchievementsCount(long groupId) throws SystemException {
099                    return socialActivityAchievementPersistence.countByGroupId(groupId);
100            }
101    
102            @Override
103            public int getGroupAchievementsCount(long groupId, String name)
104                    throws SystemException {
105    
106                    return socialActivityAchievementPersistence.countByG_N(groupId, name);
107            }
108    
109            @Override
110            public List<SocialActivityAchievement> getGroupFirstAchievements(
111                            long groupId)
112                    throws SystemException {
113    
114                    return socialActivityAchievementPersistence.findByG_F(groupId, true);
115            }
116    
117            @Override
118            public int getGroupFirstAchievementsCount(long groupId)
119                    throws SystemException {
120    
121                    return socialActivityAchievementPersistence.countByG_F(groupId, true);
122            }
123    
124            @Override
125            public int getUserAchievementCount(long userId, long groupId, String name)
126                    throws SystemException {
127    
128                    return socialActivityAchievementPersistence.countByG_U_N(
129                            groupId, userId, name);
130            }
131    
132            @Override
133            public List<SocialActivityAchievement> getUserAchievements(
134                            long userId, long groupId, String name)
135                    throws SystemException {
136    
137                    return socialActivityAchievementPersistence.findByG_U(groupId, userId);
138            }
139    
140    }