Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for Tld. /* * 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.*; /* * CFAstRamTldTable in-memory RAM DbIO implementation * for Tld. */ public class CFAstRamTldTable implements ICFAstTldTable { private CFAstRamSchema schema; private Map<CFAstTldPKey, CFAstTldBuff> dictByPKey = new HashMap<CFAstTldPKey, CFAstTldBuff>(); private SortedMap<CFAstTldByTenantIdxKey, SortedMap<CFAstTldPKey, CFAstTldBuff>> dictByTenantIdx = new TreeMap<CFAstTldByTenantIdxKey, SortedMap<CFAstTldPKey, CFAstTldBuff>>(); private SortedMap<CFAstTldByNameIdxKey, CFAstTldBuff> dictByNameIdx = new TreeMap<CFAstTldByNameIdxKey, CFAstTldBuff>(); public CFAstRamTldTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createTld(CFAstAuthorization Authorization, CFAstTldBuff Buff) { CFAstTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(((CFAstRamTenantTable) schema.getTableTenant()).nextTLDIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTLDId(pkey.getRequiredTLDId()); CFAstTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAstTldByNameIdxKey keyNameIdx = schema.getFactoryTld().newNameIdxKey(); keyNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTld", pkey); } if (dictByNameIdx.containsKey(keyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTld", "TLDNameIdx", keyNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createTld", "Container", "Tenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAstTldPKey, CFAstTldBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFAstTldPKey, CFAstTldBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByNameIdx.put(keyNameIdx, Buff); } public CFAstTldBuff readDerived(CFAstAuthorization Authorization, CFAstTldPKey PKey) { final String S_ProcName = "CFAstRamTld.readDerived() "; CFAstTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTLDId(PKey.getRequiredTLDId()); CFAstTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTldBuff lockDerived(CFAstAuthorization Authorization, CFAstTldPKey PKey) { final String S_ProcName = "CFAstRamTld.readDerived() "; CFAstTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTLDId(PKey.getRequiredTLDId()); CFAstTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTldBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTld.readAllDerived() "; CFAstTldBuff[] retList = new CFAstTldBuff[dictByPKey.values().size()]; Iterator<CFAstTldBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstTldBuff[] readDerivedByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamTld.readDerivedByTenantIdx() "; CFAstTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFAstTldBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstTldPKey, CFAstTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFAstTldBuff[subdictTenantIdx.size()]; Iterator<CFAstTldBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstTldBuff[0]; } return (recArray); } public CFAstTldBuff readDerivedByNameIdx(CFAstAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAstRamTld.readDerivedByNameIdx() "; CFAstTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFAstTldBuff buff; if (dictByNameIdx.containsKey(key)) { buff = dictByNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstTldBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long TenantId, long TLDId) { final String S_ProcName = "CFAstRamTld.readDerivedByIdIdx() "; CFAstTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTLDId(TLDId); CFAstTldBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstTldBuff readBuff(CFAstAuthorization Authorization, CFAstTldPKey PKey) { final String S_ProcName = "CFAstRamTld.readBuff() "; CFAstTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTLD"))) { buff = null; } return (buff); } public CFAstTldBuff lockBuff(CFAstAuthorization Authorization, CFAstTldPKey PKey) { final String S_ProcName = "CFAstRamTld.readBuff() "; CFAstTldBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("GTLD"))) { buff = null; } return (buff); } public CFAstTldBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamTld.readAllBuff() "; CFAstTldBuff buff; ArrayList<CFAstTldBuff> filteredList = new ArrayList<CFAstTldBuff>(); CFAstTldBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("GTLD")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstTldBuff[0])); } public CFAstTldBuff readBuffByIdIdx(CFAstAuthorization Authorization, long TenantId, long TLDId) { final String S_ProcName = "CFAstRamTld.readBuffByIdIdx() "; CFAstTldBuff buff = readDerivedByIdIdx(Authorization, TenantId, TLDId); if ((buff != null) && buff.getClassCode().equals("GTLD")) { return ((CFAstTldBuff) buff); } else { return (null); } } public CFAstTldBuff[] readBuffByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamTld.readBuffByTenantIdx() "; CFAstTldBuff buff; ArrayList<CFAstTldBuff> filteredList = new ArrayList<CFAstTldBuff>(); CFAstTldBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("GTLD")) { filteredList.add((CFAstTldBuff) buff); } } return (filteredList.toArray(new CFAstTldBuff[0])); } public CFAstTldBuff readBuffByNameIdx(CFAstAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAstRamTld.readBuffByNameIdx() "; CFAstTldBuff buff = readDerivedByNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("GTLD")) { return ((CFAstTldBuff) buff); } else { return (null); } } public void updateTld(CFAstAuthorization Authorization, CFAstTldBuff Buff) { CFAstTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(Buff.getRequiredTLDId()); CFAstTldBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTld", "Existing record not found", "Tld", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTld", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstTldByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAstTldByTenantIdxKey newKeyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAstTldByNameIdxKey existingKeyNameIdx = schema.getFactoryTld().newNameIdxKey(); existingKeyNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyNameIdx.setRequiredName(existing.getRequiredName()); CFAstTldByNameIdxKey newKeyNameIdx = schema.getFactoryTld().newNameIdxKey(); newKeyNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); newKeyNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyNameIdx.equals(newKeyNameIdx)) { if (dictByNameIdx.containsKey(newKeyNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTld", "TLDNameIdx", newKeyNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTld", "Container", "Tenant", "Tenant", null); } } } // Update is valid SortedMap<CFAstTldPKey, CFAstTldBuff> 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<CFAstTldPKey, CFAstTldBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByNameIdx.remove(existingKeyNameIdx); dictByNameIdx.put(newKeyNameIdx, Buff); } public void deleteTld(CFAstAuthorization Authorization, CFAstTldBuff Buff) { final String S_ProcName = "CFAstRamTldTable.deleteTld() "; CFAstTldPKey pkey = schema.getFactoryTld().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTLDId(Buff.getRequiredTLDId()); CFAstTldBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTld", pkey); } CFAstTldByTenantIdxKey keyTenantIdx = schema.getFactoryTld().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAstTldByNameIdxKey keyNameIdx = schema.getFactoryTld().newNameIdxKey(); keyNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); keyNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableTopDomain().deleteTopDomainByTLDIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTLDId()); SortedMap<CFAstTldPKey, CFAstTldBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByNameIdx.remove(keyNameIdx); } public void deleteTldByIdIdx(CFAstAuthorization Authorization, long argTenantId, long argTLDId) { CFAstTldPKey key = schema.getFactoryTld().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTLDId(argTLDId); deleteTldByIdIdx(Authorization, key); } public void deleteTldByIdIdx(CFAstAuthorization Authorization, CFAstTldPKey argKey) { CFAstTldBuff cur; LinkedList<CFAstTldBuff> matchSet = new LinkedList<CFAstTldBuff>(); Iterator<CFAstTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByTenantIdx(CFAstAuthorization Authorization, long argTenantId) { CFAstTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTldByTenantIdx(Authorization, key); } public void deleteTldByTenantIdx(CFAstAuthorization Authorization, CFAstTldByTenantIdxKey argKey) { CFAstTldBuff cur; LinkedList<CFAstTldBuff> matchSet = new LinkedList<CFAstTldBuff>(); Iterator<CFAstTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public void deleteTldByNameIdx(CFAstAuthorization Authorization, long argTenantId, String argName) { CFAstTldByNameIdxKey key = schema.getFactoryTld().newNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTldByNameIdx(Authorization, key); } public void deleteTldByNameIdx(CFAstAuthorization Authorization, CFAstTldByNameIdxKey argKey) { CFAstTldBuff cur; LinkedList<CFAstTldBuff> matchSet = new LinkedList<CFAstTldBuff>(); Iterator<CFAstTldBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstTldBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTld(Authorization, cur); } } public CFAstCursor openTldCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamTldCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openTldCursorByTenantIdx(CFAstAuthorization Authorization, long TenantId) { CFAstCursor cursor; CFAstTldByTenantIdxKey key = schema.getFactoryTld().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstTldPKey, CFAstTldBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAstRamTldCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAstRamTldCursor(Authorization, schema, new ArrayList<CFAstTldBuff>()); } return (cursor); } public void closeTldCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstTldBuff nextTldCursor(CFAstCursor Cursor) { CFAstRamTldCursor cursor = (CFAstRamTldCursor) Cursor; CFAstTldBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstTldBuff prevTldCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFAstTldBuff firstTldCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstTldBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public CFAstTldBuff lastTldCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTldCursor"); } public CFAstTldBuff nthTldCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstTldBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTldCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }