net.sourceforge.msscodefactory.v1_11.MSSBamRam.MSSBamRamSchemaRefTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.v1_11.MSSBamRam.MSSBamRamSchemaRefTable.java

Source

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

/*
 *   MSS Code Factory 1.11
 *
 *   Copyright (c) 2012-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/>.
 *   
 * ***********************************************************************
 *
 *   $Revision$
 */

package net.sourceforge.msscodefactory.v1_11.MSSBamRam;

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.v1_11.MSSBam.*;
import net.sourceforge.msscodefactory.v1_11.MSSBamBL.*;
import net.sourceforge.msscodefactory.v1_11.MSSBamBLRam.*;

/*
 *   MSSBamRamSchemaRefTable in-memory RAM DbIO implementation
 *   for SchemaRef.
 */
public class MSSBamRamSchemaRefTable implements IMSSBamSchemaRefTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> dictByPKey = new HashMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>();
    private SortedMap<MSSBamSchemaRefBySchemaIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>> dictBySchemaIdx = new TreeMap<MSSBamSchemaRefBySchemaIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>>();
    private SortedMap<MSSBamSchemaRefByRefSchemaIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>> dictByRefSchemaIdx = new TreeMap<MSSBamSchemaRefByRefSchemaIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>>();

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

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

        pkey.setClassCode(Buff.getClassCode());

        pkey.setRequiredId(Buff.getRequiredId());

        MSSBamSchemaRefBySchemaIdxKey keySchemaIdx = schema.getFactorySchemaRef().newSchemaIdxKey();
        keySchemaIdx.setRequiredSchemaId(Buff.getRequiredSchemaId());

        MSSBamSchemaRefByRefSchemaIdxKey keyRefSchemaIdx = schema.getFactorySchemaRef().newRefSchemaIdxKey();
        keyRefSchemaIdx.setOptionalRefSchemaId(Buff.getOptionalRefSchemaId());

        // Validate unique indexes

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

        // Validate foreign keys

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

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableSchemaDef().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredSchemaId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createSchemaRef", "Container", "Schema", "SchemaDef", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdictSchemaIdx;
        if (dictBySchemaIdx.containsKey(keySchemaIdx)) {
            subdictSchemaIdx = dictBySchemaIdx.get(keySchemaIdx);
        } else {
            subdictSchemaIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>();
            dictBySchemaIdx.put(keySchemaIdx, subdictSchemaIdx);
        }
        subdictSchemaIdx.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdictRefSchemaIdx;
        if (dictByRefSchemaIdx.containsKey(keyRefSchemaIdx)) {
            subdictRefSchemaIdx = dictByRefSchemaIdx.get(keyRefSchemaIdx);
        } else {
            subdictRefSchemaIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>();
            dictByRefSchemaIdx.put(keyRefSchemaIdx, subdictRefSchemaIdx);
        }
        subdictRefSchemaIdx.put(pkey, Buff);

    }

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

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

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

    public MSSBamSchemaRefBuff[] 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<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamSchemaRefBuff)) {
                    filteredList.add((MSSBamSchemaRefBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
        }
    }

    public MSSBamSchemaRefBuff[] 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<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamSchemaRefBuff)) {
                    filteredList.add((MSSBamSchemaRefBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
        }
    }

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

    public MSSBamSchemaRefBuff[] 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<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
            for (int idx = 0; idx < buffList.length; idx++) {
                buff = buffList[idx];
                if ((buff != null) && (buff instanceof MSSBamSchemaRefBuff)) {
                    filteredList.add((MSSBamSchemaRefBuff) buff);
                }
            }
            return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
        }
    }

    public MSSBamSchemaRefBuff 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 MSSBamSchemaRefBuff) {
            return ((MSSBamSchemaRefBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamSchemaRefBuff[] readDerivedBySchemaIdx(MSSBamAuthorization Authorization, long SchemaId) {
        final String S_ProcName = "MSSBamRamSchemaRef.readDerivedBySchemaIdx() ";
        MSSBamSchemaRefBySchemaIdxKey key = schema.getFactorySchemaRef().newSchemaIdxKey();
        key.setRequiredSchemaId(SchemaId);

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

    public MSSBamSchemaRefBuff[] readDerivedByRefSchemaIdx(MSSBamAuthorization Authorization, Long RefSchemaId) {
        final String S_ProcName = "MSSBamRamSchemaRef.readDerivedByRefSchemaIdx() ";
        MSSBamSchemaRefByRefSchemaIdxKey key = schema.getFactorySchemaRef().newRefSchemaIdxKey();
        key.setOptionalRefSchemaId(RefSchemaId);

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

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

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

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

    public MSSBamSchemaRefBuff lockBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) {
        final String S_ProcName = "MSSBamRamSchemaRef.readBuff() ";
        MSSBamSchemaRefBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SCRF"))) {
            buff = null;
        }
        return (buff);
    }

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

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

    public MSSBamSchemaRefBuff[] readBuffByTenantIdx(MSSBamAuthorization Authorization, long TenantId) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByTenantIdx() ";
        MSSBamAnyObjBuff buff;
        ArrayList<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
        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((MSSBamSchemaRefBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
    }

    public MSSBamSchemaRefBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByScopeIdx() ";
        MSSBamAnyObjBuff buff;
        ArrayList<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
        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((MSSBamSchemaRefBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
    }

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

    public MSSBamSchemaRefBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "MSSBamRamAnyObj.readBuffByAuthorIdx() ";
        MSSBamAnyObjBuff buff;
        ArrayList<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
        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((MSSBamSchemaRefBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
    }

    public MSSBamSchemaRefBuff 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 ((MSSBamSchemaRefBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamSchemaRefBuff[] readBuffBySchemaIdx(MSSBamAuthorization Authorization, long SchemaId) {
        final String S_ProcName = "MSSBamRamSchemaRef.readBuffBySchemaIdx() ";
        MSSBamSchemaRefBuff buff;
        ArrayList<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
        MSSBamSchemaRefBuff[] buffList = readDerivedBySchemaIdx(Authorization, SchemaId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("SCRF")) {
                filteredList.add((MSSBamSchemaRefBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
    }

    public MSSBamSchemaRefBuff[] readBuffByRefSchemaIdx(MSSBamAuthorization Authorization, Long RefSchemaId) {
        final String S_ProcName = "MSSBamRamSchemaRef.readBuffByRefSchemaIdx() ";
        MSSBamSchemaRefBuff buff;
        ArrayList<MSSBamSchemaRefBuff> filteredList = new ArrayList<MSSBamSchemaRefBuff>();
        MSSBamSchemaRefBuff[] buffList = readDerivedByRefSchemaIdx(Authorization, RefSchemaId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("SCRF")) {
                filteredList.add((MSSBamSchemaRefBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSchemaRefBuff[0]));
    }

    public void updateSchemaRef(MSSBamAuthorization Authorization, MSSBamSchemaRefBuff Buff) {
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        MSSBamSchemaRefBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSchemaRef",
                    "Existing record not found", "SchemaRef", pkey);
        }
        MSSBamSchemaRefBySchemaIdxKey existingKeySchemaIdx = schema.getFactorySchemaRef().newSchemaIdxKey();
        existingKeySchemaIdx.setRequiredSchemaId(existing.getRequiredSchemaId());

        MSSBamSchemaRefBySchemaIdxKey newKeySchemaIdx = schema.getFactorySchemaRef().newSchemaIdxKey();
        newKeySchemaIdx.setRequiredSchemaId(Buff.getRequiredSchemaId());

        MSSBamSchemaRefByRefSchemaIdxKey existingKeyRefSchemaIdx = schema.getFactorySchemaRef()
                .newRefSchemaIdxKey();
        existingKeyRefSchemaIdx.setOptionalRefSchemaId(existing.getOptionalRefSchemaId());

        MSSBamSchemaRefByRefSchemaIdxKey newKeyRefSchemaIdx = schema.getFactorySchemaRef().newRefSchemaIdxKey();
        newKeyRefSchemaIdx.setOptionalRefSchemaId(Buff.getOptionalRefSchemaId());

        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

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

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableSchemaDef().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredSchemaId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateSchemaRef", "Container", "Schema", "SchemaDef", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdict;

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

        subdict = dictBySchemaIdx.get(existingKeySchemaIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictBySchemaIdx.containsKey(newKeySchemaIdx)) {
            subdict = dictBySchemaIdx.get(newKeySchemaIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>();
            dictBySchemaIdx.put(newKeySchemaIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByRefSchemaIdx.get(existingKeyRefSchemaIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByRefSchemaIdx.containsKey(newKeyRefSchemaIdx)) {
            subdict = dictByRefSchemaIdx.get(newKeyRefSchemaIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff>();
            dictByRefSchemaIdx.put(newKeyRefSchemaIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteSchemaRef(MSSBamAuthorization Authorization, MSSBamSchemaRefBuff Buff) {
        final String S_ProcName = "MSSBamRamSchemaRefTable.deleteSchemaRef() ";
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();

        pkey.setRequiredId(schema.nextAnyObjIdGen());
        MSSBamSchemaRefBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteSchemaRef",
                    "Existing record not found", "SchemaRef", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSchemaRef",
                    pkey);
        }
        MSSBamSchemaRefBySchemaIdxKey keySchemaIdx = schema.getFactorySchemaRef().newSchemaIdxKey();
        keySchemaIdx.setRequiredSchemaId(existing.getRequiredSchemaId());

        MSSBamSchemaRefByRefSchemaIdxKey keyRefSchemaIdx = schema.getFactorySchemaRef().newRefSchemaIdxKey();
        keyRefSchemaIdx.setOptionalRefSchemaId(existing.getOptionalRefSchemaId());

        // Validate reverse foreign keys

        // Delete is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictBySchemaIdx.get(keySchemaIdx);
        subdict.remove(pkey);

        subdict = dictByRefSchemaIdx.get(keyRefSchemaIdx);
        subdict.remove(pkey);

    }

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

    public MSSBamCursor openSchemaRefCursorBySchemaIdx(MSSBamAuthorization Authorization, long SchemaId) {
        MSSBamCursor cursor;
        MSSBamSchemaRefBySchemaIdxKey key = schema.getFactorySchemaRef().newSchemaIdxKey();
        key.setRequiredSchemaId(SchemaId);

        if (dictBySchemaIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdictSchemaIdx = dictBySchemaIdx.get(key);
            cursor = new MSSBamRamSchemaRefCursor(Authorization, schema, subdictSchemaIdx.values());
        } else {
            cursor = new MSSBamRamSchemaRefCursor(Authorization, schema, new ArrayList<MSSBamSchemaRefBuff>());
        }
        return (cursor);
    }

    public MSSBamCursor openSchemaRefCursorByRefSchemaIdx(MSSBamAuthorization Authorization, Long RefSchemaId) {
        MSSBamCursor cursor;
        MSSBamSchemaRefByRefSchemaIdxKey key = schema.getFactorySchemaRef().newRefSchemaIdxKey();
        key.setOptionalRefSchemaId(RefSchemaId);

        if (dictByRefSchemaIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamSchemaRefBuff> subdictRefSchemaIdx = dictByRefSchemaIdx.get(key);
            cursor = new MSSBamRamSchemaRefCursor(Authorization, schema, subdictRefSchemaIdx.values());
        } else {
            cursor = new MSSBamRamSchemaRefCursor(Authorization, schema, new ArrayList<MSSBamSchemaRefBuff>());
        }
        return (cursor);
    }

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

    public MSSBamSchemaRefBuff nextSchemaRefCursor(MSSBamCursor Cursor) {
        MSSBamRamSchemaRefCursor cursor = (MSSBamRamSchemaRefCursor) Cursor;
        MSSBamSchemaRefBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamSchemaRefBuff firstSchemaRefCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamSchemaRefBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSchemaRefCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamSchemaRefBuff nthSchemaRefCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamSchemaRefBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSchemaRefCursor(Cursor);
        }
        return (rec);
    }
}