Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for AuditAction. /* * 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.CFAstRam; import java.sql.*; 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.*; import net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.*; /* * CFAstRamAuditActionTable in-memory RAM DbIO implementation * for AuditAction. */ public class CFAstRamAuditActionTable implements ICFAstAuditActionTable { private CFAstRamSchema schema; private Map<CFAstAuditActionPKey, CFAstAuditActionBuff> dictByPKey = new HashMap<CFAstAuditActionPKey, CFAstAuditActionBuff>(); private SortedMap<CFAstAuditActionByUDescrIdxKey, CFAstAuditActionBuff> dictByUDescrIdx = new TreeMap<CFAstAuditActionByUDescrIdxKey, CFAstAuditActionBuff>(); public CFAstRamAuditActionTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createAuditAction(CFAstAuthorization Authorization, CFAstAuditActionBuff Buff) { CFAstAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); Buff.setRequiredAuditActionId(pkey.getRequiredAuditActionId()); CFAstAuditActionByUDescrIdxKey keyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createAuditAction", pkey); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createAuditAction", "AuditActionUDescrIdx", keyUDescrIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); } public CFAstAuditActionBuff readDerived(CFAstAuthorization Authorization, CFAstAuditActionPKey PKey) { final String S_ProcName = "CFAstRamAuditAction.readDerived() "; CFAstAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(PKey.getRequiredAuditActionId()); CFAstAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstAuditActionBuff lockDerived(CFAstAuthorization Authorization, CFAstAuditActionPKey PKey) { final String S_ProcName = "CFAstRamAuditAction.readDerived() "; CFAstAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(PKey.getRequiredAuditActionId()); CFAstAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstAuditActionBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamAuditAction.readAllDerived() "; CFAstAuditActionBuff[] retList = new CFAstAuditActionBuff[dictByPKey.values().size()]; Iterator<CFAstAuditActionBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstAuditActionBuff readDerivedByUDescrIdx(CFAstAuthorization Authorization, String Description) { final String S_ProcName = "CFAstRamAuditAction.readDerivedByUDescrIdx() "; CFAstAuditActionByUDescrIdxKey key = schema.getFactoryAuditAction().newUDescrIdxKey(); key.setRequiredDescription(Description); CFAstAuditActionBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public CFAstAuditActionBuff readDerivedByIdIdx(CFAstAuthorization Authorization, short AuditActionId) { final String S_ProcName = "CFAstRamAuditAction.readDerivedByIdIdx() "; CFAstAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(AuditActionId); CFAstAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstAuditActionBuff readBuff(CFAstAuthorization Authorization, CFAstAuditActionPKey PKey) { final String S_ProcName = "CFAstRamAuditAction.readBuff() "; CFAstAuditActionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("AUDT"))) { buff = null; } return (buff); } public CFAstAuditActionBuff lockBuff(CFAstAuthorization Authorization, CFAstAuditActionPKey PKey) { final String S_ProcName = "CFAstRamAuditAction.readBuff() "; CFAstAuditActionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("AUDT"))) { buff = null; } return (buff); } public CFAstAuditActionBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamAuditAction.readAllBuff() "; CFAstAuditActionBuff buff; ArrayList<CFAstAuditActionBuff> filteredList = new ArrayList<CFAstAuditActionBuff>(); CFAstAuditActionBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("AUDT")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstAuditActionBuff[0])); } public CFAstAuditActionBuff readBuffByIdIdx(CFAstAuthorization Authorization, short AuditActionId) { final String S_ProcName = "CFAstRamAuditAction.readBuffByIdIdx() "; CFAstAuditActionBuff buff = readDerivedByIdIdx(Authorization, AuditActionId); if ((buff != null) && buff.getClassCode().equals("AUDT")) { return ((CFAstAuditActionBuff) buff); } else { return (null); } } public CFAstAuditActionBuff readBuffByUDescrIdx(CFAstAuthorization Authorization, String Description) { final String S_ProcName = "CFAstRamAuditAction.readBuffByUDescrIdx() "; CFAstAuditActionBuff buff = readDerivedByUDescrIdx(Authorization, Description); if ((buff != null) && buff.getClassCode().equals("AUDT")) { return ((CFAstAuditActionBuff) buff); } else { return (null); } } public void updateAuditAction(CFAstAuthorization Authorization, CFAstAuditActionBuff Buff) { CFAstAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); CFAstAuditActionBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAuditAction", "Existing record not found", "AuditAction", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAuditAction", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstAuditActionByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); CFAstAuditActionByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Check unique indexes if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateAuditAction", "AuditActionUDescrIdx", newKeyUDescrIdx); } } // Validate foreign keys // Update is valid SortedMap<CFAstAuditActionPKey, CFAstAuditActionBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); } public void deleteAuditAction(CFAstAuthorization Authorization, CFAstAuditActionBuff Buff) { final String S_ProcName = "CFAstRamAuditActionTable.deleteAuditAction() "; CFAstAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); CFAstAuditActionBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAuditAction", pkey); } CFAstAuditActionByUDescrIdxKey keyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); // Validate reverse foreign keys // Delete is valid SortedMap<CFAstAuditActionPKey, CFAstAuditActionBuff> subdict; dictByPKey.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); } public void deleteAuditActionByIdIdx(CFAstAuthorization Authorization, short argAuditActionId) { CFAstAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(argAuditActionId); deleteAuditActionByIdIdx(Authorization, key); } public void deleteAuditActionByIdIdx(CFAstAuthorization Authorization, CFAstAuditActionPKey argKey) { CFAstAuditActionBuff cur; LinkedList<CFAstAuditActionBuff> matchSet = new LinkedList<CFAstAuditActionBuff>(); Iterator<CFAstAuditActionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstAuditActionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteAuditAction(Authorization, cur); } } public void deleteAuditActionByUDescrIdx(CFAstAuthorization Authorization, String argDescription) { CFAstAuditActionByUDescrIdxKey key = schema.getFactoryAuditAction().newUDescrIdxKey(); key.setRequiredDescription(argDescription); deleteAuditActionByUDescrIdx(Authorization, key); } public void deleteAuditActionByUDescrIdx(CFAstAuthorization Authorization, CFAstAuditActionByUDescrIdxKey argKey) { CFAstAuditActionBuff cur; LinkedList<CFAstAuditActionBuff> matchSet = new LinkedList<CFAstAuditActionBuff>(); Iterator<CFAstAuditActionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstAuditActionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteAuditAction(Authorization, cur); } } public CFAstCursor openAuditActionCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamAuditActionCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeAuditActionCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstAuditActionBuff nextAuditActionCursor(CFAstCursor Cursor) { CFAstRamAuditActionCursor cursor = (CFAstRamAuditActionCursor) Cursor; CFAstAuditActionBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstAuditActionBuff prevAuditActionCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstAuditActionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public CFAstAuditActionBuff firstAuditActionCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstAuditActionBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public CFAstAuditActionBuff lastAuditActionCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAuditActionCursor"); } public CFAstAuditActionBuff nthAuditActionCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstAuditActionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }