net.sourceforge.msscodefactory.v1_10.MSSBamRam.MSSBamRamChainTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.v1_10.MSSBamRam.MSSBamRamChainTable.java

Source

// Description: Java6 in-memory RAM DbIO implementation for Chain.

/*
 *   MSS Code Factory 1.10
 *
 *   Copyright (c) 2012 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/>.
 *   
 * ***********************************************************************
 *
 *   Code manufactured
 *   by MSS Code Factory version 1.9.3294
 *
 *   $Revision: 26 $
 */

package net.sourceforge.msscodefactory.v1_10.MSSBamRam;

import java.sql.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v1_9.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.v1_10.MSSBam.*;
import net.sourceforge.msscodefactory.v1_10.MSSBamBL.*;
import net.sourceforge.msscodefactory.v1_10.MSSBamBLRam.*;

/*
 *   MSSBamRamChainTable in-memory RAM DbIO implementation
 *   for Chain.
 */
public class MSSBamRamChainTable implements IMSSBamChainTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamAnyObjPKey, MSSBamChainBuff> dictByPKey = new HashMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
    private SortedMap<MSSBamChainByTableIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>> dictByTableIdx = new TreeMap<MSSBamChainByTableIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>>();
    private SortedMap<MSSBamChainByPrevRelIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>> dictByPrevRelIdx = new TreeMap<MSSBamChainByPrevRelIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>>();
    private SortedMap<MSSBamChainByNextRelIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>> dictByNextRelIdx = new TreeMap<MSSBamChainByNextRelIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff>>();

    public MSSBamRamChainTable(MSSBamBLRamSchema argSchema) {
        schema = argSchema;
    }

    public void createChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();

        pkey.setClassCode(Buff.getClassCode());

        pkey.setRequiredId(Buff.getRequiredId());

        MSSBamChainByTableIdxKey keyTableIdx = schema.getFactoryChain().newTableIdxKey();
        keyTableIdx.setRequiredTableId(Buff.getRequiredTableId());

        MSSBamChainByPrevRelIdxKey keyPrevRelIdx = schema.getFactoryChain().newPrevRelIdxKey();
        keyPrevRelIdx.setOptionalPrevRelationId(Buff.getOptionalPrevRelationId());

        MSSBamChainByNextRelIdxKey keyNextRelIdx = schema.getFactoryChain().newNextRelIdxKey();
        keyNextRelIdx.setOptionalNextRelationId(Buff.getOptionalNextRelationId());

        // Validate unique indexes

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

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableAnyObj().readDerivedByIdIdx(Authorization, Buff.getRequiredId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createChain", "Superclass", "SuperClass", "AnyObj", null);
                }
            }
        }

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableTable().readDerivedByIdIdx(Authorization, Buff.getRequiredTableId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createChain", "Container", "Table", "Table", null);
                }
            }
        }

        {
            boolean allNull = true;
            if (Buff.getOptionalPrevRelationId() != null) {
                allNull = false;
            }
            if (!allNull) {
                if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization,
                        Buff.getOptionalPrevRelationId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createChain", "Lookup", "PrevRelation", "Relation", null);
                }
            }
        }

        {
            boolean allNull = true;
            if (Buff.getOptionalNextRelationId() != null) {
                allNull = false;
            }
            if (!allNull) {
                if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization,
                        Buff.getOptionalNextRelationId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createChain", "Lookup", "NextRelation", "Relation", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictTableIdx;
        if (dictByTableIdx.containsKey(keyTableIdx)) {
            subdictTableIdx = dictByTableIdx.get(keyTableIdx);
        } else {
            subdictTableIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByTableIdx.put(keyTableIdx, subdictTableIdx);
        }
        subdictTableIdx.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictPrevRelIdx;
        if (dictByPrevRelIdx.containsKey(keyPrevRelIdx)) {
            subdictPrevRelIdx = dictByPrevRelIdx.get(keyPrevRelIdx);
        } else {
            subdictPrevRelIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByPrevRelIdx.put(keyPrevRelIdx, subdictPrevRelIdx);
        }
        subdictPrevRelIdx.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictNextRelIdx;
        if (dictByNextRelIdx.containsKey(keyNextRelIdx)) {
            subdictNextRelIdx = dictByNextRelIdx.get(keyNextRelIdx);
        } else {
            subdictNextRelIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByNextRelIdx.put(keyNextRelIdx, subdictNextRelIdx);
        }
        subdictNextRelIdx.put(pkey, Buff);

    }

    public MSSBamChainBuff readDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) {
        final String S_ProcName = "MSSBamRamChain.readDerived() ";
        MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        MSSBamChainBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamChainBuff[] readDerivedByTenantIdx(MSSBamAuthorization Authorization, long TenantId) {
        final String S_ProcName = "MSSBamRamAnyObj.readDerivedByTenantIdx() ";
        MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByTenantIdx(Authorization, TenantId);
        if (buffList == null) {
            return (null);
        } else {
            MSSBamAnyObjBuff buff;
            ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamChainBuff)) {
                    filteredList.add((MSSBamChainBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamChainBuff[0]));
        }
    }

    public MSSBamChainBuff[] readDerivedByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "MSSBamRamAnyObj.readDerivedByScopeIdx() ";
        MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByScopeIdx(Authorization, ScopeId);
        if (buffList == null) {
            return (null);
        } else {
            MSSBamAnyObjBuff buff;
            ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamChainBuff)) {
                    filteredList.add((MSSBamChainBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamChainBuff[0]));
        }
    }

    public MSSBamChainBuff[] readDerivedByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "MSSBamRamAnyObj.readDerivedByAuthorIdx() ";
        MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByAuthorIdx(Authorization, AuthorId);
        if (buffList == null) {
            return (null);
        } else {
            MSSBamAnyObjBuff buff;
            ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamChainBuff)) {
                    filteredList.add((MSSBamChainBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamChainBuff[0]));
        }
    }

    public MSSBamChainBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) {
        final String S_ProcName = "MSSBamRamAnyObj.readDerivedByUNameIdx() ";
        MSSBamAnyObjBuff buff = schema.getTableAnyObj().readDerivedByUNameIdx(Authorization, ScopeId, Name);
        if (buff == null) {
            return (null);
        } else if (buff instanceof MSSBamChainBuff) {
            return ((MSSBamChainBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamChainBuff[] readDerivedByTableIdx(MSSBamAuthorization Authorization, long TableId) {
        final String S_ProcName = "MSSBamRamChain.readDerivedByTableIdx() ";
        MSSBamChainByTableIdxKey key = schema.getFactoryChain().newTableIdxKey();
        key.setRequiredTableId(TableId);

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

    public MSSBamChainBuff[] readDerivedByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        final String S_ProcName = "MSSBamRamChain.readDerivedByPrevRelIdx() ";
        MSSBamChainByPrevRelIdxKey key = schema.getFactoryChain().newPrevRelIdxKey();
        key.setOptionalPrevRelationId(PrevRelationId);

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

    public MSSBamChainBuff[] readDerivedByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        final String S_ProcName = "MSSBamRamChain.readDerivedByNextRelIdx() ";
        MSSBamChainByNextRelIdxKey key = schema.getFactoryChain().newNextRelIdxKey();
        key.setOptionalNextRelationId(NextRelationId);

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

    public MSSBamChainBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) {
        final String S_ProcName = "MSSBamRamAnyObj.readDerivedByIdIdx() ";
        MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey();
        key.setRequiredId(Id);

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

    public MSSBamChainBuff readBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) {
        final String S_ProcName = "MSSBamRamChain.readBuff() ";
        MSSBamChainBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CHN"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamChainBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByIdIdx() ";
        MSSBamAnyObjBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("ANYO")) {
            return ((MSSBamChainBuff) buff);
        } else {
            return (null);
        }
    }

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

    public MSSBamChainBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByScopeIdx() ";
        MSSBamAnyObjBuff buff;
        ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
        MSSBamAnyObjBuff[] buffList = readDerivedByScopeIdx(Authorization, ScopeId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ANYO")) {
                filteredList.add((MSSBamChainBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamChainBuff[0]));
    }

    public MSSBamChainBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByAuthorIdx() ";
        MSSBamAnyObjBuff buff;
        ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
        MSSBamAnyObjBuff[] buffList = readDerivedByAuthorIdx(Authorization, AuthorId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ANYO")) {
                filteredList.add((MSSBamChainBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamChainBuff[0]));
    }

    public MSSBamChainBuff readBuffByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByUNameIdx() ";
        MSSBamAnyObjBuff buff = readDerivedByUNameIdx(Authorization, ScopeId, Name);
        if ((buff != null) && buff.getClassCode().equals("ANYO")) {
            return ((MSSBamChainBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamChainBuff[] readBuffByTableIdx(MSSBamAuthorization Authorization, long TableId) {
        final String S_ProcName = "MSSBamRamChain.readBuffByTableIdx() ";
        MSSBamChainBuff buff;
        ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
        MSSBamChainBuff[] buffList = readDerivedByTableIdx(Authorization, TableId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CHN")) {
                filteredList.add((MSSBamChainBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamChainBuff[0]));
    }

    public MSSBamChainBuff[] readBuffByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        final String S_ProcName = "MSSBamRamChain.readBuffByPrevRelIdx() ";
        MSSBamChainBuff buff;
        ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
        MSSBamChainBuff[] buffList = readDerivedByPrevRelIdx(Authorization, PrevRelationId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CHN")) {
                filteredList.add((MSSBamChainBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamChainBuff[0]));
    }

    public MSSBamChainBuff[] readBuffByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        final String S_ProcName = "MSSBamRamChain.readBuffByNextRelIdx() ";
        MSSBamChainBuff buff;
        ArrayList<MSSBamChainBuff> filteredList = new ArrayList<MSSBamChainBuff>();
        MSSBamChainBuff[] buffList = readDerivedByNextRelIdx(Authorization, NextRelationId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CHN")) {
                filteredList.add((MSSBamChainBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamChainBuff[0]));
    }

    public void updateChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        MSSBamChainBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateChain",
                    "Existing record not found", "Chain", pkey);
        }
        MSSBamChainByTableIdxKey existingKeyTableIdx = schema.getFactoryChain().newTableIdxKey();
        existingKeyTableIdx.setRequiredTableId(existing.getRequiredTableId());

        MSSBamChainByTableIdxKey newKeyTableIdx = schema.getFactoryChain().newTableIdxKey();
        newKeyTableIdx.setRequiredTableId(Buff.getRequiredTableId());

        MSSBamChainByPrevRelIdxKey existingKeyPrevRelIdx = schema.getFactoryChain().newPrevRelIdxKey();
        existingKeyPrevRelIdx.setOptionalPrevRelationId(existing.getOptionalPrevRelationId());

        MSSBamChainByPrevRelIdxKey newKeyPrevRelIdx = schema.getFactoryChain().newPrevRelIdxKey();
        newKeyPrevRelIdx.setOptionalPrevRelationId(Buff.getOptionalPrevRelationId());

        MSSBamChainByNextRelIdxKey existingKeyNextRelIdx = schema.getFactoryChain().newNextRelIdxKey();
        existingKeyNextRelIdx.setOptionalNextRelationId(existing.getOptionalNextRelationId());

        MSSBamChainByNextRelIdxKey newKeyNextRelIdx = schema.getFactoryChain().newNextRelIdxKey();
        newKeyNextRelIdx.setOptionalNextRelationId(Buff.getOptionalNextRelationId());

        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableAnyObj().readDerivedByIdIdx(Authorization, Buff.getRequiredId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateChain", "Superclass", "SuperClass", "AnyObj", null);
                }
            }
        }

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableTable().readDerivedByIdIdx(Authorization, Buff.getRequiredTableId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateChain", "Container", "Table", "Table", null);
                }
            }
        }

        {
            boolean allNull = true;
            if (Buff.getOptionalPrevRelationId() != null) {
                allNull = false;
            }
            if (allNull) {
                if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization,
                        Buff.getOptionalPrevRelationId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateChain", "Lookup", "PrevRelation", "Relation", null);
                }
            }
        }

        {
            boolean allNull = true;
            if (Buff.getOptionalNextRelationId() != null) {
                allNull = false;
            }
            if (allNull) {
                if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization,
                        Buff.getOptionalNextRelationId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateChain", "Lookup", "NextRelation", "Relation", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdict;

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

        subdict = dictByTableIdx.get(existingKeyTableIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByTableIdx.containsKey(newKeyTableIdx)) {
            subdict = dictByTableIdx.get(newKeyTableIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByTableIdx.put(newKeyTableIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByPrevRelIdx.get(existingKeyPrevRelIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByPrevRelIdx.containsKey(newKeyPrevRelIdx)) {
            subdict = dictByPrevRelIdx.get(newKeyPrevRelIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByPrevRelIdx.put(newKeyPrevRelIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByNextRelIdx.get(existingKeyNextRelIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByNextRelIdx.containsKey(newKeyNextRelIdx)) {
            subdict = dictByNextRelIdx.get(newKeyNextRelIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamChainBuff>();
            dictByNextRelIdx.put(newKeyNextRelIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        final String S_ProcName = "MSSBamRamChainTable.deleteChain() ";
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();

        pkey.setRequiredId(schema.nextAnyObjIdGen());
        MSSBamChainBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteChain",
                    "Existing record not found", "Chain", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteChain", pkey);
        }
        MSSBamChainByTableIdxKey keyTableIdx = schema.getFactoryChain().newTableIdxKey();
        keyTableIdx.setRequiredTableId(existing.getRequiredTableId());

        MSSBamChainByPrevRelIdxKey keyPrevRelIdx = schema.getFactoryChain().newPrevRelIdxKey();
        keyPrevRelIdx.setOptionalPrevRelationId(existing.getOptionalPrevRelationId());

        MSSBamChainByNextRelIdxKey keyNextRelIdx = schema.getFactoryChain().newNextRelIdxKey();
        keyNextRelIdx.setOptionalNextRelationId(existing.getOptionalNextRelationId());

        // Validate reverse foreign keys

        // Delete is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByTableIdx.get(keyTableIdx);
        subdict.remove(pkey);

        subdict = dictByPrevRelIdx.get(keyPrevRelIdx);
        subdict.remove(pkey);

        subdict = dictByNextRelIdx.get(keyNextRelIdx);
        subdict.remove(pkey);

    }

    public MSSBamCursor openChainCursorAll(MSSBamAuthorization Authorization) {
        MSSBamCursor cursor = new MSSBamRamChainCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public MSSBamCursor openChainCursorByTableIdx(MSSBamAuthorization Authorization, long TableId) {
        MSSBamCursor cursor;
        MSSBamChainByTableIdxKey key = schema.getFactoryChain().newTableIdxKey();
        key.setRequiredTableId(TableId);

        if (dictByTableIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictTableIdx = dictByTableIdx.get(key);
            cursor = new MSSBamRamChainCursor(Authorization, schema, subdictTableIdx.values());
        } else {
            cursor = new MSSBamRamChainCursor(Authorization, schema, new ArrayList<MSSBamChainBuff>());
        }
        return (cursor);
    }

    public MSSBamCursor openChainCursorByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        MSSBamCursor cursor;
        MSSBamChainByPrevRelIdxKey key = schema.getFactoryChain().newPrevRelIdxKey();
        key.setOptionalPrevRelationId(PrevRelationId);

        if (dictByPrevRelIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictPrevRelIdx = dictByPrevRelIdx.get(key);
            cursor = new MSSBamRamChainCursor(Authorization, schema, subdictPrevRelIdx.values());
        } else {
            cursor = new MSSBamRamChainCursor(Authorization, schema, new ArrayList<MSSBamChainBuff>());
        }
        return (cursor);
    }

    public MSSBamCursor openChainCursorByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        MSSBamCursor cursor;
        MSSBamChainByNextRelIdxKey key = schema.getFactoryChain().newNextRelIdxKey();
        key.setOptionalNextRelationId(NextRelationId);

        if (dictByNextRelIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamChainBuff> subdictNextRelIdx = dictByNextRelIdx.get(key);
            cursor = new MSSBamRamChainCursor(Authorization, schema, subdictNextRelIdx.values());
        } else {
            cursor = new MSSBamRamChainCursor(Authorization, schema, new ArrayList<MSSBamChainBuff>());
        }
        return (cursor);
    }

    public void closeChainCursor(MSSBamCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public MSSBamChainBuff nextChainCursor(MSSBamCursor Cursor) {
        MSSBamRamChainCursor cursor = (MSSBamRamChainCursor) Cursor;
        MSSBamChainBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public MSSBamChainBuff prevChainCursor(MSSBamCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        MSSBamChainBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextChainCursor(Cursor);
        }
        return (rec);
    }

    public MSSBamChainBuff firstChainCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamChainBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextChainCursor(Cursor);
        }
        return (rec);
    }

    public MSSBamChainBuff lastChainCursor(MSSBamCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastChainCursor");
    }

    public MSSBamChainBuff nthChainCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamChainBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextChainCursor(Cursor);
        }
        return (rec);
    }
}