Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for Tenant. /* * 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.*; /* * CFAstRamTenantTable in-memory RAM DbIO implementation * for Tenant. */ public class CFAstRamTenantTable implements ICFAstTenantTable { private CFAstRamSchema schema; private Map<CFAstTenantPKey, CFAstTenantBuff> dictByPKey = new HashMap<CFAstTenantPKey, CFAstTenantBuff>(); private SortedMap<CFAstTenantByClusterIdxKey, SortedMap<CFAstTenantPKey, CFAstTenantBuff>> dictByClusterIdx = new TreeMap<CFAstTenantByClusterIdxKey, SortedMap<CFAstTenantPKey, CFAstTenantBuff>>(); private SortedMap<CFAstTenantByUNameIdxKey, CFAstTenantBuff> dictByUNameIdx = new TreeMap<CFAstTenantByUNameIdxKey, CFAstTenantBuff>(); private Map<CFAstRamTenantId32Gen, CFAstRamTenantId32Gen> id32Generator = new HashMap<CFAstRamTenantId32Gen, CFAstRamTenantId32Gen>(); private Map<CFAstRamTenantId64Gen, CFAstRamTenantId64Gen> id64Generator = new HashMap<CFAstRamTenantId64Gen, CFAstRamTenantId64Gen>(); public CFAstRamTenantTable(CFAstRamSchema argSchema) { schema = argSchema; } protected int generateNextId32(long argId, short argSliceId) { CFAstRamTenantId32Gen key = new CFAstRamTenantId32Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFAstRamTenantId32Gen useGen = id32Generator.get(key); if (useGen == null) { id32Generator.put(key, key); useGen = key; } int retNext = useGen.getNextId(); return (retNext); } protected long generateNextId64(long argId, short argSliceId) { CFAstRamTenantId64Gen key = new CFAstRamTenantId64Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFAstRamTenantId64Gen useGen = id64Generator.get(key); if (useGen == null) { id64Generator.put(key, key); useGen = key; } long retNext = useGen.getNextId(); return (retNext); } public int nextTSecGroupIdGen(CFAstAuthorization Authorization, CFAstTenantPKey pkey) { int retval = nextTSecGroupIdGen(Authorization, pkey.getRequiredId()); return (retval); } public int nextTSecGroupIdGen(CFAstAuthorization Authorization, long argId) { int retNext = generateNextId32(argId, (short) 113); return (retNext); } public long nextTSecGroupMemberIdGen(CFAstAuthorization Authorization, CFAstTenantPKey pkey) { long retval = nextTSecGroupMemberIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextTSecGroupMemberIdGen(CFAstAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 114); return (retNext); } public long nextTSecGroupIncludeIdGen(CFAstAuthorization Authorization, CFAstTenantPKey pkey) { long retval = nextTSecGroupIncludeIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextTSecGroupIncludeIdGen(CFAstAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 115); return (retNext); } public long nextTLDIdGen(CFAstAuthorization Authorization, CFAstTenantPKey pkey) { long retval = nextTLDIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextTLDIdGen(CFAstAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 211); return (retNext); } public long nextDomainIdGen(CFAstAuthorization Authorization, CFAstTenantPKey pkey) { long retval = nextDomainIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextDomainIdGen(CFAstAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 212); return (retNext); } public void createTenant(CFAstAuthorization Authorization, CFAstTenantBuff Buff) { CFAstTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(schema.nextTenantIdGen()); Buff.setRequiredId(pkey.getRequiredId()); CFAstTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTenant", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTenant", "TenantUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createTenant", "Container", "TenantCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAstTenantPKey, CFAstTenantBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFAstTenantPKey, CFAstTenantBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFAstTenantBuff readDerived(CFAstAuthorization Authorization, CFAstTenantPKey PKey) { final String S_ProcName = "CFAstRamTenant.readDerived() "; CFAstTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAstTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTenantBuff lockDerived(CFAstAuthorization Authorization, CFAstTenantPKey PKey) { final String S_ProcName = "CFAstRamTenant.readDerived() "; CFAstTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFAstTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTenantBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTenant.readAllDerived() "; CFAstTenantBuff[] retList = new CFAstTenantBuff[dictByPKey.values().size()]; Iterator<CFAstTenantBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstTenantBuff[] readDerivedByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamTenant.readDerivedByClusterIdx() "; CFAstTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAstTenantBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstTenantPKey, CFAstTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAstTenantBuff[subdictClusterIdx.size()]; Iterator<CFAstTenantBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstTenantBuff[0]; } return (recArray); } public CFAstTenantBuff readDerivedByUNameIdx(CFAstAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "CFAstRamTenant.readDerivedByUNameIdx() "; CFAstTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredTenantName(TenantName); CFAstTenantBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstTenantBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long Id) { final String S_ProcName = "CFAstRamTenant.readDerivedByIdIdx() "; CFAstTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(Id); CFAstTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTenantBuff readBuff(CFAstAuthorization Authorization, CFAstTenantPKey PKey) { final String S_ProcName = "CFAstRamTenant.readBuff() "; CFAstTenantBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TENT"))) { buff = null; } return (buff); } public CFAstTenantBuff lockBuff(CFAstAuthorization Authorization, CFAstTenantPKey PKey) { final String S_ProcName = "CFAstRamTenant.readBuff() "; CFAstTenantBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TENT"))) { buff = null; } return (buff); } public CFAstTenantBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTenant.readAllBuff() "; CFAstTenantBuff buff; ArrayList<CFAstTenantBuff> filteredList = new ArrayList<CFAstTenantBuff>(); CFAstTenantBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TENT")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstTenantBuff[0])); } public CFAstTenantBuff readBuffByIdIdx(CFAstAuthorization Authorization, long Id) { final String S_ProcName = "CFAstRamTenant.readBuffByIdIdx() "; CFAstTenantBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((CFAstTenantBuff) buff); } else { return (null); } } public CFAstTenantBuff[] readBuffByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamTenant.readBuffByClusterIdx() "; CFAstTenantBuff buff; ArrayList<CFAstTenantBuff> filteredList = new ArrayList<CFAstTenantBuff>(); CFAstTenantBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TENT")) { filteredList.add((CFAstTenantBuff) buff); } } return (filteredList.toArray(new CFAstTenantBuff[0])); } public CFAstTenantBuff readBuffByUNameIdx(CFAstAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "CFAstRamTenant.readBuffByUNameIdx() "; CFAstTenantBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, TenantName); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((CFAstTenantBuff) buff); } else { return (null); } } public void updateTenant(CFAstAuthorization Authorization, CFAstTenantBuff Buff) { CFAstTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAstTenantBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTenant", "Existing record not found", "Tenant", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTenant", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstTenantByClusterIdxKey existingKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstTenantByClusterIdxKey newKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstTenantByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); CFAstTenantByUNameIdxKey newKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); newKeyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTenant", "TenantUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTenant", "Container", "TenantCluster", "Cluster", null); } } } // Update is valid SortedMap<CFAstTenantPKey, CFAstTenantBuff> 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<CFAstTenantPKey, CFAstTenantBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTenant(CFAstAuthorization Authorization, CFAstTenantBuff Buff) { final String S_ProcName = "CFAstRamTenantTable.deleteTenant() "; CFAstTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFAstTenantBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTenant", pkey); } CFAstTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); // Validate reverse foreign keys // Delete is valid schema.getTableTSecGroup().deleteTSecGroupByTenantIdx(Authorization, Buff.getRequiredId()); schema.getTableDomainBase().deleteDomainBaseByTenantIdx(Authorization, Buff.getRequiredId()); schema.getTableTld().deleteTldByTenantIdx(Authorization, Buff.getRequiredId()); SortedMap<CFAstTenantPKey, CFAstTenantBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTenantByIdIdx(CFAstAuthorization Authorization, long argId) { CFAstTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(argId); deleteTenantByIdIdx(Authorization, key); } public void deleteTenantByIdIdx(CFAstAuthorization Authorization, CFAstTenantPKey argKey) { CFAstTenantBuff cur; LinkedList<CFAstTenantBuff> matchSet = new LinkedList<CFAstTenantBuff>(); Iterator<CFAstTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public void deleteTenantByClusterIdx(CFAstAuthorization Authorization, long argClusterId) { CFAstTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteTenantByClusterIdx(Authorization, key); } public void deleteTenantByClusterIdx(CFAstAuthorization Authorization, CFAstTenantByClusterIdxKey argKey) { CFAstTenantBuff cur; LinkedList<CFAstTenantBuff> matchSet = new LinkedList<CFAstTenantBuff>(); Iterator<CFAstTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public void deleteTenantByUNameIdx(CFAstAuthorization Authorization, long argClusterId, String argTenantName) { CFAstTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredTenantName(argTenantName); deleteTenantByUNameIdx(Authorization, key); } public void deleteTenantByUNameIdx(CFAstAuthorization Authorization, CFAstTenantByUNameIdxKey argKey) { CFAstTenantBuff cur; LinkedList<CFAstTenantBuff> matchSet = new LinkedList<CFAstTenantBuff>(); Iterator<CFAstTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public CFAstCursor openTenantCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamTenantCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openTenantCursorByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { CFAstCursor cursor; CFAstTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstTenantPKey, CFAstTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAstRamTenantCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAstRamTenantCursor(Authorization, schema, new ArrayList<CFAstTenantBuff>()); } return (cursor); } public void closeTenantCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstTenantBuff nextTenantCursor(CFAstCursor Cursor) { CFAstRamTenantCursor cursor = (CFAstRamTenantCursor) Cursor; CFAstTenantBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstTenantBuff prevTenantCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public CFAstTenantBuff firstTenantCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstTenantBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public CFAstTenantBuff lastTenantCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTenantCursor"); } public CFAstTenantBuff nthTenantCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }