net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstRam.CFAstRamSecDeviceTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstRam.CFAstRamSecDeviceTable.java

Source

// Description: Java7 in-memory RAM DbIO implementation for SecDevice.

/*
 *   Code Factory Asterisk 11 Configuration Model
 *
 *   Copyright (c) 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/>.
 *   
 *      This source code incorporates modified modules originally licensed
 *      under the Apache 2.0 license by MSS Code Factory including CFSecurity
 *      (net-sourceforge-msscodefactory-2.1-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.1-cfinternet.xml), and
 *      CFCrm 2.1 (net-sourceforge-msscodefactory-2.1-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 2.0
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 2.0 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstRam;

import java.sql.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAst.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_1.CFAstRam.*;

/*
 *   CFAstRamSecDeviceTable in-memory RAM DbIO implementation
 *   for SecDevice.
 */
public class CFAstRamSecDeviceTable implements ICFAstSecDeviceTable {
    private CFAstRamSchema schema;
    private Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> dictByPKey = new HashMap<CFAstSecDevicePKey, CFAstSecDeviceBuff>();
    private Map<CFAstSecDeviceByUserIdxKey, Map<CFAstSecDevicePKey, CFAstSecDeviceBuff>> dictByUserIdx = new HashMap<CFAstSecDeviceByUserIdxKey, Map<CFAstSecDevicePKey, CFAstSecDeviceBuff>>();

    public CFAstRamSecDeviceTable(CFAstRamSchema argSchema) {
        schema = argSchema;
    }

    public void createSecDevice(CFAstAuthorization Authorization, CFAstSecDeviceBuff Buff) {
        final String S_ProcName = "createSecDevice";
        CFAstSecDevicePKey pkey = schema.getFactorySecDevice().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        pkey.setRequiredDevName(Buff.getRequiredDevName());
        Buff.setRequiredSecUserId(pkey.getRequiredSecUserId());
        Buff.setRequiredDevName(pkey.getRequiredDevName());
        CFAstSecDeviceByUserIdxKey keyUserIdx = schema.getFactorySecDevice().newUserIdxKey();
        keyUserIdx.setRequiredSecUserId(Buff.getRequiredSecUserId());

        // Validate unique indexes

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

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableSecUser().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredSecUserId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName,
                            "Container", "SecDeviceSecUser", "SecUser", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdictUserIdx;
        if (dictByUserIdx.containsKey(keyUserIdx)) {
            subdictUserIdx = dictByUserIdx.get(keyUserIdx);
        } else {
            subdictUserIdx = new HashMap<CFAstSecDevicePKey, CFAstSecDeviceBuff>();
            dictByUserIdx.put(keyUserIdx, subdictUserIdx);
        }
        subdictUserIdx.put(pkey, Buff);

    }

    public CFAstSecDeviceBuff readDerived(CFAstAuthorization Authorization, CFAstSecDevicePKey PKey) {
        final String S_ProcName = "CFAstRamSecDevice.readDerived() ";
        CFAstSecDevicePKey key = schema.getFactorySecDevice().newPKey();
        key.setRequiredSecUserId(PKey.getRequiredSecUserId());
        key.setRequiredDevName(PKey.getRequiredDevName());
        CFAstSecDeviceBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstSecDeviceBuff lockDerived(CFAstAuthorization Authorization, CFAstSecDevicePKey PKey) {
        final String S_ProcName = "CFAstRamSecDevice.readDerived() ";
        CFAstSecDevicePKey key = schema.getFactorySecDevice().newPKey();
        key.setRequiredSecUserId(PKey.getRequiredSecUserId());
        key.setRequiredDevName(PKey.getRequiredDevName());
        CFAstSecDeviceBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstSecDeviceBuff[] readDerivedByUserIdx(CFAstAuthorization Authorization, UUID SecUserId) {
        final String S_ProcName = "CFAstRamSecDevice.readDerivedByUserIdx() ";
        CFAstSecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey();
        key.setRequiredSecUserId(SecUserId);

        CFAstSecDeviceBuff[] recArray;
        if (dictByUserIdx.containsKey(key)) {
            Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdictUserIdx = dictByUserIdx.get(key);
            recArray = new CFAstSecDeviceBuff[subdictUserIdx.size()];
            Iterator<CFAstSecDeviceBuff> iter = subdictUserIdx.values().iterator();
            int idx = 0;
            while (iter.hasNext()) {
                recArray[idx++] = iter.next();
            }
        } else {
            Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdictUserIdx = new HashMap<CFAstSecDevicePKey, CFAstSecDeviceBuff>();
            dictByUserIdx.put(key, subdictUserIdx);
            recArray = new CFAstSecDeviceBuff[0];
        }
        return (recArray);
    }

    public CFAstSecDeviceBuff readDerivedByIdIdx(CFAstAuthorization Authorization, UUID SecUserId, String DevName) {
        final String S_ProcName = "CFAstRamSecDevice.readDerivedByIdIdx() ";
        CFAstSecDevicePKey key = schema.getFactorySecDevice().newPKey();
        key.setRequiredSecUserId(SecUserId);
        key.setRequiredDevName(DevName);

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

    public CFAstSecDeviceBuff readBuff(CFAstAuthorization Authorization, CFAstSecDevicePKey PKey) {
        final String S_ProcName = "CFAstRamSecDevice.readBuff() ";
        CFAstSecDeviceBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstSecDeviceBuff lockBuff(CFAstAuthorization Authorization, CFAstSecDevicePKey PKey) {
        final String S_ProcName = "CFAstRamSecDevice.readBuff() ";
        CFAstSecDeviceBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstSecDeviceBuff readBuffByIdIdx(CFAstAuthorization Authorization, UUID SecUserId, String DevName) {
        final String S_ProcName = "CFAstRamSecDevice.readBuffByIdIdx() ";
        CFAstSecDeviceBuff buff = readDerivedByIdIdx(Authorization, SecUserId, DevName);
        if ((buff != null) && buff.getClassCode().equals("SDEV")) {
            return ((CFAstSecDeviceBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstSecDeviceBuff[] readBuffByUserIdx(CFAstAuthorization Authorization, UUID SecUserId) {
        final String S_ProcName = "CFAstRamSecDevice.readBuffByUserIdx() ";
        CFAstSecDeviceBuff buff;
        ArrayList<CFAstSecDeviceBuff> filteredList = new ArrayList<CFAstSecDeviceBuff>();
        CFAstSecDeviceBuff[] buffList = readDerivedByUserIdx(Authorization, SecUserId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("SDEV")) {
                filteredList.add((CFAstSecDeviceBuff) buff);
            }
        }
        return (filteredList.toArray(new CFAstSecDeviceBuff[0]));
    }

    public void updateSecDevice(CFAstAuthorization Authorization, CFAstSecDeviceBuff Buff) {
        CFAstSecDevicePKey pkey = schema.getFactorySecDevice().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        pkey.setRequiredDevName(Buff.getRequiredDevName());
        CFAstSecDeviceBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecDevice",
                    "Existing record not found", "SecDevice", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecDevice",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAstSecDeviceByUserIdxKey existingKeyUserIdx = schema.getFactorySecDevice().newUserIdxKey();
        existingKeyUserIdx.setRequiredSecUserId(existing.getRequiredSecUserId());

        CFAstSecDeviceByUserIdxKey newKeyUserIdx = schema.getFactorySecDevice().newUserIdxKey();
        newKeyUserIdx.setRequiredSecUserId(Buff.getRequiredSecUserId());

        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableSecUser().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredSecUserId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateSecDevice", "Container", "SecDeviceSecUser", "SecUser", null);
                }
            }
        }

        // Update is valid

        Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdict;

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

        subdict = dictByUserIdx.get(existingKeyUserIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByUserIdx.containsKey(newKeyUserIdx)) {
            subdict = dictByUserIdx.get(newKeyUserIdx);
        } else {
            subdict = new HashMap<CFAstSecDevicePKey, CFAstSecDeviceBuff>();
            dictByUserIdx.put(newKeyUserIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteSecDevice(CFAstAuthorization Authorization, CFAstSecDeviceBuff Buff) {
        final String S_ProcName = "CFAstRamSecDeviceTable.deleteSecDevice() ";
        CFAstSecDevicePKey pkey = schema.getFactorySecDevice().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        pkey.setRequiredDevName(Buff.getRequiredDevName());
        CFAstSecDeviceBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecDevice",
                    pkey);
        }
        CFAstSecDeviceByUserIdxKey keyUserIdx = schema.getFactorySecDevice().newUserIdxKey();
        keyUserIdx.setRequiredSecUserId(existing.getRequiredSecUserId());

        // Validate reverse foreign keys

        // Delete is valid

        Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByUserIdx.get(keyUserIdx);
        subdict.remove(pkey);

    }

    public void deleteSecDeviceByIdIdx(CFAstAuthorization Authorization, UUID argSecUserId, String argDevName) {
        CFAstSecDevicePKey key = schema.getFactorySecDevice().newPKey();
        key.setRequiredSecUserId(argSecUserId);
        key.setRequiredDevName(argDevName);
        deleteSecDeviceByIdIdx(Authorization, key);
    }

    public void deleteSecDeviceByIdIdx(CFAstAuthorization Authorization, CFAstSecDevicePKey argKey) {
        CFAstSecDeviceBuff cur;
        LinkedList<CFAstSecDeviceBuff> matchSet = new LinkedList<CFAstSecDeviceBuff>();
        Iterator<CFAstSecDeviceBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstSecDeviceBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteSecDevice(Authorization, cur);
        }
    }

    public void deleteSecDeviceByUserIdx(CFAstAuthorization Authorization, UUID argSecUserId) {
        CFAstSecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey();
        key.setRequiredSecUserId(argSecUserId);
        deleteSecDeviceByUserIdx(Authorization, key);
    }

    public void deleteSecDeviceByUserIdx(CFAstAuthorization Authorization, CFAstSecDeviceByUserIdxKey argKey) {
        CFAstSecDeviceBuff cur;
        LinkedList<CFAstSecDeviceBuff> matchSet = new LinkedList<CFAstSecDeviceBuff>();
        Iterator<CFAstSecDeviceBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstSecDeviceBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteSecDevice(Authorization, cur);
        }
    }

    public CFAstCursor openSecDeviceCursorAll(CFAstAuthorization Authorization) {
        CFAstCursor cursor = new CFAstRamSecDeviceCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public CFAstCursor openSecDeviceCursorByUserIdx(CFAstAuthorization Authorization, UUID SecUserId) {
        CFAstCursor cursor;
        CFAstSecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey();
        key.setRequiredSecUserId(SecUserId);

        if (dictByUserIdx.containsKey(key)) {
            Map<CFAstSecDevicePKey, CFAstSecDeviceBuff> subdictUserIdx = dictByUserIdx.get(key);
            cursor = new CFAstRamSecDeviceCursor(Authorization, schema, subdictUserIdx.values());
        } else {
            cursor = new CFAstRamSecDeviceCursor(Authorization, schema, new ArrayList<CFAstSecDeviceBuff>());
        }
        return (cursor);
    }

    public void closeSecDeviceCursor(CFAstCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public CFAstSecDeviceBuff nextSecDeviceCursor(CFAstCursor Cursor) {
        CFAstRamSecDeviceCursor cursor = (CFAstRamSecDeviceCursor) Cursor;
        CFAstSecDeviceBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public CFAstSecDeviceBuff prevSecDeviceCursor(CFAstCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        CFAstSecDeviceBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecDeviceCursor(Cursor);
        }
        return (rec);
    }

    public CFAstSecDeviceBuff firstSecDeviceCursor(CFAstCursor Cursor) {
        int targetRowIdx = 1;
        CFAstSecDeviceBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecDeviceCursor(Cursor);
        }
        return (rec);
    }

    public CFAstSecDeviceBuff lastSecDeviceCursor(CFAstCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecDeviceCursor");
    }

    public CFAstSecDeviceBuff nthSecDeviceCursor(CFAstCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAstSecDeviceBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextSecDeviceCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}