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

Java tutorial

Introduction

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

Source

// Description: Java 6 PostgreSQL Jdbc DbIO implementation for Attachment.

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

/*
 *   MSSBamPg8AttachmentTable PostgreSQL Jdbc DbIO implementation
 *   for Attachment.
 *
 *   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 MSSBamPg8AttachmentTable implements IMSSBamAttachmentTable {
    private MSSBamPg8Schema schema;

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

    public void createAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) {
        final String S_ProcName = "createAttachment ";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            long AttachmentId = schema.nextAttachmentIdGen();
            long ContactId = Buff.getRequiredContactId();
            String Description = Buff.getRequiredDescription();
            Short MimeTypeId = Buff.getOptionalMimeTypeId();
            String Attachment = Buff.getRequiredAttachment();
            int Revision = 1;
            String sql = "INSERT INTO mssbam110.Attachment( " + "attachmentid, " + "contactid, " + "description, "
                    + "mimetypeid, " + "attachment" + ", revision )" + "VALUES ( " + AttachmentId + ", " + ContactId
                    + ", " + MSSBamPg8Schema.getQuotedString(Description) + ", "
                    + ((MimeTypeId == null) ? "null" : MimeTypeId.toString()) + ", "
                    + MSSBamPg8Schema.getQuotedString(Attachment) + ", " + 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.setRequiredAttachmentId(AttachmentId);
            Buff.setRequiredRevision(Revision);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public final static String S_sqlSelectAttachmentDistinctClassCode = "SELECT " + "DISTINCT attc.ClassCode "
            + "FROM mssbam110.Attachment AS attc ";

    public final static String S_sqlSelectAttachmentBuff = "SELECT " + "attc.AttachmentId, " + "attc.ContactId, "
            + "attc.Description, " + "attc.MimeTypeId, " + "attc.Attachment, " + "attc.Revision "
            + "FROM mssbam110.Attachment AS attc ";

    protected MSSBamAttachmentBuff unpackAttachmentResultSetToBuff(ResultSet resultSet) throws SQLException {
        final String S_ProcName = "unpackAttachmentResultSetToBuff";
        int idxcol = 1;
        MSSBamAttachmentBuff buff = schema.getFactoryAttachment().newBuff();
        buff.setRequiredAttachmentId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredContactId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredDescription(resultSet.getString(idxcol));
        idxcol++;
        {
            short colVal = resultSet.getShort(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalMimeTypeId(null);
            } else {
                buff.setOptionalMimeTypeId(colVal);
            }
        }
        idxcol++;
        buff.setRequiredAttachment(resultSet.getString(idxcol));
        idxcol++;
        buff.setRequiredRevision(resultSet.getInt(idxcol));
        return (buff);
    }

    public MSSBamAttachmentBuff readDerived(MSSBamAuthorization Authorization, MSSBamAttachmentPKey PKey) {
        final String S_ProcName = "readDerived()";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamAttachmentBuff buff;
        long AttachmentId = PKey.getRequiredAttachmentId();
        buff = readBuff(Authorization, PKey);
        return (buff);
    }

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

    public MSSBamAttachmentBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long AttachmentId) {
        final String S_ProcName = "MSSBamPg8AttachmentTable.readDerivedByIdIdx() ";
        MSSBamAttachmentBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        buff = readBuffByIdIdx(Authorization, AttachmentId);
        return (buff);
    }

    public MSSBamAttachmentBuff[] readDerivedByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "readDerivedByContactIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamAttachmentBuff[] buffList = readBuffByContactIdx(Authorization, ContactId);
        return (buffList);

    }

    public MSSBamAttachmentBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long ContactId,
            String Description) {
        final String S_ProcName = "MSSBamPg8AttachmentTable.readDerivedByUDescrIdx() ";
        MSSBamAttachmentBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        buff = readBuffByUDescrIdx(Authorization, ContactId, Description);
        return (buff);
    }

    public MSSBamAttachmentBuff[] readDerivedByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) {
        final String S_ProcName = "readDerivedByMimeTypeIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamAttachmentBuff[] buffList = readBuffByMimeTypeIdx(Authorization, MimeTypeId);
        return (buffList);

    }

    public MSSBamAttachmentBuff readBuff(MSSBamAuthorization Authorization, MSSBamAttachmentPKey PKey) {
        final String S_ProcName = "readBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            long AttachmentId = PKey.getRequiredAttachmentId();
            String sql = S_sqlSelectAttachmentBuff + "WHERE " + "attc.AttachmentId = " + Long.toString(AttachmentId)
                    + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(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 MSSBamAttachmentBuff[] 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_sqlSelectAttachmentBuff + "ORDER BY " + "attc.AttachmentId ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamAttachmentBuff> buffList = new ArrayList<MSSBamAttachmentBuff>();
            while (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamAttachmentBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamAttachmentBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long AttachmentId) {
        final String S_ProcName = "readBuffByIdIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAttachmentBuff + "WHERE " + "attc.AttachmentId = " + Long.toString(AttachmentId)
                    + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(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 MSSBamAttachmentBuff[] readBuffByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        final String S_ProcName = "readBuffByContactIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAttachmentBuff + "WHERE " + "attc.ContactId = " + Long.toString(ContactId) + " "
                    + "ORDER BY " + "attc.AttachmentId ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamAttachmentBuff> buffList = new ArrayList<MSSBamAttachmentBuff>();
            while (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamAttachmentBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamAttachmentBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, long ContactId,
            String Description) {
        final String S_ProcName = "readBuffByUDescrIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAttachmentBuff + "WHERE " + "attc.ContactId = " + Long.toString(ContactId) + " "
                    + "AND " + "attc.Description = " + MSSBamPg8Schema.getQuotedString(Description) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(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 MSSBamAttachmentBuff[] readBuffByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) {
        final String S_ProcName = "readBuffByMimeTypeIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectAttachmentBuff + "WHERE "
                    + ((MimeTypeId == null) ? "attc.MimeTypeId is null "
                            : "attc.MimeTypeId = " + MimeTypeId.toString() + " ")
                    + "ORDER BY " + "attc.AttachmentId ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamAttachmentBuff> buffList = new ArrayList<MSSBamAttachmentBuff>();
            while (resultSet.next()) {
                MSSBamAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamAttachmentBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void updateAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) {
        final String S_ProcName = "updateAttachment";
        try {
            Connection cnx = schema.getCnx();
            long AttachmentId = Buff.getRequiredAttachmentId();
            long ContactId = Buff.getRequiredContactId();
            String Description = Buff.getRequiredDescription();
            Short MimeTypeId = Buff.getOptionalMimeTypeId();
            String Attachment = Buff.getRequiredAttachment();

            int Revision = Buff.getRequiredRevision();
            MSSBamAttachmentBuff readBuff = readBuffByIdIdx(Authorization, AttachmentId);
            int oldRevision = readBuff.getRequiredRevision();
            if (oldRevision != Revision) {
                throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                        Buff);
            }
            int newRevision = Revision + 1;
            String sql = "UPDATE mssbam110.Attachment " + "SET " + "AttachmentId = "
                    + MSSBamPg8Schema.getInt64String(AttachmentId) + ", " + "ContactId = "
                    + MSSBamPg8Schema.getInt64String(ContactId) + ", " + "Description = "
                    + MSSBamPg8Schema.getQuotedString(Description) + ", " + "MimeTypeId = "
                    + ((MimeTypeId != null) ? MSSBamPg8Schema.getInt16String(MimeTypeId) : "null") + ", "
                    + "Attachment = " + MSSBamPg8Schema.getQuotedString(Attachment) + ", " + "Revision = "
                    + Integer.toString(newRevision) + " " + "WHERE " + "AttachmentId = "
                    + Long.toString(AttachmentId) + " " + "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 deleteAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) {
        final String S_ProcName = "deleteAttachment";
        try {
            Connection cnx = schema.getCnx();
            long AttachmentId = Buff.getRequiredAttachmentId();
            long ContactId = Buff.getRequiredContactId();
            String Description = Buff.getRequiredDescription();
            Short MimeTypeId = Buff.getOptionalMimeTypeId();
            String Attachment = Buff.getRequiredAttachment();

            int Revision = Buff.getRequiredRevision();
            MSSBamAttachmentBuff readBuff = readBuffByIdIdx(Authorization, AttachmentId);
            int oldRevision = readBuff.getRequiredRevision();
            if (oldRevision != Revision) {
                throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                        Buff);
            }
            String sql = "DELETE FROM mssbam110.Attachment " + "WHERE " + "AttachmentId = "
                    + Long.toString(AttachmentId) + " " + "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 openAttachmentCursorAll(MSSBamAuthorization Authorization) {
        String sql = "SELECT " + "attc.AttachmentId, " + "attc.ContactId, " + "attc.Description, "
                + "attc.MimeTypeId, " + "attc.Attachment, " + "attc.Revision "
                + "FROM mssbam110.Attachment AS attc " + "ORDER BY " + "attc.AttachmentId ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openAttachmentCursorByContactIdx(MSSBamAuthorization Authorization, long ContactId) {
        String sql = "SELECT " + "attc.AttachmentId, " + "attc.ContactId, " + "attc.Description, "
                + "attc.MimeTypeId, " + "attc.Attachment, " + "attc.Revision "
                + "FROM mssbam110.Attachment AS attc " + "WHERE " + "attc.ContactId = " + Long.toString(ContactId)
                + " " + "ORDER BY " + "attc.AttachmentId ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openAttachmentCursorByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) {
        String sql = "SELECT " + "attc.AttachmentId, " + "attc.ContactId, " + "attc.Description, "
                + "attc.MimeTypeId, " + "attc.Attachment, " + "attc.Revision "
                + "FROM mssbam110.Attachment AS attc " + "WHERE "
                + ((MimeTypeId == null) ? "attc.MimeTypeId is null "
                        : "attc.MimeTypeId = " + MimeTypeId.toString() + " ")
                + "ORDER BY " + "attc.AttachmentId ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

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

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

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

    public MSSBamAttachmentBuff firstAttachmentCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamAttachmentBuff buff = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextAttachmentCursor(Cursor);
        }
        return (buff);
    }

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

    public MSSBamAttachmentBuff nthAttachmentCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamAttachmentBuff buff = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextAttachmentCursor(Cursor);
        }
        return (buff);
    }
}