public void addActivityAchievement(long userId, long groupId, SocialAchievement achievement)
      throws PortalException, SystemException {

    SocialActivityAchievement activityAchievement =
        socialActivityAchievementPersistence.fetchByG_U_N(groupId, userId, achievement.getName());

    if (activityAchievement != null) {
      return;
    }

    User user = userPersistence.findByPrimaryKey(userId);

    long activityAchievementId = counterLocalService.increment();

    activityAchievement = socialActivityAchievementPersistence.create(activityAchievementId);

    activityAchievement.setGroupId(groupId);
    activityAchievement.setCompanyId(user.getCompanyId());
    activityAchievement.setUserId(userId);
    activityAchievement.setCreateDate(System.currentTimeMillis());

    int count = socialActivityAchievementPersistence.countByG_N(groupId, achievement.getName());

    if (count == 0) {
      activityAchievement.setFirstInGroup(true);
    }

    activityAchievement.setName(achievement.getName());

    socialActivityAchievementPersistence.update(activityAchievement, false);

    socialActivityCounterLocalService.incrementUserAchievementCounter(userId, groupId);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    SocialActivityAchievement newSocialActivityAchievement = _persistence.create(pk);

    newSocialActivityAchievement.setGroupId(ServiceTestUtil.nextLong());

    newSocialActivityAchievement.setCompanyId(ServiceTestUtil.nextLong());

    newSocialActivityAchievement.setUserId(ServiceTestUtil.nextLong());

    newSocialActivityAchievement.setCreateDate(ServiceTestUtil.nextLong());

    newSocialActivityAchievement.setName(ServiceTestUtil.randomString());

    newSocialActivityAchievement.setFirstInGroup(ServiceTestUtil.randomBoolean());

    _persistence.update(newSocialActivityAchievement);

    SocialActivityAchievement existingSocialActivityAchievement =
        _persistence.findByPrimaryKey(newSocialActivityAchievement.getPrimaryKey());

    Assert.assertEquals(
        existingSocialActivityAchievement.getActivityAchievementId(),
        newSocialActivityAchievement.getActivityAchievementId());
    Assert.assertEquals(
        existingSocialActivityAchievement.getGroupId(), newSocialActivityAchievement.getGroupId());
    Assert.assertEquals(
        existingSocialActivityAchievement.getCompanyId(),
        newSocialActivityAchievement.getCompanyId());
    Assert.assertEquals(
        existingSocialActivityAchievement.getUserId(), newSocialActivityAchievement.getUserId());
    Assert.assertEquals(
        existingSocialActivityAchievement.getCreateDate(),
        newSocialActivityAchievement.getCreateDate());
    Assert.assertEquals(
        existingSocialActivityAchievement.getName(), newSocialActivityAchievement.getName());
    Assert.assertEquals(
        existingSocialActivityAchievement.getFirstInGroup(),
        newSocialActivityAchievement.getFirstInGroup());
  }
  protected SocialActivityAchievement addSocialActivityAchievement() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    SocialActivityAchievement socialActivityAchievement = _persistence.create(pk);

    socialActivityAchievement.setGroupId(ServiceTestUtil.nextLong());

    socialActivityAchievement.setCompanyId(ServiceTestUtil.nextLong());

    socialActivityAchievement.setUserId(ServiceTestUtil.nextLong());

    socialActivityAchievement.setCreateDate(ServiceTestUtil.nextLong());

    socialActivityAchievement.setName(ServiceTestUtil.randomString());

    socialActivityAchievement.setFirstInGroup(ServiceTestUtil.randomBoolean());

    _persistence.update(socialActivityAchievement);

    return socialActivityAchievement;
  }