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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamDictionaryTable in-memory RAM DbIO implementation
 *   for Dictionary.
 */
public class MSSBamRamDictionaryTable implements IMSSBamDictionaryTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamAnyObjPKey, MSSBamDictionaryBuff> dictByPKey = new HashMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff>();
    private SortedMap<MSSBamDictionaryByDictTenantIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff>> dictByDictTenantIdx = new TreeMap<MSSBamDictionaryByDictTenantIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff>>();

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

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

        pkey.setClassCode(Buff.getClassCode());

        pkey.setRequiredId(Buff.getRequiredId());

        MSSBamDictionaryByDictTenantIdxKey keyDictTenantIdx = schema.getFactoryDictionary().newDictTenantIdxKey();
        keyDictTenantIdx.setRequiredDictTenantId(Buff.getRequiredDictTenantId());

        // Validate unique indexes

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

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff> subdictDictTenantIdx;
        if (dictByDictTenantIdx.containsKey(keyDictTenantIdx)) {
            subdictDictTenantIdx = dictByDictTenantIdx.get(keyDictTenantIdx);
        } else {
            subdictDictTenantIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff>();
            dictByDictTenantIdx.put(keyDictTenantIdx, subdictDictTenantIdx);
        }
        subdictDictTenantIdx.put(pkey, Buff);

    }

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

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

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

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

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

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

    public MSSBamDictionaryBuff[] readDerivedByDictTenantIdx(MSSBamAuthorization Authorization, long DictTenantId) {
        final String S_ProcName = "MSSBamRamDictionary.readDerivedByDictTenantIdx() ";
        MSSBamDictionaryByDictTenantIdxKey key = schema.getFactoryDictionary().newDictTenantIdxKey();
        key.setRequiredDictTenantId(DictTenantId);

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

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

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

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

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

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

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

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

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

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

    public MSSBamDictionaryBuff[] readBuffByDictTenantIdx(MSSBamAuthorization Authorization, long DictTenantId) {
        final String S_ProcName = "MSSBamRamDictionary.readBuffByDictTenantIdx() ";
        MSSBamDictionaryBuff buff;
        ArrayList<MSSBamDictionaryBuff> filteredList = new ArrayList<MSSBamDictionaryBuff>();
        MSSBamDictionaryBuff[] buffList = readDerivedByDictTenantIdx(Authorization, DictTenantId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("DICT")) {
                filteredList.add((MSSBamDictionaryBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamDictionaryBuff[0]));
    }

    public void updateDictionary(MSSBamAuthorization Authorization, MSSBamDictionaryBuff Buff) {
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        MSSBamDictionaryBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateDictionary",
                    "Existing record not found", "Dictionary", pkey);
        }
        MSSBamDictionaryByDictTenantIdxKey existingKeyDictTenantIdx = schema.getFactoryDictionary()
                .newDictTenantIdxKey();
        existingKeyDictTenantIdx.setRequiredDictTenantId(existing.getRequiredDictTenantId());

        MSSBamDictionaryByDictTenantIdxKey newKeyDictTenantIdx = schema.getFactoryDictionary()
                .newDictTenantIdxKey();
        newKeyDictTenantIdx.setRequiredDictTenantId(Buff.getRequiredDictTenantId());

        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

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

        // Update is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff> subdict;

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

        subdict = dictByDictTenantIdx.get(existingKeyDictTenantIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByDictTenantIdx.containsKey(newKeyDictTenantIdx)) {
            subdict = dictByDictTenantIdx.get(newKeyDictTenantIdx);
        } else {
            subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff>();
            dictByDictTenantIdx.put(newKeyDictTenantIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteDictionary(MSSBamAuthorization Authorization, MSSBamDictionaryBuff Buff) {
        final String S_ProcName = "MSSBamRamDictionaryTable.deleteDictionary() ";
        MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey();

        pkey.setRequiredId(schema.nextAnyObjIdGen());
        MSSBamDictionaryBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteDictionary",
                    "Existing record not found", "Dictionary", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteDictionary",
                    pkey);
        }
        MSSBamDictionaryByDictTenantIdxKey keyDictTenantIdx = schema.getFactoryDictionary().newDictTenantIdxKey();
        keyDictTenantIdx.setRequiredDictTenantId(existing.getRequiredDictTenantId());

        // Validate reverse foreign keys

        if (schema.getTableMSSBam().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteDictionary",
                    "Superclass", "SuperClass", "MSSBam", pkey);
        }

        if (schema.getTableTLD().readDerivedByDictIdx(Authorization, existing.getRequiredId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteDictionary",
                    "Container", "Dictionary", "TLD", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByDictTenantIdx.get(keyDictTenantIdx);
        subdict.remove(pkey);

    }

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

    public MSSBamCursor openDictionaryCursorByDictTenantIdx(MSSBamAuthorization Authorization, long DictTenantId) {
        MSSBamCursor cursor;
        MSSBamDictionaryByDictTenantIdxKey key = schema.getFactoryDictionary().newDictTenantIdxKey();
        key.setRequiredDictTenantId(DictTenantId);

        if (dictByDictTenantIdx.containsKey(key)) {
            SortedMap<MSSBamAnyObjPKey, MSSBamDictionaryBuff> subdictDictTenantIdx = dictByDictTenantIdx.get(key);
            cursor = new MSSBamRamDictionaryCursor(Authorization, schema, subdictDictTenantIdx.values());
        } else {
            cursor = new MSSBamRamDictionaryCursor(Authorization, schema, new ArrayList<MSSBamDictionaryBuff>());
        }
        return (cursor);
    }

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

    public MSSBamDictionaryBuff nextDictionaryCursor(MSSBamCursor Cursor) {
        MSSBamRamDictionaryCursor cursor = (MSSBamRamDictionaryCursor) Cursor;
        MSSBamDictionaryBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamDictionaryBuff firstDictionaryCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamDictionaryBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextDictionaryCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamDictionaryBuff nthDictionaryCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamDictionaryBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextDictionaryCursor(Cursor);
        }
        return (rec);
    }
}