001    /**
002     * Copyright (c) 2000-2012 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            public void addActivityAchievement(
034                            long userId, long groupId, SocialAchievement achievement)
035                    throws PortalException, SystemException {
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, false);
067    
068                    socialActivityCounterLocalService.incrementUserAchievementCounter(
069                            userId, groupId);
070            }
071    
072            public SocialActivityAchievement fetchUserAchievement(
073                            long userId, long groupId, String name)
074                    throws SystemException {
075    
076                    return socialActivityAchievementPersistence.fetchByG_U_N(
077                            groupId, userId, name);
078            }
079    
080            public List<SocialActivityAchievement> getGroupAchievements(long groupId)
081                    throws SystemException {
082    
083                    return socialActivityAchievementPersistence.findByGroupId(groupId);
084            }
085    
086            public List<SocialActivityAchievement> getGroupAchievements(
087                            long groupId, String name)
088                    throws SystemException {
089    
090                    return socialActivityAchievementPersistence.findByG_N(groupId, name);
091            }
092    
093            public int getGroupAchievementsCount(long groupId)
094                    throws SystemException {
095    
096                    return socialActivityAchievementPersistence.countByGroupId(groupId);
097            }
098    
099            public int getGroupAchievementsCount(long groupId, String name)
100                    throws SystemException {
101    
102                    return socialActivityAchievementPersistence.countByG_N(groupId, name);
103            }
104    
105            public List<SocialActivityAchievement> getGroupFirstAchievements(
106                            long groupId)
107                    throws SystemException {
108    
109                    return socialActivityAchievementPersistence.findByG_F(groupId, true);
110            }
111    
112            public int getGroupFirstAchievementsCount(long groupId)
113                    throws SystemException {
114    
115                    return socialActivityAchievementPersistence.countByG_F(groupId, true);
116            }
117    
118            public int getUserAchievementCount(long userId, long groupId, String name)
119                    throws SystemException {
120    
121                    return socialActivityAchievementPersistence.countByG_U_N(
122                            groupId, userId, name);
123            }
124    
125            public List<SocialActivityAchievement> getUserAchievements(
126                            long userId, long groupId, String name)
127                    throws SystemException {
128    
129                    return socialActivityAchievementPersistence.findByG_U(groupId, userId);
130            }
131    
132    }