Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for AuditAction. /* * MSS Code Factory Accounting Business Application Model * * Copyright (c) 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.cfacc.v2_0.CFAccRam; 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.cfacc.v2_0.CFAcc.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*; /* * CFAccRamAuditActionTable in-memory RAM DbIO implementation * for AuditAction. */ public class CFAccRamAuditActionTable implements ICFAccAuditActionTable { private CFAccRamSchema schema; private Map<CFAccAuditActionPKey, CFAccAuditActionBuff> dictByPKey = new HashMap<CFAccAuditActionPKey, CFAccAuditActionBuff>(); private SortedMap<CFAccAuditActionByUDescrIdxKey, CFAccAuditActionBuff> dictByUDescrIdx = new TreeMap<CFAccAuditActionByUDescrIdxKey, CFAccAuditActionBuff>(); public CFAccRamAuditActionTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createAuditAction(CFAccAuthorization Authorization, CFAccAuditActionBuff Buff) { CFAccAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); Buff.setRequiredAuditActionId(pkey.getRequiredAuditActionId()); CFAccAuditActionByUDescrIdxKey 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 CFAccAuditActionBuff readDerived(CFAccAuthorization Authorization, CFAccAuditActionPKey PKey) { final String S_ProcName = "CFAccRamAuditAction.readDerived() "; CFAccAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(PKey.getRequiredAuditActionId()); CFAccAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccAuditActionBuff lockDerived(CFAccAuthorization Authorization, CFAccAuditActionPKey PKey) { final String S_ProcName = "CFAccRamAuditAction.readDerived() "; CFAccAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(PKey.getRequiredAuditActionId()); CFAccAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccAuditActionBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamAuditAction.readAllDerived() "; CFAccAuditActionBuff[] retList = new CFAccAuditActionBuff[dictByPKey.values().size()]; Iterator<CFAccAuditActionBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccAuditActionBuff readDerivedByUDescrIdx(CFAccAuthorization Authorization, String Description) { final String S_ProcName = "CFAccRamAuditAction.readDerivedByUDescrIdx() "; CFAccAuditActionByUDescrIdxKey key = schema.getFactoryAuditAction().newUDescrIdxKey(); key.setRequiredDescription(Description); CFAccAuditActionBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public CFAccAuditActionBuff readDerivedByIdIdx(CFAccAuthorization Authorization, short AuditActionId) { final String S_ProcName = "CFAccRamAuditAction.readDerivedByIdIdx() "; CFAccAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(AuditActionId); CFAccAuditActionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccAuditActionBuff readBuff(CFAccAuthorization Authorization, CFAccAuditActionPKey PKey) { final String S_ProcName = "CFAccRamAuditAction.readBuff() "; CFAccAuditActionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("AUDT"))) { buff = null; } return (buff); } public CFAccAuditActionBuff lockBuff(CFAccAuthorization Authorization, CFAccAuditActionPKey PKey) { final String S_ProcName = "CFAccRamAuditAction.readBuff() "; CFAccAuditActionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("AUDT"))) { buff = null; } return (buff); } public CFAccAuditActionBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamAuditAction.readAllBuff() "; CFAccAuditActionBuff buff; ArrayList<CFAccAuditActionBuff> filteredList = new ArrayList<CFAccAuditActionBuff>(); CFAccAuditActionBuff[] 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 CFAccAuditActionBuff[0])); } public CFAccAuditActionBuff readBuffByIdIdx(CFAccAuthorization Authorization, short AuditActionId) { final String S_ProcName = "CFAccRamAuditAction.readBuffByIdIdx() "; CFAccAuditActionBuff buff = readDerivedByIdIdx(Authorization, AuditActionId); if ((buff != null) && buff.getClassCode().equals("AUDT")) { return ((CFAccAuditActionBuff) buff); } else { return (null); } } public CFAccAuditActionBuff readBuffByUDescrIdx(CFAccAuthorization Authorization, String Description) { final String S_ProcName = "CFAccRamAuditAction.readBuffByUDescrIdx() "; CFAccAuditActionBuff buff = readDerivedByUDescrIdx(Authorization, Description); if ((buff != null) && buff.getClassCode().equals("AUDT")) { return ((CFAccAuditActionBuff) buff); } else { return (null); } } public void updateAuditAction(CFAccAuthorization Authorization, CFAccAuditActionBuff Buff) { CFAccAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); CFAccAuditActionBuff 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); CFAccAuditActionByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); CFAccAuditActionByUDescrIdxKey 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<CFAccAuditActionPKey, CFAccAuditActionBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); } public void deleteAuditAction(CFAccAuthorization Authorization, CFAccAuditActionBuff Buff) { final String S_ProcName = "CFAccRamAuditActionTable.deleteAuditAction() "; CFAccAuditActionPKey pkey = schema.getFactoryAuditAction().newPKey(); pkey.setRequiredAuditActionId(Buff.getRequiredAuditActionId()); CFAccAuditActionBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAuditAction", pkey); } CFAccAuditActionByUDescrIdxKey keyUDescrIdx = schema.getFactoryAuditAction().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); // Validate reverse foreign keys // Delete is valid SortedMap<CFAccAuditActionPKey, CFAccAuditActionBuff> subdict; dictByPKey.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); } public void deleteAuditActionByIdIdx(CFAccAuthorization Authorization, short argAuditActionId) { CFAccAuditActionPKey key = schema.getFactoryAuditAction().newPKey(); key.setRequiredAuditActionId(argAuditActionId); deleteAuditActionByIdIdx(Authorization, key); } public void deleteAuditActionByIdIdx(CFAccAuthorization Authorization, CFAccAuditActionPKey argKey) { CFAccAuditActionBuff cur; LinkedList<CFAccAuditActionBuff> matchSet = new LinkedList<CFAccAuditActionBuff>(); Iterator<CFAccAuditActionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccAuditActionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteAuditAction(Authorization, cur); } } public void deleteAuditActionByUDescrIdx(CFAccAuthorization Authorization, String argDescription) { CFAccAuditActionByUDescrIdxKey key = schema.getFactoryAuditAction().newUDescrIdxKey(); key.setRequiredDescription(argDescription); deleteAuditActionByUDescrIdx(Authorization, key); } public void deleteAuditActionByUDescrIdx(CFAccAuthorization Authorization, CFAccAuditActionByUDescrIdxKey argKey) { CFAccAuditActionBuff cur; LinkedList<CFAccAuditActionBuff> matchSet = new LinkedList<CFAccAuditActionBuff>(); Iterator<CFAccAuditActionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccAuditActionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteAuditAction(Authorization, cur); } } public CFAccCursor openAuditActionCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamAuditActionCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeAuditActionCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccAuditActionBuff nextAuditActionCursor(CFAccCursor Cursor) { CFAccRamAuditActionCursor cursor = (CFAccRamAuditActionCursor) Cursor; CFAccAuditActionBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccAuditActionBuff prevAuditActionCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccAuditActionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public CFAccAuditActionBuff firstAuditActionCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccAuditActionBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public CFAccAuditActionBuff lastAuditActionCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAuditActionCursor"); } public CFAccAuditActionBuff nthAuditActionCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccAuditActionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAuditActionCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }