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

Java tutorial

Introduction

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

Source

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

/*
 *   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.*;

/*
 *   CFAccRamAddressTable in-memory RAM DbIO implementation
 *   for Address.
 */
public class CFAccRamAddressTable implements ICFAccAddressTable {
    private CFAccRamSchema schema;
    private Map<CFAccAddressPKey, CFAccAddressBuff> dictByPKey = new HashMap<CFAccAddressPKey, CFAccAddressBuff>();
    private SortedMap<CFAccAddressByTenantIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>> dictByTenantIdx = new TreeMap<CFAccAddressByTenantIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>>();
    private SortedMap<CFAccAddressByContactIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>> dictByContactIdx = new TreeMap<CFAccAddressByContactIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>>();
    private SortedMap<CFAccAddressByUDescrIdxKey, CFAccAddressBuff> dictByUDescrIdx = new TreeMap<CFAccAddressByUDescrIdxKey, CFAccAddressBuff>();
    private SortedMap<CFAccAddressByCountryIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>> dictByCountryIdx = new TreeMap<CFAccAddressByCountryIdxKey, SortedMap<CFAccAddressPKey, CFAccAddressBuff>>();

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

    public void createAddress(CFAccAuthorization Authorization, CFAccAddressBuff Buff) {
        CFAccAddressPKey pkey = schema.getFactoryAddress().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAddressId(((CFAccRamTenantTable) schema.getTableTenant()).nextAddressIdGen(Authorization,
                Buff.getRequiredTenantId()));
        Buff.setRequiredTenantId(pkey.getRequiredTenantId());
        Buff.setRequiredAddressId(pkey.getRequiredAddressId());
        CFAccAddressByTenantIdxKey keyTenantIdx = schema.getFactoryAddress().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAddressByContactIdxKey keyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccAddressByUDescrIdxKey keyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccAddressByCountryIdxKey keyCountryIdx = schema.getFactoryAddress().newCountryIdxKey();
        keyCountryIdx.setOptionalCountryId(Buff.getOptionalCountryId());

        // Validate unique indexes

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

        if (dictByUDescrIdx.containsKey(keyUDescrIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createAddress",
                    "AddressUDescrIdx", keyUDescrIdx);
        }

        // Validate foreign keys

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

        {
            boolean allNull = true;
            allNull = false;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(),
                        Buff.getRequiredContactId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createAddress", "Container", "AddressContact", "Contact", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

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

        SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdictContactIdx;
        if (dictByContactIdx.containsKey(keyContactIdx)) {
            subdictContactIdx = dictByContactIdx.get(keyContactIdx);
        } else {
            subdictContactIdx = new TreeMap<CFAccAddressPKey, CFAccAddressBuff>();
            dictByContactIdx.put(keyContactIdx, subdictContactIdx);
        }
        subdictContactIdx.put(pkey, Buff);

        dictByUDescrIdx.put(keyUDescrIdx, Buff);

        SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdictCountryIdx;
        if (dictByCountryIdx.containsKey(keyCountryIdx)) {
            subdictCountryIdx = dictByCountryIdx.get(keyCountryIdx);
        } else {
            subdictCountryIdx = new TreeMap<CFAccAddressPKey, CFAccAddressBuff>();
            dictByCountryIdx.put(keyCountryIdx, subdictCountryIdx);
        }
        subdictCountryIdx.put(pkey, Buff);

    }

    public CFAccAddressBuff readDerived(CFAccAuthorization Authorization, CFAccAddressPKey PKey) {
        final String S_ProcName = "CFAccRamAddress.readDerived() ";
        CFAccAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredAddressId(PKey.getRequiredAddressId());
        CFAccAddressBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAddressBuff lockDerived(CFAccAuthorization Authorization, CFAccAddressPKey PKey) {
        final String S_ProcName = "CFAccRamAddress.readDerived() ";
        CFAccAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredAddressId(PKey.getRequiredAddressId());
        CFAccAddressBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccAddressBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamAddress.readDerivedByTenantIdx() ";
        CFAccAddressByTenantIdxKey key = schema.getFactoryAddress().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccAddressBuff[] readDerivedByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        final String S_ProcName = "CFAccRamAddress.readDerivedByContactIdx() ";
        CFAccAddressByContactIdxKey key = schema.getFactoryAddress().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

    public CFAccAddressBuff readDerivedByUDescrIdx(CFAccAuthorization Authorization, long TenantId, long ContactId,
            String Description) {
        final String S_ProcName = "CFAccRamAddress.readDerivedByUDescrIdx() ";
        CFAccAddressByUDescrIdxKey key = schema.getFactoryAddress().newUDescrIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);
        key.setRequiredDescription(Description);

        CFAccAddressBuff buff;
        if (dictByUDescrIdx.containsKey(key)) {
            buff = dictByUDescrIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAddressBuff[] readDerivedByCountryIdx(CFAccAuthorization Authorization, Short CountryId) {
        final String S_ProcName = "CFAccRamAddress.readDerivedByCountryIdx() ";
        CFAccAddressByCountryIdxKey key = schema.getFactoryAddress().newCountryIdxKey();
        key.setOptionalCountryId(CountryId);

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

    public CFAccAddressBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long AddressId) {
        final String S_ProcName = "CFAccRamAddress.readDerivedByIdIdx() ";
        CFAccAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredAddressId(AddressId);

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

    public CFAccAddressBuff readBuff(CFAccAuthorization Authorization, CFAccAddressPKey PKey) {
        final String S_ProcName = "CFAccRamAddress.readBuff() ";
        CFAccAddressBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ADR"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccAddressBuff lockBuff(CFAccAuthorization Authorization, CFAccAddressPKey PKey) {
        final String S_ProcName = "CFAccRamAddress.readBuff() ";
        CFAccAddressBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ADR"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccAddressBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long AddressId) {
        final String S_ProcName = "CFAccRamAddress.readBuffByIdIdx() ";
        CFAccAddressBuff buff = readDerivedByIdIdx(Authorization, TenantId, AddressId);
        if ((buff != null) && buff.getClassCode().equals("ADR")) {
            return ((CFAccAddressBuff) buff);
        } else {
            return (null);
        }
    }

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

    public CFAccAddressBuff[] readBuffByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        final String S_ProcName = "CFAccRamAddress.readBuffByContactIdx() ";
        CFAccAddressBuff buff;
        ArrayList<CFAccAddressBuff> filteredList = new ArrayList<CFAccAddressBuff>();
        CFAccAddressBuff[] buffList = readDerivedByContactIdx(Authorization, TenantId, ContactId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ADR")) {
                filteredList.add((CFAccAddressBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAddressBuff[0]));
    }

    public CFAccAddressBuff readBuffByUDescrIdx(CFAccAuthorization Authorization, long TenantId, long ContactId,
            String Description) {
        final String S_ProcName = "CFAccRamAddress.readBuffByUDescrIdx() ";
        CFAccAddressBuff buff = readDerivedByUDescrIdx(Authorization, TenantId, ContactId, Description);
        if ((buff != null) && buff.getClassCode().equals("ADR")) {
            return ((CFAccAddressBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAccAddressBuff[] readBuffByCountryIdx(CFAccAuthorization Authorization, Short CountryId) {
        final String S_ProcName = "CFAccRamAddress.readBuffByCountryIdx() ";
        CFAccAddressBuff buff;
        ArrayList<CFAccAddressBuff> filteredList = new ArrayList<CFAccAddressBuff>();
        CFAccAddressBuff[] buffList = readDerivedByCountryIdx(Authorization, CountryId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ADR")) {
                filteredList.add((CFAccAddressBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAddressBuff[0]));
    }

    public void updateAddress(CFAccAuthorization Authorization, CFAccAddressBuff Buff) {
        CFAccAddressPKey pkey = schema.getFactoryAddress().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAddressId(Buff.getRequiredAddressId());
        CFAccAddressBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAddress",
                    "Existing record not found", "Address", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAddress",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAccAddressByTenantIdxKey existingKeyTenantIdx = schema.getFactoryAddress().newTenantIdxKey();
        existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAddressByTenantIdxKey newKeyTenantIdx = schema.getFactoryAddress().newTenantIdxKey();
        newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAddressByContactIdxKey existingKeyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        existingKeyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccAddressByContactIdxKey newKeyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        newKeyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccAddressByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        existingKeyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccAddressByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        newKeyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccAddressByCountryIdxKey existingKeyCountryIdx = schema.getFactoryAddress().newCountryIdxKey();
        existingKeyCountryIdx.setOptionalCountryId(existing.getOptionalCountryId());

        CFAccAddressByCountryIdxKey newKeyCountryIdx = schema.getFactoryAddress().newCountryIdxKey();
        newKeyCountryIdx.setOptionalCountryId(Buff.getOptionalCountryId());

        // Check unique indexes

        if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) {
            if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateAddress", "AddressUDescrIdx", newKeyUDescrIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(),
                        Buff.getRequiredContactId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateAddress", "Container", "AddressContact", "Contact", null);
                }
            }
        }

        // Update is valid

        SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdict;

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

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

        subdict = dictByContactIdx.get(existingKeyContactIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByContactIdx.containsKey(newKeyContactIdx)) {
            subdict = dictByContactIdx.get(newKeyContactIdx);
        } else {
            subdict = new TreeMap<CFAccAddressPKey, CFAccAddressBuff>();
            dictByContactIdx.put(newKeyContactIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUDescrIdx.remove(existingKeyUDescrIdx);
        dictByUDescrIdx.put(newKeyUDescrIdx, Buff);

        subdict = dictByCountryIdx.get(existingKeyCountryIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByCountryIdx.containsKey(newKeyCountryIdx)) {
            subdict = dictByCountryIdx.get(newKeyCountryIdx);
        } else {
            subdict = new TreeMap<CFAccAddressPKey, CFAccAddressBuff>();
            dictByCountryIdx.put(newKeyCountryIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteAddress(CFAccAuthorization Authorization, CFAccAddressBuff Buff) {
        final String S_ProcName = "CFAccRamAddressTable.deleteAddress() ";
        CFAccAddressPKey pkey = schema.getFactoryAddress().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAddressId(Buff.getRequiredAddressId());
        CFAccAddressBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAddress",
                    pkey);
        }
        CFAccAddressByTenantIdxKey keyTenantIdx = schema.getFactoryAddress().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAddressByContactIdxKey keyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccAddressByUDescrIdxKey keyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccAddressByCountryIdxKey keyCountryIdx = schema.getFactoryAddress().newCountryIdxKey();
        keyCountryIdx.setOptionalCountryId(existing.getOptionalCountryId());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableAddressTag().deleteAddressTagByAddressIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredAddressId());
        SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdict;

        dictByPKey.remove(pkey);

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

        subdict = dictByContactIdx.get(keyContactIdx);
        subdict.remove(pkey);

        dictByUDescrIdx.remove(keyUDescrIdx);

        subdict = dictByCountryIdx.get(keyCountryIdx);
        subdict.remove(pkey);

    }

    public void deleteAddressByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argAddressId) {
        CFAccAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredAddressId(argAddressId);
        deleteAddressByIdIdx(Authorization, key);
    }

    public void deleteAddressByIdIdx(CFAccAuthorization Authorization, CFAccAddressPKey argKey) {
        CFAccAddressBuff cur;
        LinkedList<CFAccAddressBuff> matchSet = new LinkedList<CFAccAddressBuff>();
        Iterator<CFAccAddressBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAddressBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAddress(Authorization, cur);
        }
    }

    public void deleteAddressByTenantIdx(CFAccAuthorization Authorization, long argTenantId) {
        CFAccAddressByTenantIdxKey key = schema.getFactoryAddress().newTenantIdxKey();
        key.setRequiredTenantId(argTenantId);
        deleteAddressByTenantIdx(Authorization, key);
    }

    public void deleteAddressByTenantIdx(CFAccAuthorization Authorization, CFAccAddressByTenantIdxKey argKey) {
        CFAccAddressBuff cur;
        LinkedList<CFAccAddressBuff> matchSet = new LinkedList<CFAccAddressBuff>();
        Iterator<CFAccAddressBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAddressBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAddress(Authorization, cur);
        }
    }

    public void deleteAddressByContactIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId) {
        CFAccAddressByContactIdxKey key = schema.getFactoryAddress().newContactIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        deleteAddressByContactIdx(Authorization, key);
    }

    public void deleteAddressByContactIdx(CFAccAuthorization Authorization, CFAccAddressByContactIdxKey argKey) {
        CFAccAddressBuff cur;
        LinkedList<CFAccAddressBuff> matchSet = new LinkedList<CFAccAddressBuff>();
        Iterator<CFAccAddressBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAddressBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAddress(Authorization, cur);
        }
    }

    public void deleteAddressByUDescrIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId,
            String argDescription) {
        CFAccAddressByUDescrIdxKey key = schema.getFactoryAddress().newUDescrIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        key.setRequiredDescription(argDescription);
        deleteAddressByUDescrIdx(Authorization, key);
    }

    public void deleteAddressByUDescrIdx(CFAccAuthorization Authorization, CFAccAddressByUDescrIdxKey argKey) {
        CFAccAddressBuff cur;
        LinkedList<CFAccAddressBuff> matchSet = new LinkedList<CFAccAddressBuff>();
        Iterator<CFAccAddressBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAddressBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAddress(Authorization, cur);
        }
    }

    public void deleteAddressByCountryIdx(CFAccAuthorization Authorization, Short argCountryId) {
        CFAccAddressByCountryIdxKey key = schema.getFactoryAddress().newCountryIdxKey();
        key.setOptionalCountryId(argCountryId);
        deleteAddressByCountryIdx(Authorization, key);
    }

    public void deleteAddressByCountryIdx(CFAccAuthorization Authorization, CFAccAddressByCountryIdxKey argKey) {
        CFAccAddressBuff cur;
        LinkedList<CFAccAddressBuff> matchSet = new LinkedList<CFAccAddressBuff>();
        Iterator<CFAccAddressBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAddressBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAddress(Authorization, cur);
        }
    }

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

    public CFAccCursor openAddressCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        CFAccCursor cursor;
        CFAccAddressByTenantIdxKey key = schema.getFactoryAddress().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccCursor openAddressCursorByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        CFAccCursor cursor;
        CFAccAddressByContactIdxKey key = schema.getFactoryAddress().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

        if (dictByContactIdx.containsKey(key)) {
            SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdictContactIdx = dictByContactIdx.get(key);
            cursor = new CFAccRamAddressCursor(Authorization, schema, subdictContactIdx.values());
        } else {
            cursor = new CFAccRamAddressCursor(Authorization, schema, new ArrayList<CFAccAddressBuff>());
        }
        return (cursor);
    }

    public CFAccCursor openAddressCursorByCountryIdx(CFAccAuthorization Authorization, Short CountryId) {
        CFAccCursor cursor;
        CFAccAddressByCountryIdxKey key = schema.getFactoryAddress().newCountryIdxKey();
        key.setOptionalCountryId(CountryId);

        if (dictByCountryIdx.containsKey(key)) {
            SortedMap<CFAccAddressPKey, CFAccAddressBuff> subdictCountryIdx = dictByCountryIdx.get(key);
            cursor = new CFAccRamAddressCursor(Authorization, schema, subdictCountryIdx.values());
        } else {
            cursor = new CFAccRamAddressCursor(Authorization, schema, new ArrayList<CFAccAddressBuff>());
        }
        return (cursor);
    }

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

    public CFAccAddressBuff nextAddressCursor(CFAccCursor Cursor) {
        CFAccRamAddressCursor cursor = (CFAccRamAddressCursor) Cursor;
        CFAccAddressBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAccAddressBuff firstAddressCursor(CFAccCursor Cursor) {
        int targetRowIdx = 1;
        CFAccAddressBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAddressCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAccAddressBuff nthAddressCursor(CFAccCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAccAddressBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAddressCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}