public void deleteFeeByDateIdx(
     CFSecurityAuthorization Authorization,
     long argTenantId,
     long argAccountId,
     Calendar argFeeDate) {
   CFAccFeeByDateIdxKey key = schema.getFactoryFee().newDateIdxKey();
   key.setRequiredTenantId(argTenantId);
   key.setRequiredAccountId(argAccountId);
   key.setRequiredFeeDate(argFeeDate);
   deleteFeeByDateIdx(Authorization, key);
 }
  public void deleteFee(CFSecurityAuthorization Authorization, CFAccFeeBuff Buff) {
    final String S_ProcName = "CFAccRamFeeTable.deleteFee() ";
    CFAccFeePKey pkey = schema.getFactoryFee().newPKey();
    pkey.setRequiredTenantId(Buff.getRequiredTenantId());
    pkey.setRequiredFeeId(Buff.getRequiredFeeId());
    CFAccFeeBuff existing = dictByPKey.get(pkey);
    if (existing == null) {
      return;
    }
    if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
      throw CFLib.getDefaultExceptionFactory()
          .newCollisionDetectedException(getClass(), "deleteFee", pkey);
    }
    CFAccFeeByDateIdxKey keyDateIdx = schema.getFactoryFee().newDateIdxKey();
    keyDateIdx.setRequiredTenantId(existing.getRequiredTenantId());
    keyDateIdx.setRequiredAccountId(existing.getRequiredAccountId());
    keyDateIdx.setRequiredFeeDate(existing.getRequiredFeeDate());

    CFAccFeeByTenantIdxKey keyTenantIdx = schema.getFactoryFee().newTenantIdxKey();
    keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

    CFAccFeeByAcctIdxKey keyAcctIdx = schema.getFactoryFee().newAcctIdxKey();
    keyAcctIdx.setRequiredTenantId(existing.getRequiredTenantId());
    keyAcctIdx.setRequiredAccountId(existing.getRequiredAccountId());

    CFAccFeeByLedgerIdxKey keyLedgerIdx = schema.getFactoryFee().newLedgerIdxKey();
    keyLedgerIdx.setOptionalLedgerTenantId(existing.getOptionalLedgerTenantId());
    keyLedgerIdx.setOptionalLedgerId(existing.getOptionalLedgerId());

    // Validate reverse foreign keys

    // Delete is valid

    schema
        .getTableFeeDetail()
        .deleteFeeDetailByFeeIdx(
            Authorization, Buff.getRequiredTenantId(), Buff.getRequiredFeeId());
    Map<CFAccFeePKey, CFAccFeeBuff> subdict;

    dictByPKey.remove(pkey);

    subdict = dictByDateIdx.get(keyDateIdx);
    subdict.remove(pkey);

    subdict = dictByTenantIdx.get(keyTenantIdx);
    subdict.remove(pkey);

    subdict = dictByAcctIdx.get(keyAcctIdx);
    subdict.remove(pkey);

    subdict = dictByLedgerIdx.get(keyLedgerIdx);
    subdict.remove(pkey);
  }
  public CFSecurityCursor openFeeCursorByDateIdx(
      CFSecurityAuthorization Authorization, long TenantId, long AccountId, Calendar FeeDate) {
    CFSecurityCursor cursor;
    CFAccFeeByDateIdxKey key = schema.getFactoryFee().newDateIdxKey();
    key.setRequiredTenantId(TenantId);
    key.setRequiredAccountId(AccountId);
    key.setRequiredFeeDate(FeeDate);

    if (dictByDateIdx.containsKey(key)) {
      Map<CFAccFeePKey, CFAccFeeBuff> subdictDateIdx = dictByDateIdx.get(key);
      cursor = new CFAccRamFeeCursor(Authorization, schema, subdictDateIdx.values());
    } else {
      cursor = new CFAccRamFeeCursor(Authorization, schema, new ArrayList<CFAccFeeBuff>());
    }
    return (cursor);
  }
  public CFAccFeeBuff[] readDerivedByDateIdx(
      CFSecurityAuthorization Authorization, long TenantId, long AccountId, Calendar FeeDate) {
    final String S_ProcName = "CFAccRamFee.readDerivedByDateIdx";
    CFAccFeeByDateIdxKey key = schema.getFactoryFee().newDateIdxKey();
    key.setRequiredTenantId(TenantId);
    key.setRequiredAccountId(AccountId);
    key.setRequiredFeeDate(FeeDate);

    CFAccFeeBuff[] recArray;
    if (dictByDateIdx.containsKey(key)) {
      Map<CFAccFeePKey, CFAccFeeBuff> subdictDateIdx = dictByDateIdx.get(key);
      recArray = new CFAccFeeBuff[subdictDateIdx.size()];
      Iterator<CFAccFeeBuff> iter = subdictDateIdx.values().iterator();
      int idx = 0;
      while (iter.hasNext()) {
        recArray[idx++] = iter.next();
      }
    } else {
      Map<CFAccFeePKey, CFAccFeeBuff> subdictDateIdx = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByDateIdx.put(key, subdictDateIdx);
      recArray = new CFAccFeeBuff[0];
    }
    return (recArray);
  }
  public void createFee(CFSecurityAuthorization Authorization, CFAccFeeBuff Buff) {
    final String S_ProcName = "createFee";
    CFAccFeePKey pkey = schema.getFactoryFee().newPKey();
    pkey.setRequiredTenantId(Buff.getRequiredTenantId());
    pkey.setRequiredFeeId(
        ((CFAccRamTenantTable) schema.getTableTenant())
            .nextFeeIdGen(Authorization, Buff.getRequiredTenantId()));
    Buff.setRequiredTenantId(pkey.getRequiredTenantId());
    Buff.setRequiredFeeId(pkey.getRequiredFeeId());
    CFAccFeeByDateIdxKey keyDateIdx = schema.getFactoryFee().newDateIdxKey();
    keyDateIdx.setRequiredTenantId(Buff.getRequiredTenantId());
    keyDateIdx.setRequiredAccountId(Buff.getRequiredAccountId());
    keyDateIdx.setRequiredFeeDate(Buff.getRequiredFeeDate());

    CFAccFeeByTenantIdxKey keyTenantIdx = schema.getFactoryFee().newTenantIdxKey();
    keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

    CFAccFeeByAcctIdxKey keyAcctIdx = schema.getFactoryFee().newAcctIdxKey();
    keyAcctIdx.setRequiredTenantId(Buff.getRequiredTenantId());
    keyAcctIdx.setRequiredAccountId(Buff.getRequiredAccountId());

    CFAccFeeByLedgerIdxKey keyLedgerIdx = schema.getFactoryFee().newLedgerIdxKey();
    keyLedgerIdx.setOptionalLedgerTenantId(Buff.getOptionalLedgerTenantId());
    keyLedgerIdx.setOptionalLedgerId(Buff.getOptionalLedgerId());

    // Validate unique indexes

    if (dictByPKey.containsKey(pkey)) {
      throw CFLib.getDefaultExceptionFactory()
          .newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey);
    }

    // Validate foreign keys

    {
      boolean allNull = true;
      allNull = false;
      allNull = false;
      if (!allNull) {
        if (null
            == schema
                .getTableAccount()
                .readDerivedByIdIdx(
                    Authorization, Buff.getRequiredTenantId(), Buff.getRequiredAccountId())) {
          throw CFLib.getDefaultExceptionFactory()
              .newUnresolvedRelationException(
                  getClass(), S_ProcName, "Container", "Account", "Account", null);
        }
      }
    }

    {
      boolean allNull = true;
      allNull = false;
      if (!allNull) {
        if (null
            == schema
                .getTableTenant()
                .readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) {
          throw CFLib.getDefaultExceptionFactory()
              .newUnresolvedRelationException(
                  getClass(), S_ProcName, "Owner", "Tenant", "Tenant", null);
        }
      }
    }

    // Proceed with adding the new record

    dictByPKey.put(pkey, Buff);

    Map<CFAccFeePKey, CFAccFeeBuff> subdictDateIdx;
    if (dictByDateIdx.containsKey(keyDateIdx)) {
      subdictDateIdx = dictByDateIdx.get(keyDateIdx);
    } else {
      subdictDateIdx = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByDateIdx.put(keyDateIdx, subdictDateIdx);
    }
    subdictDateIdx.put(pkey, Buff);

    Map<CFAccFeePKey, CFAccFeeBuff> subdictTenantIdx;
    if (dictByTenantIdx.containsKey(keyTenantIdx)) {
      subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx);
    } else {
      subdictTenantIdx = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx);
    }
    subdictTenantIdx.put(pkey, Buff);

    Map<CFAccFeePKey, CFAccFeeBuff> subdictAcctIdx;
    if (dictByAcctIdx.containsKey(keyAcctIdx)) {
      subdictAcctIdx = dictByAcctIdx.get(keyAcctIdx);
    } else {
      subdictAcctIdx = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByAcctIdx.put(keyAcctIdx, subdictAcctIdx);
    }
    subdictAcctIdx.put(pkey, Buff);

    Map<CFAccFeePKey, CFAccFeeBuff> subdictLedgerIdx;
    if (dictByLedgerIdx.containsKey(keyLedgerIdx)) {
      subdictLedgerIdx = dictByLedgerIdx.get(keyLedgerIdx);
    } else {
      subdictLedgerIdx = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByLedgerIdx.put(keyLedgerIdx, subdictLedgerIdx);
    }
    subdictLedgerIdx.put(pkey, Buff);
  }
  public void updateFee(CFSecurityAuthorization Authorization, CFAccFeeBuff Buff) {
    CFAccFeePKey pkey = schema.getFactoryFee().newPKey();
    pkey.setRequiredTenantId(Buff.getRequiredTenantId());
    pkey.setRequiredFeeId(Buff.getRequiredFeeId());
    CFAccFeeBuff existing = dictByPKey.get(pkey);
    if (existing == null) {
      throw CFLib.getDefaultExceptionFactory()
          .newStaleCacheDetectedException(
              getClass(), "updateFee", "Existing record not found", "Fee", pkey);
    }
    if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
      throw CFLib.getDefaultExceptionFactory()
          .newCollisionDetectedException(getClass(), "updateFee", pkey);
    }
    Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
    CFAccFeeByDateIdxKey existingKeyDateIdx = schema.getFactoryFee().newDateIdxKey();
    existingKeyDateIdx.setRequiredTenantId(existing.getRequiredTenantId());
    existingKeyDateIdx.setRequiredAccountId(existing.getRequiredAccountId());
    existingKeyDateIdx.setRequiredFeeDate(existing.getRequiredFeeDate());

    CFAccFeeByDateIdxKey newKeyDateIdx = schema.getFactoryFee().newDateIdxKey();
    newKeyDateIdx.setRequiredTenantId(Buff.getRequiredTenantId());
    newKeyDateIdx.setRequiredAccountId(Buff.getRequiredAccountId());
    newKeyDateIdx.setRequiredFeeDate(Buff.getRequiredFeeDate());

    CFAccFeeByTenantIdxKey existingKeyTenantIdx = schema.getFactoryFee().newTenantIdxKey();
    existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

    CFAccFeeByTenantIdxKey newKeyTenantIdx = schema.getFactoryFee().newTenantIdxKey();
    newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

    CFAccFeeByAcctIdxKey existingKeyAcctIdx = schema.getFactoryFee().newAcctIdxKey();
    existingKeyAcctIdx.setRequiredTenantId(existing.getRequiredTenantId());
    existingKeyAcctIdx.setRequiredAccountId(existing.getRequiredAccountId());

    CFAccFeeByAcctIdxKey newKeyAcctIdx = schema.getFactoryFee().newAcctIdxKey();
    newKeyAcctIdx.setRequiredTenantId(Buff.getRequiredTenantId());
    newKeyAcctIdx.setRequiredAccountId(Buff.getRequiredAccountId());

    CFAccFeeByLedgerIdxKey existingKeyLedgerIdx = schema.getFactoryFee().newLedgerIdxKey();
    existingKeyLedgerIdx.setOptionalLedgerTenantId(existing.getOptionalLedgerTenantId());
    existingKeyLedgerIdx.setOptionalLedgerId(existing.getOptionalLedgerId());

    CFAccFeeByLedgerIdxKey newKeyLedgerIdx = schema.getFactoryFee().newLedgerIdxKey();
    newKeyLedgerIdx.setOptionalLedgerTenantId(Buff.getOptionalLedgerTenantId());
    newKeyLedgerIdx.setOptionalLedgerId(Buff.getOptionalLedgerId());

    // Check unique indexes

    // Validate foreign keys

    {
      boolean allNull = true;

      if (allNull) {
        if (null
            == schema
                .getTableAccount()
                .readDerivedByIdIdx(
                    Authorization, Buff.getRequiredTenantId(), Buff.getRequiredAccountId())) {
          throw CFLib.getDefaultExceptionFactory()
              .newUnresolvedRelationException(
                  getClass(), "updateFee", "Container", "Account", "Account", null);
        }
      }
    }

    {
      boolean allNull = true;

      if (allNull) {
        if (null
            == schema
                .getTableTenant()
                .readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) {
          throw CFLib.getDefaultExceptionFactory()
              .newUnresolvedRelationException(
                  getClass(), "updateFee", "Owner", "Tenant", "Tenant", null);
        }
      }
    }

    // Update is valid

    Map<CFAccFeePKey, CFAccFeeBuff> subdict;

    dictByPKey.remove(pkey);
    dictByPKey.put(pkey, Buff);

    subdict = dictByDateIdx.get(existingKeyDateIdx);
    if (subdict != null) {
      subdict.remove(pkey);
    }
    if (dictByDateIdx.containsKey(newKeyDateIdx)) {
      subdict = dictByDateIdx.get(newKeyDateIdx);
    } else {
      subdict = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByDateIdx.put(newKeyDateIdx, subdict);
    }
    subdict.put(pkey, Buff);

    subdict = dictByTenantIdx.get(existingKeyTenantIdx);
    if (subdict != null) {
      subdict.remove(pkey);
    }
    if (dictByTenantIdx.containsKey(newKeyTenantIdx)) {
      subdict = dictByTenantIdx.get(newKeyTenantIdx);
    } else {
      subdict = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByTenantIdx.put(newKeyTenantIdx, subdict);
    }
    subdict.put(pkey, Buff);

    subdict = dictByAcctIdx.get(existingKeyAcctIdx);
    if (subdict != null) {
      subdict.remove(pkey);
    }
    if (dictByAcctIdx.containsKey(newKeyAcctIdx)) {
      subdict = dictByAcctIdx.get(newKeyAcctIdx);
    } else {
      subdict = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByAcctIdx.put(newKeyAcctIdx, subdict);
    }
    subdict.put(pkey, Buff);

    subdict = dictByLedgerIdx.get(existingKeyLedgerIdx);
    if (subdict != null) {
      subdict.remove(pkey);
    }
    if (dictByLedgerIdx.containsKey(newKeyLedgerIdx)) {
      subdict = dictByLedgerIdx.get(newKeyLedgerIdx);
    } else {
      subdict = new HashMap<CFAccFeePKey, CFAccFeeBuff>();
      dictByLedgerIdx.put(newKeyLedgerIdx, subdict);
    }
    subdict.put(pkey, Buff);
  }