001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    /**
025     * @author Zsolt Berentey
026     * @author Brian Wing Shun Chan
027     */
028    public class SocialActivityAchievementLocalServiceImpl
029            extends SocialActivityAchievementLocalServiceBaseImpl {
030    
031            public void addActivityAchievement(
032                            long userId, long groupId, SocialAchievement achievement)
033                    throws PortalException, SystemException {
034    
035                    SocialActivityAchievement activityAchievement =
036                            socialActivityAchievementPersistence.fetchByG_U_N(
037                                    groupId, userId, achievement.getName());
038    
039                    if (activityAchievement != null) {
040                            return;
041                    }
042    
043                    User user = userPersistence.findByPrimaryKey(userId);
044    
045                    long activityAchievementId = counterLocalService.increment();
046    
047                    activityAchievement = socialActivityAchievementPersistence.create(
048                            activityAchievementId);
049    
050                    activityAchievement.setGroupId(groupId);
051                    activityAchievement.setCompanyId(user.getCompanyId());
052                    activityAchievement.setUserId(userId);
053                    activityAchievement.setCreateDate(System.currentTimeMillis());
054    
055                    int count = socialActivityAchievementPersistence.countByG_N(
056                            groupId, achievement.getName());
057    
058                    if (count == 0) {
059                            activityAchievement.setFirstInGroup(true);
060                    }
061    
062                    activityAchievement.setName(achievement.getName());
063    
064                    socialActivityAchievementPersistence.update(activityAchievement, false);
065    
066                    socialActivityCounterLocalService.incrementUserAchievementCounter(
067                            userId, groupId);
068            }
069    
070    }