Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for Cluster. /* * 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.*; /* * CFAsteriskRamClusterTable in-memory RAM DbIO implementation * for Cluster. */ public class CFAsteriskRamClusterTable implements ICFAsteriskClusterTable { private ICFAsteriskSchema schema; private Map<CFSecurityClusterPKey, CFSecurityClusterBuff> dictByPKey = new HashMap<CFSecurityClusterPKey, CFSecurityClusterBuff>(); private Map<CFSecurityClusterByUDomainNameIdxKey, CFSecurityClusterBuff> dictByUDomainNameIdx = new HashMap<CFSecurityClusterByUDomainNameIdxKey, CFSecurityClusterBuff>(); private Map<CFSecurityClusterByUDescrIdxKey, CFSecurityClusterBuff> dictByUDescrIdx = new HashMap<CFSecurityClusterByUDescrIdxKey, CFSecurityClusterBuff>(); private Map<CFAsteriskRamClusterId32Gen, CFAsteriskRamClusterId32Gen> id32Generator = new HashMap<CFAsteriskRamClusterId32Gen, CFAsteriskRamClusterId32Gen>(); private Map<CFAsteriskRamClusterId64Gen, CFAsteriskRamClusterId64Gen> id64Generator = new HashMap<CFAsteriskRamClusterId64Gen, CFAsteriskRamClusterId64Gen>(); public CFAsteriskRamClusterTable(ICFAsteriskSchema argSchema) { schema = argSchema; } protected int generateNextId32(long argId, short argSliceId) { CFAsteriskRamClusterId32Gen key = new CFAsteriskRamClusterId32Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFAsteriskRamClusterId32Gen useGen = id32Generator.get(key); if (useGen == null) { id32Generator.put(key, key); useGen = key; } int retNext = useGen.getNextId(); return (retNext); } protected long generateNextId64(long argId, short argSliceId) { CFAsteriskRamClusterId64Gen key = new CFAsteriskRamClusterId64Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFAsteriskRamClusterId64Gen useGen = id64Generator.get(key); if (useGen == null) { id64Generator.put(key, key); useGen = key; } long retNext = useGen.getNextId(); return (retNext); } public int nextSecAppIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { int retval = nextSecAppIdGen(Authorization, pkey.getRequiredId()); return (retval); } public int nextSecAppIdGen(CFSecurityAuthorization Authorization, long argId) { int retNext = generateNextId32(argId, (short) 103); return (retNext); } public int nextSecFormIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { int retval = nextSecFormIdGen(Authorization, pkey.getRequiredId()); return (retval); } public int nextSecFormIdGen(CFSecurityAuthorization Authorization, long argId) { int retNext = generateNextId32(argId, (short) 104); return (retNext); } public int nextSecGroupIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { int retval = nextSecGroupIdGen(Authorization, pkey.getRequiredId()); return (retval); } public int nextSecGroupIdGen(CFSecurityAuthorization Authorization, long argId) { int retNext = generateNextId32(argId, (short) 105); return (retNext); } public long nextSecGroupMemberIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextSecGroupMemberIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextSecGroupMemberIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 106); return (retNext); } public long nextSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextSecGroupIncludeIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 107); return (retNext); } public long nextSecGroupFormIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextSecGroupFormIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextSecGroupFormIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 108); return (retNext); } public long nextServiceIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextServiceIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextServiceIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 111); return (retNext); } public long nextHostNodeIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextHostNodeIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextHostNodeIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 112); return (retNext); } public long nextConfigurationFileIdGen(CFSecurityAuthorization Authorization, CFSecurityClusterPKey pkey) { long retval = nextConfigurationFileIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextConfigurationFileIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 3000); return (retNext); } public void createCluster(CFSecurityAuthorization Authorization, CFSecurityClusterBuff Buff) { final String S_ProcName = "createCluster"; CFSecurityClusterPKey pkey = schema.getFactoryCluster().newPKey(); pkey.setRequiredId(schema.nextClusterIdGen()); Buff.setRequiredId(pkey.getRequiredId()); CFSecurityClusterByUDomainNameIdxKey keyUDomainNameIdx = schema.getFactoryCluster().newUDomainNameIdxKey(); keyUDomainNameIdx.setRequiredFullDomainName(Buff.getRequiredFullDomainName()); CFSecurityClusterByUDescrIdxKey keyUDescrIdx = schema.getFactoryCluster().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } if (dictByUDomainNameIdx.containsKey(keyUDomainNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ClusterUDomainNameIdx", keyUDomainNameIdx); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, "ClusterUDescrNameIdx", keyUDescrIdx); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); dictByUDomainNameIdx.put(keyUDomainNameIdx, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); } public CFSecurityClusterBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamCluster.readDerived"; CFSecurityClusterPKey key = schema.getFactoryCluster().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityClusterBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamCluster.readDerived"; CFSecurityClusterPKey key = schema.getFactoryCluster().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityClusterBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamCluster.readAllDerived"; CFSecurityClusterBuff[] retList = new CFSecurityClusterBuff[dictByPKey.values().size()]; Iterator<CFSecurityClusterBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityClusterBuff readDerivedByUDomainNameIdx(CFSecurityAuthorization Authorization, String FullDomainName) { final String S_ProcName = "CFAsteriskRamCluster.readDerivedByUDomainNameIdx"; CFSecurityClusterByUDomainNameIdxKey key = schema.getFactoryCluster().newUDomainNameIdxKey(); key.setRequiredFullDomainName(FullDomainName); CFSecurityClusterBuff buff; if (dictByUDomainNameIdx.containsKey(key)) { buff = dictByUDomainNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityClusterBuff readDerivedByUDescrIdx(CFSecurityAuthorization Authorization, String Description) { final String S_ProcName = "CFAsteriskRamCluster.readDerivedByUDescrIdx"; CFSecurityClusterByUDescrIdxKey key = schema.getFactoryCluster().newUDescrIdxKey(); key.setRequiredDescription(Description); CFSecurityClusterBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityClusterBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long Id) { final String S_ProcName = "CFAsteriskRamCluster.readDerivedByIdIdx() "; CFSecurityClusterPKey key = schema.getFactoryCluster().newPKey(); key.setRequiredId(Id); CFSecurityClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityClusterBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityClusterPKey PKey) { final String S_ProcName = "CFAsteriskRamCluster.readBuff"; CFSecurityClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) { buff = null; } return (buff); } public CFSecurityClusterBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityClusterPKey PKey) { final String S_ProcName = "lockBuff"; CFSecurityClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("CLUS"))) { buff = null; } return (buff); } public CFSecurityClusterBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamCluster.readAllBuff"; CFSecurityClusterBuff buff; ArrayList<CFSecurityClusterBuff> filteredList = new ArrayList<CFSecurityClusterBuff>(); CFSecurityClusterBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("CLUS")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityClusterBuff[0])); } public CFSecurityClusterBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long Id) { final String S_ProcName = "CFAsteriskRamCluster.readBuffByIdIdx() "; CFSecurityClusterBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("CLUS")) { return ((CFSecurityClusterBuff) buff); } else { return (null); } } public CFSecurityClusterBuff readBuffByUDomainNameIdx(CFSecurityAuthorization Authorization, String FullDomainName) { final String S_ProcName = "CFAsteriskRamCluster.readBuffByUDomainNameIdx() "; CFSecurityClusterBuff buff = readDerivedByUDomainNameIdx(Authorization, FullDomainName); if ((buff != null) && buff.getClassCode().equals("CLUS")) { return ((CFSecurityClusterBuff) buff); } else { return (null); } } public CFSecurityClusterBuff readBuffByUDescrIdx(CFSecurityAuthorization Authorization, String Description) { final String S_ProcName = "CFAsteriskRamCluster.readBuffByUDescrIdx() "; CFSecurityClusterBuff buff = readDerivedByUDescrIdx(Authorization, Description); if ((buff != null) && buff.getClassCode().equals("CLUS")) { return ((CFSecurityClusterBuff) buff); } else { return (null); } } public void updateCluster(CFSecurityAuthorization Authorization, CFSecurityClusterBuff Buff) { CFSecurityClusterPKey pkey = schema.getFactoryCluster().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateCluster", "Existing record not found", "Cluster", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateCluster", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityClusterByUDomainNameIdxKey existingKeyUDomainNameIdx = schema.getFactoryCluster() .newUDomainNameIdxKey(); existingKeyUDomainNameIdx.setRequiredFullDomainName(existing.getRequiredFullDomainName()); CFSecurityClusterByUDomainNameIdxKey newKeyUDomainNameIdx = schema.getFactoryCluster() .newUDomainNameIdxKey(); newKeyUDomainNameIdx.setRequiredFullDomainName(Buff.getRequiredFullDomainName()); CFSecurityClusterByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryCluster().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); CFSecurityClusterByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryCluster().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Check unique indexes if (!existingKeyUDomainNameIdx.equals(newKeyUDomainNameIdx)) { if (dictByUDomainNameIdx.containsKey(newKeyUDomainNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateCluster", "ClusterUDomainNameIdx", newKeyUDomainNameIdx); } } if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateCluster", "ClusterUDescrNameIdx", newKeyUDescrIdx); } } // Validate foreign keys // Update is valid Map<CFSecurityClusterPKey, CFSecurityClusterBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); dictByUDomainNameIdx.remove(existingKeyUDomainNameIdx); dictByUDomainNameIdx.put(newKeyUDomainNameIdx, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); } public void deleteCluster(CFSecurityAuthorization Authorization, CFSecurityClusterBuff Buff) { final String S_ProcName = "CFAsteriskRamClusterTable.deleteCluster() "; CFSecurityClusterPKey pkey = schema.getFactoryCluster().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteCluster", pkey); } CFSecurityClusterByUDomainNameIdxKey keyUDomainNameIdx = schema.getFactoryCluster().newUDomainNameIdxKey(); keyUDomainNameIdx.setRequiredFullDomainName(existing.getRequiredFullDomainName()); CFSecurityClusterByUDescrIdxKey keyUDescrIdx = schema.getFactoryCluster().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); // Validate reverse foreign keys // Delete is valid schema.getTableHostNode().deleteHostNodeByClusterIdx(Authorization, Buff.getRequiredId()); schema.getTableTenant().deleteTenantByClusterIdx(Authorization, Buff.getRequiredId()); schema.getTableSecApp().deleteSecAppByClusterIdx(Authorization, Buff.getRequiredId()); schema.getTableSecGroup().deleteSecGroupByClusterIdx(Authorization, Buff.getRequiredId()); schema.getTableSysCluster().deleteSysClusterByClusterIdx(Authorization, Buff.getRequiredId()); Map<CFSecurityClusterPKey, CFSecurityClusterBuff> subdict; dictByPKey.remove(pkey); dictByUDomainNameIdx.remove(keyUDomainNameIdx); dictByUDescrIdx.remove(keyUDescrIdx); } public void deleteClusterByIdIdx(CFSecurityAuthorization Authorization, long argId) { CFSecurityClusterPKey key = schema.getFactoryCluster().newPKey(); key.setRequiredId(argId); deleteClusterByIdIdx(Authorization, key); } public void deleteClusterByIdIdx(CFSecurityAuthorization Authorization, CFSecurityClusterPKey argKey) { CFSecurityClusterBuff cur; LinkedList<CFSecurityClusterBuff> matchSet = new LinkedList<CFSecurityClusterBuff>(); Iterator<CFSecurityClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteCluster(Authorization, cur); } } public void deleteClusterByUDomainNameIdx(CFSecurityAuthorization Authorization, String argFullDomainName) { CFSecurityClusterByUDomainNameIdxKey key = schema.getFactoryCluster().newUDomainNameIdxKey(); key.setRequiredFullDomainName(argFullDomainName); deleteClusterByUDomainNameIdx(Authorization, key); } public void deleteClusterByUDomainNameIdx(CFSecurityAuthorization Authorization, CFSecurityClusterByUDomainNameIdxKey argKey) { CFSecurityClusterBuff cur; LinkedList<CFSecurityClusterBuff> matchSet = new LinkedList<CFSecurityClusterBuff>(); Iterator<CFSecurityClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteCluster(Authorization, cur); } } public void deleteClusterByUDescrIdx(CFSecurityAuthorization Authorization, String argDescription) { CFSecurityClusterByUDescrIdxKey key = schema.getFactoryCluster().newUDescrIdxKey(); key.setRequiredDescription(argDescription); deleteClusterByUDescrIdx(Authorization, key); } public void deleteClusterByUDescrIdx(CFSecurityAuthorization Authorization, CFSecurityClusterByUDescrIdxKey argKey) { CFSecurityClusterBuff cur; LinkedList<CFSecurityClusterBuff> matchSet = new LinkedList<CFSecurityClusterBuff>(); Iterator<CFSecurityClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteCluster(Authorization, cur); } } public CFSecurityCursor openClusterCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamClusterCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeClusterCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityClusterBuff nextClusterCursor(CFSecurityCursor Cursor) { CFAsteriskRamClusterCursor cursor = (CFAsteriskRamClusterCursor) Cursor; CFSecurityClusterBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityClusterBuff prevClusterCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClusterCursor(Cursor); } return (rec); } public CFSecurityClusterBuff firstClusterCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityClusterBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClusterCursor(Cursor); } return (rec); } public CFSecurityClusterBuff lastClusterCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastClusterCursor"); } public CFSecurityClusterBuff nthClusterCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClusterCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }