net.sourceforge.msscodefactory.cfgcash.v2_0.CFGCashRam.CFGCashRamURLProtocolTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfgcash.v2_0.CFGCashRam.CFGCashRamURLProtocolTable.java

Source

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

/*
 *   MSS Code Factory CFGCash Java Swing Interface 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.cfgcash.v2_0.CFGCashRam;

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.cfgcash.v2_0.CFGCash.*;
import net.sourceforge.msscodefactory.cfgcash.v2_0.CFGCashRam.*;

/*
 *   CFGCashRamURLProtocolTable in-memory RAM DbIO implementation
 *   for URLProtocol.
 */
public class CFGCashRamURLProtocolTable implements ICFGCashURLProtocolTable {
    private CFGCashRamSchema schema;
    private Map<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff> dictByPKey = new HashMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff>();
    private SortedMap<CFGCashURLProtocolByUNameIdxKey, CFGCashURLProtocolBuff> dictByUNameIdx = new TreeMap<CFGCashURLProtocolByUNameIdxKey, CFGCashURLProtocolBuff>();
    private SortedMap<CFGCashURLProtocolByIsSecureIdxKey, SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff>> dictByIsSecureIdx = new TreeMap<CFGCashURLProtocolByIsSecureIdxKey, SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff>>();

    public CFGCashRamURLProtocolTable(CFGCashRamSchema argSchema) {
        schema = argSchema;
    }

    public void createURLProtocol(CFGCashAuthorization Authorization, CFGCashURLProtocolBuff Buff) {
        CFGCashURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey();
        pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId());
        Buff.setRequiredURLProtocolId(pkey.getRequiredURLProtocolId());
        CFGCashURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey();
        keyUNameIdx.setRequiredName(Buff.getRequiredName());

        CFGCashURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        keyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure());

        // Validate unique indexes

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

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

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByUNameIdx.put(keyUNameIdx, Buff);

        SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff> subdictIsSecureIdx;
        if (dictByIsSecureIdx.containsKey(keyIsSecureIdx)) {
            subdictIsSecureIdx = dictByIsSecureIdx.get(keyIsSecureIdx);
        } else {
            subdictIsSecureIdx = new TreeMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff>();
            dictByIsSecureIdx.put(keyIsSecureIdx, subdictIsSecureIdx);
        }
        subdictIsSecureIdx.put(pkey, Buff);

    }

    public CFGCashURLProtocolBuff readDerived(CFGCashAuthorization Authorization, CFGCashURLProtocolPKey PKey) {
        final String S_ProcName = "CFGCashRamURLProtocol.readDerived() ";
        CFGCashURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey();
        key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId());
        CFGCashURLProtocolBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFGCashURLProtocolBuff lockDerived(CFGCashAuthorization Authorization, CFGCashURLProtocolPKey PKey) {
        final String S_ProcName = "CFGCashRamURLProtocol.readDerived() ";
        CFGCashURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey();
        key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId());
        CFGCashURLProtocolBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFGCashURLProtocolBuff readDerivedByUNameIdx(CFGCashAuthorization Authorization, String Name) {
        final String S_ProcName = "CFGCashRamURLProtocol.readDerivedByUNameIdx() ";
        CFGCashURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey();
        key.setRequiredName(Name);

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

    public CFGCashURLProtocolBuff[] readDerivedByIsSecureIdx(CFGCashAuthorization Authorization, boolean IsSecure) {
        final String S_ProcName = "CFGCashRamURLProtocol.readDerivedByIsSecureIdx() ";
        CFGCashURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        key.setRequiredIsSecure(IsSecure);

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

    public CFGCashURLProtocolBuff readDerivedByIdIdx(CFGCashAuthorization Authorization, short URLProtocolId) {
        final String S_ProcName = "CFGCashRamURLProtocol.readDerivedByIdIdx() ";
        CFGCashURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey();
        key.setRequiredURLProtocolId(URLProtocolId);

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

    public CFGCashURLProtocolBuff readBuff(CFGCashAuthorization Authorization, CFGCashURLProtocolPKey PKey) {
        final String S_ProcName = "CFGCashRamURLProtocol.readBuff() ";
        CFGCashURLProtocolBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) {
            buff = null;
        }
        return (buff);
    }

    public CFGCashURLProtocolBuff lockBuff(CFGCashAuthorization Authorization, CFGCashURLProtocolPKey PKey) {
        final String S_ProcName = "CFGCashRamURLProtocol.readBuff() ";
        CFGCashURLProtocolBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFGCashURLProtocolBuff readBuffByIdIdx(CFGCashAuthorization Authorization, short URLProtocolId) {
        final String S_ProcName = "CFGCashRamURLProtocol.readBuffByIdIdx() ";
        CFGCashURLProtocolBuff buff = readDerivedByIdIdx(Authorization, URLProtocolId);
        if ((buff != null) && buff.getClassCode().equals("UPRT")) {
            return ((CFGCashURLProtocolBuff) buff);
        } else {
            return (null);
        }
    }

    public CFGCashURLProtocolBuff readBuffByUNameIdx(CFGCashAuthorization Authorization, String Name) {
        final String S_ProcName = "CFGCashRamURLProtocol.readBuffByUNameIdx() ";
        CFGCashURLProtocolBuff buff = readDerivedByUNameIdx(Authorization, Name);
        if ((buff != null) && buff.getClassCode().equals("UPRT")) {
            return ((CFGCashURLProtocolBuff) buff);
        } else {
            return (null);
        }
    }

    public CFGCashURLProtocolBuff[] readBuffByIsSecureIdx(CFGCashAuthorization Authorization, boolean IsSecure) {
        final String S_ProcName = "CFGCashRamURLProtocol.readBuffByIsSecureIdx() ";
        CFGCashURLProtocolBuff buff;
        ArrayList<CFGCashURLProtocolBuff> filteredList = new ArrayList<CFGCashURLProtocolBuff>();
        CFGCashURLProtocolBuff[] buffList = readDerivedByIsSecureIdx(Authorization, IsSecure);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("UPRT")) {
                filteredList.add((CFGCashURLProtocolBuff) buff);
            }
        }
        return (filteredList.toArray(new CFGCashURLProtocolBuff[0]));
    }

    public void updateURLProtocol(CFGCashAuthorization Authorization, CFGCashURLProtocolBuff Buff) {
        CFGCashURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey();
        pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId());
        CFGCashURLProtocolBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateURLProtocol",
                    "Existing record not found", "URLProtocol", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateURLProtocol",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFGCashURLProtocolByUNameIdxKey existingKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey();
        existingKeyUNameIdx.setRequiredName(existing.getRequiredName());

        CFGCashURLProtocolByUNameIdxKey newKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey();
        newKeyUNameIdx.setRequiredName(Buff.getRequiredName());

        CFGCashURLProtocolByIsSecureIdxKey existingKeyIsSecureIdx = schema.getFactoryURLProtocol()
                .newIsSecureIdxKey();
        existingKeyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure());

        CFGCashURLProtocolByIsSecureIdxKey newKeyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        newKeyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure());

        // Check unique indexes

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

        // Validate foreign keys

        // Update is valid

        SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff> subdict;

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

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

        subdict = dictByIsSecureIdx.get(existingKeyIsSecureIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByIsSecureIdx.containsKey(newKeyIsSecureIdx)) {
            subdict = dictByIsSecureIdx.get(newKeyIsSecureIdx);
        } else {
            subdict = new TreeMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff>();
            dictByIsSecureIdx.put(newKeyIsSecureIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteURLProtocol(CFGCashAuthorization Authorization, CFGCashURLProtocolBuff Buff) {
        final String S_ProcName = "CFGCashRamURLProtocolTable.deleteURLProtocol() ";
        CFGCashURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey();
        pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId());
        CFGCashURLProtocolBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteURLProtocol",
                    pkey);
        }
        CFGCashURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey();
        keyUNameIdx.setRequiredName(existing.getRequiredName());

        CFGCashURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        keyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure());

        // Validate reverse foreign keys

        // Delete is valid

        SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff> subdict;

        dictByPKey.remove(pkey);

        dictByUNameIdx.remove(keyUNameIdx);

        subdict = dictByIsSecureIdx.get(keyIsSecureIdx);
        subdict.remove(pkey);

    }

    public void deleteURLProtocolByIdIdx(CFGCashAuthorization Authorization, short argURLProtocolId) {
        CFGCashURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey();
        key.setRequiredURLProtocolId(argURLProtocolId);
        deleteURLProtocolByIdIdx(Authorization, key);
    }

    public void deleteURLProtocolByIdIdx(CFGCashAuthorization Authorization, CFGCashURLProtocolPKey argKey) {
        CFGCashURLProtocolBuff cur;
        LinkedList<CFGCashURLProtocolBuff> matchSet = new LinkedList<CFGCashURLProtocolBuff>();
        Iterator<CFGCashURLProtocolBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFGCashURLProtocolBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteURLProtocol(Authorization, cur);
        }
    }

    public void deleteURLProtocolByUNameIdx(CFGCashAuthorization Authorization, String argName) {
        CFGCashURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey();
        key.setRequiredName(argName);
        deleteURLProtocolByUNameIdx(Authorization, key);
    }

    public void deleteURLProtocolByUNameIdx(CFGCashAuthorization Authorization,
            CFGCashURLProtocolByUNameIdxKey argKey) {
        CFGCashURLProtocolBuff cur;
        LinkedList<CFGCashURLProtocolBuff> matchSet = new LinkedList<CFGCashURLProtocolBuff>();
        Iterator<CFGCashURLProtocolBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFGCashURLProtocolBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteURLProtocol(Authorization, cur);
        }
    }

    public void deleteURLProtocolByIsSecureIdx(CFGCashAuthorization Authorization, boolean argIsSecure) {
        CFGCashURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        key.setRequiredIsSecure(argIsSecure);
        deleteURLProtocolByIsSecureIdx(Authorization, key);
    }

    public void deleteURLProtocolByIsSecureIdx(CFGCashAuthorization Authorization,
            CFGCashURLProtocolByIsSecureIdxKey argKey) {
        CFGCashURLProtocolBuff cur;
        LinkedList<CFGCashURLProtocolBuff> matchSet = new LinkedList<CFGCashURLProtocolBuff>();
        Iterator<CFGCashURLProtocolBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFGCashURLProtocolBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteURLProtocol(Authorization, cur);
        }
    }

    public CFGCashCursor openURLProtocolCursorAll(CFGCashAuthorization Authorization) {
        CFGCashCursor cursor = new CFGCashRamURLProtocolCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public CFGCashCursor openURLProtocolCursorByIsSecureIdx(CFGCashAuthorization Authorization, boolean IsSecure) {
        CFGCashCursor cursor;
        CFGCashURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey();
        key.setRequiredIsSecure(IsSecure);

        if (dictByIsSecureIdx.containsKey(key)) {
            SortedMap<CFGCashURLProtocolPKey, CFGCashURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx
                    .get(key);
            cursor = new CFGCashRamURLProtocolCursor(Authorization, schema, subdictIsSecureIdx.values());
        } else {
            cursor = new CFGCashRamURLProtocolCursor(Authorization, schema,
                    new ArrayList<CFGCashURLProtocolBuff>());
        }
        return (cursor);
    }

    public void closeURLProtocolCursor(CFGCashCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public CFGCashURLProtocolBuff nextURLProtocolCursor(CFGCashCursor Cursor) {
        CFGCashRamURLProtocolCursor cursor = (CFGCashRamURLProtocolCursor) Cursor;
        CFGCashURLProtocolBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public CFGCashURLProtocolBuff prevURLProtocolCursor(CFGCashCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        CFGCashURLProtocolBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextURLProtocolCursor(Cursor);
        }
        return (rec);
    }

    public CFGCashURLProtocolBuff firstURLProtocolCursor(CFGCashCursor Cursor) {
        int targetRowIdx = 1;
        CFGCashURLProtocolBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextURLProtocolCursor(Cursor);
        }
        return (rec);
    }

    public CFGCashURLProtocolBuff lastURLProtocolCursor(CFGCashCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastURLProtocolCursor");
    }

    public CFGCashURLProtocolBuff nthURLProtocolCursor(CFGCashCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFGCashURLProtocolBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextURLProtocolCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}