Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for URLProtocol. /* * 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.*; /* * CFAstRamURLProtocolTable in-memory RAM DbIO implementation * for URLProtocol. */ public class CFAstRamURLProtocolTable implements ICFAstURLProtocolTable { private CFAstRamSchema schema; private Map<CFAstURLProtocolPKey, CFAstURLProtocolBuff> dictByPKey = new HashMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff>(); private SortedMap<CFAstURLProtocolByUNameIdxKey, CFAstURLProtocolBuff> dictByUNameIdx = new TreeMap<CFAstURLProtocolByUNameIdxKey, CFAstURLProtocolBuff>(); private SortedMap<CFAstURLProtocolByIsSecureIdxKey, SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff>> dictByIsSecureIdx = new TreeMap<CFAstURLProtocolByIsSecureIdxKey, SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff>>(); public CFAstRamURLProtocolTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createURLProtocol(CFAstAuthorization Authorization, CFAstURLProtocolBuff Buff) { CFAstURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); Buff.setRequiredURLProtocolId(pkey.getRequiredURLProtocolId()); CFAstURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(Buff.getRequiredName()); CFAstURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); keyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createURLProtocol", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createURLProtocol", "URLProtocolUNameIdx", keyUNameIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff> subdictIsSecureIdx; if (dictByIsSecureIdx.containsKey(keyIsSecureIdx)) { subdictIsSecureIdx = dictByIsSecureIdx.get(keyIsSecureIdx); } else { subdictIsSecureIdx = new TreeMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff>(); dictByIsSecureIdx.put(keyIsSecureIdx, subdictIsSecureIdx); } subdictIsSecureIdx.put(pkey, Buff); } public CFAstURLProtocolBuff readDerived(CFAstAuthorization Authorization, CFAstURLProtocolPKey PKey) { final String S_ProcName = "CFAstRamURLProtocol.readDerived() "; CFAstURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFAstURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstURLProtocolBuff lockDerived(CFAstAuthorization Authorization, CFAstURLProtocolPKey PKey) { final String S_ProcName = "CFAstRamURLProtocol.readDerived() "; CFAstURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFAstURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstURLProtocolBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamURLProtocol.readAllDerived() "; CFAstURLProtocolBuff[] retList = new CFAstURLProtocolBuff[dictByPKey.values().size()]; Iterator<CFAstURLProtocolBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstURLProtocolBuff readDerivedByUNameIdx(CFAstAuthorization Authorization, String Name) { final String S_ProcName = "CFAstRamURLProtocol.readDerivedByUNameIdx() "; CFAstURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(Name); CFAstURLProtocolBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAstURLProtocolBuff[] readDerivedByIsSecureIdx(CFAstAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAstRamURLProtocol.readDerivedByIsSecureIdx() "; CFAstURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); CFAstURLProtocolBuff[] recArray; if (dictByIsSecureIdx.containsKey(key)) { SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx.get(key); recArray = new CFAstURLProtocolBuff[subdictIsSecureIdx.size()]; Iterator<CFAstURLProtocolBuff> iter = subdictIsSecureIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstURLProtocolBuff[0]; } return (recArray); } public CFAstURLProtocolBuff readDerivedByIdIdx(CFAstAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAstRamURLProtocol.readDerivedByIdIdx() "; CFAstURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(URLProtocolId); CFAstURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstURLProtocolBuff readBuff(CFAstAuthorization Authorization, CFAstURLProtocolPKey PKey) { final String S_ProcName = "CFAstRamURLProtocol.readBuff() "; CFAstURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFAstURLProtocolBuff lockBuff(CFAstAuthorization Authorization, CFAstURLProtocolPKey PKey) { final String S_ProcName = "CFAstRamURLProtocol.readBuff() "; CFAstURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFAstURLProtocolBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamURLProtocol.readAllBuff() "; CFAstURLProtocolBuff buff; ArrayList<CFAstURLProtocolBuff> filteredList = new ArrayList<CFAstURLProtocolBuff>(); CFAstURLProtocolBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("UPRT")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstURLProtocolBuff[0])); } public CFAstURLProtocolBuff readBuffByIdIdx(CFAstAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAstRamURLProtocol.readBuffByIdIdx() "; CFAstURLProtocolBuff buff = readDerivedByIdIdx(Authorization, URLProtocolId); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFAstURLProtocolBuff) buff); } else { return (null); } } public CFAstURLProtocolBuff readBuffByUNameIdx(CFAstAuthorization Authorization, String Name) { final String S_ProcName = "CFAstRamURLProtocol.readBuffByUNameIdx() "; CFAstURLProtocolBuff buff = readDerivedByUNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFAstURLProtocolBuff) buff); } else { return (null); } } public CFAstURLProtocolBuff[] readBuffByIsSecureIdx(CFAstAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAstRamURLProtocol.readBuffByIsSecureIdx() "; CFAstURLProtocolBuff buff; ArrayList<CFAstURLProtocolBuff> filteredList = new ArrayList<CFAstURLProtocolBuff>(); CFAstURLProtocolBuff[] buffList = readDerivedByIsSecureIdx(Authorization, IsSecure); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("UPRT")) { filteredList.add((CFAstURLProtocolBuff) buff); } } return (filteredList.toArray(new CFAstURLProtocolBuff[0])); } public void updateURLProtocol(CFAstAuthorization Authorization, CFAstURLProtocolBuff Buff) { CFAstURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFAstURLProtocolBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateURLProtocol", "Existing record not found", "URLProtocol", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateURLProtocol", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstURLProtocolByUNameIdxKey existingKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAstURLProtocolByUNameIdxKey newKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); CFAstURLProtocolByIsSecureIdxKey existingKeyIsSecureIdx = schema.getFactoryURLProtocol() .newIsSecureIdxKey(); existingKeyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); CFAstURLProtocolByIsSecureIdxKey newKeyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); newKeyIsSecureIdx.setRequiredIsSecure(Buff.getRequiredIsSecure()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateURLProtocol", "URLProtocolUNameIdx", newKeyUNameIdx); } } // Validate foreign keys // Update is valid SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); subdict = dictByIsSecureIdx.get(existingKeyIsSecureIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByIsSecureIdx.containsKey(newKeyIsSecureIdx)) { subdict = dictByIsSecureIdx.get(newKeyIsSecureIdx); } else { subdict = new TreeMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff>(); dictByIsSecureIdx.put(newKeyIsSecureIdx, subdict); } subdict.put(pkey, Buff); } public void deleteURLProtocol(CFAstAuthorization Authorization, CFAstURLProtocolBuff Buff) { final String S_ProcName = "CFAstRamURLProtocolTable.deleteURLProtocol() "; CFAstURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFAstURLProtocolBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteURLProtocol", pkey); } CFAstURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(existing.getRequiredName()); CFAstURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); keyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); // Validate reverse foreign keys // Delete is valid SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff> subdict; dictByPKey.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); subdict = dictByIsSecureIdx.get(keyIsSecureIdx); subdict.remove(pkey); } public void deleteURLProtocolByIdIdx(CFAstAuthorization Authorization, short argURLProtocolId) { CFAstURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(argURLProtocolId); deleteURLProtocolByIdIdx(Authorization, key); } public void deleteURLProtocolByIdIdx(CFAstAuthorization Authorization, CFAstURLProtocolPKey argKey) { CFAstURLProtocolBuff cur; LinkedList<CFAstURLProtocolBuff> matchSet = new LinkedList<CFAstURLProtocolBuff>(); Iterator<CFAstURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByUNameIdx(CFAstAuthorization Authorization, String argName) { CFAstURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(argName); deleteURLProtocolByUNameIdx(Authorization, key); } public void deleteURLProtocolByUNameIdx(CFAstAuthorization Authorization, CFAstURLProtocolByUNameIdxKey argKey) { CFAstURLProtocolBuff cur; LinkedList<CFAstURLProtocolBuff> matchSet = new LinkedList<CFAstURLProtocolBuff>(); Iterator<CFAstURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByIsSecureIdx(CFAstAuthorization Authorization, boolean argIsSecure) { CFAstURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(argIsSecure); deleteURLProtocolByIsSecureIdx(Authorization, key); } public void deleteURLProtocolByIsSecureIdx(CFAstAuthorization Authorization, CFAstURLProtocolByIsSecureIdxKey argKey) { CFAstURLProtocolBuff cur; LinkedList<CFAstURLProtocolBuff> matchSet = new LinkedList<CFAstURLProtocolBuff>(); Iterator<CFAstURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public CFAstCursor openURLProtocolCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamURLProtocolCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openURLProtocolCursorByIsSecureIdx(CFAstAuthorization Authorization, boolean IsSecure) { CFAstCursor cursor; CFAstURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); if (dictByIsSecureIdx.containsKey(key)) { SortedMap<CFAstURLProtocolPKey, CFAstURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx.get(key); cursor = new CFAstRamURLProtocolCursor(Authorization, schema, subdictIsSecureIdx.values()); } else { cursor = new CFAstRamURLProtocolCursor(Authorization, schema, new ArrayList<CFAstURLProtocolBuff>()); } return (cursor); } public void closeURLProtocolCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstURLProtocolBuff nextURLProtocolCursor(CFAstCursor Cursor) { CFAstRamURLProtocolCursor cursor = (CFAstRamURLProtocolCursor) Cursor; CFAstURLProtocolBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstURLProtocolBuff prevURLProtocolCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFAstURLProtocolBuff firstURLProtocolCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstURLProtocolBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFAstURLProtocolBuff lastURLProtocolCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastURLProtocolCursor"); } public CFAstURLProtocolBuff nthURLProtocolCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }