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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAccRamAttachmentTable in-memory RAM DbIO implementation
 *   for Attachment.
 */
public class CFAccRamAttachmentTable implements ICFAccAttachmentTable {
    private CFAccRamSchema schema;
    private Map<CFAccAttachmentPKey, CFAccAttachmentBuff> dictByPKey = new HashMap<CFAccAttachmentPKey, CFAccAttachmentBuff>();
    private SortedMap<CFAccAttachmentByTenantIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>> dictByTenantIdx = new TreeMap<CFAccAttachmentByTenantIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>>();
    private SortedMap<CFAccAttachmentByContactIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>> dictByContactIdx = new TreeMap<CFAccAttachmentByContactIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>>();
    private SortedMap<CFAccAttachmentByUDescrIdxKey, CFAccAttachmentBuff> dictByUDescrIdx = new TreeMap<CFAccAttachmentByUDescrIdxKey, CFAccAttachmentBuff>();
    private SortedMap<CFAccAttachmentByMimeTypeIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>> dictByMimeTypeIdx = new TreeMap<CFAccAttachmentByMimeTypeIdxKey, SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff>>();

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

    public void createAttachment(CFAccAuthorization Authorization, CFAccAttachmentBuff Buff) {
        CFAccAttachmentPKey pkey = schema.getFactoryAttachment().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAttachmentId(((CFAccRamTenantTable) schema.getTableTenant())
                .nextAttachmentIdGen(Authorization, Buff.getRequiredTenantId()));
        Buff.setRequiredTenantId(pkey.getRequiredTenantId());
        Buff.setRequiredAttachmentId(pkey.getRequiredAttachmentId());
        CFAccAttachmentByTenantIdxKey keyTenantIdx = schema.getFactoryAttachment().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAttachmentByContactIdxKey keyContactIdx = schema.getFactoryAttachment().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccAttachmentByUDescrIdxKey keyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccAttachmentByMimeTypeIdxKey keyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey();
        keyMimeTypeIdx.setOptionalMimeTypeId(Buff.getOptionalMimeTypeId());

        // Validate unique indexes

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

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

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

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

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

        dictByUDescrIdx.put(keyUDescrIdx, Buff);

        SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff> subdictMimeTypeIdx;
        if (dictByMimeTypeIdx.containsKey(keyMimeTypeIdx)) {
            subdictMimeTypeIdx = dictByMimeTypeIdx.get(keyMimeTypeIdx);
        } else {
            subdictMimeTypeIdx = new TreeMap<CFAccAttachmentPKey, CFAccAttachmentBuff>();
            dictByMimeTypeIdx.put(keyMimeTypeIdx, subdictMimeTypeIdx);
        }
        subdictMimeTypeIdx.put(pkey, Buff);

    }

    public CFAccAttachmentBuff readDerived(CFAccAuthorization Authorization, CFAccAttachmentPKey PKey) {
        final String S_ProcName = "CFAccRamAttachment.readDerived() ";
        CFAccAttachmentPKey key = schema.getFactoryAttachment().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredAttachmentId(PKey.getRequiredAttachmentId());
        CFAccAttachmentBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccAttachmentBuff lockDerived(CFAccAuthorization Authorization, CFAccAttachmentPKey PKey) {
        final String S_ProcName = "CFAccRamAttachment.readDerived() ";
        CFAccAttachmentPKey key = schema.getFactoryAttachment().newPKey();
        key.setRequiredTenantId(PKey.getRequiredTenantId());
        key.setRequiredAttachmentId(PKey.getRequiredAttachmentId());
        CFAccAttachmentBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccAttachmentBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        final String S_ProcName = "CFAccRamAttachment.readDerivedByTenantIdx() ";
        CFAccAttachmentByTenantIdxKey key = schema.getFactoryAttachment().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccAttachmentBuff[] readDerivedByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        final String S_ProcName = "CFAccRamAttachment.readDerivedByContactIdx() ";
        CFAccAttachmentByContactIdxKey key = schema.getFactoryAttachment().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

    public CFAccAttachmentBuff readDerivedByUDescrIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId, String Description) {
        final String S_ProcName = "CFAccRamAttachment.readDerivedByUDescrIdx() ";
        CFAccAttachmentByUDescrIdxKey key = schema.getFactoryAttachment().newUDescrIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);
        key.setRequiredDescription(Description);

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

    public CFAccAttachmentBuff[] readDerivedByMimeTypeIdx(CFAccAuthorization Authorization, Integer MimeTypeId) {
        final String S_ProcName = "CFAccRamAttachment.readDerivedByMimeTypeIdx() ";
        CFAccAttachmentByMimeTypeIdxKey key = schema.getFactoryAttachment().newMimeTypeIdxKey();
        key.setOptionalMimeTypeId(MimeTypeId);

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

    public CFAccAttachmentBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId,
            long AttachmentId) {
        final String S_ProcName = "CFAccRamAttachment.readDerivedByIdIdx() ";
        CFAccAttachmentPKey key = schema.getFactoryAttachment().newPKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredAttachmentId(AttachmentId);

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

    public CFAccAttachmentBuff readBuff(CFAccAuthorization Authorization, CFAccAttachmentPKey PKey) {
        final String S_ProcName = "CFAccRamAttachment.readBuff() ";
        CFAccAttachmentBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ATTC"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccAttachmentBuff lockBuff(CFAccAuthorization Authorization, CFAccAttachmentPKey PKey) {
        final String S_ProcName = "CFAccRamAttachment.readBuff() ";
        CFAccAttachmentBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ATTC"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccAttachmentBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long AttachmentId) {
        final String S_ProcName = "CFAccRamAttachment.readBuffByIdIdx() ";
        CFAccAttachmentBuff buff = readDerivedByIdIdx(Authorization, TenantId, AttachmentId);
        if ((buff != null) && buff.getClassCode().equals("ATTC")) {
            return ((CFAccAttachmentBuff) buff);
        } else {
            return (null);
        }
    }

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

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

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

    public CFAccAttachmentBuff[] readBuffByMimeTypeIdx(CFAccAuthorization Authorization, Integer MimeTypeId) {
        final String S_ProcName = "CFAccRamAttachment.readBuffByMimeTypeIdx() ";
        CFAccAttachmentBuff buff;
        ArrayList<CFAccAttachmentBuff> filteredList = new ArrayList<CFAccAttachmentBuff>();
        CFAccAttachmentBuff[] buffList = readDerivedByMimeTypeIdx(Authorization, MimeTypeId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ATTC")) {
                filteredList.add((CFAccAttachmentBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAccAttachmentBuff[0]));
    }

    public void updateAttachment(CFAccAuthorization Authorization, CFAccAttachmentBuff Buff) {
        CFAccAttachmentPKey pkey = schema.getFactoryAttachment().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAttachmentId(Buff.getRequiredAttachmentId());
        CFAccAttachmentBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAttachment",
                    "Existing record not found", "Attachment", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAttachment",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAccAttachmentByTenantIdxKey existingKeyTenantIdx = schema.getFactoryAttachment().newTenantIdxKey();
        existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAttachmentByTenantIdxKey newKeyTenantIdx = schema.getFactoryAttachment().newTenantIdxKey();
        newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId());

        CFAccAttachmentByContactIdxKey existingKeyContactIdx = schema.getFactoryAttachment().newContactIdxKey();
        existingKeyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccAttachmentByContactIdxKey newKeyContactIdx = schema.getFactoryAttachment().newContactIdxKey();
        newKeyContactIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        CFAccAttachmentByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey();
        existingKeyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccAttachmentByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey();
        newKeyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId());
        newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        CFAccAttachmentByMimeTypeIdxKey existingKeyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey();
        existingKeyMimeTypeIdx.setOptionalMimeTypeId(existing.getOptionalMimeTypeId());

        CFAccAttachmentByMimeTypeIdxKey newKeyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey();
        newKeyMimeTypeIdx.setOptionalMimeTypeId(Buff.getOptionalMimeTypeId());

        // Check unique indexes

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

        // Validate foreign keys

        {
            boolean allNull = true;

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

        {
            boolean allNull = true;

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

        // Update is valid

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

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

        subdict = dictByMimeTypeIdx.get(existingKeyMimeTypeIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByMimeTypeIdx.containsKey(newKeyMimeTypeIdx)) {
            subdict = dictByMimeTypeIdx.get(newKeyMimeTypeIdx);
        } else {
            subdict = new TreeMap<CFAccAttachmentPKey, CFAccAttachmentBuff>();
            dictByMimeTypeIdx.put(newKeyMimeTypeIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteAttachment(CFAccAuthorization Authorization, CFAccAttachmentBuff Buff) {
        final String S_ProcName = "CFAccRamAttachmentTable.deleteAttachment() ";
        CFAccAttachmentPKey pkey = schema.getFactoryAttachment().newPKey();
        pkey.setRequiredTenantId(Buff.getRequiredTenantId());
        pkey.setRequiredAttachmentId(Buff.getRequiredAttachmentId());
        CFAccAttachmentBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAttachment",
                    pkey);
        }
        CFAccAttachmentByTenantIdxKey keyTenantIdx = schema.getFactoryAttachment().newTenantIdxKey();
        keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId());

        CFAccAttachmentByContactIdxKey keyContactIdx = schema.getFactoryAttachment().newContactIdxKey();
        keyContactIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        CFAccAttachmentByUDescrIdxKey keyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey();
        keyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId());
        keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        CFAccAttachmentByMimeTypeIdxKey keyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey();
        keyMimeTypeIdx.setOptionalMimeTypeId(existing.getOptionalMimeTypeId());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableAttachmentTag().deleteAttachmentTagByAttIdx(Authorization, Buff.getRequiredTenantId(),
                Buff.getRequiredAttachmentId());
        SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff> subdict;

        dictByPKey.remove(pkey);

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

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

        dictByUDescrIdx.remove(keyUDescrIdx);

        subdict = dictByMimeTypeIdx.get(keyMimeTypeIdx);
        subdict.remove(pkey);

    }

    public void deleteAttachmentByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argAttachmentId) {
        CFAccAttachmentPKey key = schema.getFactoryAttachment().newPKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredAttachmentId(argAttachmentId);
        deleteAttachmentByIdIdx(Authorization, key);
    }

    public void deleteAttachmentByIdIdx(CFAccAuthorization Authorization, CFAccAttachmentPKey argKey) {
        CFAccAttachmentBuff cur;
        LinkedList<CFAccAttachmentBuff> matchSet = new LinkedList<CFAccAttachmentBuff>();
        Iterator<CFAccAttachmentBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAttachmentBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAttachment(Authorization, cur);
        }
    }

    public void deleteAttachmentByTenantIdx(CFAccAuthorization Authorization, long argTenantId) {
        CFAccAttachmentByTenantIdxKey key = schema.getFactoryAttachment().newTenantIdxKey();
        key.setRequiredTenantId(argTenantId);
        deleteAttachmentByTenantIdx(Authorization, key);
    }

    public void deleteAttachmentByTenantIdx(CFAccAuthorization Authorization,
            CFAccAttachmentByTenantIdxKey argKey) {
        CFAccAttachmentBuff cur;
        LinkedList<CFAccAttachmentBuff> matchSet = new LinkedList<CFAccAttachmentBuff>();
        Iterator<CFAccAttachmentBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAttachmentBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAttachment(Authorization, cur);
        }
    }

    public void deleteAttachmentByContactIdx(CFAccAuthorization Authorization, long argTenantId,
            long argContactId) {
        CFAccAttachmentByContactIdxKey key = schema.getFactoryAttachment().newContactIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        deleteAttachmentByContactIdx(Authorization, key);
    }

    public void deleteAttachmentByContactIdx(CFAccAuthorization Authorization,
            CFAccAttachmentByContactIdxKey argKey) {
        CFAccAttachmentBuff cur;
        LinkedList<CFAccAttachmentBuff> matchSet = new LinkedList<CFAccAttachmentBuff>();
        Iterator<CFAccAttachmentBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAttachmentBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAttachment(Authorization, cur);
        }
    }

    public void deleteAttachmentByUDescrIdx(CFAccAuthorization Authorization, long argTenantId, long argContactId,
            String argDescription) {
        CFAccAttachmentByUDescrIdxKey key = schema.getFactoryAttachment().newUDescrIdxKey();
        key.setRequiredTenantId(argTenantId);
        key.setRequiredContactId(argContactId);
        key.setRequiredDescription(argDescription);
        deleteAttachmentByUDescrIdx(Authorization, key);
    }

    public void deleteAttachmentByUDescrIdx(CFAccAuthorization Authorization,
            CFAccAttachmentByUDescrIdxKey argKey) {
        CFAccAttachmentBuff cur;
        LinkedList<CFAccAttachmentBuff> matchSet = new LinkedList<CFAccAttachmentBuff>();
        Iterator<CFAccAttachmentBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAttachmentBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAttachment(Authorization, cur);
        }
    }

    public void deleteAttachmentByMimeTypeIdx(CFAccAuthorization Authorization, Integer argMimeTypeId) {
        CFAccAttachmentByMimeTypeIdxKey key = schema.getFactoryAttachment().newMimeTypeIdxKey();
        key.setOptionalMimeTypeId(argMimeTypeId);
        deleteAttachmentByMimeTypeIdx(Authorization, key);
    }

    public void deleteAttachmentByMimeTypeIdx(CFAccAuthorization Authorization,
            CFAccAttachmentByMimeTypeIdxKey argKey) {
        CFAccAttachmentBuff cur;
        LinkedList<CFAccAttachmentBuff> matchSet = new LinkedList<CFAccAttachmentBuff>();
        Iterator<CFAccAttachmentBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAccAttachmentBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteAttachment(Authorization, cur);
        }
    }

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

    public CFAccCursor openAttachmentCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) {
        CFAccCursor cursor;
        CFAccAttachmentByTenantIdxKey key = schema.getFactoryAttachment().newTenantIdxKey();
        key.setRequiredTenantId(TenantId);

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

    public CFAccCursor openAttachmentCursorByContactIdx(CFAccAuthorization Authorization, long TenantId,
            long ContactId) {
        CFAccCursor cursor;
        CFAccAttachmentByContactIdxKey key = schema.getFactoryAttachment().newContactIdxKey();
        key.setRequiredTenantId(TenantId);
        key.setRequiredContactId(ContactId);

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

    public CFAccCursor openAttachmentCursorByMimeTypeIdx(CFAccAuthorization Authorization, Integer MimeTypeId) {
        CFAccCursor cursor;
        CFAccAttachmentByMimeTypeIdxKey key = schema.getFactoryAttachment().newMimeTypeIdxKey();
        key.setOptionalMimeTypeId(MimeTypeId);

        if (dictByMimeTypeIdx.containsKey(key)) {
            SortedMap<CFAccAttachmentPKey, CFAccAttachmentBuff> subdictMimeTypeIdx = dictByMimeTypeIdx.get(key);
            cursor = new CFAccRamAttachmentCursor(Authorization, schema, subdictMimeTypeIdx.values());
        } else {
            cursor = new CFAccRamAttachmentCursor(Authorization, schema, new ArrayList<CFAccAttachmentBuff>());
        }
        return (cursor);
    }

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

    public CFAccAttachmentBuff nextAttachmentCursor(CFAccCursor Cursor) {
        CFAccRamAttachmentCursor cursor = (CFAccRamAttachmentCursor) Cursor;
        CFAccAttachmentBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAccAttachmentBuff firstAttachmentCursor(CFAccCursor Cursor) {
        int targetRowIdx = 1;
        CFAccAttachmentBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAttachmentCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAccAttachmentBuff nthAttachmentCursor(CFAccCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAccAttachmentBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAttachmentCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}