Java tutorial
// Description: Java 7 MySQL Jdbc DbIO implementation for SecSession. /* * CF Asterisk 11 Configuration Model * * Copyright (c) 2013-2014 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/>. * * This source code incorporates modified modules originally licensed * under the Apache 2.0 license by MSS Code Factory including CFSecurity * (net-sourceforge-msscodefactory-2.0-cfsecurity.xml), * CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and * CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the * required models being available as part of the MSS Code Factory 1.11 * distribution source and install zips. * * You can download installations of MSS Code Factory 1.11 from * http://msscodefactory.sourceforge.net/ * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstMySql; import java.math.*; import java.sql.*; import java.text.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAst.*; /* * CFAstMySqlSecSessionTable MySQL Jdbc DbIO implementation * for SecSession. */ public class CFAstMySqlSecSessionTable implements ICFAstSecSessionTable { private CFAstMySqlSchema schema; protected PreparedStatement stmtReadBuffByPKey = null; protected PreparedStatement stmtLockBuffByPKey = null; protected PreparedStatement stmtCreateByPKey = null; protected PreparedStatement stmtUpdateByPKey = null; protected PreparedStatement stmtDeleteByPKey = null; protected PreparedStatement stmtReadAllBuff = null; protected PreparedStatement stmtReadBuffByIdIdx = null; protected PreparedStatement stmtReadBuffBySecUserIdx = null; protected PreparedStatement stmtReadBuffByStartIdx = null; protected PreparedStatement stmtReadBuffByFinishIdx = null; protected PreparedStatement stmtDeleteByIdIdx = null; protected PreparedStatement stmtDeleteBySecUserIdx = null; protected PreparedStatement stmtDeleteByStartIdx = null; protected PreparedStatement stmtDeleteByFinishIdx = null; public CFAstMySqlSecSessionTable(CFAstMySqlSchema argSchema) { schema = argSchema; } public void createSecSession(CFAstAuthorization Authorization, CFAstSecSessionBuff Buff) { final String S_ProcName = "createSecSession"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } ResultSet resultSet = null; try { UUID SecUserId = Buff.getRequiredSecUserId(); Calendar Start = Buff.getRequiredStart(); Calendar Finish = Buff.getOptionalFinish(); UUID SecSessionId = UUID.randomUUID(); Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_create_secsess( ?, ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "timestamp( ? )" + ", " + "timestamp( ? )" + " )"; if (stmtCreateByPKey == null) { stmtCreateByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtCreateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtCreateByPKey.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtCreateByPKey.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtCreateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtCreateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtCreateByPKey.setString(argIdx++, "SESS"); stmtCreateByPKey.setString(argIdx++, SecSessionId.toString()); stmtCreateByPKey.setString(argIdx++, SecUserId.toString()); stmtCreateByPKey.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Start)); if (Finish != null) { stmtCreateByPKey.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Finish)); } else { stmtCreateByPKey.setNull(argIdx++, java.sql.Types.VARCHAR); } try { resultSet = stmtCreateByPKey.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff createdBuff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } Buff.setRequiredSecSessionId(createdBuff.getRequiredSecSessionId()); Buff.setRequiredSecUserId(createdBuff.getRequiredSecUserId()); Buff.setRequiredStart(createdBuff.getRequiredStart()); Buff.setOptionalFinish(createdBuff.getOptionalFinish()); Buff.setRequiredRevision(createdBuff.getRequiredRevision()); } else { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected a single-record response, " + resultSet.getRow() + " rows selected"); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } resultSet = null; } } } protected static String S_sqlSelectSecSessionDistinctClassCode = null; public String getSqlSelectSecSessionDistinctClassCode() { if (S_sqlSelectSecSessionDistinctClassCode == null) { S_sqlSelectSecSessionDistinctClassCode = "SELECT " + "DISTINCT sess.ClassCode " + "FROM " + schema.getLowerSchemaDbName() + ".SecSess AS sess "; } return (S_sqlSelectSecSessionDistinctClassCode); } protected static String S_sqlSelectSecSessionBuff = null; public String getSqlSelectSecSessionBuff() { if (S_sqlSelectSecSessionBuff == null) { S_sqlSelectSecSessionBuff = "SELECT " + "sess.SecSessionId, " + "sess.SecUserId, " + "date_format( sess.start_ts, '%Y-%m-%d %H:%i:%S' ) as start_ts, " + "date_format( sess.finish_ts, '%Y-%m-%d %H:%i:%S' ) as finish_ts, " + "sess.Revision " + "FROM " + schema.getLowerSchemaDbName() + ".SecSess AS sess "; } return (S_sqlSelectSecSessionBuff); } protected CFAstSecSessionBuff unpackSecSessionResultSetToBuff(ResultSet resultSet) throws SQLException { final String S_ProcName = "unpackSecSessionResultSetToBuff"; int idxcol = 1; CFAstSecSessionBuff buff = schema.getFactorySecSession().newBuff(); buff.setRequiredSecSessionId(CFAstMySqlSchema.convertUuidString(resultSet.getString(idxcol))); idxcol++; buff.setRequiredSecUserId(CFAstMySqlSchema.convertUuidString(resultSet.getString(idxcol))); idxcol++; buff.setRequiredStart(CFAstMySqlSchema.convertTimestampString(resultSet.getString(idxcol))); idxcol++; { Calendar colVal = CFAstMySqlSchema.convertTimestampString(resultSet.getString(idxcol)); if (resultSet.wasNull()) { buff.setOptionalFinish(null); } else { buff.setOptionalFinish(colVal); } } idxcol++; buff.setRequiredRevision(resultSet.getInt(idxcol)); return (buff); } public CFAstSecSessionBuff readDerived(CFAstAuthorization Authorization, CFAstSecSessionPKey PKey) { final String S_ProcName = "readDerived"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } CFAstSecSessionBuff buff; buff = readBuff(Authorization, PKey); return (buff); } public CFAstSecSessionBuff lockDerived(CFAstAuthorization Authorization, CFAstSecSessionPKey PKey) { final String S_ProcName = "lockDerived"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } CFAstSecSessionBuff buff; buff = lockBuff(Authorization, PKey); return (buff); } public CFAstSecSessionBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "readAllDerived"; CFAstSecSessionBuff[] buffArray; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buffArray = readAllBuff(Authorization); return (buffArray); } public CFAstSecSessionBuff readDerivedByIdIdx(CFAstAuthorization Authorization, UUID SecSessionId) { final String S_ProcName = "CFAstMySqlSecSessionTable.readDerivedByIdIdx() "; CFAstSecSessionBuff buff; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buff = readBuffByIdIdx(Authorization, SecSessionId); return (buff); } public CFAstSecSessionBuff[] readDerivedBySecUserIdx(CFAstAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "readDerivedBySecUserIdx"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } CFAstSecSessionBuff[] buffList = readBuffBySecUserIdx(Authorization, SecUserId); return (buffList); } public CFAstSecSessionBuff readDerivedByStartIdx(CFAstAuthorization Authorization, UUID SecUserId, Calendar Start) { final String S_ProcName = "CFAstMySqlSecSessionTable.readDerivedByStartIdx() "; CFAstSecSessionBuff buff; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buff = readBuffByStartIdx(Authorization, SecUserId, Start); return (buff); } public CFAstSecSessionBuff[] readDerivedByFinishIdx(CFAstAuthorization Authorization, UUID SecUserId, Calendar Finish) { final String S_ProcName = "readDerivedByFinishIdx"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } CFAstSecSessionBuff[] buffList = readBuffByFinishIdx(Authorization, SecUserId, Finish); return (buffList); } public CFAstSecSessionBuff readBuff(CFAstAuthorization Authorization, CFAstSecSessionPKey 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(); UUID SecSessionId = PKey.getRequiredSecSessionId(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_read_secsess( ?, ?, ?, ?, ?" + ", " + "?" + " )"; 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.setString(argIdx++, SecSessionId.toString()); try { resultSet = stmtReadBuffByPKey.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && 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 CFAstSecSessionBuff lockBuff(CFAstAuthorization Authorization, CFAstSecSessionPKey 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(); UUID SecSessionId = PKey.getRequiredSecSessionId(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_lock_secsess( ?, ?, ?, ?, ?" + ", " + "?" + " )"; 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.setString(argIdx++, SecSessionId.toString()); try { resultSet = stmtLockBuffByPKey.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && 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 CFAstSecSessionBuff[] readAllBuff(CFAstAuthorization 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 " + schema.getLowerSchemaDbName() + ".sp_read_secsess_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()); try { resultSet = stmtReadAllBuff.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } List<CFAstSecSessionBuff> buffList = new LinkedList<CFAstSecSessionBuff>(); while ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); buffList.add(buff); } int idx = 0; CFAstSecSessionBuff[] retBuff = new CFAstSecSessionBuff[buffList.size()]; Iterator<CFAstSecSessionBuff> 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 CFAstSecSessionBuff readBuffByIdIdx(CFAstAuthorization Authorization, UUID SecSessionId) { final String S_ProcName = "readBuffByIdIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_read_secsess_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.setString(argIdx++, SecSessionId.toString()); try { resultSet = stmtReadBuffByIdIdx.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && 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 CFAstSecSessionBuff[] readBuffBySecUserIdx(CFAstAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "readBuffBySecUserIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_read_secsess_by_secuseridx( ?, ?, ?, ?, ?" + ", " + "?" + " )"; if (stmtReadBuffBySecUserIdx == null) { stmtReadBuffBySecUserIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtReadBuffBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffBySecUserIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtReadBuffBySecUserIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtReadBuffBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtReadBuffBySecUserIdx.setString(argIdx++, SecUserId.toString()); try { resultSet = stmtReadBuffBySecUserIdx.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } List<CFAstSecSessionBuff> buffList = new LinkedList<CFAstSecSessionBuff>(); while ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); buffList.add(buff); } int idx = 0; CFAstSecSessionBuff[] retBuff = new CFAstSecSessionBuff[buffList.size()]; Iterator<CFAstSecSessionBuff> 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 CFAstSecSessionBuff readBuffByStartIdx(CFAstAuthorization Authorization, UUID SecUserId, Calendar Start) { final String S_ProcName = "readBuffByStartIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_read_secsess_by_startidx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "timestamp( ? )" + " )"; if (stmtReadBuffByStartIdx == null) { stmtReadBuffByStartIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtReadBuffByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffByStartIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtReadBuffByStartIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtReadBuffByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtReadBuffByStartIdx.setString(argIdx++, SecUserId.toString()); stmtReadBuffByStartIdx.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Start)); try { resultSet = stmtReadBuffByStartIdx.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && 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 CFAstSecSessionBuff[] readBuffByFinishIdx(CFAstAuthorization Authorization, UUID SecUserId, Calendar Finish) { final String S_ProcName = "readBuffByFinishIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_read_secsess_by_finishidx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "timestamp( ? )" + " )"; if (stmtReadBuffByFinishIdx == null) { stmtReadBuffByFinishIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtReadBuffByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffByFinishIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtReadBuffByFinishIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtReadBuffByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtReadBuffByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtReadBuffByFinishIdx.setString(argIdx++, SecUserId.toString()); if (Finish != null) { stmtReadBuffByFinishIdx.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Finish)); } else { stmtReadBuffByFinishIdx.setNull(argIdx++, java.sql.Types.VARCHAR); } try { resultSet = stmtReadBuffByFinishIdx.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } List<CFAstSecSessionBuff> buffList = new LinkedList<CFAstSecSessionBuff>(); while ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); buffList.add(buff); } int idx = 0; CFAstSecSessionBuff[] retBuff = new CFAstSecSessionBuff[buffList.size()]; Iterator<CFAstSecSessionBuff> 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 updateSecSession(CFAstAuthorization Authorization, CFAstSecSessionBuff Buff) { final String S_ProcName = "updateSecSession"; ResultSet resultSet = null; try { UUID SecSessionId = Buff.getRequiredSecSessionId(); UUID SecUserId = Buff.getRequiredSecUserId(); Calendar Start = Buff.getRequiredStart(); Calendar Finish = Buff.getOptionalFinish(); int Revision = Buff.getRequiredRevision(); Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_update_secsess( ?, ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + ", " + "timestamp( ? )" + ", " + "timestamp( ? )" + ", " + "?" + " )"; if (stmtUpdateByPKey == null) { stmtUpdateByPKey = cnx.prepareStatement(sql); } int argIdx = 1; stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtUpdateByPKey.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtUpdateByPKey.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtUpdateByPKey.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtUpdateByPKey.setString(argIdx++, "SESS"); stmtUpdateByPKey.setString(argIdx++, SecSessionId.toString()); stmtUpdateByPKey.setString(argIdx++, SecUserId.toString()); stmtUpdateByPKey.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Start)); if (Finish != null) { stmtUpdateByPKey.setString(argIdx++, CFAstMySqlSchema.getTimestampString(Finish)); } else { stmtUpdateByPKey.setNull(argIdx++, java.sql.Types.VARCHAR); } stmtUpdateByPKey.setInt(argIdx++, Revision); try { resultSet = stmtUpdateByPKey.executeQuery(); } catch (SQLException e) { if (e.getErrorCode() != 1329) { throw e; } resultSet = null; } if ((resultSet != null) && resultSet.next()) { CFAstSecSessionBuff updatedBuff = unpackSecSessionResultSetToBuff(resultSet); if ((resultSet != null) && resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } Buff.setRequiredSecUserId(updatedBuff.getRequiredSecUserId()); Buff.setRequiredStart(updatedBuff.getRequiredStart()); Buff.setOptionalFinish(updatedBuff.getOptionalFinish()); Buff.setRequiredRevision(updatedBuff.getRequiredRevision()); } else { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected a single-record response, " + resultSet.getRow() + " rows selected"); } } 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 deleteSecSession(CFAstAuthorization Authorization, CFAstSecSessionBuff Buff) { final String S_ProcName = "deleteSecSession"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); UUID SecSessionId = Buff.getRequiredSecSessionId(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_delete_secsess( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "?" + " )"; 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.setString(argIdx++, SecSessionId.toString()); stmtDeleteByPKey.setInt(argIdx++, Buff.getRequiredRevision()); ; stmtDeleteByPKey.executeUpdate(); } 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 deleteSecSessionByIdIdx(CFAstAuthorization Authorization, UUID argSecSessionId) { final String S_ProcName = "deleteSecSessionByIdIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_delete_secsess_by_ididx( ?, ?, ?, ?, ?" + ", " + "?" + " )"; if (stmtDeleteByIdIdx == null) { stmtDeleteByIdIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByIdIdx.setString(argIdx++, argSecSessionId.toString()); stmtDeleteByIdIdx.executeUpdate(); } 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 deleteSecSessionByIdIdx(CFAstAuthorization Authorization, CFAstSecSessionPKey argKey) { deleteSecSessionByIdIdx(Authorization, argKey.getRequiredSecSessionId()); } public void deleteSecSessionBySecUserIdx(CFAstAuthorization Authorization, UUID argSecUserId) { final String S_ProcName = "deleteSecSessionBySecUserIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_delete_secsess_by_secuseridx( ?, ?, ?, ?, ?" + ", " + "?" + " )"; if (stmtDeleteBySecUserIdx == null) { stmtDeleteBySecUserIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteBySecUserIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteBySecUserIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteBySecUserIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteBySecUserIdx.setString(argIdx++, argSecUserId.toString()); stmtDeleteBySecUserIdx.executeUpdate(); } 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 deleteSecSessionBySecUserIdx(CFAstAuthorization Authorization, CFAstSecSessionBySecUserIdxKey argKey) { deleteSecSessionBySecUserIdx(Authorization, argKey.getRequiredSecUserId()); } public void deleteSecSessionByStartIdx(CFAstAuthorization Authorization, UUID argSecUserId, Calendar argStart) { final String S_ProcName = "deleteSecSessionByStartIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_delete_secsess_by_startidx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "timestamp( ? )" + " )"; if (stmtDeleteByStartIdx == null) { stmtDeleteByStartIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByStartIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByStartIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByStartIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByStartIdx.setString(argIdx++, argSecUserId.toString()); stmtDeleteByStartIdx.setString(argIdx++, CFAstMySqlSchema.getTimestampString(argStart)); stmtDeleteByStartIdx.executeUpdate(); } 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 deleteSecSessionByStartIdx(CFAstAuthorization Authorization, CFAstSecSessionByStartIdxKey argKey) { deleteSecSessionByStartIdx(Authorization, argKey.getRequiredSecUserId(), argKey.getRequiredStart()); } public void deleteSecSessionByFinishIdx(CFAstAuthorization Authorization, UUID argSecUserId, Calendar argFinish) { final String S_ProcName = "deleteSecSessionByFinishIdx"; ResultSet resultSet = null; try { Connection cnx = schema.getCnx(); String sql = "call " + schema.getLowerSchemaDbName() + ".sp_delete_secsess_by_finishidx( ?, ?, ?, ?, ?" + ", " + "?" + ", " + "timestamp( ? )" + " )"; if (stmtDeleteByFinishIdx == null) { stmtDeleteByFinishIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByFinishIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByFinishIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByFinishIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByFinishIdx.setString(argIdx++, argSecUserId.toString()); if (argFinish != null) { stmtDeleteByFinishIdx.setString(argIdx++, CFAstMySqlSchema.getTimestampString(argFinish)); } else { stmtDeleteByFinishIdx.setNull(argIdx++, java.sql.Types.VARCHAR); } stmtDeleteByFinishIdx.executeUpdate(); } 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 deleteSecSessionByFinishIdx(CFAstAuthorization Authorization, CFAstSecSessionByFinishIdxKey argKey) { deleteSecSessionByFinishIdx(Authorization, argKey.getRequiredSecUserId(), argKey.getOptionalFinish()); } public CFAstCursor openSecSessionCursorAll(CFAstAuthorization Authorization) { String sql = getSqlSelectSecSessionBuff() + "ORDER BY " + "sess.SecSessionId ASC"; CFAstCursor cursor = new CFAstMySqlCursor(Authorization, schema, sql); return (cursor); } public CFAstCursor openSecSessionCursorBySecUserIdx(CFAstAuthorization Authorization, UUID SecUserId) { String sql = getSqlSelectSecSessionBuff() + "WHERE " + "sess.SecUserId = '" + SecUserId.toString() + "' " + "ORDER BY " + "sess.SecSessionId ASC"; CFAstCursor cursor = new CFAstMySqlCursor(Authorization, schema, sql); return (cursor); } public CFAstCursor openSecSessionCursorByFinishIdx(CFAstAuthorization Authorization, UUID SecUserId, Calendar Finish) { String sql = getSqlSelectSecSessionBuff() + "WHERE " + "sess.SecUserId = '" + SecUserId.toString() + "' " + "AND " + ((Finish == null) ? "sess.finish_ts is null " : "sess.finish_ts = timestamp( " + CFAstMySqlSchema.getTimestampString(Finish) + " ) ") + "ORDER BY " + "sess.SecSessionId ASC"; CFAstCursor cursor = new CFAstMySqlCursor(Authorization, schema, sql); return (cursor); } public void closeSecSessionCursor(CFAstCursor Cursor) { try { Cursor.getResultSet().close(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), "closeSecSessionCursor", e); } } public CFAstSecSessionBuff nextSecSessionCursor(CFAstCursor Cursor) { final String S_ProcName = "nextSecSessionCursor"; try { ResultSet resultSet = Cursor.getResultSet(); if (!resultSet.next()) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "No more results available"); } CFAstSecSessionBuff buff = unpackSecSessionResultSetToBuff(resultSet); return (buff); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public CFAstSecSessionBuff prevSecSessionCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstSecSessionBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextSecSessionCursor(Cursor); } return (buff); } public CFAstSecSessionBuff firstSecSessionCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstSecSessionBuff buff = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { buff = nextSecSessionCursor(Cursor); } return (buff); } public CFAstSecSessionBuff lastSecSessionCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecSessionCursor"); } public CFAstSecSessionBuff nthSecSessionCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstSecSessionBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextSecSessionCursor(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() { S_sqlSelectSecSessionDistinctClassCode = null; S_sqlSelectSecSessionBuff = null; if (stmtReadBuffByPKey != null) { try { stmtReadBuffByPKey.close(); } catch (SQLException e) { } stmtReadBuffByPKey = null; } if (stmtLockBuffByPKey != null) { try { stmtLockBuffByPKey.close(); } catch (SQLException e) { } stmtLockBuffByPKey = 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 (stmtDeleteByIdIdx != null) { try { stmtDeleteByIdIdx.close(); } catch (SQLException e) { } stmtDeleteByIdIdx = null; } if (stmtDeleteBySecUserIdx != null) { try { stmtDeleteBySecUserIdx.close(); } catch (SQLException e) { } stmtDeleteBySecUserIdx = null; } if (stmtDeleteByStartIdx != null) { try { stmtDeleteByStartIdx.close(); } catch (SQLException e) { } stmtDeleteByStartIdx = null; } if (stmtDeleteByFinishIdx != null) { try { stmtDeleteByFinishIdx.close(); } catch (SQLException e) { } stmtDeleteByFinishIdx = null; } if (stmtReadAllBuff != null) { try { stmtReadAllBuff.close(); } catch (SQLException e) { } stmtReadAllBuff = null; } if (stmtReadBuffByIdIdx != null) { try { stmtReadBuffByIdIdx.close(); } catch (SQLException e) { } stmtReadBuffByIdIdx = null; } if (stmtReadBuffBySecUserIdx != null) { try { stmtReadBuffBySecUserIdx.close(); } catch (SQLException e) { } stmtReadBuffBySecUserIdx = null; } if (stmtReadBuffByStartIdx != null) { try { stmtReadBuffByStartIdx.close(); } catch (SQLException e) { } stmtReadBuffByStartIdx = null; } if (stmtReadBuffByFinishIdx != null) { try { stmtReadBuffByFinishIdx.close(); } catch (SQLException e) { } stmtReadBuffByFinishIdx = null; } } }