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

Java tutorial

Introduction

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

Source

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

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

/*
 *   CFAccRamISOCurrencyTable in-memory RAM DbIO implementation
 *   for ISOCurrency.
 */
public class CFAccRamISOCurrencyTable implements ICFAccISOCurrencyTable {
    private CFAccRamSchema schema;
    private Map<CFAccISOCurrencyPKey, CFAccISOCurrencyBuff> dictByPKey = new HashMap<CFAccISOCurrencyPKey, CFAccISOCurrencyBuff>();
    private SortedMap<CFAccISOCurrencyByCcyCdIdxKey, CFAccISOCurrencyBuff> dictByCcyCdIdx = new TreeMap<CFAccISOCurrencyByCcyCdIdxKey, CFAccISOCurrencyBuff>();
    private SortedMap<CFAccISOCurrencyByCcyNmIdxKey, CFAccISOCurrencyBuff> dictByCcyNmIdx = new TreeMap<CFAccISOCurrencyByCcyNmIdxKey, CFAccISOCurrencyBuff>();

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

    public void createISOCurrency(CFAccAuthorization Authorization, CFAccISOCurrencyBuff Buff) {
        CFAccISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        Buff.setRequiredId(pkey.getRequiredId());
        CFAccISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        keyCcyCdIdx.setRequiredISOCode(Buff.getRequiredISOCode());

        CFAccISOCurrencyByCcyNmIdxKey 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 CFAccISOCurrencyBuff readDerived(CFAccAuthorization Authorization, CFAccISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAccRamISOCurrency.readDerived() ";
        CFAccISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccISOCurrencyBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFAccISOCurrencyBuff lockDerived(CFAccAuthorization Authorization, CFAccISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAccRamISOCurrency.readDerived() ";
        CFAccISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFAccISOCurrencyBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFAccISOCurrencyBuff readDerivedByCcyCdIdx(CFAccAuthorization Authorization, String ISOCode) {
        final String S_ProcName = "CFAccRamISOCurrency.readDerivedByCcyCdIdx() ";
        CFAccISOCurrencyByCcyCdIdxKey key = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        key.setRequiredISOCode(ISOCode);

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

    public CFAccISOCurrencyBuff readDerivedByCcyNmIdx(CFAccAuthorization Authorization, String Name) {
        final String S_ProcName = "CFAccRamISOCurrency.readDerivedByCcyNmIdx() ";
        CFAccISOCurrencyByCcyNmIdxKey key = schema.getFactoryISOCurrency().newCcyNmIdxKey();
        key.setRequiredName(Name);

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

    public CFAccISOCurrencyBuff readDerivedByIdIdx(CFAccAuthorization Authorization, short Id) {
        final String S_ProcName = "CFAccRamISOCurrency.readDerivedByIdIdx() ";
        CFAccISOCurrencyPKey key = schema.getFactoryISOCurrency().newPKey();
        key.setRequiredId(Id);

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

    public CFAccISOCurrencyBuff readBuff(CFAccAuthorization Authorization, CFAccISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAccRamISOCurrency.readBuff() ";
        CFAccISOCurrencyBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) {
            buff = null;
        }
        return (buff);
    }

    public CFAccISOCurrencyBuff lockBuff(CFAccAuthorization Authorization, CFAccISOCurrencyPKey PKey) {
        final String S_ProcName = "CFAccRamISOCurrency.readBuff() ";
        CFAccISOCurrencyBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISCY"))) {
            buff = null;
        }
        return (buff);
    }

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

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

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

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

    public void updateISOCurrency(CFAccAuthorization Authorization, CFAccISOCurrencyBuff Buff) {
        CFAccISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccISOCurrencyBuff 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);
        CFAccISOCurrencyByCcyCdIdxKey existingKeyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        existingKeyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode());

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

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

        CFAccISOCurrencyByCcyNmIdxKey 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<CFAccISOCurrencyPKey, CFAccISOCurrencyBuff> 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(CFAccAuthorization Authorization, CFAccISOCurrencyBuff Buff) {
        final String S_ProcName = "CFAccRamISOCurrencyTable.deleteISOCurrency() ";
        CFAccISOCurrencyPKey pkey = schema.getFactoryISOCurrency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFAccISOCurrencyBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCurrency",
                    pkey);
        }
        CFAccISOCurrencyByCcyCdIdxKey keyCcyCdIdx = schema.getFactoryISOCurrency().newCcyCdIdxKey();
        keyCcyCdIdx.setRequiredISOCode(existing.getRequiredISOCode());

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

        // Validate reverse foreign keys

        if (schema.getTableAccountConfig().readDerivedByDefCcyIdx(Authorization,
                existing.getRequiredId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteISOCurrency",
                    "Lookup", "DefaultCurrency", "AccountConfig", pkey);
        }

        if (schema.getTableAccountEntry().readDerivedByCvtCcyIdx(Authorization,
                existing.getRequiredId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteISOCurrency",
                    "Lookup", "ConvertedCurrency", "AccountEntry", pkey);
        }

        if (schema.getTableAccountEntry().readDerivedByBalCcyIdx(Authorization,
                existing.getRequiredId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteISOCurrency",
                    "Lookup", "BalanceCurrency", "AccountEntry", pkey);
        }

        // Delete is valid

        schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCurrencyIdx(Authorization,
                Buff.getRequiredId());
        SortedMap<CFAccISOCurrencyPKey, CFAccISOCurrencyBuff> subdict;

        dictByPKey.remove(pkey);

        dictByCcyCdIdx.remove(keyCcyCdIdx);

        dictByCcyNmIdx.remove(keyCcyNmIdx);

    }

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

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

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

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

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

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

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

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

    public CFAccISOCurrencyBuff nextISOCurrencyCursor(CFAccCursor Cursor) {
        CFAccRamISOCurrencyCursor cursor = (CFAccRamISOCurrencyCursor) Cursor;
        CFAccISOCurrencyBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

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

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

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

    public void releasePreparedStatements() {
    }
}