Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for SecApp. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; /* * CFAsteriskRamSecAppTable in-memory RAM DbIO implementation * for SecApp. */ public class CFAsteriskRamSecAppTable implements ICFAsteriskSecAppTable { private ICFAsteriskSchema schema; private Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> dictByPKey = new HashMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); private Map<CFSecuritySecAppByClusterIdxKey, Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff>> dictByClusterIdx = new HashMap<CFSecuritySecAppByClusterIdxKey, Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff>>(); private Map<CFSecuritySecAppByUJEEMountIdxKey, CFSecuritySecAppBuff> dictByUJEEMountIdx = new HashMap<CFSecuritySecAppByUJEEMountIdxKey, CFSecuritySecAppBuff>(); public CFAsteriskRamSecAppTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { final String S_ProcName = "createSecApp"; CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(((CFAsteriskRamClusterTable) schema.getTableCluster()) .nextSecAppIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecAppId(pkey.getRequiredSecAppId()); CFSecuritySecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); keyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByUJEEMountIdx.containsKey(keyUJEEMountIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "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(), S_ProcName, "Container", "SecAppCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new HashMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUJEEMountIdx.put(keyUJEEMountIdx, Buff); } public CFSecuritySecAppBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFAsteriskRamSecApp.readDerived"; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFAsteriskRamSecApp.readDerived"; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSecApp.readAllDerived"; CFSecuritySecAppBuff[] retList = new CFSecuritySecAppBuff[dictByPKey.values().size()]; Iterator<CFSecuritySecAppBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySecAppBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSecApp.readDerivedByClusterIdx"; CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecuritySecAppBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecuritySecAppBuff[subdictClusterIdx.size()]; Iterator<CFSecuritySecAppBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx = new HashMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); dictByClusterIdx.put(key, subdictClusterIdx); recArray = new CFSecuritySecAppBuff[0]; } return (recArray); } public CFSecuritySecAppBuff readDerivedByUJEEMountIdx(CFSecurityAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAsteriskRamSecApp.readDerivedByUJEEMountIdx"; CFSecuritySecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredJEEMountName(JEEMountName); CFSecuritySecAppBuff buff; if (dictByUJEEMountIdx.containsKey(key)) { buff = dictByUJEEMountIdx.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAsteriskRamSecApp.readDerivedByIdIdx() "; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecAppId(SecAppId); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFAsteriskRamSecApp.readBuff"; CFSecuritySecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFSecuritySecAppBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "lockBuff"; CFSecuritySecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFSecuritySecAppBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSecApp.readAllBuff"; CFSecuritySecAppBuff buff; ArrayList<CFSecuritySecAppBuff> filteredList = new ArrayList<CFSecuritySecAppBuff>(); CFSecuritySecAppBuff[] 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 CFSecuritySecAppBuff[0])); } public CFSecuritySecAppBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAsteriskRamSecApp.readBuffByIdIdx() "; CFSecuritySecAppBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecAppId); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFSecuritySecAppBuff) buff); } else { return (null); } } public CFSecuritySecAppBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSecApp.readBuffByClusterIdx() "; CFSecuritySecAppBuff buff; ArrayList<CFSecuritySecAppBuff> filteredList = new ArrayList<CFSecuritySecAppBuff>(); CFSecuritySecAppBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add((CFSecuritySecAppBuff) buff); } } return (filteredList.toArray(new CFSecuritySecAppBuff[0])); } public CFSecuritySecAppBuff readBuffByUJEEMountIdx(CFSecurityAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAsteriskRamSecApp.readBuffByUJEEMountIdx() "; CFSecuritySecAppBuff buff = readDerivedByUJEEMountIdx(Authorization, ClusterId, JEEMountName); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFSecuritySecAppBuff) buff); } else { return (null); } } public void updateSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFSecuritySecAppBuff 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); CFSecuritySecAppByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecAppByClusterIdxKey newKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey existingKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); existingKeyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); CFSecuritySecAppByUJEEMountIdxKey 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 Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> 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 HashMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUJEEMountIdx.remove(existingKeyUJEEMountIdx); dictByUJEEMountIdx.put(newKeyUJEEMountIdx, Buff); } public void deleteSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { final String S_ProcName = "CFAsteriskRamSecAppTable.deleteSecApp() "; CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFSecuritySecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecApp", pkey); } CFSecuritySecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey 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()); Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUJEEMountIdx.remove(keyUJEEMountIdx); } public void deleteSecAppByIdIdx(CFSecurityAuthorization Authorization, long argClusterId, int argSecAppId) { CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecAppId(argSecAppId); deleteSecAppByIdIdx(Authorization, key); } public void deleteSecAppByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecAppByClusterIdx(Authorization, key); } public void deleteSecAppByClusterIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppByClusterIdxKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByUJEEMountIdx(CFSecurityAuthorization Authorization, long argClusterId, String argJEEMountName) { CFSecuritySecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredJEEMountName(argJEEMountName); deleteSecAppByUJEEMountIdx(Authorization, key); } public void deleteSecAppByUJEEMountIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppByUJEEMountIdxKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public CFSecurityCursor openSecAppCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamSecAppCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSecAppCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAsteriskRamSecAppCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAsteriskRamSecAppCursor(Authorization, schema, new ArrayList<CFSecuritySecAppBuff>()); } return (cursor); } public void closeSecAppCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySecAppBuff nextSecAppCursor(CFSecurityCursor Cursor) { CFAsteriskRamSecAppCursor cursor = (CFAsteriskRamSecAppCursor) Cursor; CFSecuritySecAppBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySecAppBuff prevSecAppCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFSecuritySecAppBuff firstSecAppCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySecAppBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFSecuritySecAppBuff lastSecAppCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecAppCursor"); } public CFSecuritySecAppBuff nthSecAppCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }