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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAccRamPhoneTable in-memory RAM DbIO implementation
 *   for Phone.
 */
public class CFAccRamPhoneTable implements ICFAccPhoneTable {
    private CFAccRamSchema schema;
    private Map<CFAccPhonePKey, CFAccPhoneBuff> dictByPKey = new HashMap<CFAccPhonePKey, CFAccPhoneBuff>();
    private SortedMap<CFAccPhoneByTenantIdxKey, SortedMap<CFAccPhonePKey, CFAccPhoneBuff>> dictByTenantIdx = new TreeMap<CFAccPhoneByTenantIdxKey, SortedMap<CFAccPhonePKey, CFAccPhoneBuff>>();
    private SortedMap<CFAccPhoneByContactIdxKey, SortedMap<CFAccPhonePKey, CFAccPhoneBuff>> dictByContactIdx = new TreeMap<CFAccPhoneByContactIdxKey, SortedMap<CFAccPhonePKey, CFAccPhoneBuff>>();
    private SortedMap<CFAccPhoneByUDescrIdxKey, CFAccPhoneBuff> dictByUDescrIdx = new TreeMap<CFAccPhoneByUDescrIdxKey, CFAccPhoneBuff>();
    private SortedMap<CFAccPhoneByUPhoneNumberIdxKey, CFAccPhoneBuff> dictByUPhoneNumberIdx = new TreeMap<CFAccPhoneByUPhoneNumberIdxKey, CFAccPhoneBuff>();

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

    public void createPhone(CFAccAuthorization Authorization, CFAccPhoneBuff Buff) {
        CFAccPhonePKey pkey = schema.getFactoryPhone().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredPhoneId(((CFAccRamTenantTable) schema.getTableTenant()).nextPhoneIdGen(Authorization,
                Buff.getRequiredTenantId()));
        Buff.setRequiredTenantId(pkey.getRequiredTenantId());
        Buff.setRequiredPhoneId(pkey.getRequiredPhoneId());
        CFAccPhoneByTenantIdxKey keyTenantIdx = schema.getFactoryPhone().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccPhoneByContactIdxKey keyContactIdx = schema.getFactoryPhone().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccPhoneByUDescrIdxKey keyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccPhoneByUPhoneNumberIdxKey keyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey();
        keyUPhoneNumberIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyUPhoneNumberIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUPhoneNumberIdx.setRequiredPhoneNumber(Buff.getRequiredPhoneNumber());

        // Validate unique indexes

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

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

        if (dictByUPhoneNumberIdx.containsKey(keyUPhoneNumberIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createPhone",
                    "PhoneUPhoneNumberIdx", keyUPhoneNumberIdx);
        }

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createPhone", "Owner", "PhoneTenant", "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(),
                            "createPhone", "Container", "Contact", "Contact", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

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

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

        dictByUDescrIdx.put(keyUDescrIdx, Buff);

        dictByUPhoneNumberIdx.put(keyUPhoneNumberIdx, Buff);

    }

    public CFAccPhoneBuff readDerived(CFAccAuthorization Authorization, CFAccPhonePKey PKey) {
        final String S_ProcName = "CFAccRamPhone.readDerived() ";
        CFAccPhonePKey key = schema.getFactoryPhone().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredPhoneId(PKey.getRequiredPhoneId());
        CFAccPhoneBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccPhoneBuff lockDerived(CFAccAuthorization Authorization, CFAccPhonePKey PKey) {
        final String S_ProcName = "CFAccRamPhone.readDerived() ";
        CFAccPhonePKey key = schema.getFactoryPhone().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredPhoneId(PKey.getRequiredPhoneId());
        CFAccPhoneBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccPhoneBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamPhone.readDerivedByTenantIdx() ";
        CFAccPhoneByTenantIdxKey key = schema.getFactoryPhone().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccPhoneBuff[] readDerivedByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        final String S_ProcName = "CFAccRamPhone.readDerivedByContactIdx() ";
        CFAccPhoneByContactIdxKey key = schema.getFactoryPhone().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

    public CFAccPhoneBuff readDerivedByUDescrIdx(CFAccAuthorization Authorization, long TenantId, long ContactId,
            String Description) {
        final String S_ProcName = "CFAccRamPhone.readDerivedByUDescrIdx() ";
        CFAccPhoneByUDescrIdxKey key = schema.getFactoryPhone().newUDescrIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);
        key.setRequiredDescription(Description);

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

    public CFAccPhoneBuff readDerivedByUPhoneNumberIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId, String PhoneNumber) {
        final String S_ProcName = "CFAccRamPhone.readDerivedByUPhoneNumberIdx() ";
        CFAccPhoneByUPhoneNumberIdxKey key = schema.getFactoryPhone().newUPhoneNumberIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);
        key.setRequiredPhoneNumber(PhoneNumber);

        CFAccPhoneBuff buff;
        if (dictByUPhoneNumberIdx.containsKey(key)) {
            buff = dictByUPhoneNumberIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccPhoneBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long PhoneId) {
        final String S_ProcName = "CFAccRamPhone.readDerivedByIdIdx() ";
        CFAccPhonePKey key = schema.getFactoryPhone().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredPhoneId(PhoneId);

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

    public CFAccPhoneBuff readBuff(CFAccAuthorization Authorization, CFAccPhonePKey PKey) {
        final String S_ProcName = "CFAccRamPhone.readBuff() ";
        CFAccPhoneBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("PH"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccPhoneBuff lockBuff(CFAccAuthorization Authorization, CFAccPhonePKey PKey) {
        final String S_ProcName = "CFAccRamPhone.readBuff() ";
        CFAccPhoneBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("PH"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccPhoneBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long PhoneId) {
        final String S_ProcName = "CFAccRamPhone.readBuffByIdIdx() ";
        CFAccPhoneBuff buff = readDerivedByIdIdx(Authorization, TenantId, PhoneId);
        if ((buff != null) && buff.getClassCode().equals("PH")) {
            return ((CFAccPhoneBuff) buff);
        } else {
            return (null);
        }
    }

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

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

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

    public CFAccPhoneBuff readBuffByUPhoneNumberIdx(CFAccAuthorization Authorization, long TenantId, long ContactId,
            String PhoneNumber) {
        final String S_ProcName = "CFAccRamPhone.readBuffByUPhoneNumberIdx() ";
        CFAccPhoneBuff buff = readDerivedByUPhoneNumberIdx(Authorization, TenantId, ContactId, PhoneNumber);
        if ((buff != null) && buff.getClassCode().equals("PH")) {
            return ((CFAccPhoneBuff) buff);
        } else {
            return (null);
        }
    }

    public void updatePhone(CFAccAuthorization Authorization, CFAccPhoneBuff Buff) {
        CFAccPhonePKey pkey = schema.getFactoryPhone().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredPhoneId(Buff.getRequiredPhoneId());
        CFAccPhoneBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updatePhone",
                    "Existing record not found", "Phone", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updatePhone", pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAccPhoneByTenantIdxKey existingKeyTenantIdx = schema.getFactoryPhone().newTenantIdxKey();
        existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccPhoneByTenantIdxKey newKeyTenantIdx = schema.getFactoryPhone().newTenantIdxKey();
        newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccPhoneByContactIdxKey existingKeyContactIdx = schema.getFactoryPhone().newContactIdxKey();
        existingKeyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccPhoneByContactIdxKey newKeyContactIdx = schema.getFactoryPhone().newContactIdxKey();
        newKeyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccPhoneByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey();
        existingKeyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccPhoneByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey();
        newKeyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccPhoneByUPhoneNumberIdxKey existingKeyUPhoneNumberIdx = schema.getFactoryPhone()
                .newUPhoneNumberIdxKey();
        existingKeyUPhoneNumberIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyUPhoneNumberIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUPhoneNumberIdx.setRequiredPhoneNumber(existing.getRequiredPhoneNumber());

        CFAccPhoneByUPhoneNumberIdxKey newKeyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey();
        newKeyUPhoneNumberIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyUPhoneNumberIdx.setRequiredContactId(Buff.getRequiredContactId());
        newKeyUPhoneNumberIdx.setRequiredPhoneNumber(Buff.getRequiredPhoneNumber());

        // Check unique indexes

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

        if (!existingKeyUPhoneNumberIdx.equals(newKeyUPhoneNumberIdx)) {
            if (dictByUPhoneNumberIdx.containsKey(newKeyUPhoneNumberIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updatePhone",
                        "PhoneUPhoneNumberIdx", newKeyUPhoneNumberIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        {
            boolean allNull = true;

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

        // Update is valid

        SortedMap<CFAccPhonePKey, CFAccPhoneBuff> 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<CFAccPhonePKey, CFAccPhoneBuff>();
            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<CFAccPhonePKey, CFAccPhoneBuff>();
            dictByContactIdx.put(newKeyContactIdx, subdict);
        }
        subdict.put(pkey, Buff);

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

        dictByUPhoneNumberIdx.remove(existingKeyUPhoneNumberIdx);
        dictByUPhoneNumberIdx.put(newKeyUPhoneNumberIdx, Buff);

    }

    public void deletePhone(CFAccAuthorization Authorization, CFAccPhoneBuff Buff) {
        final String S_ProcName = "CFAccRamPhoneTable.deletePhone() ";
        CFAccPhonePKey pkey = schema.getFactoryPhone().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredPhoneId(Buff.getRequiredPhoneId());
        CFAccPhoneBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deletePhone", pkey);
        }
        CFAccPhoneByTenantIdxKey keyTenantIdx = schema.getFactoryPhone().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccPhoneByContactIdxKey keyContactIdx = schema.getFactoryPhone().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccPhoneByUDescrIdxKey keyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccPhoneByUPhoneNumberIdxKey keyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey();
        keyUPhoneNumberIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyUPhoneNumberIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUPhoneNumberIdx.setRequiredPhoneNumber(existing.getRequiredPhoneNumber());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTablePhoneTag().deletePhoneTagByPhoneIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredPhoneId());
        SortedMap<CFAccPhonePKey, CFAccPhoneBuff> subdict;

        dictByPKey.remove(pkey);

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

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

        dictByUDescrIdx.remove(keyUDescrIdx);

        dictByUPhoneNumberIdx.remove(keyUPhoneNumberIdx);

    }

    public void deletePhoneByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argPhoneId) {
        CFAccPhonePKey key = schema.getFactoryPhone().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredPhoneId(argPhoneId);
        deletePhoneByIdIdx(Authorization, key);
    }

    public void deletePhoneByIdIdx(CFAccAuthorization Authorization, CFAccPhonePKey argKey) {
        CFAccPhoneBuff cur;
        LinkedList<CFAccPhoneBuff> matchSet = new LinkedList<CFAccPhoneBuff>();
        Iterator<CFAccPhoneBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccPhoneBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deletePhone(Authorization, cur);
        }
    }

    public void deletePhoneByTenantIdx(CFAccAuthorization Authorization, long argTenantId) {
        CFAccPhoneByTenantIdxKey key = schema.getFactoryPhone().newTenantIdxKey();
        key.setRequiredTenantId(argTenantId);
        deletePhoneByTenantIdx(Authorization, key);
    }

    public void deletePhoneByTenantIdx(CFAccAuthorization Authorization, CFAccPhoneByTenantIdxKey argKey) {
        CFAccPhoneBuff cur;
        LinkedList<CFAccPhoneBuff> matchSet = new LinkedList<CFAccPhoneBuff>();
        Iterator<CFAccPhoneBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccPhoneBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deletePhone(Authorization, cur);
        }
    }

    public void deletePhoneByContactIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId) {
        CFAccPhoneByContactIdxKey key = schema.getFactoryPhone().newContactIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        deletePhoneByContactIdx(Authorization, key);
    }

    public void deletePhoneByContactIdx(CFAccAuthorization Authorization, CFAccPhoneByContactIdxKey argKey) {
        CFAccPhoneBuff cur;
        LinkedList<CFAccPhoneBuff> matchSet = new LinkedList<CFAccPhoneBuff>();
        Iterator<CFAccPhoneBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccPhoneBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deletePhone(Authorization, cur);
        }
    }

    public void deletePhoneByUDescrIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId,
            String argDescription) {
        CFAccPhoneByUDescrIdxKey key = schema.getFactoryPhone().newUDescrIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        key.setRequiredDescription(argDescription);
        deletePhoneByUDescrIdx(Authorization, key);
    }

    public void deletePhoneByUDescrIdx(CFAccAuthorization Authorization, CFAccPhoneByUDescrIdxKey argKey) {
        CFAccPhoneBuff cur;
        LinkedList<CFAccPhoneBuff> matchSet = new LinkedList<CFAccPhoneBuff>();
        Iterator<CFAccPhoneBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccPhoneBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deletePhone(Authorization, cur);
        }
    }

    public void deletePhoneByUPhoneNumberIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId,
            String argPhoneNumber) {
        CFAccPhoneByUPhoneNumberIdxKey key = schema.getFactoryPhone().newUPhoneNumberIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        key.setRequiredPhoneNumber(argPhoneNumber);
        deletePhoneByUPhoneNumberIdx(Authorization, key);
    }

    public void deletePhoneByUPhoneNumberIdx(CFAccAuthorization Authorization,
            CFAccPhoneByUPhoneNumberIdxKey argKey) {
        CFAccPhoneBuff cur;
        LinkedList<CFAccPhoneBuff> matchSet = new LinkedList<CFAccPhoneBuff>();
        Iterator<CFAccPhoneBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccPhoneBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deletePhone(Authorization, cur);
        }
    }

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

    public CFAccCursor openPhoneCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        CFAccCursor cursor;
        CFAccPhoneByTenantIdxKey key = schema.getFactoryPhone().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccCursor openPhoneCursorByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        CFAccCursor cursor;
        CFAccPhoneByContactIdxKey key = schema.getFactoryPhone().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

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

    public CFAccPhoneBuff nextPhoneCursor(CFAccCursor Cursor) {
        CFAccRamPhoneCursor cursor = (CFAccRamPhoneCursor) Cursor;
        CFAccPhoneBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAccPhoneBuff firstPhoneCursor(CFAccCursor Cursor) {
        int targetRowIdx = 1;
        CFAccPhoneBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextPhoneCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAccPhoneBuff nthPhoneCursor(CFAccCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAccPhoneBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextPhoneCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}