net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.CFAccRamAccountTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.CFAccRamAccountTable.java

Source

// Description: Java7 in-memory RAM DbIO implementation for Account.

/*
 *   MSS Code Factory Accounting Business Application Model
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *   This program is available as free software under the GNU GPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *   
 *   Under the terms of the GPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *     
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *     
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *      This source code incorporates modified modules originally licensed
 *      under the Apache 2.0 license by MSS Code Factory including CFSecurity
 *      (net-sourceforge-msscodefactory-2.0-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and
 *      CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 1.11
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 1.11 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam;

import java.sql.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAcc.*;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*;

/*
 *   CFAccRamAccountTable in-memory RAM DbIO implementation
 *   for Account.
 */
public class CFAccRamAccountTable implements ICFAccAccountTable {
    private CFAccRamSchema schema;
    private Map<CFAccAccountPKey, CFAccAccountBuff> dictByPKey = new HashMap<CFAccAccountPKey, CFAccAccountBuff>();
    private SortedMap<CFAccAccountByUCodeIdxKey, CFAccAccountBuff> dictByUCodeIdx = new TreeMap<CFAccAccountByUCodeIdxKey, CFAccAccountBuff>();
    private SortedMap<CFAccAccountByTenantIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>> dictByTenantIdx = new TreeMap<CFAccAccountByTenantIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>>();
    private SortedMap<CFAccAccountByRollupAcctIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>> dictByRollupAcctIdx = new TreeMap<CFAccAccountByRollupAcctIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>>();
    private SortedMap<CFAccAccountByCcyIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>> dictByCcyIdx = new TreeMap<CFAccAccountByCcyIdxKey, SortedMap<CFAccAccountPKey, CFAccAccountBuff>>();

    public CFAccRamAccountTable(CFAccRamSchema argSchema) {
        schema = argSchema;
    }

    public void createAccount(CFAccAuthorization Authorization, CFAccAccountBuff Buff) {
        CFAccAccountPKey pkey = schema.getFactoryAccount().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(schema.nextAccountIdGen());
        Buff.setRequiredTenantId(pkey.getRequiredTenantId());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAccAccountByUCodeIdxKey keyUCodeIdx = schema.getFactoryAccount().newUCodeIdxKey();
        keyUCodeIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyUCodeIdx.setRequiredAccountCode(Buff.getRequiredAccountCode());

        CFAccAccountByTenantIdxKey keyTenantIdx = schema.getFactoryAccount().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAccountByRollupAcctIdxKey keyRollupAcctIdx = schema.getFactoryAccount().newRollupAcctIdxKey();
        keyRollupAcctIdx.setOptionalRollupTenantId(Buff.getOptionalRollupTenantId());
        keyRollupAcctIdx.setOptionalRollupAccountId(Buff.getOptionalRollupAccountId());

        CFAccAccountByCcyIdxKey keyCcyIdx = schema.getFactoryAccount().newCcyIdxKey();
        keyCcyIdx.setRequiredCurrencyId(Buff.getRequiredCurrencyId());

        // Validate unique indexes

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

        if (dictByUCodeIdx.containsKey(keyUCodeIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createAccount",
                    "AccountUCodeIdx", keyUCodeIdx);
        }

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByUCodeIdx.put(keyUCodeIdx, Buff);

        SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictTenantIdx;
        if (dictByTenantIdx.containsKey(keyTenantIdx)) {
            subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx);
        } else {
            subdictTenantIdx = new TreeMap<CFAccAccountPKey, CFAccAccountBuff>();
            dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx);
        }
        subdictTenantIdx.put(pkey, Buff);

        SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictRollupAcctIdx;
        if (dictByRollupAcctIdx.containsKey(keyRollupAcctIdx)) {
            subdictRollupAcctIdx = dictByRollupAcctIdx.get(keyRollupAcctIdx);
        } else {
            subdictRollupAcctIdx = new TreeMap<CFAccAccountPKey, CFAccAccountBuff>();
            dictByRollupAcctIdx.put(keyRollupAcctIdx, subdictRollupAcctIdx);
        }
        subdictRollupAcctIdx.put(pkey, Buff);

        SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictCcyIdx;
        if (dictByCcyIdx.containsKey(keyCcyIdx)) {
            subdictCcyIdx = dictByCcyIdx.get(keyCcyIdx);
        } else {
            subdictCcyIdx = new TreeMap<CFAccAccountPKey, CFAccAccountBuff>();
            dictByCcyIdx.put(keyCcyIdx, subdictCcyIdx);
        }
        subdictCcyIdx.put(pkey, Buff);

    }

    public CFAccAccountBuff readDerived(CFAccAuthorization Authorization, CFAccAccountPKey PKey) {
        final String S_ProcName = "CFAccRamAccount.readDerived() ";
        CFAccAccountPKey key = schema.getFactoryAccount().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredId(PKey.getRequiredId());
        CFAccAccountBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff lockDerived(CFAccAuthorization Authorization, CFAccAccountPKey PKey) {
        final String S_ProcName = "CFAccRamAccount.readDerived() ";
        CFAccAccountPKey key = schema.getFactoryAccount().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredId(PKey.getRequiredId());
        CFAccAccountBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff[] readAllDerived(CFAccAuthorization Authorization) {
        final String S_ProcName = "CFAccRamAccount.readAllDerived() ";
        CFAccAccountBuff[] retList = new CFAccAccountBuff[dictByPKey.values().size()];
        Iterator<CFAccAccountBuff> iter = dictByPKey.values().iterator();
        int idx = 0;
        while (iter.hasNext()) {
            retList[idx++] = iter.next();
        }
        return (retList);
    }

    public CFAccAccountBuff readDerivedByUCodeIdx(CFAccAuthorization Authorization, long TenantId,
            String AccountCode) {
        final String S_ProcName = "CFAccRamAccount.readDerivedByUCodeIdx() ";
        CFAccAccountByUCodeIdxKey key = schema.getFactoryAccount().newUCodeIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredAccountCode(AccountCode);

        CFAccAccountBuff buff;
        if (dictByUCodeIdx.containsKey(key)) {
            buff = dictByUCodeIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamAccount.readDerivedByTenantIdx() ";
        CFAccAccountByTenantIdxKey key = schema.getFactoryAccount().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

        CFAccAccountBuff[] recArray;
        if (dictByTenantIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictTenantIdx = dictByTenantIdx.get(key);
            recArray = new CFAccAccountBuff[subdictTenantIdx.size()];
            Iterator<CFAccAccountBuff> iter = subdictTenantIdx.values().iterator();
            int idx = 0;
            while (iter.hasNext()) {
                recArray[idx++] = iter.next();
            }
        } else {
            recArray = new CFAccAccountBuff[0];
        }
        return (recArray);
    }

    public CFAccAccountBuff[] readDerivedByRollupAcctIdx(CFAccAuthorization Authorization, Long RollupTenantId,
            Long RollupAccountId) {
        final String S_ProcName = "CFAccRamAccount.readDerivedByRollupAcctIdx() ";
        CFAccAccountByRollupAcctIdxKey key = schema.getFactoryAccount().newRollupAcctIdxKey();
        key.setOptionalRollupTenantId(RollupTenantId);
        key.setOptionalRollupAccountId(RollupAccountId);

        CFAccAccountBuff[] recArray;
        if (dictByRollupAcctIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictRollupAcctIdx = dictByRollupAcctIdx.get(key);
            recArray = new CFAccAccountBuff[subdictRollupAcctIdx.size()];
            Iterator<CFAccAccountBuff> iter = subdictRollupAcctIdx.values().iterator();
            int idx = 0;
            while (iter.hasNext()) {
                recArray[idx++] = iter.next();
            }
        } else {
            recArray = new CFAccAccountBuff[0];
        }
        return (recArray);
    }

    public CFAccAccountBuff[] readDerivedByCcyIdx(CFAccAuthorization Authorization, short CurrencyId) {
        final String S_ProcName = "CFAccRamAccount.readDerivedByCcyIdx() ";
        CFAccAccountByCcyIdxKey key = schema.getFactoryAccount().newCcyIdxKey();
        key.setRequiredCurrencyId(CurrencyId);

        CFAccAccountBuff[] recArray;
        if (dictByCcyIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictCcyIdx = dictByCcyIdx.get(key);
            recArray = new CFAccAccountBuff[subdictCcyIdx.size()];
            Iterator<CFAccAccountBuff> iter = subdictCcyIdx.values().iterator();
            int idx = 0;
            while (iter.hasNext()) {
                recArray[idx++] = iter.next();
            }
        } else {
            recArray = new CFAccAccountBuff[0];
        }
        return (recArray);
    }

    public CFAccAccountBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long Id) {
        final String S_ProcName = "CFAccRamAccount.readDerivedByIdIdx() ";
        CFAccAccountPKey key = schema.getFactoryAccount().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredId(Id);

        CFAccAccountBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff readBuff(CFAccAuthorization Authorization, CFAccAccountPKey PKey) {
        final String S_ProcName = "CFAccRamAccount.readBuff() ";
        CFAccAccountBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ACCT"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff lockBuff(CFAccAuthorization Authorization, CFAccAccountPKey PKey) {
        final String S_ProcName = "CFAccRamAccount.readBuff() ";
        CFAccAccountBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ACCT"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccAccountBuff[] readAllBuff(CFAccAuthorization Authorization) {
        final String S_ProcName = "CFAccRamAccount.readAllBuff() ";
        CFAccAccountBuff buff;
        ArrayList<CFAccAccountBuff> filteredList = new ArrayList<CFAccAccountBuff>();
        CFAccAccountBuff[] buffList = readAllDerived(Authorization);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ACCT")) {
                filteredList.add(buff);
            }
        }
        return (filteredList.toArray(new CFAccAccountBuff[0]));
    }

    public CFAccAccountBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long Id) {
        final String S_ProcName = "CFAccRamAccount.readBuffByIdIdx() ";
        CFAccAccountBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id);
        if ((buff != null) && buff.getClassCode().equals("ACCT")) {
            return ((CFAccAccountBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAccAccountBuff readBuffByUCodeIdx(CFAccAuthorization Authorization, long TenantId,
            String AccountCode) {
        final String S_ProcName = "CFAccRamAccount.readBuffByUCodeIdx() ";
        CFAccAccountBuff buff = readDerivedByUCodeIdx(Authorization, TenantId, AccountCode);
        if ((buff != null) && buff.getClassCode().equals("ACCT")) {
            return ((CFAccAccountBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAccAccountBuff[] readBuffByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamAccount.readBuffByTenantIdx() ";
        CFAccAccountBuff buff;
        ArrayList<CFAccAccountBuff> filteredList = new ArrayList<CFAccAccountBuff>();
        CFAccAccountBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ACCT")) {
                filteredList.add((CFAccAccountBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAccountBuff[0]));
    }

    public CFAccAccountBuff[] readBuffByRollupAcctIdx(CFAccAuthorization Authorization, Long RollupTenantId,
            Long RollupAccountId) {
        final String S_ProcName = "CFAccRamAccount.readBuffByRollupAcctIdx() ";
        CFAccAccountBuff buff;
        ArrayList<CFAccAccountBuff> filteredList = new ArrayList<CFAccAccountBuff>();
        CFAccAccountBuff[] buffList = readDerivedByRollupAcctIdx(Authorization, RollupTenantId, RollupAccountId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ACCT")) {
                filteredList.add((CFAccAccountBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAccountBuff[0]));
    }

    public CFAccAccountBuff[] readBuffByCcyIdx(CFAccAuthorization Authorization, short CurrencyId) {
        final String S_ProcName = "CFAccRamAccount.readBuffByCcyIdx() ";
        CFAccAccountBuff buff;
        ArrayList<CFAccAccountBuff> filteredList = new ArrayList<CFAccAccountBuff>();
        CFAccAccountBuff[] buffList = readDerivedByCcyIdx(Authorization, CurrencyId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ACCT")) {
                filteredList.add((CFAccAccountBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAccountBuff[0]));
    }

    public void updateAccount(CFAccAuthorization Authorization, CFAccAccountBuff Buff) {
        CFAccAccountPKey pkey = schema.getFactoryAccount().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccAccountBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAccount",
                    "Existing record not found", "Account", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAccount",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAccAccountByUCodeIdxKey existingKeyUCodeIdx = schema.getFactoryAccount().newUCodeIdxKey();
        existingKeyUCodeIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyUCodeIdx.setRequiredAccountCode(existing.getRequiredAccountCode());

        CFAccAccountByUCodeIdxKey newKeyUCodeIdx = schema.getFactoryAccount().newUCodeIdxKey();
        newKeyUCodeIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyUCodeIdx.setRequiredAccountCode(Buff.getRequiredAccountCode());

        CFAccAccountByTenantIdxKey existingKeyTenantIdx = schema.getFactoryAccount().newTenantIdxKey();
        existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAccountByTenantIdxKey newKeyTenantIdx = schema.getFactoryAccount().newTenantIdxKey();
        newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAccountByRollupAcctIdxKey existingKeyRollupAcctIdx = schema.getFactoryAccount().newRollupAcctIdxKey();
        existingKeyRollupAcctIdx.setOptionalRollupTenantId(existing.getOptionalRollupTenantId());
        existingKeyRollupAcctIdx.setOptionalRollupAccountId(existing.getOptionalRollupAccountId());

        CFAccAccountByRollupAcctIdxKey newKeyRollupAcctIdx = schema.getFactoryAccount().newRollupAcctIdxKey();
        newKeyRollupAcctIdx.setOptionalRollupTenantId(Buff.getOptionalRollupTenantId());
        newKeyRollupAcctIdx.setOptionalRollupAccountId(Buff.getOptionalRollupAccountId());

        CFAccAccountByCcyIdxKey existingKeyCcyIdx = schema.getFactoryAccount().newCcyIdxKey();
        existingKeyCcyIdx.setRequiredCurrencyId(existing.getRequiredCurrencyId());

        CFAccAccountByCcyIdxKey newKeyCcyIdx = schema.getFactoryAccount().newCcyIdxKey();
        newKeyCcyIdx.setRequiredCurrencyId(Buff.getRequiredCurrencyId());

        // Check unique indexes

        if (!existingKeyUCodeIdx.equals(newKeyUCodeIdx)) {
            if (dictByUCodeIdx.containsKey(newKeyUCodeIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateAccount", "AccountUCodeIdx", newKeyUCodeIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        // Update is valid

        SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdict;

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

        dictByUCodeIdx.remove(existingKeyUCodeIdx);
        dictByUCodeIdx.put(newKeyUCodeIdx, Buff);

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

        subdict = dictByRollupAcctIdx.get(existingKeyRollupAcctIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByRollupAcctIdx.containsKey(newKeyRollupAcctIdx)) {
            subdict = dictByRollupAcctIdx.get(newKeyRollupAcctIdx);
        } else {
            subdict = new TreeMap<CFAccAccountPKey, CFAccAccountBuff>();
            dictByRollupAcctIdx.put(newKeyRollupAcctIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByCcyIdx.get(existingKeyCcyIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByCcyIdx.containsKey(newKeyCcyIdx)) {
            subdict = dictByCcyIdx.get(newKeyCcyIdx);
        } else {
            subdict = new TreeMap<CFAccAccountPKey, CFAccAccountBuff>();
            dictByCcyIdx.put(newKeyCcyIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteAccount(CFAccAuthorization Authorization, CFAccAccountBuff Buff) {
        final String S_ProcName = "CFAccRamAccountTable.deleteAccount() ";
        CFAccAccountPKey pkey = schema.getFactoryAccount().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccAccountBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAccount",
                    pkey);
        }
        CFAccAccountByUCodeIdxKey keyUCodeIdx = schema.getFactoryAccount().newUCodeIdxKey();
        keyUCodeIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyUCodeIdx.setRequiredAccountCode(existing.getRequiredAccountCode());

        CFAccAccountByTenantIdxKey keyTenantIdx = schema.getFactoryAccount().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAccountByRollupAcctIdxKey keyRollupAcctIdx = schema.getFactoryAccount().newRollupAcctIdxKey();
        keyRollupAcctIdx.setOptionalRollupTenantId(existing.getOptionalRollupTenantId());
        keyRollupAcctIdx.setOptionalRollupAccountId(existing.getOptionalRollupAccountId());

        CFAccAccountByCcyIdxKey keyCcyIdx = schema.getFactoryAccount().newCcyIdxKey();
        keyCcyIdx.setRequiredCurrencyId(existing.getRequiredCurrencyId());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableAccount().deleteAccountByRollupAcctIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredId());
        schema.getTableAccountContact().deleteAccountContactByIdIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredId());
        schema.getTableAccountEntry().deleteAccountEntryByAcctIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredId());
        SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdict;

        dictByPKey.remove(pkey);

        dictByUCodeIdx.remove(keyUCodeIdx);

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

        subdict = dictByRollupAcctIdx.get(keyRollupAcctIdx);
        subdict.remove(pkey);

        subdict = dictByCcyIdx.get(keyCcyIdx);
        subdict.remove(pkey);

    }

    public void deleteAccountByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argId) {
        CFAccAccountPKey key = schema.getFactoryAccount().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredId(argId);
        deleteAccountByIdIdx(Authorization, key);
    }

    public void deleteAccountByIdIdx(CFAccAuthorization Authorization, CFAccAccountPKey argKey) {
        CFAccAccountBuff cur;
        LinkedList<CFAccAccountBuff> matchSet = new LinkedList<CFAccAccountBuff>();
        Iterator<CFAccAccountBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAccountBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAccount(Authorization, cur);
        }
    }

    public void deleteAccountByUCodeIdx(CFAccAuthorization Authorization, long argTenantId, String argAccountCode) {
        CFAccAccountByUCodeIdxKey key = schema.getFactoryAccount().newUCodeIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredAccountCode(argAccountCode);
        deleteAccountByUCodeIdx(Authorization, key);
    }

    public void deleteAccountByUCodeIdx(CFAccAuthorization Authorization, CFAccAccountByUCodeIdxKey argKey) {
        CFAccAccountBuff cur;
        LinkedList<CFAccAccountBuff> matchSet = new LinkedList<CFAccAccountBuff>();
        Iterator<CFAccAccountBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAccountBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAccount(Authorization, cur);
        }
    }

    public void deleteAccountByTenantIdx(CFAccAuthorization Authorization, long argTenantId) {
        CFAccAccountByTenantIdxKey key = schema.getFactoryAccount().newTenantIdxKey();
        key.setRequiredTenantId(argTenantId);
        deleteAccountByTenantIdx(Authorization, key);
    }

    public void deleteAccountByTenantIdx(CFAccAuthorization Authorization, CFAccAccountByTenantIdxKey argKey) {
        CFAccAccountBuff cur;
        LinkedList<CFAccAccountBuff> matchSet = new LinkedList<CFAccAccountBuff>();
        Iterator<CFAccAccountBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAccountBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAccount(Authorization, cur);
        }
    }

    public void deleteAccountByRollupAcctIdx(CFAccAuthorization Authorization, Long argRollupTenantId,
            Long argRollupAccountId) {
        CFAccAccountByRollupAcctIdxKey key = schema.getFactoryAccount().newRollupAcctIdxKey();
        key.setOptionalRollupTenantId(argRollupTenantId);
        key.setOptionalRollupAccountId(argRollupAccountId);
        deleteAccountByRollupAcctIdx(Authorization, key);
    }

    public void deleteAccountByRollupAcctIdx(CFAccAuthorization Authorization,
            CFAccAccountByRollupAcctIdxKey argKey) {
        CFAccAccountBuff cur;
        LinkedList<CFAccAccountBuff> matchSet = new LinkedList<CFAccAccountBuff>();
        Iterator<CFAccAccountBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAccountBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAccount(Authorization, cur);
        }
    }

    public void deleteAccountByCcyIdx(CFAccAuthorization Authorization, short argCurrencyId) {
        CFAccAccountByCcyIdxKey key = schema.getFactoryAccount().newCcyIdxKey();
        key.setRequiredCurrencyId(argCurrencyId);
        deleteAccountByCcyIdx(Authorization, key);
    }

    public void deleteAccountByCcyIdx(CFAccAuthorization Authorization, CFAccAccountByCcyIdxKey argKey) {
        CFAccAccountBuff cur;
        LinkedList<CFAccAccountBuff> matchSet = new LinkedList<CFAccAccountBuff>();
        Iterator<CFAccAccountBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAccountBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAccount(Authorization, cur);
        }
    }

    public CFAccCursor openAccountCursorAll(CFAccAuthorization Authorization) {
        CFAccCursor cursor = new CFAccRamAccountCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public CFAccCursor openAccountCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        CFAccCursor cursor;
        CFAccAccountByTenantIdxKey key = schema.getFactoryAccount().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

        if (dictByTenantIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictTenantIdx = dictByTenantIdx.get(key);
            cursor = new CFAccRamAccountCursor(Authorization, schema, subdictTenantIdx.values());
        } else {
            cursor = new CFAccRamAccountCursor(Authorization, schema, new ArrayList<CFAccAccountBuff>());
        }
        return (cursor);
    }

    public CFAccCursor openAccountCursorByRollupAcctIdx(CFAccAuthorization Authorization, Long RollupTenantId,
            Long RollupAccountId) {
        CFAccCursor cursor;
        CFAccAccountByRollupAcctIdxKey key = schema.getFactoryAccount().newRollupAcctIdxKey();
        key.setOptionalRollupTenantId(RollupTenantId);
        key.setOptionalRollupAccountId(RollupAccountId);

        if (dictByRollupAcctIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictRollupAcctIdx = dictByRollupAcctIdx.get(key);
            cursor = new CFAccRamAccountCursor(Authorization, schema, subdictRollupAcctIdx.values());
        } else {
            cursor = new CFAccRamAccountCursor(Authorization, schema, new ArrayList<CFAccAccountBuff>());
        }
        return (cursor);
    }

    public CFAccCursor openAccountCursorByCcyIdx(CFAccAuthorization Authorization, short CurrencyId) {
        CFAccCursor cursor;
        CFAccAccountByCcyIdxKey key = schema.getFactoryAccount().newCcyIdxKey();
        key.setRequiredCurrencyId(CurrencyId);

        if (dictByCcyIdx.containsKey(key)) {
            SortedMap<CFAccAccountPKey, CFAccAccountBuff> subdictCcyIdx = dictByCcyIdx.get(key);
            cursor = new CFAccRamAccountCursor(Authorization, schema, subdictCcyIdx.values());
        } else {
            cursor = new CFAccRamAccountCursor(Authorization, schema, new ArrayList<CFAccAccountBuff>());
        }
        return (cursor);
    }

    public void closeAccountCursor(CFAccCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public CFAccAccountBuff nextAccountCursor(CFAccCursor Cursor) {
        CFAccRamAccountCursor cursor = (CFAccRamAccountCursor) Cursor;
        CFAccAccountBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public CFAccAccountBuff prevAccountCursor(CFAccCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        CFAccAccountBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAccountCursor(Cursor);
        }
        return (rec);
    }

    public CFAccAccountBuff firstAccountCursor(CFAccCursor Cursor) {
        int targetRowIdx = 1;
        CFAccAccountBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAccountCursor(Cursor);
        }
        return (rec);
    }

    public CFAccAccountBuff lastAccountCursor(CFAccCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAccountCursor");
    }

    public CFAccAccountBuff nthAccountCursor(CFAccCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAccAccountBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAccountCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}