net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.CFAstRamClusterTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.CFAstRamClusterTable.java

Source

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

/*
 *   CF Asterisk 11 Configuration Model
 *
 *   Copyright (c) 2013-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.0-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and
 *      CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 1.11
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 1.11 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

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

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

/*
 *   CFAstRamClusterTable in-memory RAM DbIO implementation
 *   for Cluster.
 */
public class CFAstRamClusterTable implements ICFAstClusterTable {
    private CFAstRamSchema schema;
    private Map<CFAstClusterPKey, CFAstClusterBuff> dictByPKey = new HashMap<CFAstClusterPKey, CFAstClusterBuff>();
    private SortedMap<CFAstClusterByUDomainNameIdxKey, CFAstClusterBuff> dictByUDomainNameIdx = new TreeMap<CFAstClusterByUDomainNameIdxKey, CFAstClusterBuff>();
    private Map<CFAstRamClusterId32Gen, CFAstRamClusterId32Gen> id32Generator = new HashMap<CFAstRamClusterId32Gen, CFAstRamClusterId32Gen>();
    private Map<CFAstRamClusterId64Gen, CFAstRamClusterId64Gen> id64Generator = new HashMap<CFAstRamClusterId64Gen, CFAstRamClusterId64Gen>();

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

    protected int generateNextId32(long argId, short argSliceId) {
        CFAstRamClusterId32Gen key = new CFAstRamClusterId32Gen();
        key.setRequiredId(argId);
        key.setRequiredSliceId(argSliceId);

        CFAstRamClusterId32Gen useGen = id32Generator.get(key);
        if (useGen == null) {
            id32Generator.put(key, key);
            useGen = key;
        }

        int retNext = useGen.getNextId();

        return (retNext);
    }

    protected long generateNextId64(long argId, short argSliceId) {
        CFAstRamClusterId64Gen key = new CFAstRamClusterId64Gen();
        key.setRequiredId(argId);
        key.setRequiredSliceId(argSliceId);

        CFAstRamClusterId64Gen useGen = id64Generator.get(key);
        if (useGen == null) {
            id64Generator.put(key, key);
            useGen = key;
        }

        long retNext = useGen.getNextId();

        return (retNext);
    }

    public int nextSecAppIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        int retval = nextSecAppIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public int nextSecAppIdGen(CFAstAuthorization Authorization, long argId) {
        int retNext = generateNextId32(argId, (short) 103);
        return (retNext);
    }

    public int nextSecFormIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        int retval = nextSecFormIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public int nextSecFormIdGen(CFAstAuthorization Authorization, long argId) {
        int retNext = generateNextId32(argId, (short) 104);
        return (retNext);
    }

    public int nextSecGroupIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        int retval = nextSecGroupIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public int nextSecGroupIdGen(CFAstAuthorization Authorization, long argId) {
        int retNext = generateNextId32(argId, (short) 105);
        return (retNext);
    }

    public long nextSecGroupMemberIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextSecGroupMemberIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextSecGroupMemberIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 106);
        return (retNext);
    }

    public long nextSecGroupIncludeIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextSecGroupIncludeIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextSecGroupIncludeIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 107);
        return (retNext);
    }

    public long nextSecGroupFormIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextSecGroupFormIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextSecGroupFormIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 108);
        return (retNext);
    }

    public long nextServiceIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextServiceIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextServiceIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 111);
        return (retNext);
    }

    public long nextHostNodeIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextHostNodeIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextHostNodeIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 112);
        return (retNext);
    }

    public long nextConfigurationFileIdGen(CFAstAuthorization Authorization, CFAstClusterPKey pkey) {
        long retval = nextConfigurationFileIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextConfigurationFileIdGen(CFAstAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 3000);
        return (retNext);
    }

    public void createCluster(CFAstAuthorization Authorization, CFAstClusterBuff Buff) {
        CFAstClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(schema.nextClusterIdGen());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAstClusterByUDomainNameIdxKey keyUDomainNameIdx = schema.getFactoryCluster().newUDomainNameIdxKey();
        keyUDomainNameIdx.setRequiredFullDomainName(Buff.getRequiredFullDomainName());

        // Validate unique indexes

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

        if (dictByUDomainNameIdx.containsKey(keyUDomainNameIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createCluster",
                    "ClusterUDomainNameIdx", keyUDomainNameIdx);
        }

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByUDomainNameIdx.put(keyUDomainNameIdx, Buff);

    }

    public CFAstClusterBuff readDerived(CFAstAuthorization Authorization, CFAstClusterPKey PKey) {
        final String S_ProcName = "CFAstRamCluster.readDerived() ";
        CFAstClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAstClusterBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstClusterBuff lockDerived(CFAstAuthorization Authorization, CFAstClusterPKey PKey) {
        final String S_ProcName = "CFAstRamCluster.readDerived() ";
        CFAstClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAstClusterBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstClusterBuff readDerivedByUDomainNameIdx(CFAstAuthorization Authorization, String FullDomainName) {
        final String S_ProcName = "CFAstRamCluster.readDerivedByUDomainNameIdx() ";
        CFAstClusterByUDomainNameIdxKey key = schema.getFactoryCluster().newUDomainNameIdxKey();
        key.setRequiredFullDomainName(FullDomainName);

        CFAstClusterBuff buff;
        if (dictByUDomainNameIdx.containsKey(key)) {
            buff = dictByUDomainNameIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstClusterBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAstRamCluster.readDerivedByIdIdx() ";
        CFAstClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(Id);

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

    public CFAstClusterBuff readBuff(CFAstAuthorization Authorization, CFAstClusterPKey PKey) {
        final String S_ProcName = "CFAstRamCluster.readBuff() ";
        CFAstClusterBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstClusterBuff lockBuff(CFAstAuthorization Authorization, CFAstClusterPKey PKey) {
        final String S_ProcName = "CFAstRamCluster.readBuff() ";
        CFAstClusterBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstClusterBuff readBuffByIdIdx(CFAstAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAstRamCluster.readBuffByIdIdx() ";
        CFAstClusterBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("CLUS")) {
            return ((CFAstClusterBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstClusterBuff readBuffByUDomainNameIdx(CFAstAuthorization Authorization, String FullDomainName) {
        final String S_ProcName = "CFAstRamCluster.readBuffByUDomainNameIdx() ";
        CFAstClusterBuff buff = readDerivedByUDomainNameIdx(Authorization, FullDomainName);
        if ((buff != null) && buff.getClassCode().equals("CLUS")) {
            return ((CFAstClusterBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateCluster(CFAstAuthorization Authorization, CFAstClusterBuff Buff) {
        CFAstClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstClusterBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateCluster",
                    "Existing record not found", "Cluster", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateCluster",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAstClusterByUDomainNameIdxKey existingKeyUDomainNameIdx = schema.getFactoryCluster()
                .newUDomainNameIdxKey();
        existingKeyUDomainNameIdx.setRequiredFullDomainName(existing.getRequiredFullDomainName());

        CFAstClusterByUDomainNameIdxKey newKeyUDomainNameIdx = schema.getFactoryCluster().newUDomainNameIdxKey();
        newKeyUDomainNameIdx.setRequiredFullDomainName(Buff.getRequiredFullDomainName());

        // Check unique indexes

        if (!existingKeyUDomainNameIdx.equals(newKeyUDomainNameIdx)) {
            if (dictByUDomainNameIdx.containsKey(newKeyUDomainNameIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateCluster", "ClusterUDomainNameIdx", newKeyUDomainNameIdx);
            }
        }

        // Validate foreign keys

        // Update is valid

        SortedMap<CFAstClusterPKey, CFAstClusterBuff> subdict;

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

        dictByUDomainNameIdx.remove(existingKeyUDomainNameIdx);
        dictByUDomainNameIdx.put(newKeyUDomainNameIdx, Buff);

    }

    public void deleteCluster(CFAstAuthorization Authorization, CFAstClusterBuff Buff) {
        final String S_ProcName = "CFAstRamClusterTable.deleteCluster() ";
        CFAstClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstClusterBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteCluster",
                    pkey);
        }
        CFAstClusterByUDomainNameIdxKey keyUDomainNameIdx = schema.getFactoryCluster().newUDomainNameIdxKey();
        keyUDomainNameIdx.setRequiredFullDomainName(existing.getRequiredFullDomainName());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableHostNode().deleteHostNodeByClusterIdx(Authorization, Buff.getRequiredId());
        schema.getTableTenant().deleteTenantByClusterIdx(Authorization, Buff.getRequiredId());
        schema.getTableSecApp().deleteSecAppByClusterIdx(Authorization, Buff.getRequiredId());
        schema.getTableSecGroup().deleteSecGroupByClusterIdx(Authorization, Buff.getRequiredId());
        SortedMap<CFAstClusterPKey, CFAstClusterBuff> subdict;

        dictByPKey.remove(pkey);

        dictByUDomainNameIdx.remove(keyUDomainNameIdx);

    }

    public void deleteClusterByIdIdx(CFAstAuthorization Authorization, long argId) {
        CFAstClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(argId);
        deleteClusterByIdIdx(Authorization, key);
    }

    public void deleteClusterByIdIdx(CFAstAuthorization Authorization, CFAstClusterPKey argKey) {
        CFAstClusterBuff cur;
        LinkedList<CFAstClusterBuff> matchSet = new LinkedList<CFAstClusterBuff>();
        Iterator<CFAstClusterBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstClusterBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteCluster(Authorization, cur);
        }
    }

    public void deleteClusterByUDomainNameIdx(CFAstAuthorization Authorization, String argFullDomainName) {
        CFAstClusterByUDomainNameIdxKey key = schema.getFactoryCluster().newUDomainNameIdxKey();
        key.setRequiredFullDomainName(argFullDomainName);
        deleteClusterByUDomainNameIdx(Authorization, key);
    }

    public void deleteClusterByUDomainNameIdx(CFAstAuthorization Authorization,
            CFAstClusterByUDomainNameIdxKey argKey) {
        CFAstClusterBuff cur;
        LinkedList<CFAstClusterBuff> matchSet = new LinkedList<CFAstClusterBuff>();
        Iterator<CFAstClusterBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstClusterBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteCluster(Authorization, cur);
        }
    }

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

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

    public CFAstClusterBuff nextClusterCursor(CFAstCursor Cursor) {
        CFAstRamClusterCursor cursor = (CFAstRamClusterCursor) Cursor;
        CFAstClusterBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAstClusterBuff firstClusterCursor(CFAstCursor Cursor) {
        int targetRowIdx = 1;
        CFAstClusterBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextClusterCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAstClusterBuff nthClusterCursor(CFAstCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAstClusterBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextClusterCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}