Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for TSecGroup. /* * 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.*; /* * CFAstRamTSecGroupTable in-memory RAM DbIO implementation * for TSecGroup. */ public class CFAstRamTSecGroupTable implements ICFAstTSecGroupTable { private CFAstRamSchema schema; private Map<CFAstTSecGroupPKey, CFAstTSecGroupBuff> dictByPKey = new HashMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff>(); private SortedMap<CFAstTSecGroupByTenantIdxKey, SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff>> dictByTenantIdx = new TreeMap<CFAstTSecGroupByTenantIdxKey, SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff>>(); private SortedMap<CFAstTSecGroupByUNameIdxKey, CFAstTSecGroupBuff> dictByUNameIdx = new TreeMap<CFAstTSecGroupByUNameIdxKey, CFAstTSecGroupBuff>(); public CFAstRamTSecGroupTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createTSecGroup(CFAstAuthorization Authorization, CFAstTSecGroupBuff Buff) { CFAstTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(((CFAstRamTenantTable) schema.getTableTenant()) .nextTSecGroupIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTSecGroupId(pkey.getRequiredTSecGroupId()); CFAstTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAstTSecGroupByUNameIdxKey keyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTSecGroup", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTSecGroup", "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(), "createTSecGroup", "Container", "TSecGroupTenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFAstTSecGroupBuff readDerived(CFAstAuthorization Authorization, CFAstTSecGroupPKey PKey) { final String S_ProcName = "CFAstRamTSecGroup.readDerived() "; CFAstTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFAstTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTSecGroupBuff lockDerived(CFAstAuthorization Authorization, CFAstTSecGroupPKey PKey) { final String S_ProcName = "CFAstRamTSecGroup.readDerived() "; CFAstTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFAstTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTSecGroupBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTSecGroup.readAllDerived() "; CFAstTSecGroupBuff[] retList = new CFAstTSecGroupBuff[dictByPKey.values().size()]; Iterator<CFAstTSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstTSecGroupBuff[] readDerivedByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamTSecGroup.readDerivedByTenantIdx() "; CFAstTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFAstTSecGroupBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFAstTSecGroupBuff[subdictTenantIdx.size()]; Iterator<CFAstTSecGroupBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstTSecGroupBuff[0]; } return (recArray); } public CFAstTSecGroupBuff readDerivedByUNameIdx(CFAstAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAstRamTSecGroup.readDerivedByUNameIdx() "; CFAstTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFAstTSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstTSecGroupBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAstRamTSecGroup.readDerivedByIdIdx() "; CFAstTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTSecGroupId(TSecGroupId); CFAstTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTSecGroupBuff readBuff(CFAstAuthorization Authorization, CFAstTSecGroupPKey PKey) { final String S_ProcName = "CFAstRamTSecGroup.readBuff() "; CFAstTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFAstTSecGroupBuff lockBuff(CFAstAuthorization Authorization, CFAstTSecGroupPKey PKey) { final String S_ProcName = "CFAstRamTSecGroup.readBuff() "; CFAstTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFAstTSecGroupBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTSecGroup.readAllBuff() "; CFAstTSecGroupBuff buff; ArrayList<CFAstTSecGroupBuff> filteredList = new ArrayList<CFAstTSecGroupBuff>(); CFAstTSecGroupBuff[] 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 CFAstTSecGroupBuff[0])); } public CFAstTSecGroupBuff readBuffByIdIdx(CFAstAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAstRamTSecGroup.readBuffByIdIdx() "; CFAstTSecGroupBuff buff = readDerivedByIdIdx(Authorization, TenantId, TSecGroupId); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFAstTSecGroupBuff) buff); } else { return (null); } } public CFAstTSecGroupBuff[] readBuffByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamTSecGroup.readBuffByTenantIdx() "; CFAstTSecGroupBuff buff; ArrayList<CFAstTSecGroupBuff> filteredList = new ArrayList<CFAstTSecGroupBuff>(); CFAstTSecGroupBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add((CFAstTSecGroupBuff) buff); } } return (filteredList.toArray(new CFAstTSecGroupBuff[0])); } public CFAstTSecGroupBuff readBuffByUNameIdx(CFAstAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAstRamTSecGroup.readBuffByUNameIdx() "; CFAstTSecGroupBuff buff = readDerivedByUNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFAstTSecGroupBuff) buff); } else { return (null); } } public void updateTSecGroup(CFAstAuthorization Authorization, CFAstTSecGroupBuff Buff) { CFAstTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFAstTSecGroupBuff 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); CFAstTSecGroupByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAstTSecGroupByTenantIdxKey newKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAstTSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAstTSecGroupByUNameIdxKey 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 SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff> 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 TreeMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTSecGroup(CFAstAuthorization Authorization, CFAstTSecGroupBuff Buff) { final String S_ProcName = "CFAstRamTSecGroupTable.deleteTSecGroup() "; CFAstTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFAstTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTSecGroup", pkey); } CFAstTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAstTSecGroupByUNameIdxKey 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()); SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTSecGroupByIdIdx(CFAstAuthorization Authorization, long argTenantId, int argTSecGroupId) { CFAstTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTSecGroupId(argTSecGroupId); deleteTSecGroupByIdIdx(Authorization, key); } public void deleteTSecGroupByIdIdx(CFAstAuthorization Authorization, CFAstTSecGroupPKey argKey) { CFAstTSecGroupBuff cur; LinkedList<CFAstTSecGroupBuff> matchSet = new LinkedList<CFAstTSecGroupBuff>(); Iterator<CFAstTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByTenantIdx(CFAstAuthorization Authorization, long argTenantId) { CFAstTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTSecGroupByTenantIdx(Authorization, key); } public void deleteTSecGroupByTenantIdx(CFAstAuthorization Authorization, CFAstTSecGroupByTenantIdxKey argKey) { CFAstTSecGroupBuff cur; LinkedList<CFAstTSecGroupBuff> matchSet = new LinkedList<CFAstTSecGroupBuff>(); Iterator<CFAstTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByUNameIdx(CFAstAuthorization Authorization, long argTenantId, String argName) { CFAstTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTSecGroupByUNameIdx(Authorization, key); } public void deleteTSecGroupByUNameIdx(CFAstAuthorization Authorization, CFAstTSecGroupByUNameIdxKey argKey) { CFAstTSecGroupBuff cur; LinkedList<CFAstTSecGroupBuff> matchSet = new LinkedList<CFAstTSecGroupBuff>(); Iterator<CFAstTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public CFAstCursor openTSecGroupCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamTSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openTSecGroupCursorByTenantIdx(CFAstAuthorization Authorization, long TenantId) { CFAstCursor cursor; CFAstTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstTSecGroupPKey, CFAstTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAstRamTSecGroupCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAstRamTSecGroupCursor(Authorization, schema, new ArrayList<CFAstTSecGroupBuff>()); } return (cursor); } public void closeTSecGroupCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstTSecGroupBuff nextTSecGroupCursor(CFAstCursor Cursor) { CFAstRamTSecGroupCursor cursor = (CFAstRamTSecGroupCursor) Cursor; CFAstTSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstTSecGroupBuff prevTSecGroupCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFAstTSecGroupBuff firstTSecGroupCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstTSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFAstTSecGroupBuff lastTSecGroupCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTSecGroupCursor"); } public CFAstTSecGroupBuff nthTSecGroupCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }