net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.CFAccRamTenantTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.CFAccRamTenantTable.java

Source

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

/*
 *   MSS Code Factory Accounting Business Application 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.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.cfacc.v2_0.CFAccRam;

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.cfacc.v2_0.CFAcc.*;
import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*;

/*
 *   CFAccRamTenantTable in-memory RAM DbIO implementation
 *   for Tenant.
 */
public class CFAccRamTenantTable implements ICFAccTenantTable {
    private CFAccRamSchema schema;
    private Map<CFAccTenantPKey, CFAccTenantBuff> dictByPKey = new HashMap<CFAccTenantPKey, CFAccTenantBuff>();
    private SortedMap<CFAccTenantByClusterIdxKey, SortedMap<CFAccTenantPKey, CFAccTenantBuff>> dictByClusterIdx = new TreeMap<CFAccTenantByClusterIdxKey, SortedMap<CFAccTenantPKey, CFAccTenantBuff>>();
    private SortedMap<CFAccTenantByUNameIdxKey, CFAccTenantBuff> dictByUNameIdx = new TreeMap<CFAccTenantByUNameIdxKey, CFAccTenantBuff>();
    private Map<CFAccRamTenantId32Gen, CFAccRamTenantId32Gen> id32Generator = new HashMap<CFAccRamTenantId32Gen, CFAccRamTenantId32Gen>();
    private Map<CFAccRamTenantId64Gen, CFAccRamTenantId64Gen> id64Generator = new HashMap<CFAccRamTenantId64Gen, CFAccRamTenantId64Gen>();

    public CFAccRamTenantTable(CFAccRamSchema argSchema) {
        schema = argSchema;
    }

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

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

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

        long retNext = useGen.getNextId();

        return (retNext);
    }

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

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

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

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

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

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

    public long nextTLDIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextTLDIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

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

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

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

    public long nextAddressIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextAddressIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextAddressIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 500);
        return (retNext);
    }

    public long nextAttachmentIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextAttachmentIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextAttachmentIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 501);
        return (retNext);
    }

    public long nextContactIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextContactIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextContactIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 502);
        return (retNext);
    }

    public long nextContactListIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextContactListIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextContactListIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 503);
        return (retNext);
    }

    public long nextContactURLIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextContactURLIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextContactURLIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 504);
        return (retNext);
    }

    public long nextMemoIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextMemoIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextMemoIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 505);
        return (retNext);
    }

    public long nextPhoneIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextPhoneIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextPhoneIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 506);
        return (retNext);
    }

    public long nextTagIdGen(CFAccAuthorization Authorization, CFAccTenantPKey pkey) {
        long retval = nextTagIdGen(Authorization, pkey.getRequiredId());
        return (retval);
    }

    public long nextTagIdGen(CFAccAuthorization Authorization, long argId) {
        long retNext = generateNextId64(argId, (short) 507);
        return (retNext);
    }

    public void createTenant(CFAccAuthorization Authorization, CFAccTenantBuff Buff) {
        CFAccTenantPKey pkey = schema.getFactoryTenant().newPKey();
        pkey.setRequiredId(schema.nextTenantIdGen());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAccTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId());

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

        // Validate unique indexes

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

        if (dictByUNameIdx.containsKey(keyUNameIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTenant",
                    "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(),
                            "createTenant", "Container", "TenantCluster", "Cluster", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<CFAccTenantPKey, CFAccTenantBuff> subdictClusterIdx;
        if (dictByClusterIdx.containsKey(keyClusterIdx)) {
            subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx);
        } else {
            subdictClusterIdx = new TreeMap<CFAccTenantPKey, CFAccTenantBuff>();
            dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx);
        }
        subdictClusterIdx.put(pkey, Buff);

        dictByUNameIdx.put(keyUNameIdx, Buff);

    }

    public CFAccTenantBuff readDerived(CFAccAuthorization Authorization, CFAccTenantPKey PKey) {
        final String S_ProcName = "CFAccRamTenant.readDerived() ";
        CFAccTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccTenantBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccTenantBuff lockDerived(CFAccAuthorization Authorization, CFAccTenantPKey PKey) {
        final String S_ProcName = "CFAccRamTenant.readDerived() ";
        CFAccTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccTenantBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccTenantBuff[] readDerivedByClusterIdx(CFAccAuthorization Authorization, long ClusterId) {
        final String S_ProcName = "CFAccRamTenant.readDerivedByClusterIdx() ";
        CFAccTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

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

    public CFAccTenantBuff readDerivedByUNameIdx(CFAccAuthorization Authorization, long ClusterId,
            String TenantName) {
        final String S_ProcName = "CFAccRamTenant.readDerivedByUNameIdx() ";
        CFAccTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey();
        key.setRequiredClusterId(ClusterId);
        key.setRequiredTenantName(TenantName);

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

    public CFAccTenantBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAccRamTenant.readDerivedByIdIdx() ";
        CFAccTenantPKey key = schema.getFactoryTenant().newPKey();
        key.setRequiredId(Id);

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

    public CFAccTenantBuff readBuff(CFAccAuthorization Authorization, CFAccTenantPKey PKey) {
        final String S_ProcName = "CFAccRamTenant.readBuff() ";
        CFAccTenantBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("TENT"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccTenantBuff lockBuff(CFAccAuthorization Authorization, CFAccTenantPKey PKey) {
        final String S_ProcName = "CFAccRamTenant.readBuff() ";
        CFAccTenantBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("TENT"))) {
            buff = null;
        }
        return (buff);
    }

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

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

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

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

    public void updateTenant(CFAccAuthorization Authorization, CFAccTenantBuff Buff) {
        CFAccTenantPKey pkey = schema.getFactoryTenant().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccTenantBuff 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);
        CFAccTenantByClusterIdxKey existingKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey();
        existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId());

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

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

        CFAccTenantByUNameIdxKey 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

        SortedMap<CFAccTenantPKey, CFAccTenantBuff> 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 TreeMap<CFAccTenantPKey, CFAccTenantBuff>();
            dictByClusterIdx.put(newKeyClusterIdx, subdict);
        }
        subdict.put(pkey, Buff);

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

    }

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

        CFAccTenantByUNameIdxKey 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());
        schema.getTableContactList().deleteContactListByTenantIdx(Authorization, Buff.getRequiredId());
        schema.getTableTag().deleteTagByTenantIdx(Authorization, Buff.getRequiredId());
        schema.getTableAccountConfig().deleteAccountConfigByIdIdx(Authorization, Buff.getRequiredId());
        schema.getTableAccount().deleteAccountByTenantIdx(Authorization, Buff.getRequiredId());
        SortedMap<CFAccTenantPKey, CFAccTenantBuff> subdict;

        dictByPKey.remove(pkey);

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

        dictByUNameIdx.remove(keyUNameIdx);

    }

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

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

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

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

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

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

    public CFAccCursor openTenantCursorAll(CFAccAuthorization Authorization) {
        CFAccCursor cursor = new CFAccRamTenantCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public CFAccCursor openTenantCursorByClusterIdx(CFAccAuthorization Authorization, long ClusterId) {
        CFAccCursor cursor;
        CFAccTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey();
        key.setRequiredClusterId(ClusterId);

        if (dictByClusterIdx.containsKey(key)) {
            SortedMap<CFAccTenantPKey, CFAccTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key);
            cursor = new CFAccRamTenantCursor(Authorization, schema, subdictClusterIdx.values());
        } else {
            cursor = new CFAccRamTenantCursor(Authorization, schema, new ArrayList<CFAccTenantBuff>());
        }
        return (cursor);
    }

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

    public CFAccTenantBuff nextTenantCursor(CFAccCursor Cursor) {
        CFAccRamTenantCursor cursor = (CFAccRamTenantCursor) Cursor;
        CFAccTenantBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

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

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

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

    public void releasePreparedStatements() {
    }
}