net.sourceforge.msscodefactory.cfensyntax.v2_1.CFEnSyntaxRam.CFEnSyntaxRamISOCountryTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfensyntax.v2_1.CFEnSyntaxRam.CFEnSyntaxRamISOCountryTable.java

Source

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

/*
 *   CFEnSyntax
 *
 *   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.1-cfsecurity.xml),
 *      CFInternet (net-sourceforge-msscodefactory-2.1-cfinternet.xml), and
 *      CFCrm 2.1 (net-sourceforge-msscodefactory-2.1-cfcrm.xml), with all of the
 *      required models being available as part of the MSS Code Factory 2.0
 *      distribution source and install zips.
 *   
 *      You can download installations of MSS Code Factory 2.0 from
 *      http://msscodefactory.sourceforge.net/
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfensyntax.v2_1.CFEnSyntaxRam;

import java.sql.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cfensyntax.v2_1.CFEnSyntax.*;
import net.sourceforge.msscodefactory.cfensyntax.v2_1.CFEnSyntaxRam.*;

/*
 *   CFEnSyntaxRamISOCountryTable in-memory RAM DbIO implementation
 *   for ISOCountry.
 */
public class CFEnSyntaxRamISOCountryTable implements ICFEnSyntaxISOCountryTable {
    private CFEnSyntaxRamSchema schema;
    private Map<CFEnSyntaxISOCountryPKey, CFEnSyntaxISOCountryBuff> dictByPKey = new HashMap<CFEnSyntaxISOCountryPKey, CFEnSyntaxISOCountryBuff>();
    private Map<CFEnSyntaxISOCountryByISOCodeIdxKey, CFEnSyntaxISOCountryBuff> dictByISOCodeIdx = new HashMap<CFEnSyntaxISOCountryByISOCodeIdxKey, CFEnSyntaxISOCountryBuff>();
    private Map<CFEnSyntaxISOCountryByNameIdxKey, CFEnSyntaxISOCountryBuff> dictByNameIdx = new HashMap<CFEnSyntaxISOCountryByNameIdxKey, CFEnSyntaxISOCountryBuff>();

    public CFEnSyntaxRamISOCountryTable(CFEnSyntaxRamSchema argSchema) {
        schema = argSchema;
    }

    public void createISOCountry(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryBuff Buff) {
        final String S_ProcName = "createISOCountry";
        CFEnSyntaxISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        Buff.setRequiredId(pkey.getRequiredId());
        CFEnSyntaxISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey();
        keyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode());

        CFEnSyntaxISOCountryByNameIdxKey keyNameIdx = schema.getFactoryISOCountry().newNameIdxKey();
        keyNameIdx.setRequiredName(Buff.getRequiredName());

        // Validate unique indexes

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

        if (dictByISOCodeIdx.containsKey(keyISOCodeIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName,
                    "ISOCountryCodeIdx", keyISOCodeIdx);
        }

        if (dictByNameIdx.containsKey(keyNameIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName,
                    "ISOCountryNameIdx", keyNameIdx);
        }

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        dictByISOCodeIdx.put(keyISOCodeIdx, Buff);

        dictByNameIdx.put(keyNameIdx, Buff);

    }

    public CFEnSyntaxISOCountryBuff readDerived(CFEnSyntaxAuthorization Authorization,
            CFEnSyntaxISOCountryPKey PKey) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readDerived() ";
        CFEnSyntaxISOCountryPKey key = schema.getFactoryISOCountry().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFEnSyntaxISOCountryBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFEnSyntaxISOCountryBuff lockDerived(CFEnSyntaxAuthorization Authorization,
            CFEnSyntaxISOCountryPKey PKey) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readDerived() ";
        CFEnSyntaxISOCountryPKey key = schema.getFactoryISOCountry().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        CFEnSyntaxISOCountryBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public CFEnSyntaxISOCountryBuff readDerivedByISOCodeIdx(CFEnSyntaxAuthorization Authorization, String ISOCode) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readDerivedByISOCodeIdx() ";
        CFEnSyntaxISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey();
        key.setRequiredISOCode(ISOCode);

        CFEnSyntaxISOCountryBuff buff;
        if (dictByISOCodeIdx.containsKey(key)) {
            buff = dictByISOCodeIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFEnSyntaxISOCountryBuff readDerivedByNameIdx(CFEnSyntaxAuthorization Authorization, String Name) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readDerivedByNameIdx() ";
        CFEnSyntaxISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey();
        key.setRequiredName(Name);

        CFEnSyntaxISOCountryBuff buff;
        if (dictByNameIdx.containsKey(key)) {
            buff = dictByNameIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public CFEnSyntaxISOCountryBuff readDerivedByIdIdx(CFEnSyntaxAuthorization Authorization, short Id) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readDerivedByIdIdx() ";
        CFEnSyntaxISOCountryPKey key = schema.getFactoryISOCountry().newPKey();
        key.setRequiredId(Id);

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

    public CFEnSyntaxISOCountryBuff readBuff(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryPKey PKey) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readBuff() ";
        CFEnSyntaxISOCountryBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) {
            buff = null;
        }
        return (buff);
    }

    public CFEnSyntaxISOCountryBuff lockBuff(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryPKey PKey) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readBuff() ";
        CFEnSyntaxISOCountryBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ISOC"))) {
            buff = null;
        }
        return (buff);
    }

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

    public CFEnSyntaxISOCountryBuff readBuffByIdIdx(CFEnSyntaxAuthorization Authorization, short Id) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readBuffByIdIdx() ";
        CFEnSyntaxISOCountryBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("ISOC")) {
            return ((CFEnSyntaxISOCountryBuff) buff);
        } else {
            return (null);
        }
    }

    public CFEnSyntaxISOCountryBuff readBuffByISOCodeIdx(CFEnSyntaxAuthorization Authorization, String ISOCode) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readBuffByISOCodeIdx() ";
        CFEnSyntaxISOCountryBuff buff = readDerivedByISOCodeIdx(Authorization, ISOCode);
        if ((buff != null) && buff.getClassCode().equals("ISOC")) {
            return ((CFEnSyntaxISOCountryBuff) buff);
        } else {
            return (null);
        }
    }

    public CFEnSyntaxISOCountryBuff readBuffByNameIdx(CFEnSyntaxAuthorization Authorization, String Name) {
        final String S_ProcName = "CFEnSyntaxRamISOCountry.readBuffByNameIdx() ";
        CFEnSyntaxISOCountryBuff buff = readDerivedByNameIdx(Authorization, Name);
        if ((buff != null) && buff.getClassCode().equals("ISOC")) {
            return ((CFEnSyntaxISOCountryBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateISOCountry(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryBuff Buff) {
        CFEnSyntaxISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFEnSyntaxISOCountryBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateISOCountry",
                    "Existing record not found", "ISOCountry", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateISOCountry",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        CFEnSyntaxISOCountryByISOCodeIdxKey existingKeyISOCodeIdx = schema.getFactoryISOCountry()
                .newISOCodeIdxKey();
        existingKeyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode());

        CFEnSyntaxISOCountryByISOCodeIdxKey newKeyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey();
        newKeyISOCodeIdx.setRequiredISOCode(Buff.getRequiredISOCode());

        CFEnSyntaxISOCountryByNameIdxKey existingKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey();
        existingKeyNameIdx.setRequiredName(existing.getRequiredName());

        CFEnSyntaxISOCountryByNameIdxKey newKeyNameIdx = schema.getFactoryISOCountry().newNameIdxKey();
        newKeyNameIdx.setRequiredName(Buff.getRequiredName());

        // Check unique indexes

        if (!existingKeyISOCodeIdx.equals(newKeyISOCodeIdx)) {
            if (dictByISOCodeIdx.containsKey(newKeyISOCodeIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateISOCountry", "ISOCountryCodeIdx", newKeyISOCodeIdx);
            }
        }

        if (!existingKeyNameIdx.equals(newKeyNameIdx)) {
            if (dictByNameIdx.containsKey(newKeyNameIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateISOCountry", "ISOCountryNameIdx", newKeyNameIdx);
            }
        }

        // Validate foreign keys

        // Update is valid

        Map<CFEnSyntaxISOCountryPKey, CFEnSyntaxISOCountryBuff> subdict;

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

        dictByISOCodeIdx.remove(existingKeyISOCodeIdx);
        dictByISOCodeIdx.put(newKeyISOCodeIdx, Buff);

        dictByNameIdx.remove(existingKeyNameIdx);
        dictByNameIdx.put(newKeyNameIdx, Buff);

    }

    public void deleteISOCountry(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryBuff Buff) {
        final String S_ProcName = "CFEnSyntaxRamISOCountryTable.deleteISOCountry() ";
        CFEnSyntaxISOCountryPKey pkey = schema.getFactoryISOCountry().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        CFEnSyntaxISOCountryBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            return;
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteISOCountry",
                    pkey);
        }
        CFEnSyntaxISOCountryByISOCodeIdxKey keyISOCodeIdx = schema.getFactoryISOCountry().newISOCodeIdxKey();
        keyISOCodeIdx.setRequiredISOCode(existing.getRequiredISOCode());

        CFEnSyntaxISOCountryByNameIdxKey keyNameIdx = schema.getFactoryISOCountry().newNameIdxKey();
        keyNameIdx.setRequiredName(existing.getRequiredName());

        // Validate reverse foreign keys

        // Delete is valid

        schema.getTableISOCountryCurrency().deleteISOCountryCurrencyByCountryIdx(Authorization,
                Buff.getRequiredId());
        schema.getTableISOCountryLanguage().deleteISOCountryLanguageByCountryIdx(Authorization,
                Buff.getRequiredId());
        Map<CFEnSyntaxISOCountryPKey, CFEnSyntaxISOCountryBuff> subdict;

        dictByPKey.remove(pkey);

        dictByISOCodeIdx.remove(keyISOCodeIdx);

        dictByNameIdx.remove(keyNameIdx);

    }

    public void deleteISOCountryByIdIdx(CFEnSyntaxAuthorization Authorization, short argId) {
        CFEnSyntaxISOCountryPKey key = schema.getFactoryISOCountry().newPKey();
        key.setRequiredId(argId);
        deleteISOCountryByIdIdx(Authorization, key);
    }

    public void deleteISOCountryByIdIdx(CFEnSyntaxAuthorization Authorization, CFEnSyntaxISOCountryPKey argKey) {
        CFEnSyntaxISOCountryBuff cur;
        LinkedList<CFEnSyntaxISOCountryBuff> matchSet = new LinkedList<CFEnSyntaxISOCountryBuff>();
        Iterator<CFEnSyntaxISOCountryBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFEnSyntaxISOCountryBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCountry(Authorization, cur);
        }
    }

    public void deleteISOCountryByISOCodeIdx(CFEnSyntaxAuthorization Authorization, String argISOCode) {
        CFEnSyntaxISOCountryByISOCodeIdxKey key = schema.getFactoryISOCountry().newISOCodeIdxKey();
        key.setRequiredISOCode(argISOCode);
        deleteISOCountryByISOCodeIdx(Authorization, key);
    }

    public void deleteISOCountryByISOCodeIdx(CFEnSyntaxAuthorization Authorization,
            CFEnSyntaxISOCountryByISOCodeIdxKey argKey) {
        CFEnSyntaxISOCountryBuff cur;
        LinkedList<CFEnSyntaxISOCountryBuff> matchSet = new LinkedList<CFEnSyntaxISOCountryBuff>();
        Iterator<CFEnSyntaxISOCountryBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFEnSyntaxISOCountryBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCountry(Authorization, cur);
        }
    }

    public void deleteISOCountryByNameIdx(CFEnSyntaxAuthorization Authorization, String argName) {
        CFEnSyntaxISOCountryByNameIdxKey key = schema.getFactoryISOCountry().newNameIdxKey();
        key.setRequiredName(argName);
        deleteISOCountryByNameIdx(Authorization, key);
    }

    public void deleteISOCountryByNameIdx(CFEnSyntaxAuthorization Authorization,
            CFEnSyntaxISOCountryByNameIdxKey argKey) {
        CFEnSyntaxISOCountryBuff cur;
        LinkedList<CFEnSyntaxISOCountryBuff> matchSet = new LinkedList<CFEnSyntaxISOCountryBuff>();
        Iterator<CFEnSyntaxISOCountryBuff> values = dictByPKey.values().iterator();
        while (values.hasNext()) {
            cur = values.next();
            if (argKey.equals(cur)) {
                matchSet.add(cur);
            }
        }
        Iterator<CFEnSyntaxISOCountryBuff> iterMatch = matchSet.iterator();
        while (iterMatch.hasNext()) {
            cur = iterMatch.next();
            deleteISOCountry(Authorization, cur);
        }
    }

    public CFEnSyntaxCursor openISOCountryCursorAll(CFEnSyntaxAuthorization Authorization) {
        CFEnSyntaxCursor cursor = new CFEnSyntaxRamISOCountryCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public void closeISOCountryCursor(CFEnSyntaxCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public CFEnSyntaxISOCountryBuff nextISOCountryCursor(CFEnSyntaxCursor Cursor) {
        CFEnSyntaxRamISOCountryCursor cursor = (CFEnSyntaxRamISOCountryCursor) Cursor;
        CFEnSyntaxISOCountryBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public CFEnSyntaxISOCountryBuff prevISOCountryCursor(CFEnSyntaxCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        CFEnSyntaxISOCountryBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextISOCountryCursor(Cursor);
        }
        return (rec);
    }

    public CFEnSyntaxISOCountryBuff firstISOCountryCursor(CFEnSyntaxCursor Cursor) {
        int targetRowIdx = 1;
        CFEnSyntaxISOCountryBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextISOCountryCursor(Cursor);
        }
        return (rec);
    }

    public CFEnSyntaxISOCountryBuff lastISOCountryCursor(CFEnSyntaxCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastISOCountryCursor");
    }

    public CFEnSyntaxISOCountryBuff nthISOCountryCursor(CFEnSyntaxCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        CFEnSyntaxISOCountryBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextISOCountryCursor(Cursor);
        }
        return (rec);
    }

    public void releasePreparedStatements() {
    }
}