Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for TSecGroup. /* * 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.*; /* * CFAsteriskRamTSecGroupTable in-memory RAM DbIO implementation * for TSecGroup. */ public class CFAsteriskRamTSecGroupTable implements ICFAsteriskTSecGroupTable { private ICFAsteriskSchema schema; private Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> dictByPKey = new HashMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); private Map<CFSecurityTSecGroupByTenantIdxKey, Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>> dictByTenantIdx = new HashMap<CFSecurityTSecGroupByTenantIdxKey, Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>>(); private Map<CFSecurityTSecGroupByUNameIdxKey, CFSecurityTSecGroupBuff> dictByUNameIdx = new HashMap<CFSecurityTSecGroupByUNameIdxKey, CFSecurityTSecGroupBuff>(); public CFAsteriskRamTSecGroupTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { final String S_ProcName = "createTSecGroup"; CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(((CFAsteriskRamTenantTable) schema.getTableTenant()) .nextTSecGroupIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTSecGroupId(pkey.getRequiredTSecGroupId()); CFSecurityTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey keyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); 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, "TSecGroupUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Container", "TSecGroupTenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new HashMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFSecurityTSecGroupBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamTSecGroup.readDerived"; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamTSecGroup.readDerived"; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamTSecGroup.readAllDerived"; CFSecurityTSecGroupBuff[] retList = new CFSecurityTSecGroupBuff[dictByPKey.values().size()]; Iterator<CFSecurityTSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityTSecGroupBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAsteriskRamTSecGroup.readDerivedByTenantIdx"; CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFSecurityTSecGroupBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFSecurityTSecGroupBuff[subdictTenantIdx.size()]; Iterator<CFSecurityTSecGroupBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx = new HashMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); dictByTenantIdx.put(key, subdictTenantIdx); recArray = new CFSecurityTSecGroupBuff[0]; } return (recArray); } public CFSecurityTSecGroupBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAsteriskRamTSecGroup.readDerivedByUNameIdx"; CFSecurityTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFSecurityTSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAsteriskRamTSecGroup.readDerivedByIdIdx() "; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTSecGroupId(TSecGroupId); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFAsteriskRamTSecGroup.readBuff"; CFSecurityTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFSecurityTSecGroupBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "lockBuff"; CFSecurityTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFSecurityTSecGroupBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamTSecGroup.readAllBuff"; CFSecurityTSecGroupBuff buff; ArrayList<CFSecurityTSecGroupBuff> filteredList = new ArrayList<CFSecurityTSecGroupBuff>(); CFSecurityTSecGroupBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityTSecGroupBuff[0])); } public CFSecurityTSecGroupBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAsteriskRamTSecGroup.readBuffByIdIdx() "; CFSecurityTSecGroupBuff buff = readDerivedByIdIdx(Authorization, TenantId, TSecGroupId); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFSecurityTSecGroupBuff) buff); } else { return (null); } } public CFSecurityTSecGroupBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAsteriskRamTSecGroup.readBuffByTenantIdx() "; CFSecurityTSecGroupBuff buff; ArrayList<CFSecurityTSecGroupBuff> filteredList = new ArrayList<CFSecurityTSecGroupBuff>(); CFSecurityTSecGroupBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add((CFSecurityTSecGroupBuff) buff); } } return (filteredList.toArray(new CFSecurityTSecGroupBuff[0])); } public CFSecurityTSecGroupBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAsteriskRamTSecGroup.readBuffByUNameIdx() "; CFSecurityTSecGroupBuff buff = readDerivedByUNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFSecurityTSecGroupBuff) buff); } else { return (null); } } public void updateTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTSecGroup", "Existing record not found", "TSecGroup", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTSecGroup", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityTSecGroupByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFSecurityTSecGroupByTenantIdxKey newKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFSecurityTSecGroupByUNameIdxKey newKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); newKeyUNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTSecGroup", "TSecGroupUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTSecGroup", "Container", "TSecGroupTenant", "Tenant", null); } } } // Update is valid Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByTenantIdx.get(existingKeyTenantIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByTenantIdx.containsKey(newKeyTenantIdx)) { subdict = dictByTenantIdx.get(newKeyTenantIdx); } else { subdict = new HashMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { final String S_ProcName = "CFAsteriskRamTSecGroupTable.deleteTSecGroup() "; CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTSecGroup", pkey); } CFSecurityTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey keyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableTSecGroupInclude().deleteTSecGroupIncludeByGroupIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); schema.getTableTSecGroupMember().deleteTSecGroupMemberByGroupIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); schema.getTableTSecGroupInclude().deleteTSecGroupIncludeByIncludeIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTSecGroupByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, int argTSecGroupId) { CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTSecGroupId(argTSecGroupId); deleteTSecGroupByIdIdx(Authorization, key); } public void deleteTSecGroupByIdIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTSecGroupByTenantIdx(Authorization, key); } public void deleteTSecGroupByTenantIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupByTenantIdxKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByUNameIdx(CFSecurityAuthorization Authorization, long argTenantId, String argName) { CFSecurityTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTSecGroupByUNameIdx(Authorization, key); } public void deleteTSecGroupByUNameIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupByUNameIdxKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public CFSecurityCursor openTSecGroupCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamTSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openTSecGroupCursorByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { CFSecurityCursor cursor; CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAsteriskRamTSecGroupCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAsteriskRamTSecGroupCursor(Authorization, schema, new ArrayList<CFSecurityTSecGroupBuff>()); } return (cursor); } public void closeTSecGroupCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityTSecGroupBuff nextTSecGroupCursor(CFSecurityCursor Cursor) { CFAsteriskRamTSecGroupCursor cursor = (CFAsteriskRamTSecGroupCursor) Cursor; CFSecurityTSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityTSecGroupBuff prevTSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFSecurityTSecGroupBuff firstTSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityTSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFSecurityTSecGroupBuff lastTSecGroupCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTSecGroupCursor"); } public CFSecurityTSecGroupBuff nthTSecGroupCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }