Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecApp. /* * 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.*; /* * CFAstRamSecAppTable in-memory RAM DbIO implementation * for SecApp. */ public class CFAstRamSecAppTable implements ICFAstSecAppTable { private CFAstRamSchema schema; private Map<CFAstSecAppPKey, CFAstSecAppBuff> dictByPKey = new HashMap<CFAstSecAppPKey, CFAstSecAppBuff>(); private SortedMap<CFAstSecAppByClusterIdxKey, SortedMap<CFAstSecAppPKey, CFAstSecAppBuff>> dictByClusterIdx = new TreeMap<CFAstSecAppByClusterIdxKey, SortedMap<CFAstSecAppPKey, CFAstSecAppBuff>>(); private SortedMap<CFAstSecAppByUJEEMountIdxKey, CFAstSecAppBuff> dictByUJEEMountIdx = new TreeMap<CFAstSecAppByUJEEMountIdxKey, CFAstSecAppBuff>(); public CFAstRamSecAppTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createSecApp(CFAstAuthorization Authorization, CFAstSecAppBuff Buff) { CFAstSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(((CFAstRamClusterTable) schema.getTableCluster()).nextSecAppIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecAppId(pkey.getRequiredSecAppId()); CFAstSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); keyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createSecApp", pkey); } if (dictByUJEEMountIdx.containsKey(keyUJEEMountIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecApp", "SecAppUJEEMountIdx", keyUJEEMountIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createSecApp", "Container", "SecAppCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAstSecAppPKey, CFAstSecAppBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFAstSecAppPKey, CFAstSecAppBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUJEEMountIdx.put(keyUJEEMountIdx, Buff); } public CFAstSecAppBuff readDerived(CFAstAuthorization Authorization, CFAstSecAppPKey PKey) { final String S_ProcName = "CFAstRamSecApp.readDerived() "; CFAstSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFAstSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecAppBuff lockDerived(CFAstAuthorization Authorization, CFAstSecAppPKey PKey) { final String S_ProcName = "CFAstRamSecApp.readDerived() "; CFAstSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFAstSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecAppBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecApp.readAllDerived() "; CFAstSecAppBuff[] retList = new CFAstSecAppBuff[dictByPKey.values().size()]; Iterator<CFAstSecAppBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstSecAppBuff[] readDerivedByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSecApp.readDerivedByClusterIdx() "; CFAstSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAstSecAppBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstSecAppPKey, CFAstSecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAstSecAppBuff[subdictClusterIdx.size()]; Iterator<CFAstSecAppBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstSecAppBuff[0]; } return (recArray); } public CFAstSecAppBuff readDerivedByUJEEMountIdx(CFAstAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAstRamSecApp.readDerivedByUJEEMountIdx() "; CFAstSecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredJEEMountName(JEEMountName); CFAstSecAppBuff buff; if (dictByUJEEMountIdx.containsKey(key)) { buff = dictByUJEEMountIdx.get(key); } else { buff = null; } return (buff); } public CFAstSecAppBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAstRamSecApp.readDerivedByIdIdx() "; CFAstSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecAppId(SecAppId); CFAstSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSecAppBuff readBuff(CFAstAuthorization Authorization, CFAstSecAppPKey PKey) { final String S_ProcName = "CFAstRamSecApp.readBuff() "; CFAstSecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFAstSecAppBuff lockBuff(CFAstAuthorization Authorization, CFAstSecAppPKey PKey) { final String S_ProcName = "CFAstRamSecApp.readBuff() "; CFAstSecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFAstSecAppBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSecApp.readAllBuff() "; CFAstSecAppBuff buff; ArrayList<CFAstSecAppBuff> filteredList = new ArrayList<CFAstSecAppBuff>(); CFAstSecAppBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstSecAppBuff[0])); } public CFAstSecAppBuff readBuffByIdIdx(CFAstAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAstRamSecApp.readBuffByIdIdx() "; CFAstSecAppBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecAppId); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFAstSecAppBuff) buff); } else { return (null); } } public CFAstSecAppBuff[] readBuffByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSecApp.readBuffByClusterIdx() "; CFAstSecAppBuff buff; ArrayList<CFAstSecAppBuff> filteredList = new ArrayList<CFAstSecAppBuff>(); CFAstSecAppBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add((CFAstSecAppBuff) buff); } } return (filteredList.toArray(new CFAstSecAppBuff[0])); } public CFAstSecAppBuff readBuffByUJEEMountIdx(CFAstAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAstRamSecApp.readBuffByUJEEMountIdx() "; CFAstSecAppBuff buff = readDerivedByUJEEMountIdx(Authorization, ClusterId, JEEMountName); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFAstSecAppBuff) buff); } else { return (null); } } public void updateSecApp(CFAstAuthorization Authorization, CFAstSecAppBuff Buff) { CFAstSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFAstSecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecApp", "Existing record not found", "SecApp", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecApp", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstSecAppByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstSecAppByClusterIdxKey newKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAstSecAppByUJEEMountIdxKey existingKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); existingKeyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); CFAstSecAppByUJEEMountIdxKey newKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); newKeyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName()); // Check unique indexes if (!existingKeyUJEEMountIdx.equals(newKeyUJEEMountIdx)) { if (dictByUJEEMountIdx.containsKey(newKeyUJEEMountIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateSecApp", "SecAppUJEEMountIdx", newKeyUJEEMountIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSecApp", "Container", "SecAppCluster", "Cluster", null); } } } // Update is valid SortedMap<CFAstSecAppPKey, CFAstSecAppBuff> 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<CFAstSecAppPKey, CFAstSecAppBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUJEEMountIdx.remove(existingKeyUJEEMountIdx); dictByUJEEMountIdx.put(newKeyUJEEMountIdx, Buff); } public void deleteSecApp(CFAstAuthorization Authorization, CFAstSecAppBuff Buff) { final String S_ProcName = "CFAstRamSecAppTable.deleteSecApp() "; CFAstSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFAstSecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecApp", pkey); } CFAstSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); keyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); // Validate reverse foreign keys // Delete is valid schema.getTableSecForm().deleteSecFormBySecAppIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecAppId()); SortedMap<CFAstSecAppPKey, CFAstSecAppBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUJEEMountIdx.remove(keyUJEEMountIdx); } public void deleteSecAppByIdIdx(CFAstAuthorization Authorization, long argClusterId, int argSecAppId) { CFAstSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecAppId(argSecAppId); deleteSecAppByIdIdx(Authorization, key); } public void deleteSecAppByIdIdx(CFAstAuthorization Authorization, CFAstSecAppPKey argKey) { CFAstSecAppBuff cur; LinkedList<CFAstSecAppBuff> matchSet = new LinkedList<CFAstSecAppBuff>(); Iterator<CFAstSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByClusterIdx(CFAstAuthorization Authorization, long argClusterId) { CFAstSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecAppByClusterIdx(Authorization, key); } public void deleteSecAppByClusterIdx(CFAstAuthorization Authorization, CFAstSecAppByClusterIdxKey argKey) { CFAstSecAppBuff cur; LinkedList<CFAstSecAppBuff> matchSet = new LinkedList<CFAstSecAppBuff>(); Iterator<CFAstSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByUJEEMountIdx(CFAstAuthorization Authorization, long argClusterId, String argJEEMountName) { CFAstSecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredJEEMountName(argJEEMountName); deleteSecAppByUJEEMountIdx(Authorization, key); } public void deleteSecAppByUJEEMountIdx(CFAstAuthorization Authorization, CFAstSecAppByUJEEMountIdxKey argKey) { CFAstSecAppBuff cur; LinkedList<CFAstSecAppBuff> matchSet = new LinkedList<CFAstSecAppBuff>(); Iterator<CFAstSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public CFAstCursor openSecAppCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamSecAppCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openSecAppCursorByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { CFAstCursor cursor; CFAstSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAstSecAppPKey, CFAstSecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAstRamSecAppCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAstRamSecAppCursor(Authorization, schema, new ArrayList<CFAstSecAppBuff>()); } return (cursor); } public void closeSecAppCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstSecAppBuff nextSecAppCursor(CFAstCursor Cursor) { CFAstRamSecAppCursor cursor = (CFAstRamSecAppCursor) Cursor; CFAstSecAppBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstSecAppBuff prevSecAppCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstSecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFAstSecAppBuff firstSecAppCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstSecAppBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFAstSecAppBuff lastSecAppCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecAppCursor"); } public CFAstSecAppBuff nthSecAppCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstSecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }