net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8AccessFrequencyTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.v1_10.MSSBamPg8.MSSBamPg8AccessFrequencyTable.java

Source

// Description: Java 6 PostgreSQL Jdbc 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.MSSBamPg8;

import java.math.*;
import java.sql.*;
import java.text.*;
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.*;

/*
 *   MSSBamPg8AccessFrequencyTable PostgreSQL Jdbc DbIO implementation
 *   for AccessFrequency.
 *
 *   Data redaction is the responsibility of another layer.  The raw database
 *   interface returns everything regardless of whether the end user is
 *   authorized to see the data.  A redaction layer replaces protected/redacted
 *   buffs with default values.  They should not be included in client-side
 *   filter sets, and the network redaction layer should actually eliminate them
 *   before transmitting data to the client.  The client should never see
 *   redacted data in order to comply with data privacy regulations in Canada
 *   and the US.
 */
public class MSSBamPg8AccessFrequencyTable implements IMSSBamAccessFrequencyTable {
    private MSSBamPg8Schema schema;

    public MSSBamPg8AccessFrequencyTable(MSSBamPg8Schema argSchema) {
        schema = argSchema;
    }

    public void createAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        final String S_ProcName = "createAccessFrequency ";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            short Id = Buff.getRequiredId();
            String Name = Buff.getRequiredName();
            int Revision = 1;
            String sql = "INSERT INTO mssbam110.accfreq( " + "id, " + "name" + ", revision )" + "VALUES ( " + Id
                    + ", " + MSSBamPg8Schema.getQuotedString(Name) + ", " + Integer.toString(Revision) + " )";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            int rowsAffected = stmt.executeUpdate(sql);
            if (rowsAffected != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by insert, not " + rowsAffected);
            }
            Buff.setRequiredRevision(Revision);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public final static String S_sqlSelectAccessFrequencyDistinctClassCode = "SELECT " + "DISTINCT afrq.ClassCode "
            + "FROM mssbam110.accfreq AS afrq ";

    public final static String S_sqlSelectAccessFrequencyBuff = "SELECT " + "afrq.Id, " + "afrq.Name, "
            + "afrq.Revision " + "FROM mssbam110.accfreq AS afrq ";

    protected MSSBamAccessFrequencyBuff unpackAccessFrequencyResultSetToBuff(ResultSet resultSet)
            throws SQLException {
        final String S_ProcName = "unpackAccessFrequencyResultSetToBuff";
        int idxcol = 1;
        MSSBamAccessFrequencyBuff buff = schema.getFactoryAccessFrequency().newBuff();
        buff.setRequiredId(resultSet.getShort(idxcol));
        idxcol++;
        buff.setRequiredName(resultSet.getString(idxcol));
        idxcol++;
        buff.setRequiredRevision(resultSet.getInt(idxcol));
        return (buff);
    }

    public MSSBamAccessFrequencyBuff readDerived(MSSBamAuthorization Authorization,
            MSSBamAccessFrequencyPKey PKey) {
        final String S_ProcName = "readDerived()";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamAccessFrequencyBuff buff;
        short Id = PKey.getRequiredId();
        buff = readBuff(Authorization, PKey);
        return (buff);
    }

    public MSSBamAccessFrequencyBuff[] readAllDerived(MSSBamAuthorization Authorization) {
        final String S_ProcName = "readAllDerived";
        MSSBamAccessFrequencyBuff[] buffArray;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        buffArray = readAllBuff(Authorization);
        return (buffArray);
    }

    public MSSBamAccessFrequencyBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, short Id) {
        final String S_ProcName = "MSSBamPg8AccessFrequencyTable.readDerivedByIdIdx() ";
        MSSBamAccessFrequencyBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        buff = readBuffByIdIdx(Authorization, Id);
        return (buff);
    }

    public MSSBamAccessFrequencyBuff[] readDerivedByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        final String S_ProcName = "readDerivedByUNameIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamAccessFrequencyBuff[] buffList = readBuffByUNameIdx(Authorization, Name);
        return (buffList);

    }

    public MSSBamAccessFrequencyBuff readBuff(MSSBamAuthorization Authorization, MSSBamAccessFrequencyPKey PKey) {
        final String S_ProcName = "readBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            short Id = PKey.getRequiredId();
            String sql = S_sqlSelectAccessFrequencyBuff + "WHERE " + "afrq.Id = " + Short.toString(Id) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamAccessFrequencyBuff buff = unpackAccessFrequencyResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamAccessFrequencyBuff[] readAllBuff(MSSBamAuthorization Authorization) {
        final String S_ProcName = "readAllBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAccessFrequencyBuff + "ORDER BY " + "afrq.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamAccessFrequencyBuff> buffList = new ArrayList<MSSBamAccessFrequencyBuff>();
            while (resultSet.next()) {
                MSSBamAccessFrequencyBuff buff = unpackAccessFrequencyResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamAccessFrequencyBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamAccessFrequencyBuff readBuffByIdIdx(MSSBamAuthorization Authorization, short Id) {
        final String S_ProcName = "readBuffByIdIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAccessFrequencyBuff + "WHERE " + "afrq.Id = " + Short.toString(Id) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamAccessFrequencyBuff buff = unpackAccessFrequencyResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamAccessFrequencyBuff[] readBuffByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        final String S_ProcName = "readBuffByUNameIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAccessFrequencyBuff + "WHERE " + "afrq.Name = "
                    + MSSBamPg8Schema.getQuotedString(Name) + " " + "ORDER BY " + "afrq.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamAccessFrequencyBuff> buffList = new ArrayList<MSSBamAccessFrequencyBuff>();
            while (resultSet.next()) {
                MSSBamAccessFrequencyBuff buff = unpackAccessFrequencyResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamAccessFrequencyBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void updateAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        final String S_ProcName = "updateAccessFrequency";
        try {
            Connection cnx = schema.getCnx();
            short Id = Buff.getRequiredId();
            String Name = Buff.getRequiredName();

            int Revision = Buff.getRequiredRevision();
            MSSBamAccessFrequencyBuff readBuff = readBuffByIdIdx(Authorization, Id);
            int oldRevision = readBuff.getRequiredRevision();
            if (oldRevision != Revision) {
                throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                        Buff);
            }
            int newRevision = Revision + 1;
            String sql = "UPDATE mssbam110.accfreq " + "SET " + "Id = " + MSSBamPg8Schema.getInt16String(Id) + ", "
                    + "Name = " + MSSBamPg8Schema.getQuotedString(Name) + ", " + "Revision = "
                    + Integer.toString(newRevision) + " " + "WHERE " + "Id = " + Short.toString(Id) + " " + "AND "
                    + "Revision = " + Integer.toString(Revision);
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            int rowsAffected = stmt.executeUpdate(sql);
            if (rowsAffected != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by update, not " + rowsAffected);
            }
            Buff.setRequiredRevision(newRevision);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void deleteAccessFrequency(MSSBamAuthorization Authorization, MSSBamAccessFrequencyBuff Buff) {
        final String S_ProcName = "deleteAccessFrequency";
        try {
            Connection cnx = schema.getCnx();
            short Id = Buff.getRequiredId();
            String Name = Buff.getRequiredName();

            int Revision = Buff.getRequiredRevision();
            MSSBamAccessFrequencyBuff readBuff = readBuffByIdIdx(Authorization, Id);
            int oldRevision = readBuff.getRequiredRevision();
            if (oldRevision != Revision) {
                throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                        Buff);
            }
            String sql = "DELETE FROM mssbam110.accfreq " + "WHERE " + "Id = " + Short.toString(Id) + " " + "AND "
                    + "Revision = " + Integer.toString(Revision);
            ;
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            int rowsAffected = stmt.executeUpdate(sql);
            if (rowsAffected != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by delete, not " + rowsAffected);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamCursor openAccessFrequencyCursorAll(MSSBamAuthorization Authorization) {
        String sql = "SELECT " + "afrq.Id, " + "afrq.Name, " + "afrq.Revision " + "FROM mssbam110.accfreq AS afrq "
                + "ORDER BY " + "afrq.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openAccessFrequencyCursorByUNameIdx(MSSBamAuthorization Authorization, String Name) {
        String sql = "SELECT " + "afrq.Id, " + "afrq.Name, " + "afrq.Revision " + "FROM mssbam110.accfreq AS afrq "
                + "WHERE " + "afrq.Name = " + MSSBamPg8Schema.getQuotedString(Name) + " " + "ORDER BY "
                + "afrq.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public void closeAccessFrequencyCursor(MSSBamCursor Cursor) {
        try {
            Cursor.getResultSet().close();
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), "closeAccessFrequencyCursor", e);
        }
    }

    public MSSBamAccessFrequencyBuff nextAccessFrequencyCursor(MSSBamCursor Cursor) {
        final String S_ProcName = "nextAccessFrequencyCursor";
        try {
            ResultSet resultSet = Cursor.getResultSet();
            if (!resultSet.next()) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "No more results available");
            }
            MSSBamAccessFrequencyBuff buff = unpackAccessFrequencyResultSetToBuff(resultSet);
            return (buff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

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

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

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

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