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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamRamAccessFrequencyTable in-memory RAM DbIO implementation
 *   for AccessFrequency.
 */
public class MSSBamRamAccessFrequencyTable implements IMSSBamAccessFrequencyTable {
    private MSSBamBLRamSchema schema;
    private Map<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff> dictByPKey = new HashMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff>();
    private SortedMap<MSSBamAccessFrequencyByUNameIdxKey, SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff>> dictByUNameIdx = new TreeMap<MSSBamAccessFrequencyByUNameIdxKey, SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff>>();

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

    public void createAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        MSSBamAccessFrequencyPKey pkey = schema.getFactoryAccessFrequency().newPKey();

        pkey.setRequiredId(Buff.getRequiredId());
        Buff.setRequiredId(pkey.getRequiredId());

        MSSBamAccessFrequencyByUNameIdxKey keyUNameIdx = schema.getFactoryAccessFrequency().newUNameIdxKey();
        keyUNameIdx.setRequiredName(Buff.getRequiredName());

        // Validate unique indexes

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

        // Validate foreign keys

        // Proceed with adding the new record

        dictByPKey.put(pkey, Buff);

        SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff> subdictUNameIdx;
        if (dictByUNameIdx.containsKey(keyUNameIdx)) {
            subdictUNameIdx = dictByUNameIdx.get(keyUNameIdx);
        } else {
            subdictUNameIdx = new TreeMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff>();
            dictByUNameIdx.put(keyUNameIdx, subdictUNameIdx);
        }
        subdictUNameIdx.put(pkey, Buff);

    }

    public MSSBamAccessFrequencyBuff readDerived(MSSBamAuthorization Authorization,
            MSSBamAccessFrequencyPKey PKey) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readDerived() ";
        MSSBamAccessFrequencyPKey key = schema.getFactoryAccessFrequency().newPKey();
        key.setRequiredId(PKey.getRequiredId());
        MSSBamAccessFrequencyBuff buff;
        if (dictByPKey.containsKey(key)) {
            buff = dictByPKey.get(key);
        } else {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamAccessFrequencyBuff[] readDerivedByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readDerivedByUNameIdx() ";
        MSSBamAccessFrequencyByUNameIdxKey key = schema.getFactoryAccessFrequency().newUNameIdxKey();
        key.setRequiredName(Name);

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

    public MSSBamAccessFrequencyBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, short Id) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readDerivedByIdIdx() ";
        MSSBamAccessFrequencyPKey key = schema.getFactoryAccessFrequency().newPKey();
        key.setRequiredId(Id);

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

    public MSSBamAccessFrequencyBuff readBuff(MSSBamAuthorization Authorization, MSSBamAccessFrequencyPKey PKey) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readBuff() ";
        MSSBamAccessFrequencyBuff buff = readDerived(Authorization, PKey);
        if ((buff != null) && (!buff.getClassCode().equals("AFRQ"))) {
            buff = null;
        }
        return (buff);
    }

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

    public MSSBamAccessFrequencyBuff readBuffByIdIdx(MSSBamAuthorization Authorization, short Id) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readBuffByIdIdx() ";
        MSSBamAccessFrequencyBuff buff = readDerivedByIdIdx(Authorization, Id);
        if ((buff != null) && buff.getClassCode().equals("AFRQ")) {
            return ((MSSBamAccessFrequencyBuff) buff);
        } else {
            return (null);
        }
    }

    public MSSBamAccessFrequencyBuff[] readBuffByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        final String S_ProcName = "MSSBamRamAccessFrequency.readBuffByUNameIdx() ";
        MSSBamAccessFrequencyBuff buff;
        ArrayList<MSSBamAccessFrequencyBuff> filteredList = new ArrayList<MSSBamAccessFrequencyBuff>();
        MSSBamAccessFrequencyBuff[] buffList = readDerivedByUNameIdx(Authorization, Name);
        for (int idx = 0; idx < buffList.length; idx++) {
            buff = buffList[idx];
            if ((buff != null) && buff.getClassCode().equals("AFRQ")) {
                filteredList.add((MSSBamAccessFrequencyBuff) buff);
            }
        }
        return (filteredList.toArray(new MSSBamAccessFrequencyBuff[0]));
    }

    public void updateAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        MSSBamAccessFrequencyPKey pkey = schema.getFactoryAccessFrequency().newPKey();
        pkey.setRequiredId(Buff.getRequiredId());
        MSSBamAccessFrequencyBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(),
                    "updateAccessFrequency", "Existing record not found", "AccessFrequency", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(),
                    "updateAccessFrequency", pkey);
        }
        Buff.setRequiredRevision(Buff.getRequiredRevision() + 1);
        MSSBamAccessFrequencyByUNameIdxKey existingKeyUNameIdx = schema.getFactoryAccessFrequency()
                .newUNameIdxKey();
        existingKeyUNameIdx.setRequiredName(existing.getRequiredName());

        MSSBamAccessFrequencyByUNameIdxKey newKeyUNameIdx = schema.getFactoryAccessFrequency().newUNameIdxKey();
        newKeyUNameIdx.setRequiredName(Buff.getRequiredName());

        // Check unique indexes

        // Validate foreign keys

        // Update is valid

        SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff> subdict;

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

        subdict = dictByUNameIdx.get(existingKeyUNameIdx);
        if (subdict != null) {
            subdict.remove(pkey);
        }
        if (dictByUNameIdx.containsKey(newKeyUNameIdx)) {
            subdict = dictByUNameIdx.get(newKeyUNameIdx);
        } else {
            subdict = new TreeMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff>();
            dictByUNameIdx.put(newKeyUNameIdx, subdict);
        }
        subdict.put(pkey, Buff);

    }

    public void deleteAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        final String S_ProcName = "MSSBamRamAccessFrequencyTable.deleteAccessFrequency() ";
        MSSBamAccessFrequencyPKey pkey = schema.getFactoryAccessFrequency().newPKey();

        pkey.setRequiredId(Buff.getRequiredId());
        MSSBamAccessFrequencyBuff existing = dictByPKey.get(pkey);
        if (existing == null) {
            throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(),
                    "deleteAccessFrequency", "Existing record not found", "AccessFrequency", pkey);
        }
        if (existing.getRequiredRevision() != Buff.getRequiredRevision()) {
            throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(),
                    "deleteAccessFrequency", pkey);
        }
        MSSBamAccessFrequencyByUNameIdxKey keyUNameIdx = schema.getFactoryAccessFrequency().newUNameIdxKey();
        keyUNameIdx.setRequiredName(existing.getRequiredName());

        // Validate reverse foreign keys

        // Delete is valid

        SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff> subdict;

        dictByPKey.remove(pkey);

        subdict = dictByUNameIdx.get(keyUNameIdx);
        subdict.remove(pkey);

    }

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

    public MSSBamCursor openAccessFrequencyCursorByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        MSSBamCursor cursor;
        MSSBamAccessFrequencyByUNameIdxKey key = schema.getFactoryAccessFrequency().newUNameIdxKey();
        key.setRequiredName(Name);

        if (dictByUNameIdx.containsKey(key)) {
            SortedMap<MSSBamAccessFrequencyPKey, MSSBamAccessFrequencyBuff> subdictUNameIdx = dictByUNameIdx
                    .get(key);
            cursor = new MSSBamRamAccessFrequencyCursor(Authorization, schema, subdictUNameIdx.values());
        } else {
            cursor = new MSSBamRamAccessFrequencyCursor(Authorization, schema,
                    new ArrayList<MSSBamAccessFrequencyBuff>());
        }
        return (cursor);
    }

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

    public MSSBamAccessFrequencyBuff nextAccessFrequencyCursor(MSSBamCursor Cursor) {
        MSSBamRamAccessFrequencyCursor cursor = (MSSBamRamAccessFrequencyCursor) Cursor;
        MSSBamAccessFrequencyBuff rec = cursor.getCursor().next();
        cursor.setRowIdx(cursor.getRowIdx() + 1);
        return (rec);
    }

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

    public MSSBamAccessFrequencyBuff firstAccessFrequencyCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamAccessFrequencyBuff rec = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAccessFrequencyCursor(Cursor);
        }
        return (rec);
    }

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

    public MSSBamAccessFrequencyBuff nthAccessFrequencyCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamAccessFrequencyBuff rec = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            rec = nextAccessFrequencyCursor(Cursor);
        }
        return (rec);
    }
}