Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for SysCluster. /* * 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.*; /* * CFAsteriskRamSysClusterTable in-memory RAM DbIO implementation * for SysCluster. */ public class CFAsteriskRamSysClusterTable implements ICFAsteriskSysClusterTable { private ICFAsteriskSchema schema; private Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> dictByPKey = new HashMap<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>(); private Map<CFSecuritySysClusterByClusterIdxKey, Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>> dictByClusterIdx = new HashMap<CFSecuritySysClusterByClusterIdxKey, Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>>(); public CFAsteriskRamSysClusterTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createSysCluster(CFSecurityAuthorization Authorization, CFSecuritySysClusterBuff Buff) { final String S_ProcName = "createSysCluster"; CFSecuritySysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); Buff.setRequiredSingletonId(pkey.getRequiredSingletonId()); CFSecuritySysClusterByClusterIdxKey keyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } // 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", "SysClusterCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new HashMap<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); } public CFSecuritySysClusterBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySysClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamSysCluster.readDerived"; CFSecuritySysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(PKey.getRequiredSingletonId()); CFSecuritySysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySysClusterBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySysClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamSysCluster.readDerived"; CFSecuritySysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(PKey.getRequiredSingletonId()); CFSecuritySysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySysClusterBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSysCluster.readAllDerived"; CFSecuritySysClusterBuff[] retList = new CFSecuritySysClusterBuff[dictByPKey.values().size()]; Iterator<CFSecuritySysClusterBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySysClusterBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSysCluster.readDerivedByClusterIdx"; CFSecuritySysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecuritySysClusterBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecuritySysClusterBuff[subdictClusterIdx.size()]; Iterator<CFSecuritySysClusterBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> subdictClusterIdx = new HashMap<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>(); dictByClusterIdx.put(key, subdictClusterIdx); recArray = new CFSecuritySysClusterBuff[0]; } return (recArray); } public CFSecuritySysClusterBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, int SingletonId) { final String S_ProcName = "CFAsteriskRamSysCluster.readDerivedByIdIdx() "; CFSecuritySysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(SingletonId); CFSecuritySysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySysClusterBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySysClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamSysCluster.readBuff"; CFSecuritySysClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SYSC"))) { buff = null; } return (buff); } public CFSecuritySysClusterBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySysClusterPKey PKey) { final String S_ProcName = "lockBuff"; CFSecuritySysClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SYSC"))) { buff = null; } return (buff); } public CFSecuritySysClusterBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamSysCluster.readAllBuff"; CFSecuritySysClusterBuff buff; ArrayList<CFSecuritySysClusterBuff> filteredList = new ArrayList<CFSecuritySysClusterBuff>(); CFSecuritySysClusterBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SYSC")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecuritySysClusterBuff[0])); } public CFSecuritySysClusterBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, int SingletonId) { final String S_ProcName = "CFAsteriskRamSysCluster.readBuffByIdIdx() "; CFSecuritySysClusterBuff buff = readDerivedByIdIdx(Authorization, SingletonId); if ((buff != null) && buff.getClassCode().equals("SYSC")) { return ((CFSecuritySysClusterBuff) buff); } else { return (null); } } public CFSecuritySysClusterBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAsteriskRamSysCluster.readBuffByClusterIdx() "; CFSecuritySysClusterBuff buff; ArrayList<CFSecuritySysClusterBuff> filteredList = new ArrayList<CFSecuritySysClusterBuff>(); CFSecuritySysClusterBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SYSC")) { filteredList.add((CFSecuritySysClusterBuff) buff); } } return (filteredList.toArray(new CFSecuritySysClusterBuff[0])); } public void updateSysCluster(CFSecurityAuthorization Authorization, CFSecuritySysClusterBuff Buff) { CFSecuritySysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); CFSecuritySysClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSysCluster", "Existing record not found", "SysCluster", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSysCluster", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecuritySysClusterByClusterIdxKey existingKeyClusterIdx = schema.getFactorySysCluster() .newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySysClusterByClusterIdxKey newKeyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSysCluster", "Container", "SysClusterCluster", "Cluster", null); } } } // Update is valid Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> 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<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); } public void deleteSysCluster(CFSecurityAuthorization Authorization, CFSecuritySysClusterBuff Buff) { final String S_ProcName = "CFAsteriskRamSysClusterTable.deleteSysCluster() "; CFSecuritySysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); CFSecuritySysClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSysCluster", pkey); } CFSecuritySysClusterByClusterIdxKey keyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); // Validate reverse foreign keys // Delete is valid Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); } public void deleteSysClusterByIdIdx(CFSecurityAuthorization Authorization, int argSingletonId) { CFSecuritySysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(argSingletonId); deleteSysClusterByIdIdx(Authorization, key); } public void deleteSysClusterByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySysClusterPKey argKey) { CFSecuritySysClusterBuff cur; LinkedList<CFSecuritySysClusterBuff> matchSet = new LinkedList<CFSecuritySysClusterBuff>(); Iterator<CFSecuritySysClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySysClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSysCluster(Authorization, cur); } } public void deleteSysClusterByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecuritySysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSysClusterByClusterIdx(Authorization, key); } public void deleteSysClusterByClusterIdx(CFSecurityAuthorization Authorization, CFSecuritySysClusterByClusterIdxKey argKey) { CFSecuritySysClusterBuff cur; LinkedList<CFSecuritySysClusterBuff> matchSet = new LinkedList<CFSecuritySysClusterBuff>(); Iterator<CFSecuritySysClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySysClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSysCluster(Authorization, cur); } } public CFSecurityCursor openSysClusterCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamSysClusterCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSysClusterCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecuritySysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAsteriskRamSysClusterCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAsteriskRamSysClusterCursor(Authorization, schema, new ArrayList<CFSecuritySysClusterBuff>()); } return (cursor); } public void closeSysClusterCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySysClusterBuff nextSysClusterCursor(CFSecurityCursor Cursor) { CFAsteriskRamSysClusterCursor cursor = (CFAsteriskRamSysClusterCursor) Cursor; CFSecuritySysClusterBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySysClusterBuff prevSysClusterCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySysClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public CFSecuritySysClusterBuff firstSysClusterCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySysClusterBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public CFSecuritySysClusterBuff lastSysClusterCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSysClusterCursor"); } public CFSecuritySysClusterBuff nthSysClusterCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySysClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }