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

Java tutorial

Introduction

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

Source

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

/*
 *   CF Asterisk 11 Configuration Model
 *
 *   Copyright (c) 2013-2014 Mark Sobkow
 *   
 *   This program is available as free software under the GNU GPL v3, or
 *   under a commercial license from Mark Sobkow.  For commercial licensing
 *   details, please contact msobkow@sasktel.net.
 *   
 *   Under the terms of the GPL:
 *   
 *      This program is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *     
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *     
 *      You should have received a copy of the GNU General Public License
 *      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *   
 *      This source code incorporates modified modules originally licensed
 *      under the Apache 2.0 license by MSS Code Factory including CFSecurity
 *      (net-sourceforge-msscodefactory-2.0-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and
 *      CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 1.11
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 1.11 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

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

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

/*
 *   CFAstRamISOCurrencyTable in-memory RAM DbIO implementation
 *   for ISOCurrency.
 */
public class CFAstRamISOCurrencyTable implements ICFAstISOCurrencyTable {
    private CFAstRamSchema schema;
    private Map<CFAstISOCurrencyPKey, CFAstISOCurrencyBuff> dictByPKey = new HashMap<CFAstISOCurrencyPKey, CFAstISOCurrencyBuff>();
    private SortedMap<CFAstISOCurrencyByCcyCdIdxKey, CFAstISOCurrencyBuff> dictByCcyCdIdx = new TreeMap<CFAstISOCurrencyByCcyCdIdxKey, CFAstISOCurrencyBuff>();
    private SortedMap<CFAstISOCurrencyByCcyNmIdxKey, CFAstISOCurrencyBuff> dictByCcyNmIdx = new TreeMap<CFAstISOCurrencyByCcyNmIdxKey, CFAstISOCurrencyBuff>();

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

    public void createISOCurrency(CFAstAuthorization Authorization, CFAstISOCurrencyBuff Buff) {
        CFAstISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAstISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        keyCcyCdIdx.setRequiredISOCode(Buff.getRequiredISOCode());

        CFAstISOCurrencyByCcyNmIdxKey keyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        keyCcyNmIdx.setRequiredName(Buff.getRequiredName());

        // Validate unique indexes

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

        if (dictByCcyCdIdx.containsKey(keyCcyCdIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                    "createISOCurrency", "ISOCurrencyCodeIdx", keyCcyCdIdx);
        }

        if (dictByCcyNmIdx.containsKey(keyCcyNmIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                    "createISOCurrency", "ISOCurrencyNameIdx", keyCcyNmIdx);
        }

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByCcyCdIdx.put(keyCcyCdIdx, Buff);

        dictByCcyNmIdx.put(keyCcyNmIdx, Buff);

    }

    public CFAstISOCurrencyBuff readDerived(CFAstAuthorization Authorization, CFAstISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAstRamISOCurrency.readDerived() ";
        CFAstISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAstISOCurrencyBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstISOCurrencyBuff lockDerived(CFAstAuthorization Authorization, CFAstISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAstRamISOCurrency.readDerived() ";
        CFAstISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAstISOCurrencyBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstISOCurrencyBuff readDerivedByCcyCdIdx(CFAstAuthorization Authorization, String ISOCode) {
        final String S_ProcName = "CFAstRamISOCurrency.readDerivedByCcyCdIdx() ";
        CFAstISOCurrencyByCcyCdIdxKey key = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        key.setRequiredISOCode(ISOCode);

        CFAstISOCurrencyBuff buff;
        if (dictByCcyCdIdx.containsKey(key)) {
            buff = dictByCcyCdIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstISOCurrencyBuff readDerivedByCcyNmIdx(CFAstAuthorization Authorization, String Name) {
        final String S_ProcName = "CFAstRamISOCurrency.readDerivedByCcyNmIdx() ";
        CFAstISOCurrencyByCcyNmIdxKey key = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        key.setRequiredName(Name);

        CFAstISOCurrencyBuff buff;
        if (dictByCcyNmIdx.containsKey(key)) {
            buff = dictByCcyNmIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAstISOCurrencyBuff readDerivedByIdIdx(CFAstAuthorization Authorization, short Id) {
        final String S_ProcName = "CFAstRamISOCurrency.readDerivedByIdIdx() ";
        CFAstISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(Id);

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

    public CFAstISOCurrencyBuff readBuff(CFAstAuthorization Authorization, CFAstISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAstRamISOCurrency.readBuff() ";
        CFAstISOCurrencyBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAstISOCurrencyBuff lockBuff(CFAstAuthorization Authorization, CFAstISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAstRamISOCurrency.readBuff() ";
        CFAstISOCurrencyBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFAstISOCurrencyBuff readBuffByIdIdx(CFAstAuthorization Authorization, short Id) {
        final String S_ProcName = "CFAstRamISOCurrency.readBuffByIdIdx() ";
        CFAstISOCurrencyBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("ISCY")) {
            return ((CFAstISOCurrencyBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstISOCurrencyBuff readBuffByCcyCdIdx(CFAstAuthorization Authorization, String ISOCode) {
        final String S_ProcName = "CFAstRamISOCurrency.readBuffByCcyCdIdx() ";
        CFAstISOCurrencyBuff buff = readDerivedByCcyCdIdx(Authorization, ISOCode);
        if ((buff != null) && buff.getClassCode().equals("ISCY")) {
            return ((CFAstISOCurrencyBuff) buff);
        } else {
            return (null);
        }
    }

    public CFAstISOCurrencyBuff readBuffByCcyNmIdx(CFAstAuthorization Authorization, String Name) {
        final String S_ProcName = "CFAstRamISOCurrency.readBuffByCcyNmIdx() ";
        CFAstISOCurrencyBuff buff = readDerivedByCcyNmIdx(Authorization, Name);
        if ((buff != null) && buff.getClassCode().equals("ISCY")) {
            return ((CFAstISOCurrencyBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateISOCurrency(CFAstAuthorization Authorization, CFAstISOCurrencyBuff Buff) {
        CFAstISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstISOCurrencyBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateISOCurrency",
                    "Existing record not found", "ISOCurrency", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateISOCurrency",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFAstISOCurrencyByCcyCdIdxKey existingKeyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        existingKeyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode());

        CFAstISOCurrencyByCcyCdIdxKey newKeyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        newKeyCcyCdIdx.setRequiredISOCode(Buff.getRequiredISOCode());

        CFAstISOCurrencyByCcyNmIdxKey existingKeyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        existingKeyCcyNmIdx.setRequiredName(existing.getRequiredName());

        CFAstISOCurrencyByCcyNmIdxKey newKeyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        newKeyCcyNmIdx.setRequiredName(Buff.getRequiredName());

        // Check unique indexes

        if (!existingKeyCcyCdIdx.equals(newKeyCcyCdIdx)) {
            if (dictByCcyCdIdx.containsKey(newKeyCcyCdIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateISOCurrency", "ISOCurrencyCodeIdx", newKeyCcyCdIdx);
            }
        }

        if (!existingKeyCcyNmIdx.equals(newKeyCcyNmIdx)) {
            if (dictByCcyNmIdx.containsKey(newKeyCcyNmIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateISOCurrency", "ISOCurrencyNameIdx", newKeyCcyNmIdx);
            }
        }

        // Validate foreign keys

        // Update is valid

        SortedMap<CFAstISOCurrencyPKey, CFAstISOCurrencyBuff> subdict;

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

        dictByCcyCdIdx.remove(existingKeyCcyCdIdx);
        dictByCcyCdIdx.put(newKeyCcyCdIdx, Buff);

        dictByCcyNmIdx.remove(existingKeyCcyNmIdx);
        dictByCcyNmIdx.put(newKeyCcyNmIdx, Buff);

    }

    public void deleteISOCurrency(CFAstAuthorization Authorization, CFAstISOCurrencyBuff Buff) {
        final String S_ProcName = "CFAstRamISOCurrencyTable.deleteISOCurrency() ";
        CFAstISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAstISOCurrencyBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCurrency",
                    pkey);
        }
        CFAstISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        keyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode());

        CFAstISOCurrencyByCcyNmIdxKey keyCcyNmIdx = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        keyCcyNmIdx.setRequiredName(existing.getRequiredName());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCurrencyIdx(Authorization,
                Buff.getRequiredId());
        SortedMap<CFAstISOCurrencyPKey, CFAstISOCurrencyBuff> subdict;

        dictByPKey.remove(pkey);

        dictByCcyCdIdx.remove(keyCcyCdIdx);

        dictByCcyNmIdx.remove(keyCcyNmIdx);

    }

    public void deleteISOCurrencyByIdIdx(CFAstAuthorization Authorization, short argId) {
        CFAstISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(argId);
        deleteISOCurrencyByIdIdx(Authorization, key);
    }

    public void deleteISOCurrencyByIdIdx(CFAstAuthorization Authorization, CFAstISOCurrencyPKey argKey) {
        CFAstISOCurrencyBuff cur;
        LinkedList<CFAstISOCurrencyBuff> matchSet = new LinkedList<CFAstISOCurrencyBuff>();
        Iterator<CFAstISOCurrencyBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstISOCurrencyBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCurrency(Authorization, cur);
        }
    }

    public void deleteISOCurrencyByCcyCdIdx(CFAstAuthorization Authorization, String argISOCode) {
        CFAstISOCurrencyByCcyCdIdxKey key = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        key.setRequiredISOCode(argISOCode);
        deleteISOCurrencyByCcyCdIdx(Authorization, key);
    }

    public void deleteISOCurrencyByCcyCdIdx(CFAstAuthorization Authorization,
            CFAstISOCurrencyByCcyCdIdxKey argKey) {
        CFAstISOCurrencyBuff cur;
        LinkedList<CFAstISOCurrencyBuff> matchSet = new LinkedList<CFAstISOCurrencyBuff>();
        Iterator<CFAstISOCurrencyBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstISOCurrencyBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCurrency(Authorization, cur);
        }
    }

    public void deleteISOCurrencyByCcyNmIdx(CFAstAuthorization Authorization, String argName) {
        CFAstISOCurrencyByCcyNmIdxKey key = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        key.setRequiredName(argName);
        deleteISOCurrencyByCcyNmIdx(Authorization, key);
    }

    public void deleteISOCurrencyByCcyNmIdx(CFAstAuthorization Authorization,
            CFAstISOCurrencyByCcyNmIdxKey argKey) {
        CFAstISOCurrencyBuff cur;
        LinkedList<CFAstISOCurrencyBuff> matchSet = new LinkedList<CFAstISOCurrencyBuff>();
        Iterator<CFAstISOCurrencyBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFAstISOCurrencyBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCurrency(Authorization, cur);
        }
    }

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

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

    public CFAstISOCurrencyBuff nextISOCurrencyCursor(CFAstCursor Cursor) {
        CFAstRamISOCurrencyCursor cursor = (CFAstRamISOCurrencyCursor) Cursor;
        CFAstISOCurrencyBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public CFAstISOCurrencyBuff firstISOCurrencyCursor(CFAstCursor Cursor) {
        int targetRowIdx = 1;
        CFAstISOCurrencyBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextISOCurrencyCursor(Cursor);
        }
        return (rec);
    }

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

    public CFAstISOCurrencyBuff nthISOCurrencyCursor(CFAstCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFAstISOCurrencyBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextISOCurrencyCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}