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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamPg8ChainTable PostgreSQL Jdbc DbIO implementation
 *   for Chain.
 *
 *   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 MSSBamPg8ChainTable implements IMSSBamChainTable {
    private MSSBamPg8Schema schema;

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

    public void createChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        final String S_ProcName = "createChain ";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            long TableId = Buff.getRequiredTableId();
            String Suffix = Buff.getOptionalSuffix();
            Long PrevRelationId = Buff.getOptionalPrevRelationId();
            Long NextRelationId = Buff.getOptionalNextRelationId();
            String sql = "INSERT INTO mssbam110.chain_def( " + "id, " + "tableid, " + "suffix, "
                    + "prevrelationid, " + "nextrelationid" + " )" + "VALUES ( " + Id + ", " + TableId + ", "
                    + MSSBamPg8Schema.getQuotedString(Suffix) + ", "
                    + ((PrevRelationId == null) ? "null" : PrevRelationId.toString()) + ", "
                    + ((NextRelationId == null) ? "null" : NextRelationId.toString()) + " )";
            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);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public final static String S_sqlSelectChainDistinctClassCode = "SELECT " + "DISTINCT anyo.ClassCode "
            + "FROM mssbam110.any_obj AS anyo " + "INNER JOIN mssbam110.chain_def chn ON " + "chn.Id = anyo.Id ";

    public final static String S_sqlSelectChainBuff = "SELECT " + "anyo.ClassCode, " + "anyo.Id, "
            + "anyo.TenantId, " + "anyo.ScopeId, " + "anyo.Name, " + "anyo.short_name, " + "anyo.Label, "
            + "anyo.short_descr, " + "anyo.descr, " + "anyo.AuthorId, " + "chn.TableId, " + "chn.Suffix, "
            + "chn.PrevRelationId, " + "chn.NextRelationId, " + "anyo.Revision " + "FROM mssbam110.any_obj AS anyo "
            + "INNER JOIN mssbam110.chain_def chn ON " + "chn.Id = anyo.Id ";

    protected MSSBamChainBuff unpackChainResultSetToBuff(ResultSet resultSet) throws SQLException {
        final String S_ProcName = "unpackChainResultSetToBuff";
        int idxcol = 1;
        String classCode = resultSet.getString(idxcol);
        idxcol++;
        MSSBamChainBuff buff;
        if (classCode.equals("CHN")) {
            buff = schema.getFactoryChain().newBuff();
        } else {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Unrecognized class code \"" + classCode + "\"");
        }
        buff.setRequiredId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredTenantId(resultSet.getLong(idxcol));
        idxcol++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalScopeId(null);
            } else {
                buff.setOptionalScopeId(colVal);
            }
        }
        idxcol++;
        buff.setRequiredName(resultSet.getString(idxcol));
        idxcol++;
        {
            String colVal = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalShortName(null);
            } else {
                buff.setOptionalShortName(colVal);
            }
        }
        idxcol++;
        {
            String colVal = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalLabel(null);
            } else {
                buff.setOptionalLabel(colVal);
            }
        }
        idxcol++;
        {
            String colVal = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalShortDescription(null);
            } else {
                buff.setOptionalShortDescription(colVal);
            }
        }
        idxcol++;
        {
            String colVal = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalDescription(null);
            } else {
                buff.setOptionalDescription(colVal);
            }
        }
        idxcol++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalAuthorId(null);
            } else {
                buff.setOptionalAuthorId(colVal);
            }
        }
        idxcol++;
        buff.setRequiredTableId(resultSet.getLong(idxcol));
        idxcol++;
        {
            String colVal = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalSuffix(null);
            } else {
                buff.setOptionalSuffix(colVal);
            }
        }
        idxcol++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalPrevRelationId(null);
            } else {
                buff.setOptionalPrevRelationId(colVal);
            }
        }
        idxcol++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalNextRelationId(null);
            } else {
                buff.setOptionalNextRelationId(colVal);
            }
        }
        idxcol++;
        buff.setRequiredRevision(resultSet.getInt(idxcol));
        return (buff);
    }

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

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

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

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

    }

    public MSSBamChainBuff[] readDerivedByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "readDerivedByScopeIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamChainBuff[] buffList = readBuffByScopeIdx(Authorization, ScopeId);
        return (buffList);

    }

    public MSSBamChainBuff[] readDerivedByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "readDerivedByAuthorIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamChainBuff[] buffList = readBuffByAuthorIdx(Authorization, AuthorId);
        return (buffList);

    }

    public MSSBamChainBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) {
        final String S_ProcName = "MSSBamPg8ChainTable.readDerivedByUNameIdx() ";
        MSSBamChainBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        buff = readBuffByUNameIdx(Authorization, ScopeId, Name);
        return (buff);
    }

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

    }

    public MSSBamChainBuff[] readDerivedByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        final String S_ProcName = "readDerivedByPrevRelIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamChainBuff[] buffList = readBuffByPrevRelIdx(Authorization, PrevRelationId);
        return (buffList);

    }

    public MSSBamChainBuff[] readDerivedByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        final String S_ProcName = "readDerivedByNextRelIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamChainBuff[] buffList = readBuffByNextRelIdx(Authorization, NextRelationId);
        return (buffList);

    }

    public MSSBamChainBuff readBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey 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 Id = PKey.getRequiredId();
            String sql = S_sqlSelectChainBuff + "WHERE " + "anyo.Id = " + Long.toString(Id) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(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 MSSBamChainBuff[] 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_sqlSelectChainBuff + "WHERE " + "anyo.ClassCode = 'CHN' " + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

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

    public MSSBamChainBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "readBuffByScopeIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE "
                    + ((ScopeId == null) ? "anyo.ScopeId is null " : "anyo.ScopeId = " + ScopeId.toString() + " ")
                    + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamChainBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "readBuffByAuthorIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE " + ((AuthorId == null) ? "anyo.AuthorId is null "
                    : "anyo.AuthorId = " + AuthorId.toString() + " ") + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamChainBuff readBuffByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) {
        final String S_ProcName = "readBuffByUNameIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE "
                    + ((ScopeId == null) ? "anyo.ScopeId is null " : "anyo.ScopeId = " + ScopeId.toString() + " ")
                    + "AND " + "anyo.Name = " + MSSBamPg8Schema.getQuotedString(Name) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(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 MSSBamChainBuff[] readBuffByTableIdx(MSSBamAuthorization Authorization, long TableId) {
        final String S_ProcName = "readBuffByTableIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE " + "chn.TableId = " + Long.toString(TableId) + " "
                    + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamChainBuff[] readBuffByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        final String S_ProcName = "readBuffByPrevRelIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE "
                    + ((PrevRelationId == null) ? "chn.PrevRelationId is null "
                            : "chn.PrevRelationId = " + PrevRelationId.toString() + " ")
                    + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamChainBuff[] readBuffByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        final String S_ProcName = "readBuffByNextRelIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectChainBuff + "WHERE "
                    + ((NextRelationId == null) ? "chn.NextRelationId is null "
                            : "chn.NextRelationId = " + NextRelationId.toString() + " ")
                    + "ORDER BY " + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamChainBuff> buffList = new ArrayList<MSSBamChainBuff>();
            while (resultSet.next()) {
                MSSBamChainBuff buff = unpackChainResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamChainBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void updateChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        final String S_ProcName = "updateChain";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            long TableId = Buff.getRequiredTableId();
            String Suffix = Buff.getOptionalSuffix();
            Long PrevRelationId = Buff.getOptionalPrevRelationId();
            Long NextRelationId = Buff.getOptionalNextRelationId();

            String sql = "UPDATE mssbam110.chain_def " + "SET " + "Id = " + MSSBamPg8Schema.getInt64String(Id)
                    + ", " + "TableId = " + MSSBamPg8Schema.getInt64String(TableId) + ", " + "Suffix = "
                    + ((Suffix != null) ? MSSBamPg8Schema.getQuotedString(Suffix) : "null") + ", "
                    + "PrevRelationId = "
                    + ((PrevRelationId != null) ? MSSBamPg8Schema.getInt64String(PrevRelationId) : "null") + ", "
                    + "NextRelationId = "
                    + ((NextRelationId != null) ? MSSBamPg8Schema.getInt64String(NextRelationId) : "null") + " "
                    + "WHERE " + "Id = " + Long.toString(Id) + " ";
            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);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void deleteChain(MSSBamAuthorization Authorization, MSSBamChainBuff Buff) {
        final String S_ProcName = "deleteChain";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            long TableId = Buff.getRequiredTableId();
            String Suffix = Buff.getOptionalSuffix();
            Long PrevRelationId = Buff.getOptionalPrevRelationId();
            Long NextRelationId = Buff.getOptionalNextRelationId();

            String sql = "DELETE FROM mssbam110.chain_def " + "WHERE " + "Id = " + Long.toString(Id) + " ";
            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 openChainCursorAll(MSSBamAuthorization Authorization) {
        String sql = "SELECT " + "anyo.ClassCode, " + "chn.Id, " + "chn.TableId, " + "chn.Suffix, "
                + "chn.PrevRelationId, " + "chn.NextRelationId, " + "chn.Revision "
                + "FROM mssbam110.chain_def AS chn " + "INNER JOIN mssbam110.any_obj anyo ON "

                + "chn.Id = anyo.Id " + "ORDER BY " + "anyo.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openChainCursorByTableIdx(MSSBamAuthorization Authorization, long TableId) {
        String sql = "SELECT " + "anyo.ClassCode, " + "chn.Id, " + "chn.TableId, " + "chn.Suffix, "
                + "chn.PrevRelationId, " + "chn.NextRelationId, " + "chn.Revision "
                + "FROM mssbam110.chain_def AS chn " + "INNER JOIN mssbam110.any_obj anyo ON "

                + "chn.Id = anyo.Id " + "WHERE " + "chn.TableId = " + Long.toString(TableId) + " " + "ORDER BY "
                + "chn.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openChainCursorByPrevRelIdx(MSSBamAuthorization Authorization, Long PrevRelationId) {
        String sql = "SELECT " + "anyo.ClassCode, " + "chn.Id, " + "chn.TableId, " + "chn.Suffix, "
                + "chn.PrevRelationId, " + "chn.NextRelationId, " + "chn.Revision "
                + "FROM mssbam110.chain_def AS chn " + "INNER JOIN mssbam110.any_obj anyo ON "

                + "chn.Id = anyo.Id " + "WHERE " + ((PrevRelationId == null) ? "chn.PrevRelationId is null "
                        : "chn.PrevRelationId = " + PrevRelationId.toString() + " ")
                + "ORDER BY " + "chn.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

    public MSSBamCursor openChainCursorByNextRelIdx(MSSBamAuthorization Authorization, Long NextRelationId) {
        String sql = "SELECT " + "anyo.ClassCode, " + "chn.Id, " + "chn.TableId, " + "chn.Suffix, "
                + "chn.PrevRelationId, " + "chn.NextRelationId, " + "chn.Revision "
                + "FROM mssbam110.chain_def AS chn " + "INNER JOIN mssbam110.any_obj anyo ON "

                + "chn.Id = anyo.Id " + "WHERE " + ((NextRelationId == null) ? "chn.NextRelationId is null "
                        : "chn.NextRelationId = " + NextRelationId.toString() + " ")
                + "ORDER BY " + "chn.Id ASC";
        MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql);
        return (cursor);
    }

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

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

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

    public MSSBamChainBuff firstChainCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamChainBuff buff = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextChainCursor(Cursor);
        }
        return (buff);
    }

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

    public MSSBamChainBuff nthChainCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamChainBuff buff = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextChainCursor(Cursor);
        }
        return (buff);
    }
}