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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAsteriskRamTenantTable in-memory RAM DbIO implementation
 *   for Tenant.
 */
public class CFAsteriskRamTenantTable implements ICFAsteriskTenantTable {
    private ICFAsteriskSchema schema;
    private Map<CFSecurityTenantPKey, CFSecurityTenantBuff> dictByPKey = new HashMap<CFSecurityTenantPKey, CFSecurityTenantBuff>();
    private Map<CFSecurityTenantByClusterIdxKey, Map<CFSecurityTenantPKey, CFSecurityTenantBuff>> dictByClusterIdx = new HashMap<CFSecurityTenantByClusterIdxKey, Map<CFSecurityTenantPKey, CFSecurityTenantBuff>>();
    private Map<CFSecurityTenantByUNameIdxKey, CFSecurityTenantBuff> dictByUNameIdx = new HashMap<CFSecurityTenantByUNameIdxKey, CFSecurityTenantBuff>();
    private Map<CFAsteriskRamTenantId32Gen, CFAsteriskRamTenantId32Gen> id32Generator = new HashMap<CFAsteriskRamTenantId32Gen, CFAsteriskRamTenantId32Gen>();
    private Map<CFAsteriskRamTenantId64Gen, CFAsteriskRamTenantId64Gen> id64Generator = new HashMap<CFAsteriskRamTenantId64Gen, CFAsteriskRamTenantId64Gen>();

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

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

        CFAsteriskRamTenantId32Gen 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) {
        CFAsteriskRamTenantId64Gen key = new CFAsteriskRamTenantId64Gen();
        key.setRequiredId(argId);
        key.setRequiredSliceId(argSliceId);

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

        long retNext = useGen.getNextId();

        return (retNext);
    }

    public int nextTSecGroupIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) {
        int retval = nextTSecGroupIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public int nextTSecGroupIdGen(CFSecurityAuthorization Authorization, long argId) {
        int retNext = generateNextId32(argId, (short) 113);
        return (retNext);
    }

    public long nextTSecGroupMemberIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) {
        long retval = nextTSecGroupMemberIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextTSecGroupMemberIdGen(CFSecurityAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 114);
        return (retNext);
    }

    public long nextTSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) {
        long retval = nextTSecGroupIncludeIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextTSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 115);
        return (retNext);
    }

    public long nextTldIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) {
        long retval = nextTldIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextTldIdGen(CFSecurityAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 211);
        return (retNext);
    }

    public long nextDomainIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) {
        long retval = nextDomainIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextDomainIdGen(CFSecurityAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 212);
        return (retNext);
    }

    public void createTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) {
        final String S_ProcName = "createTenant";
        CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey();
        pkey.setRequiredId(schema.nextTenantIdGen());
        Buff.setRequiredId(pkey.getRequiredId());
        CFSecurityTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        CFSecurityTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey();
        keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        keyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName());

        // Validate unique indexes

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

        if (dictByUNameIdx.containsKey(keyUNameIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName,
                    "TenantUNameIdx", keyUNameIdx);
        }

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        Map<CFSecurityTenantPKey, CFSecurityTenantBuff> subdictClusterIdx;
        if (dictByClusterIdx.containsKey(keyClusterIdx)) {
            subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx);
        } else {
            subdictClusterIdx = new HashMap<CFSecurityTenantPKey, CFSecurityTenantBuff>();
            dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx);
        }
        subdictClusterIdx.put(pkey, Buff);

        dictByUNameIdx.put(keyUNameIdx, Buff);

    }

    public CFSecurityTenantBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) {
        final String S_ProcName = "CFAsteriskRamTenant.readDerived";
        CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFSecurityTenantBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFSecurityTenantBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) {
        final String S_ProcName = "CFAsteriskRamTenant.readDerived";
        CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFSecurityTenantBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFSecurityTenantBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) {
        final String S_ProcName = "CFAsteriskRamTenant.readDerivedByClusterIdx";
        CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

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

    public CFSecurityTenantBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId,
            String TenantName) {
        final String S_ProcName = "CFAsteriskRamTenant.readDerivedByUNameIdx";
        CFSecurityTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey();
        key.setRequiredClusterId(ClusterId);
        key.setRequiredTenantName(TenantName);

        CFSecurityTenantBuff buff;
        if (dictByUNameIdx.containsKey(key)) {
            buff = dictByUNameIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFSecurityTenantBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAsteriskRamTenant.readDerivedByIdIdx() ";
        CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(Id);

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

    public CFSecurityTenantBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) {
        final String S_ProcName = "CFAsteriskRamTenant.readBuff";
        CFSecurityTenantBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("TENT"))) {
            buff = null;
        }
        return (buff);
    }

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

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

    public CFSecurityTenantBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAsteriskRamTenant.readBuffByIdIdx() ";
        CFSecurityTenantBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("TENT")) {
            return ((CFSecurityTenantBuff) buff);
        } else {
            return (null);
        }
    }

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

    public CFSecurityTenantBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId,
            String TenantName) {
        final String S_ProcName = "CFAsteriskRamTenant.readBuffByUNameIdx() ";
        CFSecurityTenantBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, TenantName);
        if ((buff != null) && buff.getClassCode().equals("TENT")) {
            return ((CFSecurityTenantBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) {
        CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFSecurityTenantBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTenant",
                    "Existing record not found", "Tenant", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTenant",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFSecurityTenantByClusterIdxKey existingKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        CFSecurityTenantByClusterIdxKey newKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

        CFSecurityTenantByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey();
        existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId());
        existingKeyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName());

        CFSecurityTenantByUNameIdxKey newKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey();
        newKeyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId());
        newKeyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName());

        // Check unique indexes

        if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) {
            if (dictByUNameIdx.containsKey(newKeyUNameIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateTenant", "TenantUNameIdx", newKeyUNameIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        // Update is valid

        Map<CFSecurityTenantPKey, CFSecurityTenantBuff> 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 HashMap<CFSecurityTenantPKey, CFSecurityTenantBuff>();
            dictByClusterIdx.put(newKeyClusterIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUNameIdx.remove(existingKeyUNameIdx);
        dictByUNameIdx.put(newKeyUNameIdx, Buff);

    }

    public void deleteTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) {
        final String S_ProcName = "CFAsteriskRamTenantTable.deleteTenant() ";
        CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFSecurityTenantBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTenant",
                    pkey);
        }
        CFSecurityTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

        CFSecurityTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey();
        keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId());
        keyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableTSecGroup().deleteTSecGroupByTenantIdx(Authorization, Buff.getRequiredId());
        schema.getTableDomainBase().deleteDomainBaseByTenantIdx(Authorization, Buff.getRequiredId());
        schema.getTableTld().deleteTldByTenantIdx(Authorization, Buff.getRequiredId());
        Map<CFSecurityTenantPKey, CFSecurityTenantBuff> subdict;

        dictByPKey.remove(pkey);

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

        dictByUNameIdx.remove(keyUNameIdx);

    }

    public void deleteTenantByIdIdx(CFSecurityAuthorization Authorization, long argId) {
        CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(argId);
        deleteTenantByIdIdx(Authorization, key);
    }

    public void deleteTenantByIdIdx(CFSecurityAuthorization Authorization, CFSecurityTenantPKey argKey) {
        CFSecurityTenantBuff cur;
        LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>();
        Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteTenant(Authorization, cur);
        }
    }

    public void deleteTenantByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) {
        CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey();
        key.setRequiredClusterId(argClusterId);
        deleteTenantByClusterIdx(Authorization, key);
    }

    public void deleteTenantByClusterIdx(CFSecurityAuthorization Authorization,
            CFSecurityTenantByClusterIdxKey argKey) {
        CFSecurityTenantBuff cur;
        LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>();
        Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteTenant(Authorization, cur);
        }
    }

    public void deleteTenantByUNameIdx(CFSecurityAuthorization Authorization, long argClusterId,
            String argTenantName) {
        CFSecurityTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey();
        key.setRequiredClusterId(argClusterId);
        key.setRequiredTenantName(argTenantName);
        deleteTenantByUNameIdx(Authorization, key);
    }

    public void deleteTenantByUNameIdx(CFSecurityAuthorization Authorization,
            CFSecurityTenantByUNameIdxKey argKey) {
        CFSecurityTenantBuff cur;
        LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>();
        Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteTenant(Authorization, cur);
        }
    }

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

    public CFSecurityCursor openTenantCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) {
        CFSecurityCursor cursor;
        CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

        if (dictByClusterIdx.containsKey(key)) {
            Map<CFSecurityTenantPKey, CFSecurityTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key);
            cursor = new CFAsteriskRamTenantCursor(Authorization, schema, subdictClusterIdx.values());
        } else {
            cursor = new CFAsteriskRamTenantCursor(Authorization, schema, new ArrayList<CFSecurityTenantBuff>());
        }
        return (cursor);
    }

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

    public CFSecurityTenantBuff nextTenantCursor(CFSecurityCursor Cursor) {
        CFAsteriskRamTenantCursor cursor = (CFAsteriskRamTenantCursor) Cursor;
        CFSecurityTenantBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFSecurityTenantBuff firstTenantCursor(CFSecurityCursor Cursor) {
        int targetRowIdx = 1;
        CFSecurityTenantBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextTenantCursor(Cursor);
        }
        return (rec);
    }

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

    public CFSecurityTenantBuff nthTenantCursor(CFSecurityCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFSecurityTenantBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextTenantCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}