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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamSecFormTable in-memory RAM DbIO implementation
 *   for SecForm.
 */
public class MSSBamRamSecFormTable implements IMSSBamSecFormTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamSecFormPKey, MSSBamSecFormBuff> dictByPKey = new HashMap<MSSBamSecFormPKey, MSSBamSecFormBuff>();
    private SortedMap<MSSBamSecFormBySecAppIdxKey, SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff>> dictBySecAppIdx = new TreeMap<MSSBamSecFormBySecAppIdxKey, SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff>>();
    private SortedMap<MSSBamSecFormByUJEEServletIdxKey, MSSBamSecFormBuff> dictByUJEEServletIdx = new TreeMap<MSSBamSecFormByUJEEServletIdxKey, MSSBamSecFormBuff>();

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

    public void createSecForm(MSSBamAuthorization Authorization, MSSBamSecFormBuff Buff) {
        MSSBamSecFormPKey pkey = schema.getFactorySecForm().newPKey();

        pkey.setRequiredSecFormId(schema.nextSecFormIdGen());
        Buff.setRequiredSecFormId(pkey.getRequiredSecFormId());

        MSSBamSecFormBySecAppIdxKey keySecAppIdx = schema.getFactorySecForm().newSecAppIdxKey();
        keySecAppIdx.setRequiredSecAppId(Buff.getRequiredSecAppId());

        MSSBamSecFormByUJEEServletIdxKey keyUJEEServletIdx = schema.getFactorySecForm().newUJEEServletIdxKey();
        keyUJEEServletIdx.setRequiredSecAppId(Buff.getRequiredSecAppId());
        keyUJEEServletIdx.setRequiredJEEServletMapName(Buff.getRequiredJEEServletMapName());

        // Validate unique indexes

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

        if (dictByUJEEServletIdx.containsKey(keyUJEEServletIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecForm",
                    "SecFormUJEEServletIdx", keyUJEEServletIdx);
        }

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableSecApp().readDerivedByIdIdx(Authorization, Buff.getRequiredSecAppId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createSecForm", "Container", "SecFormApplication", "SecApp", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff> subdictSecAppIdx;
        if (dictBySecAppIdx.containsKey(keySecAppIdx)) {
            subdictSecAppIdx = dictBySecAppIdx.get(keySecAppIdx);
        } else {
            subdictSecAppIdx = new TreeMap<MSSBamSecFormPKey, MSSBamSecFormBuff>();
            dictBySecAppIdx.put(keySecAppIdx, subdictSecAppIdx);
        }
        subdictSecAppIdx.put(pkey, Buff);

        dictByUJEEServletIdx.put(keyUJEEServletIdx, Buff);

    }

    public MSSBamSecFormBuff readDerived(MSSBamAuthorization Authorization, MSSBamSecFormPKey PKey) {
        final String S_ProcName = "MSSBamRamSecForm.readDerived() ";
        MSSBamSecFormPKey key = schema.getFactorySecForm().newPKey();
        key.setRequiredSecFormId(PKey.getRequiredSecFormId());
        MSSBamSecFormBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecFormBuff[] readDerivedBySecAppIdx(MSSBamAuthorization Authorization, int SecAppId) {
        final String S_ProcName = "MSSBamRamSecForm.readDerivedBySecAppIdx() ";
        MSSBamSecFormBySecAppIdxKey key = schema.getFactorySecForm().newSecAppIdxKey();
        key.setRequiredSecAppId(SecAppId);

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

    public MSSBamSecFormBuff readDerivedByUJEEServletIdx(MSSBamAuthorization Authorization, int SecAppId,
            String JEEServletMapName) {
        final String S_ProcName = "MSSBamRamSecForm.readDerivedByUJEEServletIdx() ";
        MSSBamSecFormByUJEEServletIdxKey key = schema.getFactorySecForm().newUJEEServletIdxKey();
        key.setRequiredSecAppId(SecAppId);
        key.setRequiredJEEServletMapName(JEEServletMapName);

        MSSBamSecFormBuff buff;
        if (dictByUJEEServletIdx.containsKey(key)) {
            buff = dictByUJEEServletIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public MSSBamSecFormBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, int SecFormId) {
        final String S_ProcName = "MSSBamRamSecForm.readDerivedByIdIdx() ";
        MSSBamSecFormPKey key = schema.getFactorySecForm().newPKey();
        key.setRequiredSecFormId(SecFormId);

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

    public MSSBamSecFormBuff readBuff(MSSBamAuthorization Authorization, MSSBamSecFormPKey PKey) {
        final String S_ProcName = "MSSBamRamSecForm.readBuff() ";
        MSSBamSecFormBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SFRM"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecFormBuff readBuffByIdIdx(MSSBamAuthorization Authorization, int SecFormId) {
        final String S_ProcName = "MSSBamRamSecForm.readBuffByIdIdx() ";
        MSSBamSecFormBuff buff = readDerivedByIdIdx(Authorization, SecFormId);
        if ((buff != null) && buff.getClassCode().equals("SFRM")) {
            return ((MSSBamSecFormBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamSecFormBuff[] readBuffBySecAppIdx(MSSBamAuthorization Authorization, int SecAppId) {
        final String S_ProcName = "MSSBamRamSecForm.readBuffBySecAppIdx() ";
        MSSBamSecFormBuff buff;
        ArrayList<MSSBamSecFormBuff> filteredList = new ArrayList<MSSBamSecFormBuff>();
        MSSBamSecFormBuff[] buffList = readDerivedBySecAppIdx(Authorization, SecAppId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("SFRM")) {
                filteredList.add((MSSBamSecFormBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSecFormBuff[0]));
    }

    public MSSBamSecFormBuff readBuffByUJEEServletIdx(MSSBamAuthorization Authorization, int SecAppId,
            String JEEServletMapName) {
        final String S_ProcName = "MSSBamRamSecForm.readBuffByUJEEServletIdx() ";
        MSSBamSecFormBuff buff = readDerivedByUJEEServletIdx(Authorization, SecAppId, JEEServletMapName);
        if ((buff != null) && buff.getClassCode().equals("SFRM")) {
            return ((MSSBamSecFormBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateSecForm(MSSBamAuthorization Authorization, MSSBamSecFormBuff Buff) {
        MSSBamSecFormPKey pkey = schema.getFactorySecForm().newPKey();
        pkey.setRequiredSecFormId(Buff.getRequiredSecFormId());
        MSSBamSecFormBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecForm",
                    "Existing record not found", "SecForm", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecForm",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamSecFormBySecAppIdxKey existingKeySecAppIdx = schema.getFactorySecForm().newSecAppIdxKey();
        existingKeySecAppIdx.setRequiredSecAppId(existing.getRequiredSecAppId());

        MSSBamSecFormBySecAppIdxKey newKeySecAppIdx = schema.getFactorySecForm().newSecAppIdxKey();
        newKeySecAppIdx.setRequiredSecAppId(Buff.getRequiredSecAppId());

        MSSBamSecFormByUJEEServletIdxKey existingKeyUJEEServletIdx = schema.getFactorySecForm()
                .newUJEEServletIdxKey();
        existingKeyUJEEServletIdx.setRequiredSecAppId(existing.getRequiredSecAppId());
        existingKeyUJEEServletIdx.setRequiredJEEServletMapName(existing.getRequiredJEEServletMapName());

        MSSBamSecFormByUJEEServletIdxKey newKeyUJEEServletIdx = schema.getFactorySecForm().newUJEEServletIdxKey();
        newKeyUJEEServletIdx.setRequiredSecAppId(Buff.getRequiredSecAppId());
        newKeyUJEEServletIdx.setRequiredJEEServletMapName(Buff.getRequiredJEEServletMapName());

        // Check unique indexes

        if (!existingKeyUJEEServletIdx.equals(newKeyUJEEServletIdx)) {
            if (dictByUJEEServletIdx.containsKey(newKeyUJEEServletIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateSecForm", "SecFormUJEEServletIdx", newKeyUJEEServletIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableSecApp().readDerivedByIdIdx(Authorization, Buff.getRequiredSecAppId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateSecForm", "Container", "SecFormApplication", "SecApp", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff> subdict;

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

        subdict = dictBySecAppIdx.get(existingKeySecAppIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictBySecAppIdx.containsKey(newKeySecAppIdx)) {
            subdict = dictBySecAppIdx.get(newKeySecAppIdx);
        } else {
            subdict = new TreeMap<MSSBamSecFormPKey, MSSBamSecFormBuff>();
            dictBySecAppIdx.put(newKeySecAppIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUJEEServletIdx.remove(existingKeyUJEEServletIdx);
        dictByUJEEServletIdx.put(newKeyUJEEServletIdx, Buff);

    }

    public void deleteSecForm(MSSBamAuthorization Authorization, MSSBamSecFormBuff Buff) {
        final String S_ProcName = "MSSBamRamSecFormTable.deleteSecForm() ";
        MSSBamSecFormPKey pkey = schema.getFactorySecForm().newPKey();

        pkey.setRequiredSecFormId(schema.nextSecFormIdGen());
        MSSBamSecFormBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteSecForm",
                    "Existing record not found", "SecForm", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecForm",
                    pkey);
        }
        MSSBamSecFormBySecAppIdxKey keySecAppIdx = schema.getFactorySecForm().newSecAppIdxKey();
        keySecAppIdx.setRequiredSecAppId(existing.getRequiredSecAppId());

        MSSBamSecFormByUJEEServletIdxKey keyUJEEServletIdx = schema.getFactorySecForm().newUJEEServletIdxKey();
        keyUJEEServletIdx.setRequiredSecAppId(existing.getRequiredSecAppId());
        keyUJEEServletIdx.setRequiredJEEServletMapName(existing.getRequiredJEEServletMapName());

        // Validate reverse foreign keys

        if (schema.getTableSecGroupForm().readDerivedByFormIdx(Authorization,
                existing.getRequiredSecFormId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecForm",
                    "Lookup", "SecGroupFormForm", "SecGroupForm", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictBySecAppIdx.get(keySecAppIdx);
        subdict.remove(pkey);

        dictByUJEEServletIdx.remove(keyUJEEServletIdx);

    }

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

    public MSSBamCursor openSecFormCursorBySecAppIdx(MSSBamAuthorization Authorization, int SecAppId) {
        MSSBamCursor cursor;
        MSSBamSecFormBySecAppIdxKey key = schema.getFactorySecForm().newSecAppIdxKey();
        key.setRequiredSecAppId(SecAppId);

        if (dictBySecAppIdx.containsKey(key)) {
            SortedMap<MSSBamSecFormPKey, MSSBamSecFormBuff> subdictSecAppIdx = dictBySecAppIdx.get(key);
            cursor = new MSSBamRamSecFormCursor(Authorization, schema, subdictSecAppIdx.values());
        } else {
            cursor = new MSSBamRamSecFormCursor(Authorization, schema, new ArrayList<MSSBamSecFormBuff>());
        }
        return (cursor);
    }

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

    public MSSBamSecFormBuff nextSecFormCursor(MSSBamCursor Cursor) {
        MSSBamRamSecFormCursor cursor = (MSSBamRamSecFormCursor) Cursor;
        MSSBamSecFormBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamSecFormBuff firstSecFormCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamSecFormBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecFormCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamSecFormBuff nthSecFormCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamSecFormBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecFormCursor(Cursor);
        }
        return (rec);
    }
}