Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for SecDevice. /* * 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.*; /* * CFBamRamSecDeviceTable in-memory RAM DbIO implementation * for SecDevice. */ public class CFBamRamSecDeviceTable implements ICFBamSecDeviceTable { private ICFBamSchema schema; private Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> dictByPKey = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>(); private Map<CFSecuritySecDeviceByUserIdxKey, Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>> dictByUserIdx = new HashMap<CFSecuritySecDeviceByUserIdxKey, Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>>(); public CFBamRamSecDeviceTable(ICFBamSchema argSchema) { schema = argSchema; } public void createSecDevice(CFSecurityAuthorization Authorization, CFSecuritySecDeviceBuff Buff) { final String S_ProcName = "createSecDevice"; CFSecuritySecDevicePKey pkey = schema.getFactorySecDevice().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); pkey.setRequiredDevName(Buff.getRequiredDevName()); Buff.setRequiredSecUserId(pkey.getRequiredSecUserId()); Buff.setRequiredDevName(pkey.getRequiredDevName()); CFSecuritySecDeviceByUserIdxKey keyUserIdx = schema.getFactorySecDevice().newUserIdxKey(); keyUserIdx.setRequiredSecUserId(Buff.getRequiredSecUserId()); // 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.getTableSecUser().readDerivedByIdIdx(Authorization, Buff.getRequiredSecUserId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Container", "SecDeviceSecUser", "SecUser", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdictUserIdx; if (dictByUserIdx.containsKey(keyUserIdx)) { subdictUserIdx = dictByUserIdx.get(keyUserIdx); } else { subdictUserIdx = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>(); dictByUserIdx.put(keyUserIdx, subdictUserIdx); } subdictUserIdx.put(pkey, Buff); } public CFSecuritySecDeviceBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) { final String S_ProcName = "CFBamRamSecDevice.readDerived"; CFSecuritySecDevicePKey key = schema.getFactorySecDevice().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); key.setRequiredDevName(PKey.getRequiredDevName()); CFSecuritySecDeviceBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecDeviceBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) { final String S_ProcName = "CFBamRamSecDevice.readDerived"; CFSecuritySecDevicePKey key = schema.getFactorySecDevice().newPKey(); key.setRequiredSecUserId(PKey.getRequiredSecUserId()); key.setRequiredDevName(PKey.getRequiredDevName()); CFSecuritySecDeviceBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecDeviceBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamSecDevice.readAllDerived"; CFSecuritySecDeviceBuff[] retList = new CFSecuritySecDeviceBuff[dictByPKey.values().size()]; Iterator<CFSecuritySecDeviceBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySecDeviceBuff[] readDerivedByUserIdx(CFSecurityAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFBamRamSecDevice.readDerivedByUserIdx"; CFSecuritySecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey(); key.setRequiredSecUserId(SecUserId); CFSecuritySecDeviceBuff[] recArray; if (dictByUserIdx.containsKey(key)) { Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdictUserIdx = dictByUserIdx.get(key); recArray = new CFSecuritySecDeviceBuff[subdictUserIdx.size()]; Iterator<CFSecuritySecDeviceBuff> iter = subdictUserIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdictUserIdx = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>(); dictByUserIdx.put(key, subdictUserIdx); recArray = new CFSecuritySecDeviceBuff[0]; } return (recArray); } public CFSecuritySecDeviceBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, UUID SecUserId, String DevName) { final String S_ProcName = "CFBamRamSecDevice.readDerivedByIdIdx() "; CFSecuritySecDevicePKey key = schema.getFactorySecDevice().newPKey(); key.setRequiredSecUserId(SecUserId); key.setRequiredDevName(DevName); CFSecuritySecDeviceBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecDeviceBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) { final String S_ProcName = "CFBamRamSecDevice.readBuff"; CFSecuritySecDeviceBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) { buff = null; } return (buff); } public CFSecuritySecDeviceBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey PKey) { final String S_ProcName = "lockBuff"; CFSecuritySecDeviceBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SDEV"))) { buff = null; } return (buff); } public CFSecuritySecDeviceBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamSecDevice.readAllBuff"; CFSecuritySecDeviceBuff buff; ArrayList<CFSecuritySecDeviceBuff> filteredList = new ArrayList<CFSecuritySecDeviceBuff>(); CFSecuritySecDeviceBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SDEV")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecuritySecDeviceBuff[0])); } public CFSecuritySecDeviceBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, UUID SecUserId, String DevName) { final String S_ProcName = "CFBamRamSecDevice.readBuffByIdIdx() "; CFSecuritySecDeviceBuff buff = readDerivedByIdIdx(Authorization, SecUserId, DevName); if ((buff != null) && buff.getClassCode().equals("SDEV")) { return ((CFSecuritySecDeviceBuff) buff); } else { return (null); } } public CFSecuritySecDeviceBuff[] readBuffByUserIdx(CFSecurityAuthorization Authorization, UUID SecUserId) { final String S_ProcName = "CFBamRamSecDevice.readBuffByUserIdx() "; CFSecuritySecDeviceBuff buff; ArrayList<CFSecuritySecDeviceBuff> filteredList = new ArrayList<CFSecuritySecDeviceBuff>(); CFSecuritySecDeviceBuff[] buffList = readDerivedByUserIdx(Authorization, SecUserId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SDEV")) { filteredList.add((CFSecuritySecDeviceBuff) buff); } } return (filteredList.toArray(new CFSecuritySecDeviceBuff[0])); } public void updateSecDevice(CFSecurityAuthorization Authorization, CFSecuritySecDeviceBuff Buff) { CFSecuritySecDevicePKey pkey = schema.getFactorySecDevice().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); pkey.setRequiredDevName(Buff.getRequiredDevName()); CFSecuritySecDeviceBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecDevice", "Existing record not found", "SecDevice", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecDevice", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecuritySecDeviceByUserIdxKey existingKeyUserIdx = schema.getFactorySecDevice().newUserIdxKey(); existingKeyUserIdx.setRequiredSecUserId(existing.getRequiredSecUserId()); CFSecuritySecDeviceByUserIdxKey newKeyUserIdx = schema.getFactorySecDevice().newUserIdxKey(); newKeyUserIdx.setRequiredSecUserId(Buff.getRequiredSecUserId()); // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableSecUser().readDerivedByIdIdx(Authorization, Buff.getRequiredSecUserId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSecDevice", "Container", "SecDeviceSecUser", "SecUser", null); } } } // Update is valid Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByUserIdx.get(existingKeyUserIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByUserIdx.containsKey(newKeyUserIdx)) { subdict = dictByUserIdx.get(newKeyUserIdx); } else { subdict = new HashMap<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff>(); dictByUserIdx.put(newKeyUserIdx, subdict); } subdict.put(pkey, Buff); } public void deleteSecDevice(CFSecurityAuthorization Authorization, CFSecuritySecDeviceBuff Buff) { final String S_ProcName = "CFBamRamSecDeviceTable.deleteSecDevice() "; String classCode; CFSecuritySecDevicePKey pkey = schema.getFactorySecDevice().newPKey(); pkey.setRequiredSecUserId(Buff.getRequiredSecUserId()); pkey.setRequiredDevName(Buff.getRequiredDevName()); CFSecuritySecDeviceBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecDevice", pkey); } CFSecuritySecDeviceByUserIdxKey keyUserIdx = schema.getFactorySecDevice().newUserIdxKey(); keyUserIdx.setRequiredSecUserId(existing.getRequiredSecUserId()); // Validate reverse foreign keys // Delete is valid Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdict; dictByPKey.remove(pkey); subdict = dictByUserIdx.get(keyUserIdx); subdict.remove(pkey); } public void deleteSecDeviceByIdIdx(CFSecurityAuthorization Authorization, UUID argSecUserId, String argDevName) { CFSecuritySecDevicePKey key = schema.getFactorySecDevice().newPKey(); key.setRequiredSecUserId(argSecUserId); key.setRequiredDevName(argDevName); deleteSecDeviceByIdIdx(Authorization, key); } public void deleteSecDeviceByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySecDevicePKey argKey) { boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } CFSecuritySecDeviceBuff cur; LinkedList<CFSecuritySecDeviceBuff> matchSet = new LinkedList<CFSecuritySecDeviceBuff>(); Iterator<CFSecuritySecDeviceBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecDeviceBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableSecDevice().readDerivedByIdIdx(Authorization, cur.getRequiredSecUserId(), cur.getRequiredDevName()); deleteSecDevice(Authorization, cur); } } public void deleteSecDeviceByUserIdx(CFSecurityAuthorization Authorization, UUID argSecUserId) { CFSecuritySecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey(); key.setRequiredSecUserId(argSecUserId); deleteSecDeviceByUserIdx(Authorization, key); } public void deleteSecDeviceByUserIdx(CFSecurityAuthorization Authorization, CFSecuritySecDeviceByUserIdxKey argKey) { CFSecuritySecDeviceBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFSecuritySecDeviceBuff> matchSet = new LinkedList<CFSecuritySecDeviceBuff>(); Iterator<CFSecuritySecDeviceBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecDeviceBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableSecDevice().readDerivedByIdIdx(Authorization, cur.getRequiredSecUserId(), cur.getRequiredDevName()); deleteSecDevice(Authorization, cur); } } public CFSecurityCursor openSecDeviceCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamSecDeviceCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSecDeviceCursorByUserIdx(CFSecurityAuthorization Authorization, UUID SecUserId) { CFSecurityCursor cursor; CFSecuritySecDeviceByUserIdxKey key = schema.getFactorySecDevice().newUserIdxKey(); key.setRequiredSecUserId(SecUserId); if (dictByUserIdx.containsKey(key)) { Map<CFSecuritySecDevicePKey, CFSecuritySecDeviceBuff> subdictUserIdx = dictByUserIdx.get(key); cursor = new CFBamRamSecDeviceCursor(Authorization, schema, subdictUserIdx.values()); } else { cursor = new CFBamRamSecDeviceCursor(Authorization, schema, new ArrayList<CFSecuritySecDeviceBuff>()); } return (cursor); } public void closeSecDeviceCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySecDeviceBuff nextSecDeviceCursor(CFSecurityCursor Cursor) { CFBamRamSecDeviceCursor cursor = (CFBamRamSecDeviceCursor) Cursor; CFSecuritySecDeviceBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySecDeviceBuff prevSecDeviceCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySecDeviceBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecDeviceCursor(Cursor); } return (rec); } public CFSecuritySecDeviceBuff firstSecDeviceCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySecDeviceBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecDeviceCursor(Cursor); } return (rec); } public CFSecuritySecDeviceBuff lastSecDeviceCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecDeviceCursor"); } public CFSecuritySecDeviceBuff nthSecDeviceCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySecDeviceBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecDeviceCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }