Java tutorial
// Description: Java 6 PostgreSQL Jdbc DbIO implementation for AccessSecurity. /* * MSS Code Factory 1.10 * * Copyright (c) 2012 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * *********************************************************************** * * Code manufactured * by MSS Code Factory version 1.9.3294 * * $Revision: 26 $ */ package net.sourceforge.msscodefactory.v1_10.MSSBamPg8; import java.math.*; import java.sql.*; import java.text.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_9.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.v1_10.MSSBam.*; /* * MSSBamPg8AccessSecurityTable PostgreSQL Jdbc DbIO implementation * for AccessSecurity. * * Data redaction is the responsibility of another layer. The raw database * interface returns everything regardless of whether the end user is * authorized to see the data. A redaction layer replaces protected/redacted * buffs with default values. They should not be included in client-side * filter sets, and the network redaction layer should actually eliminate them * before transmitting data to the client. The client should never see * redacted data in order to comply with data privacy regulations in Canada * and the US. */ public class MSSBamPg8AccessSecurityTable implements IMSSBamAccessSecurityTable { private MSSBamPg8Schema schema; public MSSBamPg8AccessSecurityTable(MSSBamPg8Schema argSchema) { schema = argSchema; } public void createAccessSecurity(MSSBamAuthorization Authorization, MSSBamAccessSecurityBuff Buff) { final String S_ProcName = "createAccessSecurity "; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } try { Connection cnx = schema.getCnx(); short Id = Buff.getRequiredId(); String Name = Buff.getRequiredName(); int Revision = 1; String sql = "INSERT INTO mssbam110.accsec( " + "id, " + "name" + ", revision )" + "VALUES ( " + Id + ", " + MSSBamPg8Schema.getQuotedString(Name) + ", " + Integer.toString(Revision) + " )"; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); int rowsAffected = stmt.executeUpdate(sql); if (rowsAffected != 1) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected 1 row to be affected by insert, not " + rowsAffected); } Buff.setRequiredRevision(Revision); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public final static String S_sqlSelectAccessSecurityDistinctClassCode = "SELECT " + "DISTINCT asec.ClassCode " + "FROM mssbam110.accsec AS asec "; public final static String S_sqlSelectAccessSecurityBuff = "SELECT " + "asec.Id, " + "asec.Name, " + "asec.Revision " + "FROM mssbam110.accsec AS asec "; protected MSSBamAccessSecurityBuff unpackAccessSecurityResultSetToBuff(ResultSet resultSet) throws SQLException { final String S_ProcName = "unpackAccessSecurityResultSetToBuff"; int idxcol = 1; MSSBamAccessSecurityBuff buff = schema.getFactoryAccessSecurity().newBuff(); buff.setRequiredId(resultSet.getShort(idxcol)); idxcol++; buff.setRequiredName(resultSet.getString(idxcol)); idxcol++; buff.setRequiredRevision(resultSet.getInt(idxcol)); return (buff); } public MSSBamAccessSecurityBuff readDerived(MSSBamAuthorization Authorization, MSSBamAccessSecurityPKey PKey) { final String S_ProcName = "readDerived()"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } MSSBamAccessSecurityBuff buff; short Id = PKey.getRequiredId(); buff = readBuff(Authorization, PKey); return (buff); } public MSSBamAccessSecurityBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "readAllDerived"; MSSBamAccessSecurityBuff[] buffArray; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buffArray = readAllBuff(Authorization); return (buffArray); } public MSSBamAccessSecurityBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, short Id) { final String S_ProcName = "MSSBamPg8AccessSecurityTable.readDerivedByIdIdx() "; MSSBamAccessSecurityBuff buff; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buff = readBuffByIdIdx(Authorization, Id); return (buff); } public MSSBamAccessSecurityBuff[] readDerivedByUNameIdx(MSSBamAuthorization Authorization, String Name) { final String S_ProcName = "readDerivedByUNameIdx"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } MSSBamAccessSecurityBuff[] buffList = readBuffByUNameIdx(Authorization, Name); return (buffList); } public MSSBamAccessSecurityBuff readBuff(MSSBamAuthorization Authorization, MSSBamAccessSecurityPKey PKey) { final String S_ProcName = "readBuff"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } try { Connection cnx = schema.getCnx(); short Id = PKey.getRequiredId(); String sql = S_sqlSelectAccessSecurityBuff + "WHERE " + "asec.Id = " + Short.toString(Id) + " "; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); if (resultSet.next()) { MSSBamAccessSecurityBuff buff = unpackAccessSecurityResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected"); } return (buff); } else { return (null); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamAccessSecurityBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "readAllBuff"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } try { Connection cnx = schema.getCnx(); String sql = S_sqlSelectAccessSecurityBuff + "ORDER BY " + "asec.Id ASC"; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); List<MSSBamAccessSecurityBuff> buffList = new ArrayList<MSSBamAccessSecurityBuff>(); while (resultSet.next()) { MSSBamAccessSecurityBuff buff = unpackAccessSecurityResultSetToBuff(resultSet); buffList.add(buff); } return (buffList.toArray(new MSSBamAccessSecurityBuff[0])); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamAccessSecurityBuff readBuffByIdIdx(MSSBamAuthorization Authorization, short Id) { final String S_ProcName = "readBuffByIdIdx"; try { Connection cnx = schema.getCnx(); String sql = S_sqlSelectAccessSecurityBuff + "WHERE " + "asec.Id = " + Short.toString(Id) + " "; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); if (resultSet.next()) { MSSBamAccessSecurityBuff buff = unpackAccessSecurityResultSetToBuff(resultSet); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-buff response, " + resultSet.getRow() + " rows selected"); } return (buff); } else { return (null); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamAccessSecurityBuff[] readBuffByUNameIdx(MSSBamAuthorization Authorization, String Name) { final String S_ProcName = "readBuffByUNameIdx"; try { Connection cnx = schema.getCnx(); String sql = S_sqlSelectAccessSecurityBuff + "WHERE " + "asec.Name = " + MSSBamPg8Schema.getQuotedString(Name) + " " + "ORDER BY " + "asec.Id ASC"; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); List<MSSBamAccessSecurityBuff> buffList = new ArrayList<MSSBamAccessSecurityBuff>(); while (resultSet.next()) { MSSBamAccessSecurityBuff buff = unpackAccessSecurityResultSetToBuff(resultSet); buffList.add(buff); } return (buffList.toArray(new MSSBamAccessSecurityBuff[0])); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public void updateAccessSecurity(MSSBamAuthorization Authorization, MSSBamAccessSecurityBuff Buff) { final String S_ProcName = "updateAccessSecurity"; try { Connection cnx = schema.getCnx(); short Id = Buff.getRequiredId(); String Name = Buff.getRequiredName(); int Revision = Buff.getRequiredRevision(); MSSBamAccessSecurityBuff readBuff = readBuffByIdIdx(Authorization, Id); int oldRevision = readBuff.getRequiredRevision(); if (oldRevision != Revision) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName, Buff); } int newRevision = Revision + 1; String sql = "UPDATE mssbam110.accsec " + "SET " + "Id = " + MSSBamPg8Schema.getInt16String(Id) + ", " + "Name = " + MSSBamPg8Schema.getQuotedString(Name) + ", " + "Revision = " + Integer.toString(newRevision) + " " + "WHERE " + "Id = " + Short.toString(Id) + " " + "AND " + "Revision = " + Integer.toString(Revision); Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); int rowsAffected = stmt.executeUpdate(sql); if (rowsAffected != 1) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected 1 row to be affected by update, not " + rowsAffected); } Buff.setRequiredRevision(newRevision); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public void deleteAccessSecurity(MSSBamAuthorization Authorization, MSSBamAccessSecurityBuff Buff) { final String S_ProcName = "deleteAccessSecurity"; try { Connection cnx = schema.getCnx(); short Id = Buff.getRequiredId(); String Name = Buff.getRequiredName(); int Revision = Buff.getRequiredRevision(); MSSBamAccessSecurityBuff readBuff = readBuffByIdIdx(Authorization, Id); int oldRevision = readBuff.getRequiredRevision(); if (oldRevision != Revision) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName, Buff); } String sql = "DELETE FROM mssbam110.accsec " + "WHERE " + "Id = " + Short.toString(Id) + " " + "AND " + "Revision = " + Integer.toString(Revision); ; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); int rowsAffected = stmt.executeUpdate(sql); if (rowsAffected != 1) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected 1 row to be affected by delete, not " + rowsAffected); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamCursor openAccessSecurityCursorAll(MSSBamAuthorization Authorization) { String sql = "SELECT " + "asec.Id, " + "asec.Name, " + "asec.Revision " + "FROM mssbam110.accsec AS asec " + "ORDER BY " + "asec.Id ASC"; MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql); return (cursor); } public MSSBamCursor openAccessSecurityCursorByUNameIdx(MSSBamAuthorization Authorization, String Name) { String sql = "SELECT " + "asec.Id, " + "asec.Name, " + "asec.Revision " + "FROM mssbam110.accsec AS asec " + "WHERE " + "asec.Name = " + MSSBamPg8Schema.getQuotedString(Name) + " " + "ORDER BY " + "asec.Id ASC"; MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql); return (cursor); } public void closeAccessSecurityCursor(MSSBamCursor Cursor) { try { Cursor.getResultSet().close(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), "closeAccessSecurityCursor", e); } } public MSSBamAccessSecurityBuff nextAccessSecurityCursor(MSSBamCursor Cursor) { final String S_ProcName = "nextAccessSecurityCursor"; try { ResultSet resultSet = Cursor.getResultSet(); if (!resultSet.next()) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "No more results available"); } MSSBamAccessSecurityBuff buff = unpackAccessSecurityResultSetToBuff(resultSet); return (buff); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamAccessSecurityBuff prevAccessSecurityCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamAccessSecurityBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextAccessSecurityCursor(Cursor); } return (buff); } public MSSBamAccessSecurityBuff firstAccessSecurityCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamAccessSecurityBuff buff = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { buff = nextAccessSecurityCursor(Cursor); } return (buff); } public MSSBamAccessSecurityBuff lastAccessSecurityCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAccessSecurityCursor"); } public MSSBamAccessSecurityBuff nthAccessSecurityCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamAccessSecurityBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextAccessSecurityCursor(Cursor); } return (buff); } }