Java tutorial
// Description: Java 6 PostgreSQL Jdbc DbIO implementation for Cluster. /* * 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.*; /* * MSSBamPg8ClusterTable PostgreSQL Jdbc DbIO implementation * for Cluster. * * 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 MSSBamPg8ClusterTable implements IMSSBamClusterTable { private MSSBamPg8Schema schema; public MSSBamPg8ClusterTable(MSSBamPg8Schema argSchema) { schema = argSchema; } public void createCluster(MSSBamAuthorization Authorization, MSSBamClusterBuff Buff) { final String S_ProcName = "createCluster "; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } try { Connection cnx = schema.getCnx(); long Id = schema.nextClusterIdGen(); String FullDomainName = Buff.getRequiredFullDomainName(); int Revision = 1; String sql = "INSERT INTO mssbam110.cluster( " + "id, " + "fulldomainname" + ", revision )" + "VALUES ( " + Id + ", " + MSSBamPg8Schema.getQuotedString(FullDomainName) + ", " + 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.setRequiredId(Id); Buff.setRequiredRevision(Revision); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public final static String S_sqlSelectClusterDistinctClassCode = "SELECT " + "DISTINCT clus.ClassCode " + "FROM mssbam110.cluster AS clus "; public final static String S_sqlSelectClusterBuff = "SELECT " + "clus.Id, " + "clus.FullDomainName, " + "clus.Revision " + "FROM mssbam110.cluster AS clus "; protected MSSBamClusterBuff unpackClusterResultSetToBuff(ResultSet resultSet) throws SQLException { final String S_ProcName = "unpackClusterResultSetToBuff"; int idxcol = 1; MSSBamClusterBuff buff = schema.getFactoryCluster().newBuff(); buff.setRequiredId(resultSet.getLong(idxcol)); idxcol++; buff.setRequiredFullDomainName(resultSet.getString(idxcol)); idxcol++; buff.setRequiredRevision(resultSet.getInt(idxcol)); return (buff); } public MSSBamClusterBuff readDerived(MSSBamAuthorization Authorization, MSSBamClusterPKey PKey) { final String S_ProcName = "readDerived()"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } MSSBamClusterBuff buff; long Id = PKey.getRequiredId(); buff = readBuff(Authorization, PKey); return (buff); } public MSSBamClusterBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "readAllDerived"; MSSBamClusterBuff[] buffArray; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buffArray = readAllBuff(Authorization); return (buffArray); } public MSSBamClusterBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamPg8ClusterTable.readDerivedByIdIdx() "; MSSBamClusterBuff buff; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buff = readBuffByIdIdx(Authorization, Id); return (buff); } public MSSBamClusterBuff readDerivedByUDomainNameIdx(MSSBamAuthorization Authorization, String FullDomainName) { final String S_ProcName = "MSSBamPg8ClusterTable.readDerivedByUDomainNameIdx() "; MSSBamClusterBuff buff; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } buff = readBuffByUDomainNameIdx(Authorization, FullDomainName); return (buff); } public MSSBamClusterBuff readBuff(MSSBamAuthorization Authorization, MSSBamClusterPKey PKey) { final String S_ProcName = "readBuff"; if (!schema.isTransactionOpen()) { throw CFLib.getDefaultExceptionFactory().newUsageException(getClass(), S_ProcName, "Transaction not open"); } try { Connection cnx = schema.getCnx(); long Id = PKey.getRequiredId(); String sql = S_sqlSelectClusterBuff + "WHERE " + "clus.Id = " + Long.toString(Id) + " "; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); if (resultSet.next()) { MSSBamClusterBuff buff = unpackClusterResultSetToBuff(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 MSSBamClusterBuff[] 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_sqlSelectClusterBuff + "ORDER BY " + "clus.Id ASC"; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); List<MSSBamClusterBuff> buffList = new ArrayList<MSSBamClusterBuff>(); while (resultSet.next()) { MSSBamClusterBuff buff = unpackClusterResultSetToBuff(resultSet); buffList.add(buff); } return (buffList.toArray(new MSSBamClusterBuff[0])); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamClusterBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "readBuffByIdIdx"; try { Connection cnx = schema.getCnx(); String sql = S_sqlSelectClusterBuff + "WHERE " + "clus.Id = " + Long.toString(Id) + " "; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); if (resultSet.next()) { MSSBamClusterBuff buff = unpackClusterResultSetToBuff(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 MSSBamClusterBuff readBuffByUDomainNameIdx(MSSBamAuthorization Authorization, String FullDomainName) { final String S_ProcName = "readBuffByUDomainNameIdx"; try { Connection cnx = schema.getCnx(); String sql = S_sqlSelectClusterBuff + "WHERE " + "clus.FullDomainName = " + MSSBamPg8Schema.getQuotedString(FullDomainName) + " "; Statement stmt = cnx.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = stmt.executeQuery(sql); if (resultSet.next()) { MSSBamClusterBuff buff = unpackClusterResultSetToBuff(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 void updateCluster(MSSBamAuthorization Authorization, MSSBamClusterBuff Buff) { final String S_ProcName = "updateCluster"; try { Connection cnx = schema.getCnx(); long Id = Buff.getRequiredId(); String FullDomainName = Buff.getRequiredFullDomainName(); int Revision = Buff.getRequiredRevision(); MSSBamClusterBuff 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.cluster " + "SET " + "Id = " + MSSBamPg8Schema.getInt64String(Id) + ", " + "FullDomainName = " + MSSBamPg8Schema.getQuotedString(FullDomainName) + ", " + "Revision = " + Integer.toString(newRevision) + " " + "WHERE " + "Id = " + Long.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 deleteCluster(MSSBamAuthorization Authorization, MSSBamClusterBuff Buff) { final String S_ProcName = "deleteCluster"; try { Connection cnx = schema.getCnx(); long Id = Buff.getRequiredId(); String FullDomainName = Buff.getRequiredFullDomainName(); int Revision = Buff.getRequiredRevision(); MSSBamClusterBuff readBuff = readBuffByIdIdx(Authorization, Id); int oldRevision = readBuff.getRequiredRevision(); if (oldRevision != Revision) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), S_ProcName, Buff); } String sql = "DELETE FROM mssbam110.cluster " + "WHERE " + "Id = " + Long.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 openClusterCursorAll(MSSBamAuthorization Authorization) { String sql = "SELECT " + "clus.Id, " + "clus.FullDomainName, " + "clus.Revision " + "FROM mssbam110.cluster AS clus " + "ORDER BY " + "clus.Id ASC"; MSSBamCursor cursor = new MSSBamPg8Cursor(Authorization, schema, sql); return (cursor); } public void closeClusterCursor(MSSBamCursor Cursor) { try { Cursor.getResultSet().close(); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), "closeClusterCursor", e); } } public MSSBamClusterBuff nextClusterCursor(MSSBamCursor Cursor) { final String S_ProcName = "nextClusterCursor"; try { ResultSet resultSet = Cursor.getResultSet(); if (!resultSet.next()) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "No more results available"); } MSSBamClusterBuff buff = unpackClusterResultSetToBuff(resultSet); return (buff); } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } } public MSSBamClusterBuff prevClusterCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamClusterBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextClusterCursor(Cursor); } return (buff); } public MSSBamClusterBuff firstClusterCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamClusterBuff buff = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { buff = nextClusterCursor(Cursor); } return (buff); } public MSSBamClusterBuff lastClusterCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastClusterCursor"); } public MSSBamClusterBuff nthClusterCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamClusterBuff buff = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { buff = nextClusterCursor(Cursor); } return (buff); } }