public void testFindByPrimaryKeyExisting() throws Exception {
    SocialEquityLog newSocialEquityLog = addSocialEquityLog();

    SocialEquityLog existingSocialEquityLog =
        _persistence.findByPrimaryKey(newSocialEquityLog.getPrimaryKey());

    assertEquals(existingSocialEquityLog, newSocialEquityLog);
  }
  public void testCreate() throws Exception {
    long pk = nextLong();

    SocialEquityLog socialEquityLog = _persistence.create(pk);

    assertNotNull(socialEquityLog);

    assertEquals(socialEquityLog.getPrimaryKey(), pk);
  }
  public void testRemove() throws Exception {
    SocialEquityLog newSocialEquityLog = addSocialEquityLog();

    _persistence.remove(newSocialEquityLog);

    SocialEquityLog existingSocialEquityLog =
        _persistence.fetchByPrimaryKey(newSocialEquityLog.getPrimaryKey());

    assertNull(existingSocialEquityLog);
  }
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    SocialEquityLog newSocialEquityLog = addSocialEquityLog();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            SocialEquityLog.class, SocialEquityLog.class.getClassLoader());

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("equityLogId", newSocialEquityLog.getEquityLogId()));

    List<SocialEquityLog> result = _persistence.findWithDynamicQuery(dynamicQuery);

    assertEquals(1, result.size());

    SocialEquityLog existingSocialEquityLog = result.get(0);

    assertEquals(existingSocialEquityLog, newSocialEquityLog);
  }
  public void deactivateEquityLogs(long assetEntryId) throws PortalException, SystemException {

    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
      return;
    }

    SocialEquityAssetEntry equityAssetEntry = null;

    try {
      equityAssetEntry = socialEquityAssetEntryPersistence.findByAssetEntryId(assetEntryId);

      socialEquityAssetEntryPersistence.removeByAssetEntryId(assetEntryId);
    } catch (NoSuchEquityAssetEntryException nseaee) {
      return;
    }

    User user = null;

    try {
      user = userPersistence.findByPrimaryKey(equityAssetEntry.getUserId());

      if (!user.isDefaultUser()) {
        SocialEquityValue socialEquityValue =
            new SocialEquityValue(
                -equityAssetEntry.getInformationK(), -equityAssetEntry.getInformationB());

        incrementSocialEquityUser_CQ(
            equityAssetEntry.getGroupId(), user.getUserId(), socialEquityValue);
      }
    } catch (NoSuchUserException nsue) {
    }

    List<SocialEquityLog> equityLogs =
        socialEquityLogPersistence.findByAEI_T_A(
            assetEntryId, SocialEquitySettingConstants.TYPE_INFORMATION, true);

    for (SocialEquityLog equityLog : equityLogs) {
      equityLog.setActive(false);

      socialEquityLogPersistence.update(equityLog, false);
    }
  }
  public void testDynamicQueryByProjectionExisting() throws Exception {
    SocialEquityLog newSocialEquityLog = addSocialEquityLog();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(
            SocialEquityLog.class, SocialEquityLog.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("equityLogId"));

    Object newEquityLogId = newSocialEquityLog.getEquityLogId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("equityLogId", new Object[] {newEquityLogId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    assertEquals(1, result.size());

    Object existingEquityLogId = result.get(0);

    assertEquals(existingEquityLogId, newEquityLogId);
  }
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    SocialEquityLog newSocialEquityLog = addSocialEquityLog();

    _persistence.clearCache();

    SocialEquityLogModelImpl existingSocialEquityLogModelImpl =
        (SocialEquityLogModelImpl)
            _persistence.findByPrimaryKey(newSocialEquityLog.getPrimaryKey());

    assertEquals(
        existingSocialEquityLogModelImpl.getUserId(),
        existingSocialEquityLogModelImpl.getOriginalUserId());
    assertEquals(
        existingSocialEquityLogModelImpl.getAssetEntryId(),
        existingSocialEquityLogModelImpl.getOriginalAssetEntryId());
    assertTrue(
        Validator.equals(
            existingSocialEquityLogModelImpl.getActionId(),
            existingSocialEquityLogModelImpl.getOriginalActionId()));
    assertEquals(
        existingSocialEquityLogModelImpl.getActionDate(),
        existingSocialEquityLogModelImpl.getOriginalActionDate());
    assertEquals(
        existingSocialEquityLogModelImpl.getActive(),
        existingSocialEquityLogModelImpl.getOriginalActive());
    assertEquals(
        existingSocialEquityLogModelImpl.getType(),
        existingSocialEquityLogModelImpl.getOriginalType());
    assertTrue(
        Validator.equals(
            existingSocialEquityLogModelImpl.getExtraData(),
            existingSocialEquityLogModelImpl.getOriginalExtraData()));
  }
  protected void addEquityLog(
      User user, AssetEntry assetEntry, User assetEntryUser, SocialEquitySetting equitySetting)
      throws PortalException, SystemException {

    if (!isAddEquityLog(user.getUserId(), assetEntry.getEntryId(), equitySetting)) {

      return;
    }

    int actionDate = getEquityDate();

    double k = calculateK(equitySetting.getValue(), equitySetting.getLifespan());
    double b = calculateB(actionDate, equitySetting.getValue(), equitySetting.getLifespan());

    SocialEquityValue socialEquity = new SocialEquityValue(k, b);

    if (equitySetting.getType() == SocialEquitySettingConstants.TYPE_INFORMATION) {

      socialEquityLogLocalService.incrementSocialEquityAssetEntry_IQ(
          assetEntry.getEntryId(), socialEquity);

      if ((assetEntryUser != null) && !assetEntryUser.isDefaultUser()) {
        socialEquityLogLocalService.incrementSocialEquityUser_CQ(
            assetEntry.getGroupId(), assetEntryUser.getUserId(), socialEquity);
      }
    } else if (equitySetting.getType() == SocialEquitySettingConstants.TYPE_PARTICIPATION) {

      if (!user.isDefaultUser()) {
        socialEquityLogLocalService.incrementSocialEquityUser_PQ(
            assetEntry.getGroupId(), user.getUserId(), socialEquity);
      }
    }

    long equityLogId = counterLocalService.increment();

    SocialEquityLog equityLog = socialEquityLogPersistence.create(equityLogId);

    equityLog.setGroupId(assetEntry.getGroupId());
    equityLog.setCompanyId(user.getCompanyId());
    equityLog.setUserId(user.getUserId());
    equityLog.setAssetEntryId(assetEntry.getEntryId());
    equityLog.setActionId(equitySetting.getActionId());
    equityLog.setActionDate(actionDate);
    equityLog.setType(equitySetting.getType());
    equityLog.setValue(equitySetting.getValue());
    equityLog.setExpiration(actionDate + equitySetting.getLifespan());
    equityLog.setActive(true);

    socialEquityLogPersistence.update(equityLog, false);
  }
  public void deactivateEquityLogs(long userId, long assetEntryId, String actionId)
      throws PortalException, SystemException {

    if (!PropsValues.SOCIAL_EQUITY_EQUITY_LOG_ENABLED) {
      return;
    }

    User user = userPersistence.findByPrimaryKey(userId);

    AssetEntry assetEntry = assetEntryPersistence.findByPrimaryKey(assetEntryId);

    // Information Equity

    if (isSocialEquityEnabled(
        assetEntry.getGroupId(),
        assetEntry.getClassName(),
        SocialEquitySettingConstants.TYPE_INFORMATION)) {

      List<SocialEquityLog> equityLogs =
          socialEquityLogPersistence.findByAEI_AID_A_T(
              assetEntryId, actionId, true, SocialEquitySettingConstants.TYPE_INFORMATION);

      SocialEquityValue socialEquityValue = new SocialEquityValue(0, 0);

      for (SocialEquityLog equityLog : equityLogs) {
        double k = calculateK(equityLog.getValue(), equityLog.getLifespan());
        double b =
            calculateB(equityLog.getActionDate(), equityLog.getValue(), equityLog.getLifespan());

        socialEquityValue.subtract(new SocialEquityValue(k, b));

        socialEquityLogPersistence.remove(equityLog);
      }

      socialEquityLogLocalService.incrementSocialEquityAssetEntry_IQ(
          assetEntryId, socialEquityValue);

      socialEquityLogLocalService.incrementSocialEquityUser_CQ(
          assetEntry.getGroupId(), assetEntry.getUserId(), socialEquityValue);
    }

    // Participation Equity

    if (isSocialEquityEnabled(
        assetEntry.getGroupId(),
        assetEntry.getClassName(),
        SocialEquitySettingConstants.TYPE_PARTICIPATION)) {

      List<SocialEquityLog> equityLogs =
          socialEquityLogPersistence.findByU_AID_A_T(
              userId, actionId, true, SocialEquitySettingConstants.TYPE_PARTICIPATION);

      SocialEquityValue socialEquityValue = new SocialEquityValue(0, 0);

      for (SocialEquityLog equityLog : equityLogs) {
        double k = calculateK(equityLog.getValue(), equityLog.getLifespan());
        double b =
            calculateB(equityLog.getActionDate(), equityLog.getValue(), equityLog.getLifespan());

        socialEquityValue.subtract(new SocialEquityValue(k, b));

        socialEquityLogPersistence.remove(equityLog);
      }

      socialEquityLogLocalService.incrementSocialEquityUser_PQ(
          user.getGroup().getGroupId(), userId, socialEquityValue);
    }
  }
  public void testUpdateExisting() throws Exception {
    long pk = nextLong();

    SocialEquityLog newSocialEquityLog = _persistence.create(pk);

    newSocialEquityLog.setGroupId(nextLong());
    newSocialEquityLog.setCompanyId(nextLong());
    newSocialEquityLog.setUserId(nextLong());
    newSocialEquityLog.setAssetEntryId(nextLong());
    newSocialEquityLog.setActionId(randomString());
    newSocialEquityLog.setActionDate(nextInt());
    newSocialEquityLog.setActive(randomBoolean());
    newSocialEquityLog.setExpiration(nextInt());
    newSocialEquityLog.setType(nextInt());
    newSocialEquityLog.setValue(nextInt());
    newSocialEquityLog.setExtraData(randomString());

    _persistence.update(newSocialEquityLog, false);

    SocialEquityLog existingSocialEquityLog =
        _persistence.findByPrimaryKey(newSocialEquityLog.getPrimaryKey());

    assertEquals(existingSocialEquityLog.getEquityLogId(), newSocialEquityLog.getEquityLogId());
    assertEquals(existingSocialEquityLog.getGroupId(), newSocialEquityLog.getGroupId());
    assertEquals(existingSocialEquityLog.getCompanyId(), newSocialEquityLog.getCompanyId());
    assertEquals(existingSocialEquityLog.getUserId(), newSocialEquityLog.getUserId());
    assertEquals(existingSocialEquityLog.getAssetEntryId(), newSocialEquityLog.getAssetEntryId());
    assertEquals(existingSocialEquityLog.getActionId(), newSocialEquityLog.getActionId());
    assertEquals(existingSocialEquityLog.getActionDate(), newSocialEquityLog.getActionDate());
    assertEquals(existingSocialEquityLog.getActive(), newSocialEquityLog.getActive());
    assertEquals(existingSocialEquityLog.getExpiration(), newSocialEquityLog.getExpiration());
    assertEquals(existingSocialEquityLog.getType(), newSocialEquityLog.getType());
    assertEquals(existingSocialEquityLog.getValue(), newSocialEquityLog.getValue());
    assertEquals(existingSocialEquityLog.getExtraData(), newSocialEquityLog.getExtraData());
  }
  protected SocialEquityLog addSocialEquityLog() throws Exception {
    long pk = nextLong();

    SocialEquityLog socialEquityLog = _persistence.create(pk);

    socialEquityLog.setGroupId(nextLong());
    socialEquityLog.setCompanyId(nextLong());
    socialEquityLog.setUserId(nextLong());
    socialEquityLog.setAssetEntryId(nextLong());
    socialEquityLog.setActionId(randomString());
    socialEquityLog.setActionDate(nextInt());
    socialEquityLog.setActive(randomBoolean());
    socialEquityLog.setExpiration(nextInt());
    socialEquityLog.setType(nextInt());
    socialEquityLog.setValue(nextInt());
    socialEquityLog.setExtraData(randomString());

    _persistence.update(socialEquityLog, false);

    return socialEquityLog;
  }