Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecGroup. /* * 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.*; /* * CFAstRamSecGroupTable in-memory RAM DbIO implementation * for SecGroup. */ public class CFAstRamSecGroupTable implements ICFAstSecGroupTable { private CFAstRamSchema schema; private Map<CFAstSecGroupPKey, CFAstSecGroupBuff> dictByPKey = new HashMap<CFAstSecGroupPKey, CFAstSecGroupBuff>(); private SortedMap<CFAstSecGroupByClusterIdxKey, SortedMap<CFAstSecGroupPKey, CFAstSecGroupBuff>> dictByClusterIdx = new TreeMap<CFAstSecGroupByClusterIdxKey, SortedMap<CFAstSecGroupPKey, CFAstSecGroupBuff>>(); private SortedMap<CFAstSecGroupByUNameIdxKey, CFAstSecGroupBuff> dictByUNameIdx = new TreeMap<CFAstSecGroupByUNameIdxKey, CFAstSecGroupBuff>(); public CFAstRamSecGroupTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createSecGroup(CFAstAuthorization Authorization, CFAstSecGroupBuff Buff) { CFAstSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(((CFAstRamClusterTable) schema.getTableCluster()) .nextSecGroupIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecGroupId(pkey.getRequiredSecGroupId()); CFAstSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstSecGroupByUNameIdxKey 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<CFAstSecGroupPKey, CFAstSecGroupBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFAstSecGroupPKey, CFAstSecGroupBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFAstSecGroupBuff readDerived(CFAstAuthorization Authorization, CFAstSecGroupPKey PKey) { final String S_ProcName = "CFAstRamSecGroup.readDerived() "; CFAstSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFAstSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecGroupBuff lockDerived(CFAstAuthorization Authorization, CFAstSecGroupPKey PKey) { final String S_ProcName = "CFAstRamSecGroup.readDerived() "; CFAstSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFAstSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecGroupBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecGroup.readAllDerived() "; CFAstSecGroupBuff[] retList = new CFAstSecGroupBuff[dictByPKey.values().size()]; Iterator<CFAstSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstSecGroupBuff[] readDerivedByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSecGroup.readDerivedByClusterIdx() "; CFAstSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAstSecGroupBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstSecGroupPKey, CFAstSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAstSecGroupBuff[subdictClusterIdx.size()]; Iterator<CFAstSecGroupBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstSecGroupBuff[0]; } return (recArray); } public CFAstSecGroupBuff readDerivedByUNameIdx(CFAstAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAstRamSecGroup.readDerivedByUNameIdx() "; CFAstSecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredName(Name); CFAstSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstSecGroupBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAstRamSecGroup.readDerivedByIdIdx() "; CFAstSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecGroupId(SecGroupId); CFAstSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecGroupBuff readBuff(CFAstAuthorization Authorization, CFAstSecGroupPKey PKey) { final String S_ProcName = "CFAstRamSecGroup.readBuff() "; CFAstSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFAstSecGroupBuff lockBuff(CFAstAuthorization Authorization, CFAstSecGroupPKey PKey) { final String S_ProcName = "CFAstRamSecGroup.readBuff() "; CFAstSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFAstSecGroupBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecGroup.readAllBuff() "; CFAstSecGroupBuff buff; ArrayList<CFAstSecGroupBuff> filteredList = new ArrayList<CFAstSecGroupBuff>(); CFAstSecGroupBuff[] 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 CFAstSecGroupBuff[0])); } public CFAstSecGroupBuff readBuffByIdIdx(CFAstAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAstRamSecGroup.readBuffByIdIdx() "; CFAstSecGroupBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecGroupId); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFAstSecGroupBuff) buff); } else { return (null); } } public CFAstSecGroupBuff[] readBuffByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSecGroup.readBuffByClusterIdx() "; CFAstSecGroupBuff buff; ArrayList<CFAstSecGroupBuff> filteredList = new ArrayList<CFAstSecGroupBuff>(); CFAstSecGroupBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add((CFAstSecGroupBuff) buff); } } return (filteredList.toArray(new CFAstSecGroupBuff[0])); } public CFAstSecGroupBuff readBuffByUNameIdx(CFAstAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAstRamSecGroup.readBuffByUNameIdx() "; CFAstSecGroupBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, Name); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFAstSecGroupBuff) buff); } else { return (null); } } public void updateSecGroup(CFAstAuthorization Authorization, CFAstSecGroupBuff Buff) { CFAstSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFAstSecGroupBuff 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); CFAstSecGroupByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstSecGroupByClusterIdxKey newKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAstSecGroupByUNameIdxKey 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<CFAstSecGroupPKey, CFAstSecGroupBuff> 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<CFAstSecGroupPKey, CFAstSecGroupBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteSecGroup(CFAstAuthorization Authorization, CFAstSecGroupBuff Buff) { final String S_ProcName = "CFAstRamSecGroupTable.deleteSecGroup() "; CFAstSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFAstSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecGroup", pkey); } CFAstSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstSecGroupByUNameIdxKey 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<CFAstSecGroupPKey, CFAstSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteSecGroupByIdIdx(CFAstAuthorization Authorization, long argClusterId, int argSecGroupId) { CFAstSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecGroupId(argSecGroupId); deleteSecGroupByIdIdx(Authorization, key); } public void deleteSecGroupByIdIdx(CFAstAuthorization Authorization, CFAstSecGroupPKey argKey) { CFAstSecGroupBuff cur; LinkedList<CFAstSecGroupBuff> matchSet = new LinkedList<CFAstSecGroupBuff>(); Iterator<CFAstSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByClusterIdx(CFAstAuthorization Authorization, long argClusterId) { CFAstSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecGroupByClusterIdx(Authorization, key); } public void deleteSecGroupByClusterIdx(CFAstAuthorization Authorization, CFAstSecGroupByClusterIdxKey argKey) { CFAstSecGroupBuff cur; LinkedList<CFAstSecGroupBuff> matchSet = new LinkedList<CFAstSecGroupBuff>(); Iterator<CFAstSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByUNameIdx(CFAstAuthorization Authorization, long argClusterId, String argName) { CFAstSecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredName(argName); deleteSecGroupByUNameIdx(Authorization, key); } public void deleteSecGroupByUNameIdx(CFAstAuthorization Authorization, CFAstSecGroupByUNameIdxKey argKey) { CFAstSecGroupBuff cur; LinkedList<CFAstSecGroupBuff> matchSet = new LinkedList<CFAstSecGroupBuff>(); Iterator<CFAstSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public CFAstCursor openSecGroupCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openSecGroupCursorByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { CFAstCursor cursor; CFAstSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstSecGroupPKey, CFAstSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAstRamSecGroupCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAstRamSecGroupCursor(Authorization, schema, new ArrayList<CFAstSecGroupBuff>()); } return (cursor); } public void closeSecGroupCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstSecGroupBuff nextSecGroupCursor(CFAstCursor Cursor) { CFAstRamSecGroupCursor cursor = (CFAstRamSecGroupCursor) Cursor; CFAstSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstSecGroupBuff prevSecGroupCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFAstSecGroupBuff firstSecGroupCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFAstSecGroupBuff lastSecGroupCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecGroupCursor"); } public CFAstSecGroupBuff nthSecGroupCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }