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