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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAstRamSecUserTable in-memory RAM DbIO implementation
 *   for SecUser.
 */
public class CFAstRamSecUserTable implements ICFAstSecUserTable {
    private CFAstRamSchema schema;
    private Map<CFAstSecUserPKey, CFAstSecUserBuff> dictByPKey = new HashMap<CFAstSecUserPKey, CFAstSecUserBuff>();
    private Map<CFAstSecUserByUEMailIdxKey, CFAstSecUserBuff> dictByUEMailIdx = new HashMap<CFAstSecUserByUEMailIdxKey, CFAstSecUserBuff>();
    private Map<CFAstSecUserByDefDevIdxKey, CFAstSecUserBuff> dictByDefDevIdx = new HashMap<CFAstSecUserByDefDevIdxKey, CFAstSecUserBuff>();

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

    public void createSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) {
        final String S_ProcName = "createSecUser";
        CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey();
        pkey.setRequiredSecUserId(schema.nextSecUserIdGen());
        Buff.setRequiredSecUserId(pkey.getRequiredSecUserId());
        CFAstSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        keyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress());

        CFAstSecUserByDefDevIdxKey keyDefDevIdx = schema.getFactorySecUser().newDefDevIdxKey();
        keyDefDevIdx.setOptionalDefaultDevSecUserId(Buff.getOptionalDefaultDevSecUserId());
        keyDefDevIdx.setOptionalDefaultDevName(Buff.getOptionalDefaultDevName());

        // Validate unique indexes

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

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

        if (dictByDefDevIdx.containsKey(keyDefDevIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName,
                    "SecUserDefDevIdx", keyDefDevIdx);
        }

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByUEMailIdx.put(keyUEMailIdx, Buff);

        dictByDefDevIdx.put(keyDefDevIdx, Buff);

    }

    public CFAstSecUserBuff readDerived(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) {
        final String S_ProcName = "CFAstRamSecUser.readDerived() ";
        CFAstSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(PKey.getRequiredSecUserId());
        CFAstSecUserBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstSecUserBuff lockDerived(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) {
        final String S_ProcName = "CFAstRamSecUser.readDerived() ";
        CFAstSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(PKey.getRequiredSecUserId());
        CFAstSecUserBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstSecUserBuff readDerivedByUEMailIdx(CFAstAuthorization Authorization, String EMailAddress) {
        final String S_ProcName = "CFAstRamSecUser.readDerivedByUEMailIdx() ";
        CFAstSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey();
        key.setRequiredEMailAddress(EMailAddress);

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

    public CFAstSecUserBuff readDerivedByDefDevIdx(CFAstAuthorization Authorization, UUID DefaultDevSecUserId,
            String DefaultDevName) {
        final String S_ProcName = "CFAstRamSecUser.readDerivedByDefDevIdx() ";
        CFAstSecUserByDefDevIdxKey key = schema.getFactorySecUser().newDefDevIdxKey();
        key.setOptionalDefaultDevSecUserId(DefaultDevSecUserId);
        key.setOptionalDefaultDevName(DefaultDevName);

        CFAstSecUserBuff buff;
        if (dictByDefDevIdx.containsKey(key)) {
            buff = dictByDefDevIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstSecUserBuff readDerivedByIdIdx(CFAstAuthorization Authorization, UUID SecUserId) {
        final String S_ProcName = "CFAstRamSecUser.readDerivedByIdIdx() ";
        CFAstSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(SecUserId);

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

    public CFAstSecUserBuff readBuff(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) {
        final String S_ProcName = "CFAstRamSecUser.readBuff() ";
        CFAstSecUserBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstSecUserBuff lockBuff(CFAstAuthorization Authorization, CFAstSecUserPKey PKey) {
        final String S_ProcName = "CFAstRamSecUser.readBuff() ";
        CFAstSecUserBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("SUSR"))) {
            buff = null;
        }
        return (buff);
    }

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

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

    public CFAstSecUserBuff readBuffByUEMailIdx(CFAstAuthorization Authorization, String EMailAddress) {
        final String S_ProcName = "CFAstRamSecUser.readBuffByUEMailIdx() ";
        CFAstSecUserBuff buff = readDerivedByUEMailIdx(Authorization, EMailAddress);
        if ((buff != null) && buff.getClassCode().equals("SUSR")) {
            return ((CFAstSecUserBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstSecUserBuff readBuffByDefDevIdx(CFAstAuthorization Authorization, UUID DefaultDevSecUserId,
            String DefaultDevName) {
        final String S_ProcName = "CFAstRamSecUser.readBuffByDefDevIdx() ";
        CFAstSecUserBuff buff = readDerivedByDefDevIdx(Authorization, DefaultDevSecUserId, DefaultDevName);
        if ((buff != null) && buff.getClassCode().equals("SUSR")) {
            return ((CFAstSecUserBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) {
        CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        CFAstSecUserBuff 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);
        CFAstSecUserByUEMailIdxKey existingKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        existingKeyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress());

        CFAstSecUserByUEMailIdxKey newKeyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        newKeyUEMailIdx.setRequiredEMailAddress(Buff.getRequiredEMailAddress());

        CFAstSecUserByDefDevIdxKey existingKeyDefDevIdx = schema.getFactorySecUser().newDefDevIdxKey();
        existingKeyDefDevIdx.setOptionalDefaultDevSecUserId(existing.getOptionalDefaultDevSecUserId());
        existingKeyDefDevIdx.setOptionalDefaultDevName(existing.getOptionalDefaultDevName());

        CFAstSecUserByDefDevIdxKey newKeyDefDevIdx = schema.getFactorySecUser().newDefDevIdxKey();
        newKeyDefDevIdx.setOptionalDefaultDevSecUserId(Buff.getOptionalDefaultDevSecUserId());
        newKeyDefDevIdx.setOptionalDefaultDevName(Buff.getOptionalDefaultDevName());

        // Check unique indexes

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

        if (!existingKeyDefDevIdx.equals(newKeyDefDevIdx)) {
            if (dictByDefDevIdx.containsKey(newKeyDefDevIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateSecUser", "SecUserDefDevIdx", newKeyDefDevIdx);
            }
        }

        // Validate foreign keys

        // Update is valid

        Map<CFAstSecUserPKey, CFAstSecUserBuff> subdict;

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

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

        dictByDefDevIdx.remove(existingKeyDefDevIdx);
        dictByDefDevIdx.put(newKeyDefDevIdx, Buff);

    }

    public void deleteSecUser(CFAstAuthorization Authorization, CFAstSecUserBuff Buff) {
        final String S_ProcName = "CFAstRamSecUserTable.deleteSecUser() ";
        CFAstSecUserPKey pkey = schema.getFactorySecUser().newPKey();
        pkey.setRequiredSecUserId(Buff.getRequiredSecUserId());
        CFAstSecUserBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecUser",
                    pkey);
        }
        CFAstSecUserByUEMailIdxKey keyUEMailIdx = schema.getFactorySecUser().newUEMailIdxKey();
        keyUEMailIdx.setRequiredEMailAddress(existing.getRequiredEMailAddress());

        CFAstSecUserByDefDevIdxKey keyDefDevIdx = schema.getFactorySecUser().newDefDevIdxKey();
        keyDefDevIdx.setOptionalDefaultDevSecUserId(existing.getOptionalDefaultDevSecUserId());
        keyDefDevIdx.setOptionalDefaultDevName(existing.getOptionalDefaultDevName());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableSecDevice().deleteSecDeviceByUserIdx(Authorization, Buff.getRequiredSecUserId());
        schema.getTableSecSession().deleteSecSessionBySecUserIdx(Authorization, Buff.getRequiredSecUserId());
        schema.getTableSecSession().deleteSecSessionBySecProxyIdx(Authorization, Buff.getRequiredSecUserId());
        schema.getTableSecGroupMember().deleteSecGroupMemberByUserIdx(Authorization, Buff.getRequiredSecUserId());
        schema.getTableTSecGroupMember().deleteTSecGroupMemberByUserIdx(Authorization, Buff.getRequiredSecUserId());
        Map<CFAstSecUserPKey, CFAstSecUserBuff> subdict;

        dictByPKey.remove(pkey);

        dictByUEMailIdx.remove(keyUEMailIdx);

        dictByDefDevIdx.remove(keyDefDevIdx);

    }

    public void deleteSecUserByIdIdx(CFAstAuthorization Authorization, UUID argSecUserId) {
        CFAstSecUserPKey key = schema.getFactorySecUser().newPKey();
        key.setRequiredSecUserId(argSecUserId);
        deleteSecUserByIdIdx(Authorization, key);
    }

    public void deleteSecUserByIdIdx(CFAstAuthorization Authorization, CFAstSecUserPKey argKey) {
        CFAstSecUserBuff cur;
        LinkedList<CFAstSecUserBuff> matchSet = new LinkedList<CFAstSecUserBuff>();
        Iterator<CFAstSecUserBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstSecUserBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteSecUser(Authorization, cur);
        }
    }

    public void deleteSecUserByUEMailIdx(CFAstAuthorization Authorization, String argEMailAddress) {
        CFAstSecUserByUEMailIdxKey key = schema.getFactorySecUser().newUEMailIdxKey();
        key.setRequiredEMailAddress(argEMailAddress);
        deleteSecUserByUEMailIdx(Authorization, key);
    }

    public void deleteSecUserByUEMailIdx(CFAstAuthorization Authorization, CFAstSecUserByUEMailIdxKey argKey) {
        CFAstSecUserBuff cur;
        LinkedList<CFAstSecUserBuff> matchSet = new LinkedList<CFAstSecUserBuff>();
        Iterator<CFAstSecUserBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstSecUserBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteSecUser(Authorization, cur);
        }
    }

    public void deleteSecUserByDefDevIdx(CFAstAuthorization Authorization, UUID argDefaultDevSecUserId,
            String argDefaultDevName) {
        CFAstSecUserByDefDevIdxKey key = schema.getFactorySecUser().newDefDevIdxKey();
        key.setOptionalDefaultDevSecUserId(argDefaultDevSecUserId);
        key.setOptionalDefaultDevName(argDefaultDevName);
        deleteSecUserByDefDevIdx(Authorization, key);
    }

    public void deleteSecUserByDefDevIdx(CFAstAuthorization Authorization, CFAstSecUserByDefDevIdxKey argKey) {
        CFAstSecUserBuff cur;
        LinkedList<CFAstSecUserBuff> matchSet = new LinkedList<CFAstSecUserBuff>();
        Iterator<CFAstSecUserBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstSecUserBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteSecUser(Authorization, cur);
        }
    }

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

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

    public CFAstSecUserBuff nextSecUserCursor(CFAstCursor Cursor) {
        CFAstRamSecUserCursor cursor = (CFAstRamSecUserCursor) Cursor;
        CFAstSecUserBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

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

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

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

    public void releasePreparedStatements() {
    }
}