Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for URLProtocol. /* * MSS Code Factory Accounting Business Application Model * * Copyright (c) 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.cfacc.v2_0.CFAccRam; 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.cfacc.v2_0.CFAcc.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*; /* * CFAccRamURLProtocolTable in-memory RAM DbIO implementation * for URLProtocol. */ public class CFAccRamURLProtocolTable implements ICFAccURLProtocolTable { private CFAccRamSchema schema; private Map<CFAccURLProtocolPKey, CFAccURLProtocolBuff> dictByPKey = new HashMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff>(); private SortedMap<CFAccURLProtocolByUNameIdxKey, CFAccURLProtocolBuff> dictByUNameIdx = new TreeMap<CFAccURLProtocolByUNameIdxKey, CFAccURLProtocolBuff>(); private SortedMap<CFAccURLProtocolByIsSecureIdxKey, SortedMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff>> dictByIsSecureIdx = new TreeMap<CFAccURLProtocolByIsSecureIdxKey, SortedMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff>>(); public CFAccRamURLProtocolTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createURLProtocol(CFAccAuthorization Authorization, CFAccURLProtocolBuff Buff) { CFAccURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); Buff.setRequiredURLProtocolId(pkey.getRequiredURLProtocolId()); CFAccURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(Buff.getRequiredName()); CFAccURLProtocolByIsSecureIdxKey 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<CFAccURLProtocolPKey, CFAccURLProtocolBuff> subdictIsSecureIdx; if (dictByIsSecureIdx.containsKey(keyIsSecureIdx)) { subdictIsSecureIdx = dictByIsSecureIdx.get(keyIsSecureIdx); } else { subdictIsSecureIdx = new TreeMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff>(); dictByIsSecureIdx.put(keyIsSecureIdx, subdictIsSecureIdx); } subdictIsSecureIdx.put(pkey, Buff); } public CFAccURLProtocolBuff readDerived(CFAccAuthorization Authorization, CFAccURLProtocolPKey PKey) { final String S_ProcName = "CFAccRamURLProtocol.readDerived() "; CFAccURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFAccURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccURLProtocolBuff lockDerived(CFAccAuthorization Authorization, CFAccURLProtocolPKey PKey) { final String S_ProcName = "CFAccRamURLProtocol.readDerived() "; CFAccURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(PKey.getRequiredURLProtocolId()); CFAccURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccURLProtocolBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamURLProtocol.readAllDerived() "; CFAccURLProtocolBuff[] retList = new CFAccURLProtocolBuff[dictByPKey.values().size()]; Iterator<CFAccURLProtocolBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccURLProtocolBuff readDerivedByUNameIdx(CFAccAuthorization Authorization, String Name) { final String S_ProcName = "CFAccRamURLProtocol.readDerivedByUNameIdx() "; CFAccURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(Name); CFAccURLProtocolBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAccURLProtocolBuff[] readDerivedByIsSecureIdx(CFAccAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAccRamURLProtocol.readDerivedByIsSecureIdx() "; CFAccURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); CFAccURLProtocolBuff[] recArray; if (dictByIsSecureIdx.containsKey(key)) { SortedMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx.get(key); recArray = new CFAccURLProtocolBuff[subdictIsSecureIdx.size()]; Iterator<CFAccURLProtocolBuff> iter = subdictIsSecureIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAccURLProtocolBuff[0]; } return (recArray); } public CFAccURLProtocolBuff readDerivedByIdIdx(CFAccAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAccRamURLProtocol.readDerivedByIdIdx() "; CFAccURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(URLProtocolId); CFAccURLProtocolBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccURLProtocolBuff readBuff(CFAccAuthorization Authorization, CFAccURLProtocolPKey PKey) { final String S_ProcName = "CFAccRamURLProtocol.readBuff() "; CFAccURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFAccURLProtocolBuff lockBuff(CFAccAuthorization Authorization, CFAccURLProtocolPKey PKey) { final String S_ProcName = "CFAccRamURLProtocol.readBuff() "; CFAccURLProtocolBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("UPRT"))) { buff = null; } return (buff); } public CFAccURLProtocolBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamURLProtocol.readAllBuff() "; CFAccURLProtocolBuff buff; ArrayList<CFAccURLProtocolBuff> filteredList = new ArrayList<CFAccURLProtocolBuff>(); CFAccURLProtocolBuff[] 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 CFAccURLProtocolBuff[0])); } public CFAccURLProtocolBuff readBuffByIdIdx(CFAccAuthorization Authorization, short URLProtocolId) { final String S_ProcName = "CFAccRamURLProtocol.readBuffByIdIdx() "; CFAccURLProtocolBuff buff = readDerivedByIdIdx(Authorization, URLProtocolId); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFAccURLProtocolBuff) buff); } else { return (null); } } public CFAccURLProtocolBuff readBuffByUNameIdx(CFAccAuthorization Authorization, String Name) { final String S_ProcName = "CFAccRamURLProtocol.readBuffByUNameIdx() "; CFAccURLProtocolBuff buff = readDerivedByUNameIdx(Authorization, Name); if ((buff != null) && buff.getClassCode().equals("UPRT")) { return ((CFAccURLProtocolBuff) buff); } else { return (null); } } public CFAccURLProtocolBuff[] readBuffByIsSecureIdx(CFAccAuthorization Authorization, boolean IsSecure) { final String S_ProcName = "CFAccRamURLProtocol.readBuffByIsSecureIdx() "; CFAccURLProtocolBuff buff; ArrayList<CFAccURLProtocolBuff> filteredList = new ArrayList<CFAccURLProtocolBuff>(); CFAccURLProtocolBuff[] buffList = readDerivedByIsSecureIdx(Authorization, IsSecure); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("UPRT")) { filteredList.add((CFAccURLProtocolBuff) buff); } } return (filteredList.toArray(new CFAccURLProtocolBuff[0])); } public void updateURLProtocol(CFAccAuthorization Authorization, CFAccURLProtocolBuff Buff) { CFAccURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFAccURLProtocolBuff 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); CFAccURLProtocolByUNameIdxKey existingKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAccURLProtocolByUNameIdxKey newKeyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); CFAccURLProtocolByIsSecureIdxKey existingKeyIsSecureIdx = schema.getFactoryURLProtocol() .newIsSecureIdxKey(); existingKeyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); CFAccURLProtocolByIsSecureIdxKey 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<CFAccURLProtocolPKey, CFAccURLProtocolBuff> 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<CFAccURLProtocolPKey, CFAccURLProtocolBuff>(); dictByIsSecureIdx.put(newKeyIsSecureIdx, subdict); } subdict.put(pkey, Buff); } public void deleteURLProtocol(CFAccAuthorization Authorization, CFAccURLProtocolBuff Buff) { final String S_ProcName = "CFAccRamURLProtocolTable.deleteURLProtocol() "; CFAccURLProtocolPKey pkey = schema.getFactoryURLProtocol().newPKey(); pkey.setRequiredURLProtocolId(Buff.getRequiredURLProtocolId()); CFAccURLProtocolBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteURLProtocol", pkey); } CFAccURLProtocolByUNameIdxKey keyUNameIdx = schema.getFactoryURLProtocol().newUNameIdxKey(); keyUNameIdx.setRequiredName(existing.getRequiredName()); CFAccURLProtocolByIsSecureIdxKey keyIsSecureIdx = schema.getFactoryURLProtocol().newIsSecureIdxKey(); keyIsSecureIdx.setRequiredIsSecure(existing.getRequiredIsSecure()); // Validate reverse foreign keys // Delete is valid SortedMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff> subdict; dictByPKey.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); subdict = dictByIsSecureIdx.get(keyIsSecureIdx); subdict.remove(pkey); } public void deleteURLProtocolByIdIdx(CFAccAuthorization Authorization, short argURLProtocolId) { CFAccURLProtocolPKey key = schema.getFactoryURLProtocol().newPKey(); key.setRequiredURLProtocolId(argURLProtocolId); deleteURLProtocolByIdIdx(Authorization, key); } public void deleteURLProtocolByIdIdx(CFAccAuthorization Authorization, CFAccURLProtocolPKey argKey) { CFAccURLProtocolBuff cur; LinkedList<CFAccURLProtocolBuff> matchSet = new LinkedList<CFAccURLProtocolBuff>(); Iterator<CFAccURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByUNameIdx(CFAccAuthorization Authorization, String argName) { CFAccURLProtocolByUNameIdxKey key = schema.getFactoryURLProtocol().newUNameIdxKey(); key.setRequiredName(argName); deleteURLProtocolByUNameIdx(Authorization, key); } public void deleteURLProtocolByUNameIdx(CFAccAuthorization Authorization, CFAccURLProtocolByUNameIdxKey argKey) { CFAccURLProtocolBuff cur; LinkedList<CFAccURLProtocolBuff> matchSet = new LinkedList<CFAccURLProtocolBuff>(); Iterator<CFAccURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public void deleteURLProtocolByIsSecureIdx(CFAccAuthorization Authorization, boolean argIsSecure) { CFAccURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(argIsSecure); deleteURLProtocolByIsSecureIdx(Authorization, key); } public void deleteURLProtocolByIsSecureIdx(CFAccAuthorization Authorization, CFAccURLProtocolByIsSecureIdxKey argKey) { CFAccURLProtocolBuff cur; LinkedList<CFAccURLProtocolBuff> matchSet = new LinkedList<CFAccURLProtocolBuff>(); Iterator<CFAccURLProtocolBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccURLProtocolBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteURLProtocol(Authorization, cur); } } public CFAccCursor openURLProtocolCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamURLProtocolCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAccCursor openURLProtocolCursorByIsSecureIdx(CFAccAuthorization Authorization, boolean IsSecure) { CFAccCursor cursor; CFAccURLProtocolByIsSecureIdxKey key = schema.getFactoryURLProtocol().newIsSecureIdxKey(); key.setRequiredIsSecure(IsSecure); if (dictByIsSecureIdx.containsKey(key)) { SortedMap<CFAccURLProtocolPKey, CFAccURLProtocolBuff> subdictIsSecureIdx = dictByIsSecureIdx.get(key); cursor = new CFAccRamURLProtocolCursor(Authorization, schema, subdictIsSecureIdx.values()); } else { cursor = new CFAccRamURLProtocolCursor(Authorization, schema, new ArrayList<CFAccURLProtocolBuff>()); } return (cursor); } public void closeURLProtocolCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccURLProtocolBuff nextURLProtocolCursor(CFAccCursor Cursor) { CFAccRamURLProtocolCursor cursor = (CFAccRamURLProtocolCursor) Cursor; CFAccURLProtocolBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccURLProtocolBuff prevURLProtocolCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFAccURLProtocolBuff firstURLProtocolCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccURLProtocolBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public CFAccURLProtocolBuff lastURLProtocolCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastURLProtocolCursor"); } public CFAccURLProtocolBuff nthURLProtocolCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccURLProtocolBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextURLProtocolCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }