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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamSecAppTable in-memory RAM DbIO implementation
 *   for SecApp.
 */
public class MSSBamRamSecAppTable implements IMSSBamSecAppTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamSecAppPKey, MSSBamSecAppBuff> dictByPKey = new HashMap<MSSBamSecAppPKey, MSSBamSecAppBuff>();
    private SortedMap<MSSBamSecAppByClusterIdxKey, SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff>> dictByClusterIdx = new TreeMap<MSSBamSecAppByClusterIdxKey, SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff>>();
    private SortedMap<MSSBamSecAppByUJEEMountIdxKey, MSSBamSecAppBuff> dictByUJEEMountIdx = new TreeMap<MSSBamSecAppByUJEEMountIdxKey, MSSBamSecAppBuff>();

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

    public void createSecApp(MSSBamAuthorization Authorization, MSSBamSecAppBuff Buff) {
        MSSBamSecAppPKey pkey = schema.getFactorySecApp().newPKey();

        pkey.setRequiredSecAppId(schema.nextSecAppIdGen());
        Buff.setRequiredSecAppId(pkey.getRequiredSecAppId());

        MSSBamSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        MSSBamSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey();
        keyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        keyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName());

        // Validate unique indexes

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

        if (dictByUJEEMountIdx.containsKey(keyUJEEMountIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecApp",
                    "SecAppUJEEMountIdx", keyUJEEMountIdx);
        }

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredClusterId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createSecApp", "Container", "SecAppCluster", "Cluster", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff> subdictClusterIdx;
        if (dictByClusterIdx.containsKey(keyClusterIdx)) {
            subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx);
        } else {
            subdictClusterIdx = new TreeMap<MSSBamSecAppPKey, MSSBamSecAppBuff>();
            dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx);
        }
        subdictClusterIdx.put(pkey, Buff);

        dictByUJEEMountIdx.put(keyUJEEMountIdx, Buff);

    }

    public MSSBamSecAppBuff readDerived(MSSBamAuthorization Authorization, MSSBamSecAppPKey PKey) {
        final String S_ProcName = "MSSBamRamSecApp.readDerived() ";
        MSSBamSecAppPKey key = schema.getFactorySecApp().newPKey();
        key.setRequiredSecAppId(PKey.getRequiredSecAppId());
        MSSBamSecAppBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecAppBuff[] readDerivedByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) {
        final String S_ProcName = "MSSBamRamSecApp.readDerivedByClusterIdx() ";
        MSSBamSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

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

    public MSSBamSecAppBuff readDerivedByUJEEMountIdx(MSSBamAuthorization Authorization, long ClusterId,
            String JEEMountName) {
        final String S_ProcName = "MSSBamRamSecApp.readDerivedByUJEEMountIdx() ";
        MSSBamSecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey();
        key.setRequiredClusterId(ClusterId);
        key.setRequiredJEEMountName(JEEMountName);

        MSSBamSecAppBuff buff;
        if (dictByUJEEMountIdx.containsKey(key)) {
            buff = dictByUJEEMountIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public MSSBamSecAppBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, int SecAppId) {
        final String S_ProcName = "MSSBamRamSecApp.readDerivedByIdIdx() ";
        MSSBamSecAppPKey key = schema.getFactorySecApp().newPKey();
        key.setRequiredSecAppId(SecAppId);

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

    public MSSBamSecAppBuff readBuff(MSSBamAuthorization Authorization, MSSBamSecAppPKey PKey) {
        final String S_ProcName = "MSSBamRamSecApp.readBuff() ";
        MSSBamSecAppBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecAppBuff readBuffByIdIdx(MSSBamAuthorization Authorization, int SecAppId) {
        final String S_ProcName = "MSSBamRamSecApp.readBuffByIdIdx() ";
        MSSBamSecAppBuff buff = readDerivedByIdIdx(Authorization, SecAppId);
        if ((buff != null) && buff.getClassCode().equals("SAPP")) {
            return ((MSSBamSecAppBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamSecAppBuff[] readBuffByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) {
        final String S_ProcName = "MSSBamRamSecApp.readBuffByClusterIdx() ";
        MSSBamSecAppBuff buff;
        ArrayList<MSSBamSecAppBuff> filteredList = new ArrayList<MSSBamSecAppBuff>();
        MSSBamSecAppBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("SAPP")) {
                filteredList.add((MSSBamSecAppBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamSecAppBuff[0]));
    }

    public MSSBamSecAppBuff readBuffByUJEEMountIdx(MSSBamAuthorization Authorization, long ClusterId,
            String JEEMountName) {
        final String S_ProcName = "MSSBamRamSecApp.readBuffByUJEEMountIdx() ";
        MSSBamSecAppBuff buff = readDerivedByUJEEMountIdx(Authorization, ClusterId, JEEMountName);
        if ((buff != null) && buff.getClassCode().equals("SAPP")) {
            return ((MSSBamSecAppBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateSecApp(MSSBamAuthorization Authorization, MSSBamSecAppBuff Buff) {
        MSSBamSecAppPKey pkey = schema.getFactorySecApp().newPKey();
        pkey.setRequiredSecAppId(Buff.getRequiredSecAppId());
        MSSBamSecAppBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecApp",
                    "Existing record not found", "SecApp", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecApp",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamSecAppByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey();
        existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        MSSBamSecAppByClusterIdxKey newKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey();
        newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        MSSBamSecAppByUJEEMountIdxKey existingKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey();
        existingKeyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId());
        existingKeyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName());

        MSSBamSecAppByUJEEMountIdxKey newKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey();
        newKeyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        newKeyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName());

        // Check unique indexes

        if (!existingKeyUJEEMountIdx.equals(newKeyUJEEMountIdx)) {
            if (dictByUJEEMountIdx.containsKey(newKeyUJEEMountIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateSecApp", "SecAppUJEEMountIdx", newKeyUJEEMountIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredClusterId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateSecApp", "Container", "SecAppCluster", "Cluster", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff> subdict;

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

        subdict = dictByClusterIdx.get(existingKeyClusterIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByClusterIdx.containsKey(newKeyClusterIdx)) {
            subdict = dictByClusterIdx.get(newKeyClusterIdx);
        } else {
            subdict = new TreeMap<MSSBamSecAppPKey, MSSBamSecAppBuff>();
            dictByClusterIdx.put(newKeyClusterIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUJEEMountIdx.remove(existingKeyUJEEMountIdx);
        dictByUJEEMountIdx.put(newKeyUJEEMountIdx, Buff);

    }

    public void deleteSecApp(MSSBamAuthorization Authorization, MSSBamSecAppBuff Buff) {
        final String S_ProcName = "MSSBamRamSecAppTable.deleteSecApp() ";
        MSSBamSecAppPKey pkey = schema.getFactorySecApp().newPKey();

        pkey.setRequiredSecAppId(schema.nextSecAppIdGen());
        MSSBamSecAppBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteSecApp",
                    "Existing record not found", "SecApp", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecApp",
                    pkey);
        }
        MSSBamSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        MSSBamSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey();
        keyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId());
        keyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName());

        // Validate reverse foreign keys

        if (schema.getTableSecForm().readDerivedBySecAppIdx(Authorization,
                existing.getRequiredSecAppId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecApp",
                    "Container", "SecFormApplication", "SecForm", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByClusterIdx.get(keyClusterIdx);
        subdict.remove(pkey);

        dictByUJEEMountIdx.remove(keyUJEEMountIdx);

    }

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

    public MSSBamCursor openSecAppCursorByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) {
        MSSBamCursor cursor;
        MSSBamSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

        if (dictByClusterIdx.containsKey(key)) {
            SortedMap<MSSBamSecAppPKey, MSSBamSecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key);
            cursor = new MSSBamRamSecAppCursor(Authorization, schema, subdictClusterIdx.values());
        } else {
            cursor = new MSSBamRamSecAppCursor(Authorization, schema, new ArrayList<MSSBamSecAppBuff>());
        }
        return (cursor);
    }

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

    public MSSBamSecAppBuff nextSecAppCursor(MSSBamCursor Cursor) {
        MSSBamRamSecAppCursor cursor = (MSSBamRamSecAppCursor) Cursor;
        MSSBamSecAppBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamSecAppBuff firstSecAppCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamSecAppBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecAppCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamSecAppBuff nthSecAppCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamSecAppBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecAppCursor(Cursor);
        }
        return (rec);
    }
}