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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamMemoTable in-memory RAM DbIO implementation
 *   for Memo.
 */
public class MSSBamRamMemoTable implements IMSSBamMemoTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamMemoPKey, MSSBamMemoBuff> dictByPKey = new HashMap<MSSBamMemoPKey, MSSBamMemoBuff>();
    private SortedMap<MSSBamMemoByMemoContactIdxKey, SortedMap<MSSBamMemoPKey, MSSBamMemoBuff>> dictByMemoContactIdx = new TreeMap<MSSBamMemoByMemoContactIdxKey, SortedMap<MSSBamMemoPKey, MSSBamMemoBuff>>();
    private SortedMap<MSSBamMemoByUDescrIdxKey, MSSBamMemoBuff> dictByUDescrIdx = new TreeMap<MSSBamMemoByUDescrIdxKey, MSSBamMemoBuff>();

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

    public void createMemo(MSSBamAuthorization Authorization, MSSBamMemoBuff Buff) {
        MSSBamMemoPKey pkey = schema.getFactoryMemo().newPKey();

        pkey.setRequiredMemoId(schema.nextMemoIdGen());
        Buff.setRequiredMemoId(pkey.getRequiredMemoId());

        MSSBamMemoByMemoContactIdxKey keyMemoContactIdx = schema.getFactoryMemo().newMemoContactIdxKey();
        keyMemoContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        MSSBamMemoByUDescrIdxKey keyUDescrIdx = schema.getFactoryMemo().newUDescrIdxKey();
        keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription());

        // Validate unique indexes

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

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

        // Validate foreign keys

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

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamMemoPKey, MSSBamMemoBuff> subdictMemoContactIdx;
        if (dictByMemoContactIdx.containsKey(keyMemoContactIdx)) {
            subdictMemoContactIdx = dictByMemoContactIdx.get(keyMemoContactIdx);
        } else {
            subdictMemoContactIdx = new TreeMap<MSSBamMemoPKey, MSSBamMemoBuff>();
            dictByMemoContactIdx.put(keyMemoContactIdx, subdictMemoContactIdx);
        }
        subdictMemoContactIdx.put(pkey, Buff);

        dictByUDescrIdx.put(keyUDescrIdx, Buff);

    }

    public MSSBamMemoBuff readDerived(MSSBamAuthorization Authorization, MSSBamMemoPKey PKey) {
        final String S_ProcName = "MSSBamRamMemo.readDerived() ";
        MSSBamMemoPKey key = schema.getFactoryMemo().newPKey();
        key.setRequiredMemoId(PKey.getRequiredMemoId());
        MSSBamMemoBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamMemoBuff[] readDerivedByMemoContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "MSSBamRamMemo.readDerivedByMemoContactIdx() ";
        MSSBamMemoByMemoContactIdxKey key = schema.getFactoryMemo().newMemoContactIdxKey();
        key.setRequiredContactId(ContactId);

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

    public MSSBamMemoBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long ContactId,
            String Description) {
        final String S_ProcName = "MSSBamRamMemo.readDerivedByUDescrIdx() ";
        MSSBamMemoByUDescrIdxKey key = schema.getFactoryMemo().newUDescrIdxKey();
        key.setRequiredContactId(ContactId);
        key.setRequiredDescription(Description);

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

    public MSSBamMemoBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long MemoId) {
        final String S_ProcName = "MSSBamRamMemo.readDerivedByIdIdx() ";
        MSSBamMemoPKey key = schema.getFactoryMemo().newPKey();
        key.setRequiredMemoId(MemoId);

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

    public MSSBamMemoBuff readBuff(MSSBamAuthorization Authorization, MSSBamMemoPKey PKey) {
        final String S_ProcName = "MSSBamRamMemo.readBuff() ";
        MSSBamMemoBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("MMO"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamMemoBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long MemoId) {
        final String S_ProcName = "MSSBamRamMemo.readBuffByIdIdx() ";
        MSSBamMemoBuff buff = readDerivedByIdIdx(Authorization, MemoId);
        if ((buff != null) && buff.getClassCode().equals("MMO")) {
            return ((MSSBamMemoBuff) buff);
        } else {
            return (null);
        }
    }

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

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

    public void updateMemo(MSSBamAuthorization Authorization, MSSBamMemoBuff Buff) {
        MSSBamMemoPKey pkey = schema.getFactoryMemo().newPKey();
        pkey.setRequiredMemoId(Buff.getRequiredMemoId());
        MSSBamMemoBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateMemo",
                    "Existing record not found", "Memo", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateMemo", pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamMemoByMemoContactIdxKey existingKeyMemoContactIdx = schema.getFactoryMemo().newMemoContactIdxKey();
        existingKeyMemoContactIdx.setRequiredContactId(existing.getRequiredContactId());

        MSSBamMemoByMemoContactIdxKey newKeyMemoContactIdx = schema.getFactoryMemo().newMemoContactIdxKey();
        newKeyMemoContactIdx.setRequiredContactId(Buff.getRequiredContactId());

        MSSBamMemoByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryMemo().newUDescrIdxKey();
        existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        MSSBamMemoByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryMemo().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(), "updateMemo",
                        "MemoUDescrIdx", newKeyUDescrIdx);
            }
        }

        // Validate foreign keys

        {
            boolean allNull = true;

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

        // Update is valid

        SortedMap<MSSBamMemoPKey, MSSBamMemoBuff> subdict;

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

        subdict = dictByMemoContactIdx.get(existingKeyMemoContactIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByMemoContactIdx.containsKey(newKeyMemoContactIdx)) {
            subdict = dictByMemoContactIdx.get(newKeyMemoContactIdx);
        } else {
            subdict = new TreeMap<MSSBamMemoPKey, MSSBamMemoBuff>();
            dictByMemoContactIdx.put(newKeyMemoContactIdx, subdict);
        }
        subdict.put(pkey, Buff);

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

    }

    public void deleteMemo(MSSBamAuthorization Authorization, MSSBamMemoBuff Buff) {
        final String S_ProcName = "MSSBamRamMemoTable.deleteMemo() ";
        MSSBamMemoPKey pkey = schema.getFactoryMemo().newPKey();

        pkey.setRequiredMemoId(schema.nextMemoIdGen());
        MSSBamMemoBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteMemo",
                    "Existing record not found", "Memo", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteMemo", pkey);
        }
        MSSBamMemoByMemoContactIdxKey keyMemoContactIdx = schema.getFactoryMemo().newMemoContactIdxKey();
        keyMemoContactIdx.setRequiredContactId(existing.getRequiredContactId());

        MSSBamMemoByUDescrIdxKey keyUDescrIdx = schema.getFactoryMemo().newUDescrIdxKey();
        keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId());
        keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription());

        // Validate reverse foreign keys

        if (schema.getTableMemoTag().readDerivedByMemoIdx(Authorization, existing.getRequiredMemoId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteMemo",
                    "Container", "MemoTagMemo", "MemoTag", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamMemoPKey, MSSBamMemoBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByMemoContactIdx.get(keyMemoContactIdx);
        subdict.remove(pkey);

        dictByUDescrIdx.remove(keyUDescrIdx);

    }

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

    public MSSBamCursor openMemoCursorByMemoContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        MSSBamCursor cursor;
        MSSBamMemoByMemoContactIdxKey key = schema.getFactoryMemo().newMemoContactIdxKey();
        key.setRequiredContactId(ContactId);

        if (dictByMemoContactIdx.containsKey(key)) {
            SortedMap<MSSBamMemoPKey, MSSBamMemoBuff> subdictMemoContactIdx = dictByMemoContactIdx.get(key);
            cursor = new MSSBamRamMemoCursor(Authorization, schema, subdictMemoContactIdx.values());
        } else {
            cursor = new MSSBamRamMemoCursor(Authorization, schema, new ArrayList<MSSBamMemoBuff>());
        }
        return (cursor);
    }

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

    public MSSBamMemoBuff nextMemoCursor(MSSBamCursor Cursor) {
        MSSBamRamMemoCursor cursor = (MSSBamRamMemoCursor) Cursor;
        MSSBamMemoBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamMemoBuff firstMemoCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamMemoBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextMemoCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamMemoBuff nthMemoCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamMemoBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextMemoCursor(Cursor);
        }
        return (rec);
    }
}