Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecGroup. /* * 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.*; /* * CFAccRamSecGroupTable in-memory RAM DbIO implementation * for SecGroup. */ public class CFAccRamSecGroupTable implements ICFAccSecGroupTable { private CFAccRamSchema schema; private Map<CFAccSecGroupPKey, CFAccSecGroupBuff> dictByPKey = new HashMap<CFAccSecGroupPKey, CFAccSecGroupBuff>(); private SortedMap<CFAccSecGroupByClusterIdxKey, SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff>> dictByClusterIdx = new TreeMap<CFAccSecGroupByClusterIdxKey, SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff>>(); private SortedMap<CFAccSecGroupByUNameIdxKey, CFAccSecGroupBuff> dictByUNameIdx = new TreeMap<CFAccSecGroupByUNameIdxKey, CFAccSecGroupBuff>(); public CFAccRamSecGroupTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createSecGroup(CFAccAuthorization Authorization, CFAccSecGroupBuff Buff) { CFAccSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(((CFAccRamClusterTable) schema.getTableCluster()) .nextSecGroupIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecGroupId(pkey.getRequiredSecGroupId()); CFAccSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAccSecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createSecGroup", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecGroup", "SecGroupUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createSecGroup", "Container", "SecGroupCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFAccSecGroupPKey, CFAccSecGroupBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFAccSecGroupBuff readDerived(CFAccAuthorization Authorization, CFAccSecGroupPKey PKey) { final String S_ProcName = "CFAccRamSecGroup.readDerived() "; CFAccSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFAccSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecGroupBuff lockDerived(CFAccAuthorization Authorization, CFAccSecGroupPKey PKey) { final String S_ProcName = "CFAccRamSecGroup.readDerived() "; CFAccSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFAccSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecGroupBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecGroup.readAllDerived() "; CFAccSecGroupBuff[] retList = new CFAccSecGroupBuff[dictByPKey.values().size()]; Iterator<CFAccSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccSecGroupBuff[] readDerivedByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAccRamSecGroup.readDerivedByClusterIdx() "; CFAccSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAccSecGroupBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAccSecGroupBuff[subdictClusterIdx.size()]; Iterator<CFAccSecGroupBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAccSecGroupBuff[0]; } return (recArray); } public CFAccSecGroupBuff readDerivedByUNameIdx(CFAccAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAccRamSecGroup.readDerivedByUNameIdx() "; CFAccSecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredName(Name); CFAccSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAccSecGroupBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAccRamSecGroup.readDerivedByIdIdx() "; CFAccSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecGroupId(SecGroupId); CFAccSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecGroupBuff readBuff(CFAccAuthorization Authorization, CFAccSecGroupPKey PKey) { final String S_ProcName = "CFAccRamSecGroup.readBuff() "; CFAccSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFAccSecGroupBuff lockBuff(CFAccAuthorization Authorization, CFAccSecGroupPKey PKey) { final String S_ProcName = "CFAccRamSecGroup.readBuff() "; CFAccSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFAccSecGroupBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecGroup.readAllBuff() "; CFAccSecGroupBuff buff; ArrayList<CFAccSecGroupBuff> filteredList = new ArrayList<CFAccSecGroupBuff>(); CFAccSecGroupBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAccSecGroupBuff[0])); } public CFAccSecGroupBuff readBuffByIdIdx(CFAccAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAccRamSecGroup.readBuffByIdIdx() "; CFAccSecGroupBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecGroupId); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFAccSecGroupBuff) buff); } else { return (null); } } public CFAccSecGroupBuff[] readBuffByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAccRamSecGroup.readBuffByClusterIdx() "; CFAccSecGroupBuff buff; ArrayList<CFAccSecGroupBuff> filteredList = new ArrayList<CFAccSecGroupBuff>(); CFAccSecGroupBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add((CFAccSecGroupBuff) buff); } } return (filteredList.toArray(new CFAccSecGroupBuff[0])); } public CFAccSecGroupBuff readBuffByUNameIdx(CFAccAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAccRamSecGroup.readBuffByUNameIdx() "; CFAccSecGroupBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, Name); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFAccSecGroupBuff) buff); } else { return (null); } } public void updateSecGroup(CFAccAuthorization Authorization, CFAccSecGroupBuff Buff) { CFAccSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFAccSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecGroup", "Existing record not found", "SecGroup", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecGroup", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAccSecGroupByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAccSecGroupByClusterIdxKey newKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAccSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAccSecGroupByUNameIdxKey newKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); newKeyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateSecGroup", "SecGroupUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSecGroup", "Container", "SecGroupCluster", "Cluster", null); } } } // Update is valid SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByClusterIdx.get(existingKeyClusterIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByClusterIdx.containsKey(newKeyClusterIdx)) { subdict = dictByClusterIdx.get(newKeyClusterIdx); } else { subdict = new TreeMap<CFAccSecGroupPKey, CFAccSecGroupBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteSecGroup(CFAccAuthorization Authorization, CFAccSecGroupBuff Buff) { final String S_ProcName = "CFAccRamSecGroupTable.deleteSecGroup() "; CFAccSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFAccSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecGroup", pkey); } CFAccSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAccSecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableSecGroupInclude().deleteSecGroupIncludeByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupMember().deleteSecGroupMemberByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupInclude().deleteSecGroupIncludeByIncludeIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupForm().deleteSecGroupFormByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteSecGroupByIdIdx(CFAccAuthorization Authorization, long argClusterId, int argSecGroupId) { CFAccSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecGroupId(argSecGroupId); deleteSecGroupByIdIdx(Authorization, key); } public void deleteSecGroupByIdIdx(CFAccAuthorization Authorization, CFAccSecGroupPKey argKey) { CFAccSecGroupBuff cur; LinkedList<CFAccSecGroupBuff> matchSet = new LinkedList<CFAccSecGroupBuff>(); Iterator<CFAccSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByClusterIdx(CFAccAuthorization Authorization, long argClusterId) { CFAccSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecGroupByClusterIdx(Authorization, key); } public void deleteSecGroupByClusterIdx(CFAccAuthorization Authorization, CFAccSecGroupByClusterIdxKey argKey) { CFAccSecGroupBuff cur; LinkedList<CFAccSecGroupBuff> matchSet = new LinkedList<CFAccSecGroupBuff>(); Iterator<CFAccSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByUNameIdx(CFAccAuthorization Authorization, long argClusterId, String argName) { CFAccSecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredName(argName); deleteSecGroupByUNameIdx(Authorization, key); } public void deleteSecGroupByUNameIdx(CFAccAuthorization Authorization, CFAccSecGroupByUNameIdxKey argKey) { CFAccSecGroupBuff cur; LinkedList<CFAccSecGroupBuff> matchSet = new LinkedList<CFAccSecGroupBuff>(); Iterator<CFAccSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public CFAccCursor openSecGroupCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAccCursor openSecGroupCursorByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { CFAccCursor cursor; CFAccSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAccSecGroupPKey, CFAccSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAccRamSecGroupCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAccRamSecGroupCursor(Authorization, schema, new ArrayList<CFAccSecGroupBuff>()); } return (cursor); } public void closeSecGroupCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccSecGroupBuff nextSecGroupCursor(CFAccCursor Cursor) { CFAccRamSecGroupCursor cursor = (CFAccRamSecGroupCursor) Cursor; CFAccSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccSecGroupBuff prevSecGroupCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFAccSecGroupBuff firstSecGroupCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFAccSecGroupBuff lastSecGroupCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecGroupCursor"); } public CFAccSecGroupBuff nthSecGroupCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }