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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAccRamContactTable in-memory RAM DbIO implementation
 *   for Contact.
 */
public class CFAccRamContactTable implements ICFAccContactTable {
    private CFAccRamSchema schema;
    private Map<CFAccContactPKey, CFAccContactBuff> dictByPKey = new HashMap<CFAccContactPKey, CFAccContactBuff>();
    private SortedMap<CFAccContactByContTentIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>> dictByContTentIdx = new TreeMap<CFAccContactByContTentIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>>();
    private SortedMap<CFAccContactByContListIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>> dictByContListIdx = new TreeMap<CFAccContactByContListIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>>();
    private SortedMap<CFAccContactByFullNameIdxKey, CFAccContactBuff> dictByFullNameIdx = new TreeMap<CFAccContactByFullNameIdxKey, CFAccContactBuff>();
    private SortedMap<CFAccContactByTimezoneIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>> dictByTimezoneIdx = new TreeMap<CFAccContactByTimezoneIdxKey, SortedMap<CFAccContactPKey, CFAccContactBuff>>();

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

    public void createContact(CFAccAuthorization Authorization, CFAccContactBuff Buff) {
        CFAccContactPKey pkey = schema.getFactoryContact().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredContactId(((CFAccRamTenantTable) schema.getTableTenant()).nextContactIdGen(Authorization,
                Buff.getRequiredTenantId()));
        Buff.setRequiredTenantId(pkey.getRequiredTenantId());
        Buff.setRequiredContactId(pkey.getRequiredContactId());
        CFAccContactByContTentIdxKey keyContTentIdx = schema.getFactoryContact().newContTentIdxKey();
        keyContTentIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccContactByContListIdxKey keyContListIdx = schema.getFactoryContact().newContListIdxKey();
        keyContListIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyContListIdx.setRequiredContactListId(Buff.getRequiredContactListId());

        CFAccContactByFullNameIdxKey keyFullNameIdx = schema.getFactoryContact().newFullNameIdxKey();
        keyFullNameIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyFullNameIdx.setRequiredContactListId(Buff.getRequiredContactListId());
        keyFullNameIdx.setRequiredFullName(Buff.getRequiredFullName());

        CFAccContactByTimezoneIdxKey keyTimezoneIdx = schema.getFactoryContact().newTimezoneIdxKey();
        keyTimezoneIdx.setOptionalISOTimezoneId(Buff.getOptionalISOTimezoneId());

        // Validate unique indexes

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

        if (dictByFullNameIdx.containsKey(keyFullNameIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createContact",
                    "ContactUFullNameIdx", keyFullNameIdx);
        }

        // Validate foreign keys

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

        {
            boolean allNull = true;
            allNull = false;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableContactList().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredTenantId(), Buff.getRequiredContactListId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createContact", "Container", "ContactList", "ContactList", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<CFAccContactPKey, CFAccContactBuff> subdictContTentIdx;
        if (dictByContTentIdx.containsKey(keyContTentIdx)) {
            subdictContTentIdx = dictByContTentIdx.get(keyContTentIdx);
        } else {
            subdictContTentIdx = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByContTentIdx.put(keyContTentIdx, subdictContTentIdx);
        }
        subdictContTentIdx.put(pkey, Buff);

        SortedMap<CFAccContactPKey, CFAccContactBuff> subdictContListIdx;
        if (dictByContListIdx.containsKey(keyContListIdx)) {
            subdictContListIdx = dictByContListIdx.get(keyContListIdx);
        } else {
            subdictContListIdx = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByContListIdx.put(keyContListIdx, subdictContListIdx);
        }
        subdictContListIdx.put(pkey, Buff);

        dictByFullNameIdx.put(keyFullNameIdx, Buff);

        SortedMap<CFAccContactPKey, CFAccContactBuff> subdictTimezoneIdx;
        if (dictByTimezoneIdx.containsKey(keyTimezoneIdx)) {
            subdictTimezoneIdx = dictByTimezoneIdx.get(keyTimezoneIdx);
        } else {
            subdictTimezoneIdx = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByTimezoneIdx.put(keyTimezoneIdx, subdictTimezoneIdx);
        }
        subdictTimezoneIdx.put(pkey, Buff);

    }

    public CFAccContactBuff readDerived(CFAccAuthorization Authorization, CFAccContactPKey PKey) {
        final String S_ProcName = "CFAccRamContact.readDerived() ";
        CFAccContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredContactId(PKey.getRequiredContactId());
        CFAccContactBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccContactBuff lockDerived(CFAccAuthorization Authorization, CFAccContactPKey PKey) {
        final String S_ProcName = "CFAccRamContact.readDerived() ";
        CFAccContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredContactId(PKey.getRequiredContactId());
        CFAccContactBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccContactBuff[] readDerivedByContTentIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamContact.readDerivedByContTentIdx() ";
        CFAccContactByContTentIdxKey key = schema.getFactoryContact().newContTentIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccContactBuff[] readDerivedByContListIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactListId) {
        final String S_ProcName = "CFAccRamContact.readDerivedByContListIdx() ";
        CFAccContactByContListIdxKey key = schema.getFactoryContact().newContListIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactListId(ContactListId);

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

    public CFAccContactBuff readDerivedByFullNameIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactListId, String FullName) {
        final String S_ProcName = "CFAccRamContact.readDerivedByFullNameIdx() ";
        CFAccContactByFullNameIdxKey key = schema.getFactoryContact().newFullNameIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactListId(ContactListId);
        key.setRequiredFullName(FullName);

        CFAccContactBuff buff;
        if (dictByFullNameIdx.containsKey(key)) {
            buff = dictByFullNameIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccContactBuff[] readDerivedByTimezoneIdx(CFAccAuthorization Authorization, Short ISOTimezoneId) {
        final String S_ProcName = "CFAccRamContact.readDerivedByTimezoneIdx() ";
        CFAccContactByTimezoneIdxKey key = schema.getFactoryContact().newTimezoneIdxKey();
        key.setOptionalISOTimezoneId(ISOTimezoneId);

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

    public CFAccContactBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long ContactId) {
        final String S_ProcName = "CFAccRamContact.readDerivedByIdIdx() ";
        CFAccContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

    public CFAccContactBuff readBuff(CFAccAuthorization Authorization, CFAccContactPKey PKey) {
        final String S_ProcName = "CFAccRamContact.readBuff() ";
        CFAccContactBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CTC"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccContactBuff lockBuff(CFAccAuthorization Authorization, CFAccContactPKey PKey) {
        final String S_ProcName = "CFAccRamContact.readBuff() ";
        CFAccContactBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CTC"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccContactBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long ContactId) {
        final String S_ProcName = "CFAccRamContact.readBuffByIdIdx() ";
        CFAccContactBuff buff = readDerivedByIdIdx(Authorization, TenantId, ContactId);
        if ((buff != null) && buff.getClassCode().equals("CTC")) {
            return ((CFAccContactBuff) buff);
        } else {
            return (null);
        }
    }

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

    public CFAccContactBuff[] readBuffByContListIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactListId) {
        final String S_ProcName = "CFAccRamContact.readBuffByContListIdx() ";
        CFAccContactBuff buff;
        ArrayList<CFAccContactBuff> filteredList = new ArrayList<CFAccContactBuff>();
        CFAccContactBuff[] buffList = readDerivedByContListIdx(Authorization, TenantId, ContactListId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CTC")) {
                filteredList.add((CFAccContactBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccContactBuff[0]));
    }

    public CFAccContactBuff readBuffByFullNameIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactListId, String FullName) {
        final String S_ProcName = "CFAccRamContact.readBuffByFullNameIdx() ";
        CFAccContactBuff buff = readDerivedByFullNameIdx(Authorization, TenantId, ContactListId, FullName);
        if ((buff != null) && buff.getClassCode().equals("CTC")) {
            return ((CFAccContactBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAccContactBuff[] readBuffByTimezoneIdx(CFAccAuthorization Authorization, Short ISOTimezoneId) {
        final String S_ProcName = "CFAccRamContact.readBuffByTimezoneIdx() ";
        CFAccContactBuff buff;
        ArrayList<CFAccContactBuff> filteredList = new ArrayList<CFAccContactBuff>();
        CFAccContactBuff[] buffList = readDerivedByTimezoneIdx(Authorization, ISOTimezoneId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CTC")) {
                filteredList.add((CFAccContactBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccContactBuff[0]));
    }

    public void updateContact(CFAccAuthorization Authorization, CFAccContactBuff Buff) {
        CFAccContactPKey pkey = schema.getFactoryContact().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredContactId(Buff.getRequiredContactId());
        CFAccContactBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateContact",
                    "Existing record not found", "Contact", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateContact",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAccContactByContTentIdxKey existingKeyContTentIdx = schema.getFactoryContact().newContTentIdxKey();
        existingKeyContTentIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccContactByContTentIdxKey newKeyContTentIdx = schema.getFactoryContact().newContTentIdxKey();
        newKeyContTentIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccContactByContListIdxKey existingKeyContListIdx = schema.getFactoryContact().newContListIdxKey();
        existingKeyContListIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyContListIdx.setRequiredContactListId(existing.getRequiredContactListId());

        CFAccContactByContListIdxKey newKeyContListIdx = schema.getFactoryContact().newContListIdxKey();
        newKeyContListIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyContListIdx.setRequiredContactListId(Buff.getRequiredContactListId());

        CFAccContactByFullNameIdxKey existingKeyFullNameIdx = schema.getFactoryContact().newFullNameIdxKey();
        existingKeyFullNameIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyFullNameIdx.setRequiredContactListId(existing.getRequiredContactListId());
        existingKeyFullNameIdx.setRequiredFullName(existing.getRequiredFullName());

        CFAccContactByFullNameIdxKey newKeyFullNameIdx = schema.getFactoryContact().newFullNameIdxKey();
        newKeyFullNameIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyFullNameIdx.setRequiredContactListId(Buff.getRequiredContactListId());
        newKeyFullNameIdx.setRequiredFullName(Buff.getRequiredFullName());

        CFAccContactByTimezoneIdxKey existingKeyTimezoneIdx = schema.getFactoryContact().newTimezoneIdxKey();
        existingKeyTimezoneIdx.setOptionalISOTimezoneId(existing.getOptionalISOTimezoneId());

        CFAccContactByTimezoneIdxKey newKeyTimezoneIdx = schema.getFactoryContact().newTimezoneIdxKey();
        newKeyTimezoneIdx.setOptionalISOTimezoneId(Buff.getOptionalISOTimezoneId());

        // Check unique indexes

        if (!existingKeyFullNameIdx.equals(newKeyFullNameIdx)) {
            if (dictByFullNameIdx.containsKey(newKeyFullNameIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateContact", "ContactUFullNameIdx", newKeyFullNameIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableContactList().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredTenantId(), Buff.getRequiredContactListId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateContact", "Container", "ContactList", "ContactList", null);
                }
            }
        }

        // Update is valid

        SortedMap<CFAccContactPKey, CFAccContactBuff> subdict;

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

        subdict = dictByContTentIdx.get(existingKeyContTentIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByContTentIdx.containsKey(newKeyContTentIdx)) {
            subdict = dictByContTentIdx.get(newKeyContTentIdx);
        } else {
            subdict = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByContTentIdx.put(newKeyContTentIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByContListIdx.get(existingKeyContListIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByContListIdx.containsKey(newKeyContListIdx)) {
            subdict = dictByContListIdx.get(newKeyContListIdx);
        } else {
            subdict = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByContListIdx.put(newKeyContListIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByFullNameIdx.remove(existingKeyFullNameIdx);
        dictByFullNameIdx.put(newKeyFullNameIdx, Buff);

        subdict = dictByTimezoneIdx.get(existingKeyTimezoneIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByTimezoneIdx.containsKey(newKeyTimezoneIdx)) {
            subdict = dictByTimezoneIdx.get(newKeyTimezoneIdx);
        } else {
            subdict = new TreeMap<CFAccContactPKey, CFAccContactBuff>();
            dictByTimezoneIdx.put(newKeyTimezoneIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteContact(CFAccAuthorization Authorization, CFAccContactBuff Buff) {
        final String S_ProcName = "CFAccRamContactTable.deleteContact() ";
        CFAccContactPKey pkey = schema.getFactoryContact().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredContactId(Buff.getRequiredContactId());
        CFAccContactBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteContact",
                    pkey);
        }
        CFAccContactByContTentIdxKey keyContTentIdx = schema.getFactoryContact().newContTentIdxKey();
        keyContTentIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccContactByContListIdxKey keyContListIdx = schema.getFactoryContact().newContListIdxKey();
        keyContListIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyContListIdx.setRequiredContactListId(existing.getRequiredContactListId());

        CFAccContactByFullNameIdxKey keyFullNameIdx = schema.getFactoryContact().newFullNameIdxKey();
        keyFullNameIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyFullNameIdx.setRequiredContactListId(existing.getRequiredContactListId());
        keyFullNameIdx.setRequiredFullName(existing.getRequiredFullName());

        CFAccContactByTimezoneIdxKey keyTimezoneIdx = schema.getFactoryContact().newTimezoneIdxKey();
        keyTimezoneIdx.setOptionalISOTimezoneId(existing.getOptionalISOTimezoneId());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableContactTag().deleteContactTagByContactIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        schema.getTableAddress().deleteAddressByContactIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        schema.getTablePhone().deletePhoneByContactIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        schema.getTableContactURL().deleteContactURLByContactIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        schema.getTableMemo().deleteMemoByMemoContactIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        schema.getTableAccountContact().deleteAccountContactByCtctIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredContactId());
        SortedMap<CFAccContactPKey, CFAccContactBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByContTentIdx.get(keyContTentIdx);
        subdict.remove(pkey);

        subdict = dictByContListIdx.get(keyContListIdx);
        subdict.remove(pkey);

        dictByFullNameIdx.remove(keyFullNameIdx);

        subdict = dictByTimezoneIdx.get(keyTimezoneIdx);
        subdict.remove(pkey);

    }

    public void deleteContactByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId) {
        CFAccContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        deleteContactByIdIdx(Authorization, key);
    }

    public void deleteContactByIdIdx(CFAccAuthorization Authorization, CFAccContactPKey argKey) {
        CFAccContactBuff cur;
        LinkedList<CFAccContactBuff> matchSet = new LinkedList<CFAccContactBuff>();
        Iterator<CFAccContactBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccContactBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteContact(Authorization, cur);
        }
    }

    public void deleteContactByContTentIdx(CFAccAuthorization Authorization, long argTenantId) {
        CFAccContactByContTentIdxKey key = schema.getFactoryContact().newContTentIdxKey();
        key.setRequiredTenantId(argTenantId);
        deleteContactByContTentIdx(Authorization, key);
    }

    public void deleteContactByContTentIdx(CFAccAuthorization Authorization, CFAccContactByContTentIdxKey argKey) {
        CFAccContactBuff cur;
        LinkedList<CFAccContactBuff> matchSet = new LinkedList<CFAccContactBuff>();
        Iterator<CFAccContactBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccContactBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteContact(Authorization, cur);
        }
    }

    public void deleteContactByContListIdx(CFAccAuthorization Authorization, long argTenantId,
            long argContactListId) {
        CFAccContactByContListIdxKey key = schema.getFactoryContact().newContListIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactListId(argContactListId);
        deleteContactByContListIdx(Authorization, key);
    }

    public void deleteContactByContListIdx(CFAccAuthorization Authorization, CFAccContactByContListIdxKey argKey) {
        CFAccContactBuff cur;
        LinkedList<CFAccContactBuff> matchSet = new LinkedList<CFAccContactBuff>();
        Iterator<CFAccContactBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccContactBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteContact(Authorization, cur);
        }
    }

    public void deleteContactByFullNameIdx(CFAccAuthorization Authorization, long argTenantId,
            long argContactListId, String argFullName) {
        CFAccContactByFullNameIdxKey key = schema.getFactoryContact().newFullNameIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactListId(argContactListId);
        key.setRequiredFullName(argFullName);
        deleteContactByFullNameIdx(Authorization, key);
    }

    public void deleteContactByFullNameIdx(CFAccAuthorization Authorization, CFAccContactByFullNameIdxKey argKey) {
        CFAccContactBuff cur;
        LinkedList<CFAccContactBuff> matchSet = new LinkedList<CFAccContactBuff>();
        Iterator<CFAccContactBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccContactBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteContact(Authorization, cur);
        }
    }

    public void deleteContactByTimezoneIdx(CFAccAuthorization Authorization, Short argISOTimezoneId) {
        CFAccContactByTimezoneIdxKey key = schema.getFactoryContact().newTimezoneIdxKey();
        key.setOptionalISOTimezoneId(argISOTimezoneId);
        deleteContactByTimezoneIdx(Authorization, key);
    }

    public void deleteContactByTimezoneIdx(CFAccAuthorization Authorization, CFAccContactByTimezoneIdxKey argKey) {
        CFAccContactBuff cur;
        LinkedList<CFAccContactBuff> matchSet = new LinkedList<CFAccContactBuff>();
        Iterator<CFAccContactBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccContactBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteContact(Authorization, cur);
        }
    }

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

    public CFAccCursor openContactCursorByContTentIdx(CFAccAuthorization Authorization, long TenantId) {
        CFAccCursor cursor;
        CFAccContactByContTentIdxKey key = schema.getFactoryContact().newContTentIdxKey();
        key.setRequiredTenantId(TenantId);

        if (dictByContTentIdx.containsKey(key)) {
            SortedMap<CFAccContactPKey, CFAccContactBuff> subdictContTentIdx = dictByContTentIdx.get(key);
            cursor = new CFAccRamContactCursor(Authorization, schema, subdictContTentIdx.values());
        } else {
            cursor = new CFAccRamContactCursor(Authorization, schema, new ArrayList<CFAccContactBuff>());
        }
        return (cursor);
    }

    public CFAccCursor openContactCursorByContListIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactListId) {
        CFAccCursor cursor;
        CFAccContactByContListIdxKey key = schema.getFactoryContact().newContListIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactListId(ContactListId);

        if (dictByContListIdx.containsKey(key)) {
            SortedMap<CFAccContactPKey, CFAccContactBuff> subdictContListIdx = dictByContListIdx.get(key);
            cursor = new CFAccRamContactCursor(Authorization, schema, subdictContListIdx.values());
        } else {
            cursor = new CFAccRamContactCursor(Authorization, schema, new ArrayList<CFAccContactBuff>());
        }
        return (cursor);
    }

    public CFAccCursor openContactCursorByTimezoneIdx(CFAccAuthorization Authorization, Short ISOTimezoneId) {
        CFAccCursor cursor;
        CFAccContactByTimezoneIdxKey key = schema.getFactoryContact().newTimezoneIdxKey();
        key.setOptionalISOTimezoneId(ISOTimezoneId);

        if (dictByTimezoneIdx.containsKey(key)) {
            SortedMap<CFAccContactPKey, CFAccContactBuff> subdictTimezoneIdx = dictByTimezoneIdx.get(key);
            cursor = new CFAccRamContactCursor(Authorization, schema, subdictTimezoneIdx.values());
        } else {
            cursor = new CFAccRamContactCursor(Authorization, schema, new ArrayList<CFAccContactBuff>());
        }
        return (cursor);
    }

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

    public CFAccContactBuff nextContactCursor(CFAccCursor Cursor) {
        CFAccRamContactCursor cursor = (CFAccRamContactCursor) Cursor;
        CFAccContactBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAccContactBuff firstContactCursor(CFAccCursor Cursor) {
        int targetRowIdx = 1;
        CFAccContactBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextContactCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAccContactBuff nthContactCursor(CFAccCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAccContactBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextContactCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}