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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamSecUserTable in-memory RAM DbIO implementation
 *   for SecUser.
 */
public class MSSBamRamSecUserTable implements IMSSBamSecUserTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamSecUserPKey, MSSBamSecUserBuff> dictByPKey = new HashMap<MSSBamSecUserPKey, MSSBamSecUserBuff>();
    private SortedMap<MSSBamSecUserByClusterIdxKey, SortedMap<MSSBamSecUserPKey, MSSBamSecUserBuff>> dictByClusterIdx = new TreeMap<MSSBamSecUserByClusterIdxKey, SortedMap<MSSBamSecUserPKey, MSSBamSecUserBuff>>();
    private SortedMap<MSSBamSecUserByUEMailIdxKey, MSSBamSecUserBuff> dictByUEMailIdx = new TreeMap<MSSBamSecUserByUEMailIdxKey, MSSBamSecUserBuff>();

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

    public void createSecUser(MSSBamAuthorization Authorization, MSSBamSecUserBuff Buff) {
        MSSBamSecUserPKey pkey = schema.getFactorySecUser().newPKey();

        pkey.setRequiredSecUserId(schema.nextSecUserIdGen());
        Buff.setRequiredSecUserId(pkey.getRequiredSecUserId());

        MSSBamSecUserByClusterIdxKey keyClusterIdx = schema.getFactorySecUser().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        MSSBamSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        keyUEMailIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        keyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress());

        // Validate unique indexes

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

        if (dictByUEMailIdx.containsKey(keyUEMailIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecUser",
                    "SecUserUEMailAddrIdx", keyUEMailIdx);
        }

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

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

        dictByUEMailIdx.put(keyUEMailIdx, Buff);

    }

    public MSSBamSecUserBuff readDerived(MSSBamAuthorization Authorization, MSSBamSecUserPKey PKey) {
        final String S_ProcName = "MSSBamRamSecUser.readDerived() ";
        MSSBamSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(PKey.getRequiredSecUserId());
        MSSBamSecUserBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecUserBuff[] readDerivedByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) {
        final String S_ProcName = "MSSBamRamSecUser.readDerivedByClusterIdx() ";
        MSSBamSecUserByClusterIdxKey key = schema.getFactorySecUser().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

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

    public MSSBamSecUserBuff readDerivedByUEMailIdx(MSSBamAuthorization Authorization, long ClusterId,
            String EMailAddress) {
        final String S_ProcName = "MSSBamRamSecUser.readDerivedByUEMailIdx() ";
        MSSBamSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey();
        key.setRequiredClusterId(ClusterId);
        key.setRequiredEMailAddress(EMailAddress);

        MSSBamSecUserBuff buff;
        if (dictByUEMailIdx.containsKey(key)) {
            buff = dictByUEMailIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public MSSBamSecUserBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long SecUserId) {
        final String S_ProcName = "MSSBamRamSecUser.readDerivedByIdIdx() ";
        MSSBamSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(SecUserId);

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

    public MSSBamSecUserBuff readBuff(MSSBamAuthorization Authorization, MSSBamSecUserPKey PKey) {
        final String S_ProcName = "MSSBamRamSecUser.readBuff() ";
        MSSBamSecUserBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamSecUserBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long SecUserId) {
        final String S_ProcName = "MSSBamRamSecUser.readBuffByIdIdx() ";
        MSSBamSecUserBuff buff = readDerivedByIdIdx(Authorization, SecUserId);
        if ((buff != null) && buff.getClassCode().equals("SUSR")) {
            return ((MSSBamSecUserBuff) buff);
        } else {
            return (null);
        }
    }

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

    public MSSBamSecUserBuff readBuffByUEMailIdx(MSSBamAuthorization Authorization, long ClusterId,
            String EMailAddress) {
        final String S_ProcName = "MSSBamRamSecUser.readBuffByUEMailIdx() ";
        MSSBamSecUserBuff buff = readDerivedByUEMailIdx(Authorization, ClusterId, EMailAddress);
        if ((buff != null) && buff.getClassCode().equals("SUSR")) {
            return ((MSSBamSecUserBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateSecUser(MSSBamAuthorization Authorization, MSSBamSecUserBuff Buff) {
        MSSBamSecUserPKey pkey = schema.getFactorySecUser().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        MSSBamSecUserBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecUser",
                    "Existing record not found", "SecUser", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecUser",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamSecUserByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecUser().newClusterIdxKey();
        existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        MSSBamSecUserByClusterIdxKey newKeyClusterIdx = schema.getFactorySecUser().newClusterIdxKey();
        newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        MSSBamSecUserByUEMailIdxKey existingKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        existingKeyUEMailIdx.setRequiredClusterId(existing.getRequiredClusterId());
        existingKeyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress());

        MSSBamSecUserByUEMailIdxKey newKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        newKeyUEMailIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        newKeyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress());

        // Check unique indexes

        if (!existingKeyUEMailIdx.equals(newKeyUEMailIdx)) {
            if (dictByUEMailIdx.containsKey(newKeyUEMailIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateSecUser", "SecUserUEMailAddrIdx", newKeyUEMailIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        // Update is valid

        SortedMap<MSSBamSecUserPKey, MSSBamSecUserBuff> 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<MSSBamSecUserPKey, MSSBamSecUserBuff>();
            dictByClusterIdx.put(newKeyClusterIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUEMailIdx.remove(existingKeyUEMailIdx);
        dictByUEMailIdx.put(newKeyUEMailIdx, Buff);

    }

    public void deleteSecUser(MSSBamAuthorization Authorization, MSSBamSecUserBuff Buff) {
        final String S_ProcName = "MSSBamRamSecUserTable.deleteSecUser() ";
        MSSBamSecUserPKey pkey = schema.getFactorySecUser().newPKey();

        pkey.setRequiredSecUserId(schema.nextSecUserIdGen());
        MSSBamSecUserBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteSecUser",
                    "Existing record not found", "SecUser", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecUser",
                    pkey);
        }
        MSSBamSecUserByClusterIdxKey keyClusterIdx = schema.getFactorySecUser().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        MSSBamSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        keyUEMailIdx.setRequiredClusterId(existing.getRequiredClusterId());
        keyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress());

        // Validate reverse foreign keys

        if (schema.getTableSecGroupMember().readDerivedByUserIdx(Authorization,
                existing.getRequiredSecUserId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecUser",
                    "Lookup", "SecGroupMemberUser", "SecGroupMember", pkey);
        }

        if (schema.getTableSecSession().readDerivedBySecUserIdx(Authorization,
                existing.getRequiredSecUserId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecUser",
                    "Container", "SecSessionUser", "SecSession", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamSecUserPKey, MSSBamSecUserBuff> subdict;

        dictByPKey.remove(pkey);

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

        dictByUEMailIdx.remove(keyUEMailIdx);

    }

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

    public MSSBamCursor openSecUserCursorByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) {
        MSSBamCursor cursor;
        MSSBamSecUserByClusterIdxKey key = schema.getFactorySecUser().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

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

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

    public MSSBamSecUserBuff nextSecUserCursor(MSSBamCursor Cursor) {
        MSSBamRamSecUserCursor cursor = (MSSBamRamSecUserCursor) Cursor;
        MSSBamSecUserBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamSecUserBuff firstSecUserCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamSecUserBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecUserCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamSecUserBuff nthSecUserCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamSecUserBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecUserCursor(Cursor);
        }
        return (rec);
    }
}