net.sourceforge.msscodefactory.v1_10.MSSBamRam.MSSBamRamAddressTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.v1_10.MSSBamRam.MSSBamRamAddressTable.java

Source

// Description: Java6 in-memory RAM DbIO implementation for Address.

/*
 *   MSS Code Factory 1.10
 *
 *   Copyright (c) 2012 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/>.
 *   
 * ***********************************************************************
 *
 *   Code manufactured
 *   by MSS Code Factory version 1.9.3294
 *
 *   $Revision: 26 $
 */

package net.sourceforge.msscodefactory.v1_10.MSSBamRam;

import java.sql.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v1_9.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.v1_10.MSSBam.*;
import net.sourceforge.msscodefactory.v1_10.MSSBamBL.*;
import net.sourceforge.msscodefactory.v1_10.MSSBamBLRam.*;

/*
 *   MSSBamRamAddressTable in-memory RAM DbIO implementation
 *   for Address.
 */
public class MSSBamRamAddressTable implements IMSSBamAddressTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamAddressPKey, MSSBamAddressBuff> dictByPKey = new HashMap<MSSBamAddressPKey, MSSBamAddressBuff>();
    private SortedMap<MSSBamAddressByContactIdxKey, SortedMap<MSSBamAddressPKey, MSSBamAddressBuff>> dictByContactIdx = new TreeMap<MSSBamAddressByContactIdxKey, SortedMap<MSSBamAddressPKey, MSSBamAddressBuff>>();
    private SortedMap<MSSBamAddressByUDescrIdxKey, MSSBamAddressBuff> dictByUDescrIdx = new TreeMap<MSSBamAddressByUDescrIdxKey, MSSBamAddressBuff>();

    public MSSBamRamAddressTable(MSSBamBLRamSchema argSchema) {
        schema = argSchema;
    }

    public void createAddress(MSSBamAuthorization Authorization, MSSBamAddressBuff Buff) {
        MSSBamAddressPKey pkey = schema.getFactoryAddress().newPKey();

        pkey.setRequiredAddressId(schema.nextAddressIdGen());
        Buff.setRequiredAddressId(pkey.getRequiredAddressId());

        MSSBamAddressByContactIdxKey keyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        keyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        MSSBamAddressByUDescrIdxKey keyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        // Validate unique indexes

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

        if (dictByUDescrIdx.containsKey(keyUDescrIdx)) {
            throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createAddress",
                    "AddressUDescrIdx", keyUDescrIdx);
        }

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableContact().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredContactId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createAddress", "Container", "Contact", "Contact", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamAddressPKey, MSSBamAddressBuff> subdictContactIdx;
        if (dictByContactIdx.containsKey(keyContactIdx)) {
            subdictContactIdx = dictByContactIdx.get(keyContactIdx);
        } else {
            subdictContactIdx = new TreeMap<MSSBamAddressPKey, MSSBamAddressBuff>();
            dictByContactIdx.put(keyContactIdx, subdictContactIdx);
        }
        subdictContactIdx.put(pkey, Buff);

        dictByUDescrIdx.put(keyUDescrIdx, Buff);

    }

    public MSSBamAddressBuff readDerived(MSSBamAuthorization Authorization, MSSBamAddressPKey PKey) {
        final String S_ProcName = "MSSBamRamAddress.readDerived() ";
        MSSBamAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredAddressId(PKey.getRequiredAddressId());
        MSSBamAddressBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamAddressBuff[] readDerivedByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "MSSBamRamAddress.readDerivedByContactIdx() ";
        MSSBamAddressByContactIdxKey key = schema.getFactoryAddress().newContactIdxKey();
        key.setRequiredContactId(ContactId);

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

    public MSSBamAddressBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long ContactId,
            String Description) {
        final String S_ProcName = "MSSBamRamAddress.readDerivedByUDescrIdx() ";
        MSSBamAddressByUDescrIdxKey key = schema.getFactoryAddress().newUDescrIdxKey();
        key.setRequiredContactId(ContactId);
        key.setRequiredDescription(Description);

        MSSBamAddressBuff buff;
        if (dictByUDescrIdx.containsKey(key)) {
            buff = dictByUDescrIdx.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

    public MSSBamAddressBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long AddressId) {
        final String S_ProcName = "MSSBamRamAddress.readDerivedByIdIdx() ";
        MSSBamAddressPKey key = schema.getFactoryAddress().newPKey();
        key.setRequiredAddressId(AddressId);

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

    public MSSBamAddressBuff readBuff(MSSBamAuthorization Authorization, MSSBamAddressPKey PKey) {
        final String S_ProcName = "MSSBamRamAddress.readBuff() ";
        MSSBamAddressBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("ADR"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamAddressBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long AddressId) {
        final String S_ProcName = "MSSBamRamAddress.readBuffByIdIdx() ";
        MSSBamAddressBuff buff = readDerivedByIdIdx(Authorization, AddressId);
        if ((buff != null) && buff.getClassCode().equals("ADR")) {
            return ((MSSBamAddressBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamAddressBuff[] readBuffByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "MSSBamRamAddress.readBuffByContactIdx() ";
        MSSBamAddressBuff buff;
        ArrayList<MSSBamAddressBuff> filteredList = new ArrayList<MSSBamAddressBuff>();
        MSSBamAddressBuff[] buffList = readDerivedByContactIdx(Authorization, ContactId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("ADR")) {
                filteredList.add((MSSBamAddressBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamAddressBuff[0]));
    }

    public MSSBamAddressBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, long ContactId,
            String Description) {
        final String S_ProcName = "MSSBamRamAddress.readBuffByUDescrIdx() ";
        MSSBamAddressBuff buff = readDerivedByUDescrIdx(Authorization, ContactId, Description);
        if ((buff != null) && buff.getClassCode().equals("ADR")) {
            return ((MSSBamAddressBuff) buff);
        } else {
            return (null);
        }
    }

    public void updateAddress(MSSBamAuthorization Authorization, MSSBamAddressBuff Buff) {
        MSSBamAddressPKey pkey = schema.getFactoryAddress().newPKey();
        pkey.setRequiredAddressId(Buff.getRequiredAddressId());
        MSSBamAddressBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAddress",
                    "Existing record not found", "Address", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAddress",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamAddressByContactIdxKey existingKeyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        MSSBamAddressByContactIdxKey newKeyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        MSSBamAddressByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        MSSBamAddressByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        // Check unique indexes

        if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) {
            if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) {
                throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(),
                        "updateAddress", "AddressUDescrIdx", newKeyUDescrIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableContact().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredContactId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateAddress", "Container", "Contact", "Contact", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamAddressPKey, MSSBamAddressBuff> subdict;

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

        subdict = dictByContactIdx.get(existingKeyContactIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByContactIdx.containsKey(newKeyContactIdx)) {
            subdict = dictByContactIdx.get(newKeyContactIdx);
        } else {
            subdict = new TreeMap<MSSBamAddressPKey, MSSBamAddressBuff>();
            dictByContactIdx.put(newKeyContactIdx, subdict);
        }
        subdict.put(pkey, Buff);

        dictByUDescrIdx.remove(existingKeyUDescrIdx);
        dictByUDescrIdx.put(newKeyUDescrIdx, Buff);

    }

    public void deleteAddress(MSSBamAuthorization Authorization, MSSBamAddressBuff Buff) {
        final String S_ProcName = "MSSBamRamAddressTable.deleteAddress() ";
        MSSBamAddressPKey pkey = schema.getFactoryAddress().newPKey();

        pkey.setRequiredAddressId(schema.nextAddressIdGen());
        MSSBamAddressBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteAddress",
                    "Existing record not found", "Address", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAddress",
                    pkey);
        }
        MSSBamAddressByContactIdxKey keyContactIdx = schema.getFactoryAddress().newContactIdxKey();
        keyContactIdx.setRequiredContactId(existing.getRequiredContactId());

        MSSBamAddressByUDescrIdxKey keyUDescrIdx = schema.getFactoryAddress().newUDescrIdxKey();
        keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        // Validate reverse foreign keys

        if (schema.getTableAddressTag().readDerivedByAddressIdx(Authorization,
                existing.getRequiredAddressId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteAddress",
                    "Container", "AddressTagAddress", "AddressTag", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamAddressPKey, MSSBamAddressBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByContactIdx.get(keyContactIdx);
        subdict.remove(pkey);

        dictByUDescrIdx.remove(keyUDescrIdx);

    }

    public MSSBamCursor openAddressCursorAll(MSSBamAuthorization Authorization) {
        MSSBamCursor cursor = new MSSBamRamAddressCursor(Authorization, schema, dictByPKey.values());
        return (cursor);
    }

    public MSSBamCursor openAddressCursorByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        MSSBamCursor cursor;
        MSSBamAddressByContactIdxKey key = schema.getFactoryAddress().newContactIdxKey();
        key.setRequiredContactId(ContactId);

        if (dictByContactIdx.containsKey(key)) {
            SortedMap<MSSBamAddressPKey, MSSBamAddressBuff> subdictContactIdx = dictByContactIdx.get(key);
            cursor = new MSSBamRamAddressCursor(Authorization, schema, subdictContactIdx.values());
        } else {
            cursor = new MSSBamRamAddressCursor(Authorization, schema, new ArrayList<MSSBamAddressBuff>());
        }
        return (cursor);
    }

    public void closeAddressCursor(MSSBamCursor Cursor) {
        // Cursor.DataReader.Close();
    }

    public MSSBamAddressBuff nextAddressCursor(MSSBamCursor Cursor) {
        MSSBamRamAddressCursor cursor = (MSSBamRamAddressCursor) Cursor;
        MSSBamAddressBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

    public MSSBamAddressBuff prevAddressCursor(MSSBamCursor Cursor) {
        int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1;
        MSSBamAddressBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAddressCursor(Cursor);
        }
        return (rec);
    }

    public MSSBamAddressBuff firstAddressCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamAddressBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAddressCursor(Cursor);
        }
        return (rec);
    }

    public MSSBamAddressBuff lastAddressCursor(MSSBamCursor Cursor) {
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAddressCursor");
    }

    public MSSBamAddressBuff nthAddressCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamAddressBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAddressCursor(Cursor);
        }
        return (rec);
    }
}