Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for SecGroup. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; /* * CFAsteriskRamSecGroupTable in-memory RAM DbIO implementation * for SecGroup. */ public class CFAsteriskRamSecGroupTable implements ICFAsteriskSecGroupTable { private ICFAsteriskSchema schema; private Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> dictByPKey = new HashMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); private Map<CFSecuritySecGroupByClusterIdxKey, Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>> dictByClusterIdx = new HashMap<CFSecuritySecGroupByClusterIdxKey, Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>>(); private Map<CFSecuritySecGroupByUNameIdxKey, CFSecuritySecGroupBuff> dictByUNameIdx = new HashMap<CFSecuritySecGroupByUNameIdxKey, CFSecuritySecGroupBuff>(); public CFAsteriskRamSecGroupTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { final String S_ProcName = "createSecGroup"; CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(((CFAsteriskRamClusterTable) schema.getTableCluster()) .nextSecGroupIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecGroupId(pkey.getRequiredSecGroupId()); CFSecuritySecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "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(), S_ProcName, "Container", "SecGroupCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new HashMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFSecuritySecGroupBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamSecGroup.readDerived"; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamSecGroup.readDerived"; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSecGroup.readAllDerived"; CFSecuritySecGroupBuff[] retList = new CFSecuritySecGroupBuff[dictByPKey.values().size()]; Iterator<CFSecuritySecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySecGroupBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSecGroup.readDerivedByClusterIdx"; CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecuritySecGroupBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecuritySecGroupBuff[subdictClusterIdx.size()]; Iterator<CFSecuritySecGroupBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx = new HashMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); dictByClusterIdx.put(key, subdictClusterIdx); recArray = new CFSecuritySecGroupBuff[0]; } return (recArray); } public CFSecuritySecGroupBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAsteriskRamSecGroup.readDerivedByUNameIdx"; CFSecuritySecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredName(Name); CFSecuritySecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAsteriskRamSecGroup.readDerivedByIdIdx() "; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecGroupId(SecGroupId); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamSecGroup.readBuff"; CFSecuritySecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFSecuritySecGroupBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "lockBuff"; CFSecuritySecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFSecuritySecGroupBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSecGroup.readAllBuff"; CFSecuritySecGroupBuff buff; ArrayList<CFSecuritySecGroupBuff> filteredList = new ArrayList<CFSecuritySecGroupBuff>(); CFSecuritySecGroupBuff[] 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 CFSecuritySecGroupBuff[0])); } public CFSecuritySecGroupBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFAsteriskRamSecGroup.readBuffByIdIdx() "; CFSecuritySecGroupBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecGroupId); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFSecuritySecGroupBuff) buff); } else { return (null); } } public CFSecuritySecGroupBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSecGroup.readBuffByClusterIdx() "; CFSecuritySecGroupBuff buff; ArrayList<CFSecuritySecGroupBuff> filteredList = new ArrayList<CFSecuritySecGroupBuff>(); CFSecuritySecGroupBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add((CFSecuritySecGroupBuff) buff); } } return (filteredList.toArray(new CFSecuritySecGroupBuff[0])); } public CFSecuritySecGroupBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFAsteriskRamSecGroup.readBuffByUNameIdx() "; CFSecuritySecGroupBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, Name); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFSecuritySecGroupBuff) buff); } else { return (null); } } public void updateSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFSecuritySecGroupBuff 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); CFSecuritySecGroupByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecGroupByClusterIdxKey newKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFSecuritySecGroupByUNameIdxKey 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 Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> 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 HashMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { final String S_ProcName = "CFAsteriskRamSecGroupTable.deleteSecGroup() "; CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFSecuritySecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecGroup", pkey); } CFSecuritySecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey 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()); Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteSecGroupByIdIdx(CFSecurityAuthorization Authorization, long argClusterId, int argSecGroupId) { CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecGroupId(argSecGroupId); deleteSecGroupByIdIdx(Authorization, key); } public void deleteSecGroupByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecGroupByClusterIdx(Authorization, key); } public void deleteSecGroupByClusterIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupByClusterIdxKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByUNameIdx(CFSecurityAuthorization Authorization, long argClusterId, String argName) { CFSecuritySecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredName(argName); deleteSecGroupByUNameIdx(Authorization, key); } public void deleteSecGroupByUNameIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupByUNameIdxKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public CFSecurityCursor openSecGroupCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSecGroupCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAsteriskRamSecGroupCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAsteriskRamSecGroupCursor(Authorization, schema, new ArrayList<CFSecuritySecGroupBuff>()); } return (cursor); } public void closeSecGroupCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySecGroupBuff nextSecGroupCursor(CFSecurityCursor Cursor) { CFAsteriskRamSecGroupCursor cursor = (CFAsteriskRamSecGroupCursor) Cursor; CFSecuritySecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySecGroupBuff prevSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFSecuritySecGroupBuff firstSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFSecuritySecGroupBuff lastSecGroupCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecGroupCursor"); } public CFSecuritySecGroupBuff nthSecGroupCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }