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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamContactTable in-memory RAM DbIO implementation
 *   for Contact.
 */
public class MSSBamRamContactTable implements IMSSBamContactTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamContactPKey, MSSBamContactBuff> dictByPKey = new HashMap<MSSBamContactPKey, MSSBamContactBuff>();
    private SortedMap<MSSBamContactByContactListIdxKey, SortedMap<MSSBamContactPKey, MSSBamContactBuff>> dictByContactListIdx = new TreeMap<MSSBamContactByContactListIdxKey, SortedMap<MSSBamContactPKey, MSSBamContactBuff>>();
    private SortedMap<MSSBamContactByISOTimezoneIdxKey, SortedMap<MSSBamContactPKey, MSSBamContactBuff>> dictByISOTimezoneIdx = new TreeMap<MSSBamContactByISOTimezoneIdxKey, SortedMap<MSSBamContactPKey, MSSBamContactBuff>>();

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

    public void createContact(MSSBamAuthorization Authorization, MSSBamContactBuff Buff) {
        MSSBamContactPKey pkey = schema.getFactoryContact().newPKey();

        pkey.setRequiredContactId(schema.nextContactIdGen());
        Buff.setRequiredContactId(pkey.getRequiredContactId());

        MSSBamContactByContactListIdxKey keyContactListIdx = schema.getFactoryContact().newContactListIdxKey();
        keyContactListIdx.setRequiredContactListId(Buff.getRequiredContactListId());

        MSSBamContactByISOTimezoneIdxKey keyISOTimezoneIdx = schema.getFactoryContact().newISOTimezoneIdxKey();
        keyISOTimezoneIdx.setOptionalISOTimezoneId(Buff.getOptionalISOTimezoneId());

        // Validate unique indexes

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

        // Validate foreign keys

        {
            boolean allNull = true;
            allNull = false;
            if (!allNull) {
                if (null == schema.getTableContactList().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredContactListId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "createContact", "Container", "ContactList", "ContactList", null);
                }
            }
        }

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdictContactListIdx;
        if (dictByContactListIdx.containsKey(keyContactListIdx)) {
            subdictContactListIdx = dictByContactListIdx.get(keyContactListIdx);
        } else {
            subdictContactListIdx = new TreeMap<MSSBamContactPKey, MSSBamContactBuff>();
            dictByContactListIdx.put(keyContactListIdx, subdictContactListIdx);
        }
        subdictContactListIdx.put(pkey, Buff);

        SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdictISOTimezoneIdx;
        if (dictByISOTimezoneIdx.containsKey(keyISOTimezoneIdx)) {
            subdictISOTimezoneIdx = dictByISOTimezoneIdx.get(keyISOTimezoneIdx);
        } else {
            subdictISOTimezoneIdx = new TreeMap<MSSBamContactPKey, MSSBamContactBuff>();
            dictByISOTimezoneIdx.put(keyISOTimezoneIdx, subdictISOTimezoneIdx);
        }
        subdictISOTimezoneIdx.put(pkey, Buff);

    }

    public MSSBamContactBuff readDerived(MSSBamAuthorization Authorization, MSSBamContactPKey PKey) {
        final String S_ProcName = "MSSBamRamContact.readDerived() ";
        MSSBamContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredContactId(PKey.getRequiredContactId());
        MSSBamContactBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamContactBuff[] readDerivedByContactListIdx(MSSBamAuthorization Authorization, long ContactListId) {
        final String S_ProcName = "MSSBamRamContact.readDerivedByContactListIdx() ";
        MSSBamContactByContactListIdxKey key = schema.getFactoryContact().newContactListIdxKey();
        key.setRequiredContactListId(ContactListId);

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

    public MSSBamContactBuff[] readDerivedByISOTimezoneIdx(MSSBamAuthorization Authorization, Short ISOTimezoneId) {
        final String S_ProcName = "MSSBamRamContact.readDerivedByISOTimezoneIdx() ";
        MSSBamContactByISOTimezoneIdxKey key = schema.getFactoryContact().newISOTimezoneIdxKey();
        key.setOptionalISOTimezoneId(ISOTimezoneId);

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

    public MSSBamContactBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "MSSBamRamContact.readDerivedByIdIdx() ";
        MSSBamContactPKey key = schema.getFactoryContact().newPKey();
        key.setRequiredContactId(ContactId);

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

    public MSSBamContactBuff readBuff(MSSBamAuthorization Authorization, MSSBamContactPKey PKey) {
        final String S_ProcName = "MSSBamRamContact.readBuff() ";
        MSSBamContactBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("CTC"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamContactBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "MSSBamRamContact.readBuffByIdIdx() ";
        MSSBamContactBuff buff = readDerivedByIdIdx(Authorization, ContactId);
        if ((buff != null) && buff.getClassCode().equals("CTC")) {
            return ((MSSBamContactBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamContactBuff[] readBuffByContactListIdx(MSSBamAuthorization Authorization, long ContactListId) {
        final String S_ProcName = "MSSBamRamContact.readBuffByContactListIdx() ";
        MSSBamContactBuff buff;
        ArrayList<MSSBamContactBuff> filteredList = new ArrayList<MSSBamContactBuff>();
        MSSBamContactBuff[] buffList = readDerivedByContactListIdx(Authorization, ContactListId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CTC")) {
                filteredList.add((MSSBamContactBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamContactBuff[0]));
    }

    public MSSBamContactBuff[] readBuffByISOTimezoneIdx(MSSBamAuthorization Authorization, Short ISOTimezoneId) {
        final String S_ProcName = "MSSBamRamContact.readBuffByISOTimezoneIdx() ";
        MSSBamContactBuff buff;
        ArrayList<MSSBamContactBuff> filteredList = new ArrayList<MSSBamContactBuff>();
        MSSBamContactBuff[] buffList = readDerivedByISOTimezoneIdx(Authorization, ISOTimezoneId);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("CTC")) {
                filteredList.add((MSSBamContactBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamContactBuff[0]));
    }

    public void updateContact(MSSBamAuthorization Authorization, MSSBamContactBuff Buff) {
        MSSBamContactPKey pkey = schema.getFactoryContact().newPKey();
        pkey.setRequiredContactId(Buff.getRequiredContactId());
        MSSBamContactBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateContact",
                    "Existing record not found", "Contact", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateContact",
                    pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamContactByContactListIdxKey existingKeyContactListIdx = schema.getFactoryContact()
                .newContactListIdxKey();
        existingKeyContactListIdx.setRequiredContactListId(existing.getRequiredContactListId());

        MSSBamContactByContactListIdxKey newKeyContactListIdx = schema.getFactoryContact().newContactListIdxKey();
        newKeyContactListIdx.setRequiredContactListId(Buff.getRequiredContactListId());

        MSSBamContactByISOTimezoneIdxKey existingKeyISOTimezoneIdx = schema.getFactoryContact()
                .newISOTimezoneIdxKey();
        existingKeyISOTimezoneIdx.setOptionalISOTimezoneId(existing.getOptionalISOTimezoneId());

        MSSBamContactByISOTimezoneIdxKey newKeyISOTimezoneIdx = schema.getFactoryContact().newISOTimezoneIdxKey();
        newKeyISOTimezoneIdx.setOptionalISOTimezoneId(Buff.getOptionalISOTimezoneId());

        // Check unique indexes

        // Validate foreign keys

        {
            boolean allNull = true;

            if (allNull) {
                if (null == schema.getTableContactList().readDerivedByIdIdx(Authorization,
                        Buff.getRequiredContactListId())) {
                    throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(),
                            "updateContact", "Container", "ContactList", "ContactList", null);
                }
            }
        }

        // Update is valid

        SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdict;

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

        subdict = dictByContactListIdx.get(existingKeyContactListIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByContactListIdx.containsKey(newKeyContactListIdx)) {
            subdict = dictByContactListIdx.get(newKeyContactListIdx);
        } else {
            subdict = new TreeMap<MSSBamContactPKey, MSSBamContactBuff>();
            dictByContactListIdx.put(newKeyContactListIdx, subdict);
        }
        subdict.put(pkey, Buff);

        subdict = dictByISOTimezoneIdx.get(existingKeyISOTimezoneIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByISOTimezoneIdx.containsKey(newKeyISOTimezoneIdx)) {
            subdict = dictByISOTimezoneIdx.get(newKeyISOTimezoneIdx);
        } else {
            subdict = new TreeMap<MSSBamContactPKey, MSSBamContactBuff>();
            dictByISOTimezoneIdx.put(newKeyISOTimezoneIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteContact(MSSBamAuthorization Authorization, MSSBamContactBuff Buff) {
        final String S_ProcName = "MSSBamRamContactTable.deleteContact() ";
        MSSBamContactPKey pkey = schema.getFactoryContact().newPKey();

        pkey.setRequiredContactId(schema.nextContactIdGen());
        MSSBamContactBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteContact",
                    "Existing record not found", "Contact", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteContact",
                    pkey);
        }
        MSSBamContactByContactListIdxKey keyContactListIdx = schema.getFactoryContact().newContactListIdxKey();
        keyContactListIdx.setRequiredContactListId(existing.getRequiredContactListId());

        MSSBamContactByISOTimezoneIdxKey keyISOTimezoneIdx = schema.getFactoryContact().newISOTimezoneIdxKey();
        keyISOTimezoneIdx.setOptionalISOTimezoneId(existing.getOptionalISOTimezoneId());

        // Validate reverse foreign keys

        if (schema.getTableAddress().readDerivedByContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "Contact", "Address", pkey);
        }

        if (schema.getTableAttachment().readDerivedByContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "Contact", "Attachment", pkey);
        }

        if (schema.getTableContactTag().readDerivedByContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "ContactTagContact", "ContactTag", pkey);
        }

        if (schema.getTableMemo().readDerivedByMemoContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "MemoContact", "Memo", pkey);
        }

        if (schema.getTablePhone().readDerivedByContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "Contact", "Phone", pkey);
        }

        if (schema.getTableContactURL().readDerivedByContactIdx(Authorization,
                existing.getRequiredContactId()).length > 0) {
            throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContact",
                    "Container", "Contact", "ContactURL", pkey);
        }

        // Delete is valid

        SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByContactListIdx.get(keyContactListIdx);
        subdict.remove(pkey);

        subdict = dictByISOTimezoneIdx.get(keyISOTimezoneIdx);
        subdict.remove(pkey);

    }

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

    public MSSBamCursor openContactCursorByContactListIdx(MSSBamAuthorization Authorization, long ContactListId) {
        MSSBamCursor cursor;
        MSSBamContactByContactListIdxKey key = schema.getFactoryContact().newContactListIdxKey();
        key.setRequiredContactListId(ContactListId);

        if (dictByContactListIdx.containsKey(key)) {
            SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdictContactListIdx = dictByContactListIdx.get(key);
            cursor = new MSSBamRamContactCursor(Authorization, schema, subdictContactListIdx.values());
        } else {
            cursor = new MSSBamRamContactCursor(Authorization, schema, new ArrayList<MSSBamContactBuff>());
        }
        return (cursor);
    }

    public MSSBamCursor openContactCursorByISOTimezoneIdx(MSSBamAuthorization Authorization, Short ISOTimezoneId) {
        MSSBamCursor cursor;
        MSSBamContactByISOTimezoneIdxKey key = schema.getFactoryContact().newISOTimezoneIdxKey();
        key.setOptionalISOTimezoneId(ISOTimezoneId);

        if (dictByISOTimezoneIdx.containsKey(key)) {
            SortedMap<MSSBamContactPKey, MSSBamContactBuff> subdictISOTimezoneIdx = dictByISOTimezoneIdx.get(key);
            cursor = new MSSBamRamContactCursor(Authorization, schema, subdictISOTimezoneIdx.values());
        } else {
            cursor = new MSSBamRamContactCursor(Authorization, schema, new ArrayList<MSSBamContactBuff>());
        }
        return (cursor);
    }

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

    public MSSBamContactBuff nextContactCursor(MSSBamCursor Cursor) {
        MSSBamRamContactCursor cursor = (MSSBamRamContactCursor) Cursor;
        MSSBamContactBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamContactBuff firstContactCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamContactBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextContactCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamContactBuff nthContactCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamContactBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextContactCursor(Cursor);
        }
        return (rec);
    }
}