net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam.CFAsteriskRamSecDeviceTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam.CFAsteriskRamSecDeviceTable.java

Source

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

/*
 *   Code Factory Asterisk 11 Configuration Model
 *
 *   Copyright (c) 2014-2015 Mark Sobkow
 *   
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *   
 *       http://www.apache.org/licenses/LICENSE-2.0
 *   
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *   
 */

package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam;

import java.sql.*;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*;
import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*;
import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*;
import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*;

/*
 *   CFAsteriskRamSecDeviceTable in-memory RAM DbIO implementation
 *   for SecDevice.
 */
public class CFAsteriskRamSecDeviceTable implements ICFAsteriskSecDeviceTable {
    private ICFAsteriskSchema schema;
    private Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> dictByPKey = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>();
    private Map<CFSecuritySecDeviceByUserIdxKey, Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>> dictByUserIdx = new HashMap<CFSecuritySecDeviceByUserIdxKey, Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>>();

    public CFAsteriskRamSecDeviceTable(ICFAsteriskSchema argSchema) {
        schema = argSchema;
    }

    public void createSecDevice(CFSecurityAuthorization Authorization, CFSecuritySecDeviceBuff Buff) {
        final String S_ProcName = "createSecDevice";
        CFSecuritySecDevicePKey pkey = schema.getFactorySecDevice().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        pkey.setRequiredDevName(Buff.getRequiredDevName());
        Buff.setRequiredSecUserId(pkey.getRequiredSecUserId());
        Buff.setRequiredDevName(pkey.getRequiredDevName());
        CFSecuritySecDeviceByUserIdxKey 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<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdictUserIdx;
        if (dictByUserIdx.containsKey(keyUserIdx)) {
            subdictUserIdx = dictByUserIdx.get(keyUserIdx);
        } else {
            subdictUserIdx = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>();
            dictByUserIdx.put(keyUserIdx, subdictUserIdx);
        }
        subdictUserIdx.put(pkey, Buff);

    }

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

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

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

    public CFSecuritySecDeviceBuff[] readDerivedByUserIdx(CFSecurityAuthorization Authorization, UUID SecUserId) {
        final String S_ProcName = "CFAsteriskRamSecDevice.readDerivedByUserIdx";
        CFSecuritySecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey();
        key.setRequiredSecUserId(SecUserId);

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

    public CFSecuritySecDeviceBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, UUID SecUserId,
            String DevName) {
        final String S_ProcName = "CFAsteriskRamSecDevice.readDerivedByIdIdx() ";
        CFSecuritySecDevicePKey key = schema.getFactorySecDevice().newPKey();
        key.setRequiredSecUserId(SecUserId);
        key.setRequiredDevName(DevName);

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

    public CFSecuritySecDeviceBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) {
        final String S_ProcName = "CFAsteriskRamSecDevice.readBuff";
        CFSecuritySecDeviceBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) {
            buff = null;
        }
        return (buff);
    }

    public CFSecuritySecDeviceBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) {
        final String S_ProcName = "lockBuff";
        CFSecuritySecDeviceBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) {
            buff = null;
        }
        return (buff);
    }

    public CFSecuritySecDeviceBuff[] readAllBuff(CFSecurityAuthorization Authorization) {
        final String S_ProcName = "CFAsteriskRamSecDevice.readAllBuff";
        CFSecuritySecDeviceBuff buff;
        ArrayList<CFSecuritySecDeviceBuff> filteredList = new ArrayList<CFSecuritySecDeviceBuff>();
        CFSecuritySecDeviceBuff[] 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 CFSecuritySecDeviceBuff[0]));
    }

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

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

    public void updateSecDevice(CFSecurityAuthorization Authorization, CFSecuritySecDeviceBuff Buff) {
        CFSecuritySecDevicePKey pkey = schema.getFactorySecDevice().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        pkey.setRequiredDevName(Buff.getRequiredDevName());
        CFSecuritySecDeviceBuff 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);
        CFSecuritySecDeviceByUserIdxKey existingKeyUserIdx = schema.getFactorySecDevice().newUserIdxKey();
        existingKeyUserIdx.setRequiredSecUserId(existing.getRequiredSecUserId());

        CFSecuritySecDeviceByUserIdxKey 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<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> 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<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>();
            dictByUserIdx.put(newKeyUserIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

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

        // Validate reverse foreign keys

        // Delete is valid

        Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdict;

        dictByPKey.remove(pkey);

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

    }

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

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

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

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

    public CFSecurityCursor openSecDeviceCursorAll(CFSecurityAuthorization Authorization) {
        CFSecurityCursor cursor = new CFAsteriskRamSecDeviceCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public CFSecurityCursor openSecDeviceCursorByUserIdx(CFSecurityAuthorization Authorization, UUID SecUserId) {
        CFSecurityCursor cursor;
        CFSecuritySecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey();
        key.setRequiredSecUserId(SecUserId);

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

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

    public CFSecuritySecDeviceBuff nextSecDeviceCursor(CFSecurityCursor Cursor) {
        CFAsteriskRamSecDeviceCursor cursor = (CFAsteriskRamSecDeviceCursor) Cursor;
        CFSecuritySecDeviceBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

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

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

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

    public void releasePreparedStatements() {
    }
}