001    /**
002     * Copyright (c) 2000-2011 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    /**
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    }