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

Java tutorial

Introduction

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

Source

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

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

/*
 *   MSSBamPg8LegalEntityTable PostgreSQL Jdbc DbIO implementation
 *   for LegalEntity.
 *
 *   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 MSSBamPg8LegalEntityTable implements IMSSBamLegalEntityTable {
    private MSSBamPg8Schema schema;

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

    public void createLegalEntity(MSSBamAuthorization Authorization, MSSBamLegalEntityBuff Buff) {
        final String S_ProcName = "createLegalEntity ";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            Long ContainerDomainId = Buff.getOptionalContainerDomainId();
            String sql = "INSERT INTO mssbam110.legalent( " + "id, " + "container_dom_id" + " )" + "VALUES ( " + Id
                    + ", " + ((ContainerDomainId == null) ? "null" : ContainerDomainId.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_sqlSelectLegalEntityDistinctClassCode = "SELECT " + "DISTINCT anyo.ClassCode "
            + "FROM mssbam110.any_obj AS anyo " + "INNER JOIN mssbam110.scope scp ON " + "scp.Id = anyo.Id "
            + "INNER JOIN mssbam110.basedom bdm ON " + "bdm.Id = anyo.Id " + "INNER JOIN mssbam110.domain dom ON "
            + "dom.Id = anyo.Id " + "INNER JOIN mssbam110.legalent lent ON " + "lent.Id = anyo.Id ";

    public final static String S_sqlSelectLegalEntityBuff = "SELECT " + "anyo.ClassCode, " + "anyo.Id, "
            + "anyo.TenantId, " + "anyo.ScopeId, " + "anyo.Name, " + "anyo.short_name, " + "anyo.Label, "
            + "anyo.short_descr, " + "anyo.descr, " + "anyo.AuthorId, " + "dom.ContainerDomainId, "
            + "lent.container_dom_id, " + "anyo.Revision " + "FROM mssbam110.any_obj AS anyo "
            + "INNER JOIN mssbam110.scope scp ON " + "scp.Id = anyo.Id " + "INNER JOIN mssbam110.basedom bdm ON "
            + "bdm.Id = anyo.Id " + "INNER JOIN mssbam110.domain dom ON " + "dom.Id = anyo.Id "
            + "INNER JOIN mssbam110.legalent lent ON " + "lent.Id = anyo.Id ";

    protected MSSBamLegalEntityBuff unpackLegalEntityResultSetToBuff(ResultSet resultSet) throws SQLException {
        final String S_ProcName = "unpackLegalEntityResultSetToBuff";
        int idxcol = 1;
        String classCode = resultSet.getString(idxcol);
        idxcol++;
        MSSBamLegalEntityBuff buff;
        if (classCode.equals("LENT")) {
            buff = schema.getFactoryLegalEntity().newBuff();
        } else if (classCode.equals("COM")) {
            buff = schema.getFactoryCompany().newBuff();
        } else if (classCode.equals("USR")) {
            buff = schema.getFactoryUser().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++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalContainerDomainId(null);
            } else {
                buff.setOptionalContainerDomainId(colVal);
            }
        }
        idxcol++;
        {
            long colVal = resultSet.getLong(idxcol);
            if (resultSet.wasNull()) {
                buff.setOptionalContainerDomainId(null);
            } else {
                buff.setOptionalContainerDomainId(colVal);
            }
        }
        idxcol++;
        buff.setRequiredRevision(resultSet.getInt(idxcol));
        return (buff);
    }

    public MSSBamLegalEntityBuff readDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) {
        final String S_ProcName = "readDerived()";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        MSSBamLegalEntityBuff buff;
        long Id = PKey.getRequiredId();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE " + "lent.Id = " + Long.toString(Id)
                    + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            if (resultSet.next()) {
                classCode = resultSet.getString(1);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected");
                }
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        if (classCode.equals("LENT")) {
            buff = readBuff(Authorization, PKey);
        } else if (classCode.equals("COM")) {
            buff = schema.getTableCompany().readBuffByIdIdx(Authorization, PKey.getRequiredId());
        } else if (classCode.equals("USR")) {
            buff = schema.getTableUser().readBuffByIdIdx(Authorization, PKey.getRequiredId());
        } else {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Did not expect ClassCode \"" + classCode + "\"");
        }
        return (buff);
    }

    public MSSBamLegalEntityBuff[] readAllDerived(MSSBamAuthorization Authorization) {
        final String S_ProcName = "readAllDerived";
        MSSBamLegalEntityBuff[] buffArray;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        String classCode;
        ArrayList<String> classCodeList = new ArrayList<String>();
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode;
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        for (int classCodeIdx = 0; classCodeIdx < classCodeList.size(); classCodeIdx++) {
            MSSBamLegalEntityBuff[] subList;
            classCode = classCodeList.get(classCodeIdx);
            if (classCode.equals("LENT")) {
                subList = readAllBuff(Authorization);
            } else if (classCode.equals("COM")) {
                subList = schema.getTableCompany().readAllBuff(Authorization);
            } else if (classCode.equals("USR")) {
                subList = schema.getTableUser().readAllBuff(Authorization);
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
            for (int idxSubList = 0; idxSubList < subList.length; idxSubList++) {
                resultList.add(subList[idxSubList]);
            }
        }
        buffArray = resultList.toArray(new MSSBamLegalEntityBuff[0]);
        return (buffArray);
    }

    public MSSBamLegalEntityBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) {
        final String S_ProcName = "MSSBamPg8LegalEntityTable.readDerivedByIdIdx() ";
        MSSBamLegalEntityBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "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()) {
                classCode = resultSet.getString(1);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected");
                }
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        if (classCode.equals("LENT")) {
            buff = readBuffByIdIdx(Authorization, Id);
        } else if (classCode.equals("COM")) {
            buff = schema.getTableCompany().readBuffByIdIdx(Authorization, Id);
        } else if (classCode.equals("USR")) {
            buff = schema.getTableUser().readBuffByIdIdx(Authorization, Id);
        } else {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Did not expect ClassCode \"" + classCode + "\"");
        }
        return (buff);
    }

    public MSSBamLegalEntityBuff[] readDerivedByTenantIdx(MSSBamAuthorization Authorization, long TenantId) {
        final String S_ProcName = "readDerivedByTenantIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ArrayList<String> classCodeList = new ArrayList<String>();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE " + "anyo.TenantId = "
                    + Long.toString(TenantId) + " ";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        ListIterator<String> classCodeIter = classCodeList.listIterator();
        while (classCodeIter.hasNext()) {
            classCode = classCodeIter.next();
            if (classCode.equals("LENT")) {
                MSSBamLegalEntityBuff[] subList = readBuffByTenantIdx(Authorization, TenantId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("COM")) {
                MSSBamCompanyBuff[] subList = schema.getTableCompany().readBuffByTenantIdx(Authorization, TenantId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("USR")) {
                MSSBamUserBuff[] subList = schema.getTableUser().readBuffByTenantIdx(Authorization, TenantId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
        }
        return (resultList.toArray(new MSSBamLegalEntityBuff[0]));

    }

    public MSSBamLegalEntityBuff[] readDerivedByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "readDerivedByScopeIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ArrayList<String> classCodeList = new ArrayList<String>();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE "
                    + ((ScopeId == null) ? "anyo.ScopeId is null " : "anyo.ScopeId = " + ScopeId.toString() + " ");
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        ListIterator<String> classCodeIter = classCodeList.listIterator();
        while (classCodeIter.hasNext()) {
            classCode = classCodeIter.next();
            if (classCode.equals("LENT")) {
                MSSBamLegalEntityBuff[] subList = readBuffByScopeIdx(Authorization, ScopeId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("COM")) {
                MSSBamCompanyBuff[] subList = schema.getTableCompany().readBuffByScopeIdx(Authorization, ScopeId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("USR")) {
                MSSBamUserBuff[] subList = schema.getTableUser().readBuffByScopeIdx(Authorization, ScopeId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
        }
        return (resultList.toArray(new MSSBamLegalEntityBuff[0]));

    }

    public MSSBamLegalEntityBuff[] readDerivedByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "readDerivedByAuthorIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ArrayList<String> classCodeList = new ArrayList<String>();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE "
                    + ((AuthorId == null) ? "anyo.AuthorId is null "
                            : "anyo.AuthorId = " + AuthorId.toString() + " ");
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        ListIterator<String> classCodeIter = classCodeList.listIterator();
        while (classCodeIter.hasNext()) {
            classCode = classCodeIter.next();
            if (classCode.equals("LENT")) {
                MSSBamLegalEntityBuff[] subList = readBuffByAuthorIdx(Authorization, AuthorId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("COM")) {
                MSSBamCompanyBuff[] subList = schema.getTableCompany().readBuffByAuthorIdx(Authorization, AuthorId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("USR")) {
                MSSBamUserBuff[] subList = schema.getTableUser().readBuffByAuthorIdx(Authorization, AuthorId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
        }
        return (resultList.toArray(new MSSBamLegalEntityBuff[0]));

    }

    public MSSBamLegalEntityBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId,
            String Name) {
        final String S_ProcName = "MSSBamPg8LegalEntityTable.readDerivedByUNameIdx() ";
        MSSBamLegalEntityBuff buff;
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "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()) {
                classCode = resultSet.getString(1);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected");
                }
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        if (classCode.equals("LENT")) {
            buff = readBuffByUNameIdx(Authorization, ScopeId, Name);
        } else if (classCode.equals("COM")) {
            buff = schema.getTableCompany().readBuffByUNameIdx(Authorization, ScopeId, Name);
        } else if (classCode.equals("USR")) {
            buff = schema.getTableUser().readBuffByUNameIdx(Authorization, ScopeId, Name);
        } else {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Did not expect ClassCode \"" + classCode + "\"");
        }
        return (buff);
    }

    public MSSBamLegalEntityBuff[] readDerivedByContainerIdx(MSSBamAuthorization Authorization,
            Long ContainerDomainId) {
        final String S_ProcName = "readDerivedByContainerIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ArrayList<String> classCodeList = new ArrayList<String>();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE "
                    + ((ContainerDomainId == null) ? "dom.ContainerDomainId is null "
                            : "dom.ContainerDomainId = " + ContainerDomainId.toString() + " ");
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        ListIterator<String> classCodeIter = classCodeList.listIterator();
        while (classCodeIter.hasNext()) {
            classCode = classCodeIter.next();
            if (classCode.equals("LENT")) {
                MSSBamLegalEntityBuff[] subList = readBuffByContainerIdx(Authorization, ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("COM")) {
                MSSBamCompanyBuff[] subList = schema.getTableCompany().readBuffByContainerIdx(Authorization,
                        ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("USR")) {
                MSSBamUserBuff[] subList = schema.getTableUser().readBuffByContainerIdx(Authorization,
                        ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
        }
        return (resultList.toArray(new MSSBamLegalEntityBuff[0]));

    }

    public MSSBamLegalEntityBuff[] readDerivedByChildrenIdx(MSSBamAuthorization Authorization,
            Long ContainerDomainId) {
        final String S_ProcName = "readDerivedByChildrenIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ArrayList<String> classCodeList = new ArrayList<String>();
        String classCode;
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityDistinctClassCode + "WHERE "
                    + ((ContainerDomainId == null) ? "lent.container_dom_id is null "
                            : "lent.container_dom_id = " + ContainerDomainId.toString() + " ");
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            while (resultSet.next()) {
                classCode = resultSet.getString(1);
                classCodeList.add(classCode);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
        ArrayList<MSSBamLegalEntityBuff> resultList = new ArrayList<MSSBamLegalEntityBuff>();
        ListIterator<String> classCodeIter = classCodeList.listIterator();
        while (classCodeIter.hasNext()) {
            classCode = classCodeIter.next();
            if (classCode.equals("LENT")) {
                MSSBamLegalEntityBuff[] subList = readBuffByChildrenIdx(Authorization, ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("COM")) {
                MSSBamCompanyBuff[] subList = schema.getTableCompany().readBuffByChildrenIdx(Authorization,
                        ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else if (classCode.equals("USR")) {
                MSSBamUserBuff[] subList = schema.getTableUser().readBuffByChildrenIdx(Authorization,
                        ContainerDomainId);
                for (int subListIdx = 0; subListIdx < subList.length; subListIdx++) {
                    resultList.add(subList[subListIdx]);
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Did not expect ClassCode \"" + classCode + "\"");
            }
        }
        return (resultList.toArray(new MSSBamLegalEntityBuff[0]));

    }

    public MSSBamLegalEntityBuff 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_sqlSelectLegalEntityBuff + "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()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(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 MSSBamLegalEntityBuff[] 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_sqlSelectLegalEntityBuff + "WHERE " + "anyo.ClassCode = 'LENT' " + "ORDER BY "
                    + "anyo.Id ASC";
            Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet resultSet = stmt.executeQuery(sql);
            List<MSSBamLegalEntityBuff> buffList = new ArrayList<MSSBamLegalEntityBuff>();
            while (resultSet.next()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamLegalEntityBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamLegalEntityBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) {
        final String S_ProcName = "readBuffByIdIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityBuff + "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()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(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 MSSBamLegalEntityBuff[] readBuffByTenantIdx(MSSBamAuthorization Authorization, long TenantId) {
        final String S_ProcName = "readBuffByTenantIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityBuff + "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<MSSBamLegalEntityBuff> buffList = new ArrayList<MSSBamLegalEntityBuff>();
            while (resultSet.next()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamLegalEntityBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamLegalEntityBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) {
        final String S_ProcName = "readBuffByScopeIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityBuff + "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<MSSBamLegalEntityBuff> buffList = new ArrayList<MSSBamLegalEntityBuff>();
            while (resultSet.next()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamLegalEntityBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public MSSBamLegalEntityBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) {
        final String S_ProcName = "readBuffByAuthorIdx";
        try {
            Connection cnx = schema.getCnx();
            String sql = S_sqlSelectLegalEntityBuff + "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<MSSBamLegalEntityBuff> buffList = new ArrayList<MSSBamLegalEntityBuff>();
            while (resultSet.next()) {
                MSSBamLegalEntityBuff buff = unpackLegalEntityResultSetToBuff(resultSet);
                buffList.add(buff);
            }
            return (buffList.toArray(new MSSBamLegalEntityBuff[0]));
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

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

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

    public void updateLegalEntity(MSSBamAuthorization Authorization, MSSBamLegalEntityBuff Buff) {
        final String S_ProcName = "updateLegalEntity";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            Long ContainerDomainId = Buff.getOptionalContainerDomainId();

            String sql = "UPDATE mssbam110.legalent " + "SET " + "Id = " + MSSBamPg8Schema.getInt64String(Id) + ", "
                    + "container_dom_id = "
                    + ((ContainerDomainId != null) ? MSSBamPg8Schema.getInt64String(ContainerDomainId) : "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 deleteLegalEntity(MSSBamAuthorization Authorization, MSSBamLegalEntityBuff Buff) {
        final String S_ProcName = "deleteLegalEntity";
        try {
            Connection cnx = schema.getCnx();
            long Id = Buff.getRequiredId();
            Long ContainerDomainId = Buff.getOptionalContainerDomainId();

            String sql = "DELETE FROM mssbam110.legalent " + "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 openLegalEntityCursorAll(MSSBamAuthorization Authorization) {
        String sql = "SELECT " + "anyo.ClassCode, " + "lent.Id, " + "lent.container_dom_id, " + "lent.Revision "
                + "FROM mssbam110.legalent AS lent " + "INNER JOIN mssbam110.any_obj anyo ON "

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

    public MSSBamCursor openLegalEntityCursorByChildrenIdx(MSSBamAuthorization Authorization,
            Long ContainerDomainId) {
        String sql = "SELECT " + "anyo.ClassCode, " + "lent.Id, " + "lent.container_dom_id, " + "lent.Revision "
                + "FROM mssbam110.legalent AS lent " + "INNER JOIN mssbam110.any_obj anyo ON "

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

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

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

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

    public MSSBamLegalEntityBuff firstLegalEntityCursor(MSSBamCursor Cursor) {
        int targetRowIdx = 1;
        MSSBamLegalEntityBuff buff = null;
        Cursor.reset();
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextLegalEntityCursor(Cursor);
        }
        return (buff);
    }

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

    public MSSBamLegalEntityBuff nthLegalEntityCursor(MSSBamCursor Cursor, int Idx) {
        int targetRowIdx = Idx;
        MSSBamLegalEntityBuff buff = null;
        if (Cursor.getRowIdx() >= targetRowIdx) {
            Cursor.reset();
        }
        while (Cursor.getRowIdx() < targetRowIdx) {
            buff = nextLegalEntityCursor(Cursor);
        }
        return (buff);
    }
}