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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAccRamClusterTable in-memory RAM DbIO implementation
 *   for Cluster.
 */
public class CFAccRamClusterTable implements ICFAccClusterTable {
    private CFAccRamSchema schema;
    private Map<CFAccClusterPKey, CFAccClusterBuff> dictByPKey = new HashMap<CFAccClusterPKey, CFAccClusterBuff>();
    private SortedMap<CFAccClusterByUDomainNameIdxKey, CFAccClusterBuff> dictByUDomainNameIdx = new TreeMap<CFAccClusterByUDomainNameIdxKey, CFAccClusterBuff>();
    private Map<CFAccRamClusterId32Gen, CFAccRamClusterId32Gen> id32Generator = new HashMap<CFAccRamClusterId32Gen, CFAccRamClusterId32Gen>();
    private Map<CFAccRamClusterId64Gen, CFAccRamClusterId64Gen> id64Generator = new HashMap<CFAccRamClusterId64Gen, CFAccRamClusterId64Gen>();

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

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

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

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

        long retNext = useGen.getNextId();

        return (retNext);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public void createCluster(CFAccAuthorization Authorization, CFAccClusterBuff Buff) {
        CFAccClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(schema.nextClusterIdGen());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAccClusterByUDomainNameIdxKey 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 CFAccClusterBuff readDerived(CFAccAuthorization Authorization, CFAccClusterPKey PKey) {
        final String S_ProcName = "CFAccRamCluster.readDerived() ";
        CFAccClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccClusterBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccClusterBuff lockDerived(CFAccAuthorization Authorization, CFAccClusterPKey PKey) {
        final String S_ProcName = "CFAccRamCluster.readDerived() ";
        CFAccClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccClusterBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccClusterBuff readDerivedByUDomainNameIdx(CFAccAuthorization Authorization, String FullDomainName) {
        final String S_ProcName = "CFAccRamCluster.readDerivedByUDomainNameIdx() ";
        CFAccClusterByUDomainNameIdxKey key = schema.getFactoryCluster().newUDomainNameIdxKey();
        key.setRequiredFullDomainName(FullDomainName);

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

    public CFAccClusterBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long Id) {
        final String S_ProcName = "CFAccRamCluster.readDerivedByIdIdx() ";
        CFAccClusterPKey key = schema.getFactoryCluster().newPKey();
        key.setRequiredId(Id);

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

    public CFAccClusterBuff readBuff(CFAccAuthorization Authorization, CFAccClusterPKey PKey) {
        final String S_ProcName = "CFAccRamCluster.readBuff() ";
        CFAccClusterBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccClusterBuff lockBuff(CFAccAuthorization Authorization, CFAccClusterPKey PKey) {
        final String S_ProcName = "CFAccRamCluster.readBuff() ";
        CFAccClusterBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) {
            buff = null;
        }
        return (buff);
    }

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

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

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

    public void updateCluster(CFAccAuthorization Authorization, CFAccClusterBuff Buff) {
        CFAccClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccClusterBuff 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);
        CFAccClusterByUDomainNameIdxKey existingKeyUDomainNameIdx = schema.getFactoryCluster()
                .newUDomainNameIdxKey();
        existingKeyUDomainNameIdx.setRequiredFullDomainName(existing.getRequiredFullDomainName());

        CFAccClusterByUDomainNameIdxKey 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<CFAccClusterPKey, CFAccClusterBuff> subdict;

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

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

    }

    public void deleteCluster(CFAccAuthorization Authorization, CFAccClusterBuff Buff) {
        final String S_ProcName = "CFAccRamClusterTable.deleteCluster() ";
        CFAccClusterPKey pkey = schema.getFactoryCluster().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccClusterBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteCluster",
                    pkey);
        }
        CFAccClusterByUDomainNameIdxKey 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<CFAccClusterPKey, CFAccClusterBuff> subdict;

        dictByPKey.remove(pkey);

        dictByUDomainNameIdx.remove(keyUDomainNameIdx);

    }

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

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

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

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

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

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

    public CFAccClusterBuff nextClusterCursor(CFAccCursor Cursor) {
        CFAccRamClusterCursor cursor = (CFAccRamClusterCursor) Cursor;
        CFAccClusterBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

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

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

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

    public void releasePreparedStatements() {
    }
}