net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMSSql.CFCrmMSSqlAttachmentTable.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMSSql.CFCrmMSSqlAttachmentTable.java

Source

// Description: Java 7 MS SQL Server 2012 Express Advanced Edition Jdbc DbIO implementation for Attachment.

/*
 *   CF Customer Relations Management model
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *   
 *          http://www.apache.org/licenses/LICENSE-2.0
 *   
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 */

package net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmMSSql;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*;
import org.apache.commons.codec.binary.Base64;
import net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrm.*;

/*
 *   CFCrmMSSqlAttachmentTable PostgreSQL Jdbc DbIO implementation
 *   for Attachment.
 */
public class CFCrmMSSqlAttachmentTable implements ICFCrmAttachmentTable {
    private CFCrmMSSqlSchema schema;
    protected PreparedStatement stmtReadBuffByPKey = null;
    protected PreparedStatement stmtLockBuffByPKey = null;
    protected PreparedStatement stmtCreateByPKey = null;
    protected PreparedStatement stmtUpdateByPKey = null;
    protected PreparedStatement stmtDeleteByPKey = null;
    protected PreparedStatement stmtAuditCreatedByPKey = null;
    protected PreparedStatement stmtAuditUpdatedByPKey = null;
    protected PreparedStatement stmtReadAllBuff = null;
    protected PreparedStatement stmtReadBuffByIdIdx = null;
    protected PreparedStatement stmtReadBuffByTenantIdx = null;
    protected PreparedStatement stmtReadBuffByContactIdx = null;
    protected PreparedStatement stmtReadBuffByUDescrIdx = null;
    protected PreparedStatement stmtReadBuffByMimeTypeIdx = null;
    protected PreparedStatement stmtDeleteByIdIdx = null;
    protected PreparedStatement stmtDeleteByTenantIdx = null;
    protected PreparedStatement stmtDeleteByContactIdx = null;
    protected PreparedStatement stmtDeleteByUDescrIdx = null;
    protected PreparedStatement stmtDeleteByMimeTypeIdx = null;

    public CFCrmMSSqlAttachmentTable(CFCrmMSSqlSchema argSchema) {
        schema = argSchema;
    }

    public void createAttachment(CFCrmAuthorization Authorization, CFCrmAttachmentBuff Buff) {
        final String S_ProcName = "createAttachment";
        if ("ATTC".equals(Buff.getClassCode())
                && (!schema.isTenantUser(Authorization, Buff.getRequiredTenantId(), "CreateAttachment"))) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Permission denied -- User not part of TSecGroup CreateAttachment");
        }
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        try {
            Connection cnx = schema.getCnx();
            long TenantId = Buff.getRequiredTenantId();
            long AttachmentId = schema.getTableTenant().nextAttachmentIdGen(Authorization,
                    Buff.getRequiredTenantId());
            long ContactId = Buff.getRequiredContactId();
            String Description = Buff.getRequiredDescription();
            Integer MimeTypeId = Buff.getOptionalMimeTypeId();
            String Attachment = Buff.getRequiredAttachment();
            int Revision = 1;
            String sql = "INSERT INTO " + schema.getLowerDbSchemaName() + "..Attchmnt( " + "forcesynclock, "
                    + "createdby, " + "createdat, " + "updatedby, " + "updatedat, " + "tenantid, "
                    + "attachmentid, " + "contactid, " + "description, " + "mimetypeid, " + "attachment"
                    + ", revision )" + "VALUES ( " + "0, " + " ?, " + " getdate(), " + " ?, " + " getdate(), " + "?"
                    + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "?" + ", " + "1 )";
            if (stmtCreateByPKey == null) {
                stmtCreateByPKey = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtCreateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
            stmtCreateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
            stmtCreateByPKey.setLong(argIdx++, TenantId);
            stmtCreateByPKey.setLong(argIdx++, AttachmentId);
            stmtCreateByPKey.setLong(argIdx++, ContactId);
            stmtCreateByPKey.setString(argIdx++, Description);
            if (MimeTypeId != null) {
                stmtCreateByPKey.setInt(argIdx++, MimeTypeId.intValue());
            } else {
                stmtCreateByPKey.setNull(argIdx++, java.sql.Types.INTEGER);
            }
            stmtCreateByPKey.setString(argIdx++, Attachment);
            int rowsAffected = stmtCreateByPKey.executeUpdate();
            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);
            String sqlAuditCreated = "INSERT INTO " + schema.getLowerDbSchemaName()
                    + "..Attchmnt_h( auditclusterid, " + " auditsessionid, " + " auditstamp" + ", " + "tenantid"
                    + ", " + "attachmentid" + ", " + "contactid" + ", " + "description" + ", " + "mimetypeid" + ", "
                    + "attachment" + ", " + " revision, " + " auditaction ) " + "SELECT ?, ?, sysdatetime()" + ", "
                    + "attc.tenantid" + ", " + "attc.attachmentid" + ", " + "attc.contactid" + ", "
                    + "attc.description" + ", " + "attc.mimetypeid" + ", " + "attc.attachment" + ", "
                    + " attc.revision, " + " 1" + "FROM " + schema.getLowerDbSchemaName() + "..Attchmnt AS attc "
                    + " WHERE " + "attc.tenantid = ? " + "AND attc.attachmentid = ? ";
            if (stmtAuditCreatedByPKey == null) {
                stmtAuditCreatedByPKey = cnx.prepareStatement(sqlAuditCreated);
            }
            argIdx = 1;
            stmtAuditCreatedByPKey.setLong(argIdx++, Authorization.getSecClusterId());
            stmtAuditCreatedByPKey.setString(argIdx++, Authorization.getSecSessionId().toString());
            stmtAuditCreatedByPKey.setLong(argIdx++, TenantId);
            stmtAuditCreatedByPKey.setLong(argIdx++, AttachmentId);
            int rowsAudited = stmtAuditCreatedByPKey.executeUpdate();
            if (rowsAudited != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by audit via insert-selected, not " + rowsAffected);
            }

        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    protected static String S_sqlSelectAttachmentDistinctClassCode = null;

    public String getSqlSelectAttachmentDistinctClassCode() {
        if (S_sqlSelectAttachmentDistinctClassCode == null) {
            S_sqlSelectAttachmentDistinctClassCode = "SELECT " + "DISTINCT attc.ClassCode " + "FROM "
                    + schema.getLowerDbSchemaName() + "..Attchmnt AS attc ";
        }
        return (S_sqlSelectAttachmentDistinctClassCode);
    }

    protected static String S_sqlSelectAttachmentBuff = null;

    public String getSqlSelectAttachmentBuff() {
        if (S_sqlSelectAttachmentBuff == null) {
            S_sqlSelectAttachmentBuff = "SELECT " + "attc.tenantid, " + "attc.attachmentid, " + "attc.contactid, "
                    + "attc.description, " + "attc.mimetypeid, " + "attc.attachment, " + "attc.revision " + "FROM "
                    + schema.getLowerDbSchemaName() + "..Attchmnt AS attc ";
        }
        return (S_sqlSelectAttachmentBuff);
    }

    protected CFCrmAttachmentBuff unpackAttachmentResultSetToBuff(ResultSet resultSet) throws SQLException {
        final String S_ProcName = "unpackAttachmentResultSetToBuff";
        int idxcol = 1;
        CFCrmAttachmentBuff buff = schema.getFactoryAttachment().newBuff();
        {
            String colString = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setCreatedByUserId(null);
            } else if ((colString == null) || (colString.length() <= 0)) {
                buff.setCreatedByUserId(null);
            } else {
                buff.setCreatedByUserId(UUID.fromString(colString));
            }
            idxcol++;

            colString = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setCreatedAt(null);
            } else if ((colString == null) || (colString.length() <= 0)) {
                buff.setCreatedAt(null);
            } else {
                buff.setCreatedAt(CFCrmMSSqlSchema.convertTimestampString(colString));
            }
            idxcol++;
            colString = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setUpdatedByUserId(null);
            } else if ((colString == null) || (colString.length() <= 0)) {
                buff.setUpdatedByUserId(null);
            } else {
                buff.setUpdatedByUserId(UUID.fromString(colString));
            }
            idxcol++;

            colString = resultSet.getString(idxcol);
            if (resultSet.wasNull()) {
                buff.setUpdatedAt(null);
            } else if ((colString == null) || (colString.length() <= 0)) {
                buff.setUpdatedAt(null);
            } else {
                buff.setUpdatedAt(CFCrmMSSqlSchema.convertTimestampString(colString));
            }
            idxcol++;
        }
        buff.setRequiredTenantId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredAttachmentId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredContactId(resultSet.getLong(idxcol));
        idxcol++;
        buff.setRequiredDescription(resultSet.getString(idxcol));
        idxcol++;
        {
            int colVal = resultSet.getInt(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 CFCrmAttachmentBuff readDerived(CFCrmAuthorization Authorization, CFCrmAttachmentPKey PKey) {
        final String S_ProcName = "readDerived";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        CFCrmAttachmentBuff buff;
        buff = readBuff(Authorization, PKey);
        return (buff);
    }

    public CFCrmAttachmentBuff lockDerived(CFCrmAuthorization Authorization, CFCrmAttachmentPKey PKey) {
        final String S_ProcName = "lockDerived";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        CFCrmAttachmentBuff buff;
        buff = lockBuff(Authorization, PKey);
        return (buff);
    }

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

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

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

    }

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

    }

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

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

    }

    public CFCrmAttachmentBuff readBuff(CFCrmAuthorization Authorization, CFCrmAttachmentPKey PKey) {
        final String S_ProcName = "readBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            long TenantId = PKey.getRequiredTenantId();
            long AttachmentId = PKey.getRequiredAttachmentId();
            String sql = "{ call sp_read_attchmnt( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + " ) }";
            if (stmtReadBuffByPKey == null) {
                stmtReadBuffByPKey = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtReadBuffByPKey.setLong(argIdx++, TenantId);
            stmtReadBuffByPKey.setLong(argIdx++, AttachmentId);
            resultSet = stmtReadBuffByPKey.executeQuery();
            if ((resultSet != null) && resultSet.next()) {
                CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff lockBuff(CFCrmAuthorization Authorization, CFCrmAttachmentPKey PKey) {
        final String S_ProcName = "lockBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            long TenantId = PKey.getRequiredTenantId();
            long AttachmentId = PKey.getRequiredAttachmentId();
            String sql = "{ call sp_lock_attchmnt( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + " ) }";
            if (stmtLockBuffByPKey == null) {
                stmtLockBuffByPKey = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtLockBuffByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtLockBuffByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtLockBuffByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtLockBuffByPKey.setLong(argIdx++, TenantId);
            stmtLockBuffByPKey.setLong(argIdx++, AttachmentId);
            stmtLockBuffByPKey.execute();
            boolean moreResults = true;
            resultSet = null;
            while (resultSet == null) {
                try {
                    moreResults = stmtLockBuffByPKey.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        resultSet = stmtLockBuffByPKey.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtLockBuffByPKey.getUpdateCount()) {
                    break;
                }
            }
            if ((resultSet != null) && resultSet.next()) {
                CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff[] readAllBuff(CFCrmAuthorization Authorization) {
        final String S_ProcName = "readAllBuff";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_all( ?, ?, ?, ?, ? ) }";
            if (stmtReadAllBuff == null) {
                stmtReadAllBuff = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadAllBuff.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadAllBuff.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadAllBuff.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            resultSet = stmtReadAllBuff.executeQuery();
            List<CFCrmAttachmentBuff> buffList = new LinkedList<CFCrmAttachmentBuff>();
            if (resultSet != null) {
                while (resultSet.next()) {
                    CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                    buffList.add(buff);
                }
            }
            int idx = 0;
            CFCrmAttachmentBuff[] retBuff = new CFCrmAttachmentBuff[buffList.size()];
            Iterator<CFCrmAttachmentBuff> iter = buffList.iterator();
            while (iter.hasNext()) {
                retBuff[idx++] = iter.next();
            }
            return (retBuff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff readBuffByIdIdx(CFCrmAuthorization Authorization, long TenantId, long AttachmentId) {
        final String S_ProcName = "readBuffByIdIdx";
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_by_ididx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + " ) }";
            if (stmtReadBuffByIdIdx == null) {
                stmtReadBuffByIdIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByIdIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByIdIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtReadBuffByIdIdx.setLong(argIdx++, TenantId);
            stmtReadBuffByIdIdx.setLong(argIdx++, AttachmentId);
            resultSet = stmtReadBuffByIdIdx.executeQuery();
            if ((resultSet != null) && resultSet.next()) {
                CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff[] readBuffByTenantIdx(CFCrmAuthorization Authorization, long TenantId) {
        final String S_ProcName = "readBuffByTenantIdx";
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_by_tenantidx( ?, ?, ?, ?, ?" + ", " + "?" + " ) }";
            if (stmtReadBuffByTenantIdx == null) {
                stmtReadBuffByTenantIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByTenantIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByTenantIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByTenantIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByTenantIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByTenantIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtReadBuffByTenantIdx.setLong(argIdx++, TenantId);
            resultSet = stmtReadBuffByTenantIdx.executeQuery();
            List<CFCrmAttachmentBuff> buffList = new LinkedList<CFCrmAttachmentBuff>();
            if (resultSet != null) {
                while (resultSet.next()) {
                    CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                    buffList.add(buff);
                }
            }
            int idx = 0;
            CFCrmAttachmentBuff[] retBuff = new CFCrmAttachmentBuff[buffList.size()];
            Iterator<CFCrmAttachmentBuff> iter = buffList.iterator();
            while (iter.hasNext()) {
                retBuff[idx++] = iter.next();
            }
            return (retBuff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff[] readBuffByContactIdx(CFCrmAuthorization Authorization, long TenantId,
            long ContactId) {
        final String S_ProcName = "readBuffByContactIdx";
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_by_contactidx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + " ) }";
            if (stmtReadBuffByContactIdx == null) {
                stmtReadBuffByContactIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByContactIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByContactIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByContactIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByContactIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByContactIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtReadBuffByContactIdx.setLong(argIdx++, TenantId);
            stmtReadBuffByContactIdx.setLong(argIdx++, ContactId);
            resultSet = stmtReadBuffByContactIdx.executeQuery();
            List<CFCrmAttachmentBuff> buffList = new LinkedList<CFCrmAttachmentBuff>();
            if (resultSet != null) {
                while (resultSet.next()) {
                    CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                    buffList.add(buff);
                }
            }
            int idx = 0;
            CFCrmAttachmentBuff[] retBuff = new CFCrmAttachmentBuff[buffList.size()];
            Iterator<CFCrmAttachmentBuff> iter = buffList.iterator();
            while (iter.hasNext()) {
                retBuff[idx++] = iter.next();
            }
            return (retBuff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff readBuffByUDescrIdx(CFCrmAuthorization Authorization, long TenantId, long ContactId,
            String Description) {
        final String S_ProcName = "readBuffByUDescrIdx";
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_by_udescridx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", "
                    + "?" + " ) }";
            if (stmtReadBuffByUDescrIdx == null) {
                stmtReadBuffByUDescrIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByUDescrIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByUDescrIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByUDescrIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByUDescrIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByUDescrIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtReadBuffByUDescrIdx.setLong(argIdx++, TenantId);
            stmtReadBuffByUDescrIdx.setLong(argIdx++, ContactId);
            stmtReadBuffByUDescrIdx.setString(argIdx++, Description);
            resultSet = stmtReadBuffByUDescrIdx.executeQuery();
            if ((resultSet != null) && resultSet.next()) {
                CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                if (resultSet.next()) {
                    resultSet.last();
                    throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                            "Did not expect multi-record response, " + resultSet.getRow() + " rows selected");
                }
                return (buff);
            } else {
                return (null);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public CFCrmAttachmentBuff[] readBuffByMimeTypeIdx(CFCrmAuthorization Authorization, Integer MimeTypeId) {
        final String S_ProcName = "readBuffByMimeTypeIdx";
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "{ call sp_read_attchmnt_by_mimetypeidx( ?, ?, ?, ?, ?" + ", " + "?" + " ) }";
            if (stmtReadBuffByMimeTypeIdx == null) {
                stmtReadBuffByMimeTypeIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtReadBuffByMimeTypeIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByMimeTypeIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtReadBuffByMimeTypeIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtReadBuffByMimeTypeIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtReadBuffByMimeTypeIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecTenantId());
            if (MimeTypeId != null) {
                stmtReadBuffByMimeTypeIdx.setInt(argIdx++, MimeTypeId.intValue());
            } else {
                stmtReadBuffByMimeTypeIdx.setNull(argIdx++, java.sql.Types.INTEGER);
            }
            resultSet = stmtReadBuffByMimeTypeIdx.executeQuery();
            List<CFCrmAttachmentBuff> buffList = new LinkedList<CFCrmAttachmentBuff>();
            if (resultSet != null) {
                while (resultSet.next()) {
                    CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
                    buffList.add(buff);
                }
            }
            int idx = 0;
            CFCrmAttachmentBuff[] retBuff = new CFCrmAttachmentBuff[buffList.size()];
            Iterator<CFCrmAttachmentBuff> iter = buffList.iterator();
            while (iter.hasNext()) {
                retBuff[idx++] = iter.next();
            }
            return (retBuff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public void updateAttachment(CFCrmAuthorization Authorization, CFCrmAttachmentBuff Buff) {
        final String S_ProcName = "updateAttachment";
        if ("ATTC".equals(Buff.getClassCode())
                && (!schema.isTenantUser(Authorization, Buff.getRequiredTenantId(), "UpdateAttachment"))) {
            throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                    "Permission denied -- User not part of TSecGroup UpdateAttachment");
        }
        try {
            Connection cnx = schema.getCnx();
            long TenantId = Buff.getRequiredTenantId();
            long AttachmentId = Buff.getRequiredAttachmentId();
            long ContactId = Buff.getRequiredContactId();
            String Description = Buff.getRequiredDescription();
            Integer MimeTypeId = Buff.getOptionalMimeTypeId();
            String Attachment = Buff.getRequiredAttachment();
            int Revision = Buff.getRequiredRevision();
            CFCrmAttachmentPKey pkey = schema.getFactoryAttachment().newPKey();
            pkey.setRequiredTenantId(Buff.getRequiredTenantId());
            pkey.setRequiredAttachmentId(Buff.getRequiredAttachmentId());
            CFCrmAttachmentBuff readBuff = lockBuff(Authorization, pkey);
            if (readBuff == null) {
                throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), S_ProcName,
                        "Attempted to update record which could not be locked/found",
                        schema.getLowerDbSchemaName() + "..attchmnt", pkey);
            }
            int oldRevision = readBuff.getRequiredRevision();
            if (oldRevision != Revision) {
                throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName,
                        Buff);
            }
            int newRevision = Revision + 1;
            String sql = "UPDATE " + schema.getLowerDbSchemaName() + "..Attchmnt " + "SET " + "tenantid = ?" + ", "
                    + "attachmentid = ?" + ", " + "contactid = ?" + ", " + "description = ?" + ", "
                    + "mimetypeid = ?" + ", " + "attachment = ?" + ", " + "updatedby = ?, "
                    + "updatedat = sysdatetime() " + ", revision = ? " + " WHERE " + "tenantid = ? " + "AND "
                    + "attachmentid = ? " + "AND " + "revision = ? ";
            if (stmtUpdateByPKey == null) {
                stmtUpdateByPKey = cnx.prepareStatement(sql);
            }
            int argIdx = 1;

            stmtUpdateByPKey.setLong(argIdx++, TenantId);
            stmtUpdateByPKey.setLong(argIdx++, AttachmentId);
            stmtUpdateByPKey.setLong(argIdx++, ContactId);
            stmtUpdateByPKey.setString(argIdx++, Description);
            if (MimeTypeId != null) {
                stmtUpdateByPKey.setInt(argIdx++, MimeTypeId.intValue());
            } else {
                stmtUpdateByPKey.setNull(argIdx++, java.sql.Types.INTEGER);
            }
            stmtUpdateByPKey.setString(argIdx++, Attachment);
            stmtUpdateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
            stmtUpdateByPKey.setInt(argIdx++, newRevision);
            stmtUpdateByPKey.setLong(argIdx++, TenantId);
            stmtUpdateByPKey.setLong(argIdx++, AttachmentId);
            stmtUpdateByPKey.setInt(argIdx++, Revision);
            ;
            int rowsAffected = stmtUpdateByPKey.executeUpdate();
            if (rowsAffected != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by update, not " + rowsAffected);
            }
            Buff.setRequiredRevision(newRevision);
            String sqlAuditUpdated = "INSERT INTO " + schema.getLowerDbSchemaName()
                    + "..Attchmnt_h( auditclusterid, " + " auditsessionid, " + " auditstamp" + ", " + "tenantid"
                    + ", " + "attachmentid" + ", " + "contactid" + ", " + "description" + ", " + "mimetypeid" + ", "
                    + "attachment" + ", " + " revision, " + " auditaction ) " + "SELECT ?, ?, sysdatetime()" + ", "
                    + "attc.tenantid" + ", " + "attc.attachmentid" + ", " + "attc.contactid" + ", "
                    + "attc.description" + ", " + "attc.mimetypeid" + ", " + "attc.attachment" + ", "
                    + " attc.revision, " + " 2 " + "FROM " + schema.getLowerDbSchemaName() + "..Attchmnt AS attc "
                    + " WHERE " + "attc.tenantid = ? " + "AND attc.attachmentid = ? ";
            if (stmtAuditUpdatedByPKey == null) {
                stmtAuditUpdatedByPKey = cnx.prepareStatement(sqlAuditUpdated);
            }
            argIdx = 1;
            stmtAuditUpdatedByPKey.setLong(argIdx++, Authorization.getSecClusterId());
            stmtAuditUpdatedByPKey.setString(argIdx++, Authorization.getSecSessionId().toString());
            stmtAuditUpdatedByPKey.setLong(argIdx++, TenantId);
            stmtAuditUpdatedByPKey.setLong(argIdx++, AttachmentId);
            int rowsAudited = stmtAuditUpdatedByPKey.executeUpdate();
            if (rowsAudited != 1) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Expected 1 row to be affected by audit via insert-selected, not " + rowsAffected);
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void deleteAttachment(CFCrmAuthorization Authorization, CFCrmAttachmentBuff Buff) {
        final String S_ProcName = "deleteAttachment";
        try {
            Connection cnx = schema.getCnx();
            long TenantId = Buff.getRequiredTenantId();
            long AttachmentId = Buff.getRequiredAttachmentId();

            String sql = "exec sp_delete_attchmnt ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "?";
            if (stmtDeleteByPKey == null) {
                stmtDeleteByPKey = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtDeleteByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtDeleteByPKey.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtDeleteByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtDeleteByPKey.setLong(argIdx++, TenantId);
            stmtDeleteByPKey.setLong(argIdx++, AttachmentId);
            stmtDeleteByPKey.setInt(argIdx++, Buff.getRequiredRevision());
            ;
            Object stuff = null;
            boolean moreResults = stmtDeleteByPKey.execute();
            while (stuff == null) {
                try {
                    moreResults = stmtDeleteByPKey.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        stuff = stmtDeleteByPKey.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtDeleteByPKey.getUpdateCount()) {
                    break;
                }
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

    public void deleteAttachmentByIdIdx(CFCrmAuthorization Authorization, long argTenantId, long argAttachmentId) {
        final String S_ProcName = "deleteAttachmentByIdIdx";
        // MSS TODO WORKING
        throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), S_ProcName);
    }

    public void deleteAttachmentByIdIdx(CFCrmAuthorization Authorization, CFCrmAttachmentPKey argKey) {
        deleteAttachmentByIdIdx(Authorization, argKey.getRequiredTenantId(), argKey.getRequiredAttachmentId());
    }

    public void deleteAttachmentByTenantIdx(CFCrmAuthorization Authorization, long argTenantId) {
        final String S_ProcName = "deleteAttachmentByTenantIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "exec sp_delete_attchmnt_by_tenantidx ?, ?, ?, ?, ?" + ", " + "?";
            if (stmtDeleteByTenantIdx == null) {
                stmtDeleteByTenantIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtDeleteByTenantIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByTenantIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtDeleteByTenantIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtDeleteByTenantIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByTenantIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtDeleteByTenantIdx.setLong(argIdx++, argTenantId);
            Object stuff = null;
            boolean moreResults = stmtDeleteByTenantIdx.execute();
            while (stuff == null) {
                try {
                    moreResults = stmtDeleteByTenantIdx.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        stuff = stmtDeleteByTenantIdx.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtDeleteByTenantIdx.getUpdateCount()) {
                    break;
                }
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public void deleteAttachmentByTenantIdx(CFCrmAuthorization Authorization,
            CFCrmAttachmentByTenantIdxKey argKey) {
        deleteAttachmentByTenantIdx(Authorization, argKey.getRequiredTenantId());
    }

    public void deleteAttachmentByContactIdx(CFCrmAuthorization Authorization, long argTenantId,
            long argContactId) {
        final String S_ProcName = "deleteAttachmentByContactIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "exec sp_delete_attchmnt_by_contactidx ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?";
            if (stmtDeleteByContactIdx == null) {
                stmtDeleteByContactIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtDeleteByContactIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByContactIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtDeleteByContactIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtDeleteByContactIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByContactIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtDeleteByContactIdx.setLong(argIdx++, argTenantId);
            stmtDeleteByContactIdx.setLong(argIdx++, argContactId);
            Object stuff = null;
            boolean moreResults = stmtDeleteByContactIdx.execute();
            while (stuff == null) {
                try {
                    moreResults = stmtDeleteByContactIdx.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        stuff = stmtDeleteByContactIdx.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtDeleteByContactIdx.getUpdateCount()) {
                    break;
                }
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public void deleteAttachmentByContactIdx(CFCrmAuthorization Authorization,
            CFCrmAttachmentByContactIdxKey argKey) {
        deleteAttachmentByContactIdx(Authorization, argKey.getRequiredTenantId(), argKey.getRequiredContactId());
    }

    public void deleteAttachmentByUDescrIdx(CFCrmAuthorization Authorization, long argTenantId, long argContactId,
            String argDescription) {
        final String S_ProcName = "deleteAttachmentByUDescrIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "exec sp_delete_attchmnt_by_udescridx ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", "
                    + "?";
            if (stmtDeleteByUDescrIdx == null) {
                stmtDeleteByUDescrIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtDeleteByUDescrIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByUDescrIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtDeleteByUDescrIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtDeleteByUDescrIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByUDescrIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            stmtDeleteByUDescrIdx.setLong(argIdx++, argTenantId);
            stmtDeleteByUDescrIdx.setLong(argIdx++, argContactId);
            stmtDeleteByUDescrIdx.setString(argIdx++, argDescription);
            Object stuff = null;
            boolean moreResults = stmtDeleteByUDescrIdx.execute();
            while (stuff == null) {
                try {
                    moreResults = stmtDeleteByUDescrIdx.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        stuff = stmtDeleteByUDescrIdx.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtDeleteByUDescrIdx.getUpdateCount()) {
                    break;
                }
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public void deleteAttachmentByUDescrIdx(CFCrmAuthorization Authorization,
            CFCrmAttachmentByUDescrIdxKey argKey) {
        deleteAttachmentByUDescrIdx(Authorization, argKey.getRequiredTenantId(), argKey.getRequiredContactId(),
                argKey.getRequiredDescription());
    }

    public void deleteAttachmentByMimeTypeIdx(CFCrmAuthorization Authorization, Integer argMimeTypeId) {
        final String S_ProcName = "deleteAttachmentByMimeTypeIdx";
        if (!schema.isTransactionOpen()) {
            throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName,
                    "Transaction not open");
        }
        ResultSet resultSet = null;
        try {
            Connection cnx = schema.getCnx();
            String sql = "exec sp_delete_attchmnt_by_mimetypeidx ?, ?, ?, ?, ?" + ", " + "?";
            if (stmtDeleteByMimeTypeIdx == null) {
                stmtDeleteByMimeTypeIdx = cnx.prepareStatement(sql);
            }
            int argIdx = 1;
            stmtDeleteByMimeTypeIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByMimeTypeIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecUserId().toString());
            stmtDeleteByMimeTypeIdx.setString(argIdx++,
                    (Authorization == null) ? "" : Authorization.getSecSessionId().toString());
            stmtDeleteByMimeTypeIdx.setLong(argIdx++,
                    (Authorization == null) ? 0 : Authorization.getSecClusterId());
            stmtDeleteByMimeTypeIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId());
            if (argMimeTypeId != null) {
                stmtDeleteByMimeTypeIdx.setInt(argIdx++, argMimeTypeId.intValue());
            } else {
                stmtDeleteByMimeTypeIdx.setNull(argIdx++, java.sql.Types.INTEGER);
            }
            Object stuff = null;
            boolean moreResults = stmtDeleteByMimeTypeIdx.execute();
            while (stuff == null) {
                try {
                    moreResults = stmtDeleteByMimeTypeIdx.getMoreResults();
                } catch (SQLException e) {
                    throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
                }
                if (moreResults) {
                    try {
                        stuff = stmtDeleteByMimeTypeIdx.getResultSet();
                    } catch (SQLException e) {
                    }
                } else if (-1 == stmtDeleteByMimeTypeIdx.getUpdateCount()) {
                    break;
                }
            }
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                }
                resultSet = null;
            }
        }
    }

    public void deleteAttachmentByMimeTypeIdx(CFCrmAuthorization Authorization,
            CFCrmAttachmentByMimeTypeIdxKey argKey) {
        deleteAttachmentByMimeTypeIdx(Authorization, argKey.getOptionalMimeTypeId());
    }

    public CFCrmCursor openAttachmentCursorAll(CFCrmAuthorization Authorization) {
        String sql = getSqlSelectAttachmentBuff() + "ORDER BY " + "attc.TenantId ASC" + ", "
                + "attc.AttachmentId ASC";
        CFCrmCursor cursor = new CFCrmMSSqlCursor(Authorization, schema, sql);
        return (cursor);
    }

    public CFCrmCursor openAttachmentCursorByTenantIdx(CFCrmAuthorization Authorization, long TenantId) {
        String sql = getSqlSelectAttachmentBuff() + " WHERE " + "attc.tenantid = " + Long.toString(TenantId) + " "
                + "ORDER BY " + "attc.TenantId ASC" + ", " + "attc.AttachmentId ASC";
        CFCrmCursor cursor = new CFCrmMSSqlCursor(Authorization, schema, sql);
        return (cursor);
    }

    public CFCrmCursor openAttachmentCursorByContactIdx(CFCrmAuthorization Authorization, long TenantId,
            long ContactId) {
        String sql = getSqlSelectAttachmentBuff() + " WHERE " + "attc.tenantid = " + Long.toString(TenantId) + " "
                + "AND " + "attc.contactid = " + Long.toString(ContactId) + " " + "ORDER BY " + "attc.TenantId ASC"
                + ", " + "attc.AttachmentId ASC";
        CFCrmCursor cursor = new CFCrmMSSqlCursor(Authorization, schema, sql);
        return (cursor);
    }

    public CFCrmCursor openAttachmentCursorByMimeTypeIdx(CFCrmAuthorization Authorization, Integer MimeTypeId) {
        String sql = getSqlSelectAttachmentBuff() + " WHERE "
                + ((MimeTypeId == null) ? "attc.mimetypeid is null "
                        : "attc.mimetypeid = " + MimeTypeId.toString() + " ")
                + "ORDER BY " + "attc.TenantId ASC" + ", " + "attc.AttachmentId ASC";
        CFCrmCursor cursor = new CFCrmMSSqlCursor(Authorization, schema, sql);
        return (cursor);
    }

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

    public CFCrmAttachmentBuff nextAttachmentCursor(CFCrmCursor 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");
            }
            CFCrmAttachmentBuff buff = unpackAttachmentResultSetToBuff(resultSet);
            return (buff);
        } catch (SQLException e) {
            throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
        }
    }

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

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

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

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

    /**
     *   Release the prepared statements.
     *   <p>
     *   When the schema changes connections, the prepared statements
     *   have to be released because they contain connection-specific
     *   information for most databases.
     */
    public void releasePreparedStatements() {
        final String S_ProcName = "releasePreparedStatements";
        S_sqlSelectAttachmentDistinctClassCode = null;
        S_sqlSelectAttachmentBuff = null;
        if (stmtReadBuffByPKey != null) {
            try {
                stmtReadBuffByPKey.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByPKey = null;
        }
        if (stmtLockBuffByPKey != null) {
            try {
                stmtLockBuffByPKey.close();
            } catch (SQLException e) {
            }
            stmtLockBuffByPKey = null;
        }
        if (stmtAuditCreatedByPKey != null) {
            try {
                stmtAuditCreatedByPKey.close();
            } catch (SQLException e) {
            }
            stmtAuditCreatedByPKey = null;
        }
        if (stmtAuditUpdatedByPKey != null) {
            try {
                stmtAuditUpdatedByPKey.close();
            } catch (SQLException e) {
            }
            stmtAuditUpdatedByPKey = null;
        }
        if (stmtCreateByPKey != null) {
            try {
                stmtCreateByPKey.close();
            } catch (SQLException e) {
            }
            stmtCreateByPKey = null;
        }
        if (stmtUpdateByPKey != null) {
            try {
                stmtUpdateByPKey.close();
            } catch (SQLException e) {
            }
            stmtUpdateByPKey = null;
        }
        if (stmtDeleteByPKey != null) {
            try {
                stmtDeleteByPKey.close();
            } catch (SQLException e) {
            }
            stmtDeleteByPKey = null;
        }
        if (stmtDeleteByTenantIdx != null) {
            try {
                stmtDeleteByTenantIdx.close();
            } catch (SQLException e) {
                //            throw CFLib.getDefaultExceptionFactory().newDbException( getClass(),
                //               S_ProcName,
                //               e );
            } finally {
                stmtDeleteByTenantIdx = null;
            }
        }
        if (stmtDeleteByContactIdx != null) {
            try {
                stmtDeleteByContactIdx.close();
            } catch (SQLException e) {
                //            throw CFLib.getDefaultExceptionFactory().newDbException( getClass(),
                //               S_ProcName,
                //               e );
            } finally {
                stmtDeleteByContactIdx = null;
            }
        }
        if (stmtDeleteByUDescrIdx != null) {
            try {
                stmtDeleteByUDescrIdx.close();
            } catch (SQLException e) {
                //            throw CFLib.getDefaultExceptionFactory().newDbException( getClass(),
                //               S_ProcName,
                //               e );
            } finally {
                stmtDeleteByUDescrIdx = null;
            }
        }
        if (stmtDeleteByMimeTypeIdx != null) {
            try {
                stmtDeleteByMimeTypeIdx.close();
            } catch (SQLException e) {
                //            throw CFLib.getDefaultExceptionFactory().newDbException( getClass(),
                //               S_ProcName,
                //               e );
            } finally {
                stmtDeleteByMimeTypeIdx = null;
            }
        }
        if (stmtReadAllBuff != null) {
            try {
                stmtReadAllBuff.close();
            } catch (SQLException e) {
            }
            stmtReadAllBuff = null;
        }
        if (stmtReadBuffByIdIdx != null) {
            try {
                stmtReadBuffByIdIdx.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByIdIdx = null;
        }
        if (stmtReadBuffByTenantIdx != null) {
            try {
                stmtReadBuffByTenantIdx.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByTenantIdx = null;
        }
        if (stmtReadBuffByContactIdx != null) {
            try {
                stmtReadBuffByContactIdx.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByContactIdx = null;
        }
        if (stmtReadBuffByUDescrIdx != null) {
            try {
                stmtReadBuffByUDescrIdx.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByUDescrIdx = null;
        }
        if (stmtReadBuffByMimeTypeIdx != null) {
            try {
                stmtReadBuffByMimeTypeIdx.close();
            } catch (SQLException e) {
            }
            stmtReadBuffByMimeTypeIdx = null;
        }
    }
}