Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for SysCluster. /* * CFBam * * Copyright (c) 2014-2016 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.cfbam.v2_7.CFBamRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_7.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternet.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBam.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternetObj.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; /* * CFBamRamSysClusterTable in-memory RAM DbIO implementation * for SysCluster. */ public class CFBamRamSysClusterTable implements ICFBamSysClusterTable { private ICFBamSchema schema; private Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff> dictByPKey = new HashMap<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>(); private Map<CFSecuritySysClusterByClusterIdxKey, Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>> dictByClusterIdx = new HashMap<CFSecuritySysClusterByClusterIdxKey, Map<CFSecuritySysClusterPKey, CFSecuritySysClusterBuff>>(); public CFBamRamSysClusterTable(ICFBamSchema 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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysCluster.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 = "CFBamRamSysClusterTable.deleteSysCluster() "; String classCode; 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) { boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } 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(); cur = schema.getTableSysCluster().readDerivedByIdIdx(Authorization, cur.getRequiredSingletonId()); 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; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } 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(); cur = schema.getTableSysCluster().readDerivedByIdIdx(Authorization, cur.getRequiredSingletonId()); deleteSysCluster(Authorization, cur); } } public CFSecurityCursor openSysClusterCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamSysClusterCursor(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 CFBamRamSysClusterCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFBamRamSysClusterCursor(Authorization, schema, new ArrayList<CFSecuritySysClusterBuff>()); } return (cursor); } public void closeSysClusterCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySysClusterBuff nextSysClusterCursor(CFSecurityCursor Cursor) { CFBamRamSysClusterCursor cursor = (CFBamRamSysClusterCursor) 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() { } }