Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ServerProc. /* * CFBam * * Copyright (c) 2014-2016 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ 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.*; /* * CFBamRamServerProcTable in-memory RAM DbIO implementation * for ServerProc. */ public class CFBamRamServerProcTable implements ICFBamServerProcTable { private ICFBamSchema schema; private Map<CFBamScopePKey, CFBamServerProcBuff> dictByPKey = new HashMap<CFBamScopePKey, CFBamServerProcBuff>(); public CFBamRamServerProcTable(ICFBamSchema argSchema) { schema = argSchema; } public void createServerProc(CFSecurityAuthorization Authorization, CFBamServerProcBuff Buff) { final String S_ProcName = "createServerProc"; schema.getTableServerMethod().createServerMethod(Authorization, Buff); CFBamScopePKey pkey = schema.getFactoryScope().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; allNull = false; if (!allNull) { if (null == schema.getTableServerMethod().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Superclass", "SuperClass", "ServerMethod", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); } public CFBamServerProcBuff readDerived(CFSecurityAuthorization Authorization, CFBamScopePKey PKey) { final String S_ProcName = "CFBamRamServerProc.readDerived"; CFBamScopePKey key = schema.getFactoryScope().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFBamServerProcBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFBamServerProcBuff lockDerived(CFSecurityAuthorization Authorization, CFBamScopePKey PKey) { final String S_ProcName = "CFBamRamServerProc.readDerived"; CFBamScopePKey key = schema.getFactoryScope().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFBamServerProcBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFBamServerProcBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamServerProc.readAllDerived"; CFBamServerProcBuff[] retList = new CFBamServerProcBuff[dictByPKey.values().size()]; Iterator<CFBamServerProcBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFBamServerProcBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamScope.readDerivedByTenantIdx"; CFBamScopeBuff buffList[] = schema.getTableScope().readDerivedByTenantIdx(Authorization, TenantId); if (buffList == null) { return (null); } else { CFBamScopeBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFBamServerProcBuff)) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } } public CFBamServerProcBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, long TableId, String Name) { final String S_ProcName = "CFBamRamServerMethod.readDerivedByUNameIdx"; CFBamServerMethodBuff buff = schema.getTableServerMethod().readDerivedByUNameIdx(Authorization, TenantId, TableId, Name); if (buff == null) { return (null); } else if (buff instanceof CFBamServerProcBuff) { return ((CFBamServerProcBuff) buff); } else { return (null); } } public CFBamServerProcBuff[] readDerivedByTableIdx(CFSecurityAuthorization Authorization, long TenantId, long TableId) { final String S_ProcName = "CFBamRamServerMethod.readDerivedByTableIdx"; CFBamServerMethodBuff buffList[] = schema.getTableServerMethod().readDerivedByTableIdx(Authorization, TenantId, TableId); if (buffList == null) { return (null); } else { CFBamServerMethodBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFBamServerProcBuff)) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } } public CFBamServerProcBuff[] readDerivedByDefSchemaIdx(CFSecurityAuthorization Authorization, Long DefSchemaTenantId, Long DefSchemaId) { final String S_ProcName = "CFBamRamServerMethod.readDerivedByDefSchemaIdx"; CFBamServerMethodBuff buffList[] = schema.getTableServerMethod().readDerivedByDefSchemaIdx(Authorization, DefSchemaTenantId, DefSchemaId); if (buffList == null) { return (null); } else { CFBamServerMethodBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFBamServerProcBuff)) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } } public CFBamServerProcBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamScope.readDerivedByIdIdx() "; CFBamScopePKey key = schema.getFactoryScope().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFBamServerProcBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFBamServerProcBuff readBuff(CFSecurityAuthorization Authorization, CFBamScopePKey PKey) { final String S_ProcName = "CFBamRamServerProc.readBuff"; CFBamServerProcBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SRVP"))) { buff = null; } return (buff); } public CFBamServerProcBuff lockBuff(CFSecurityAuthorization Authorization, CFBamScopePKey PKey) { final String S_ProcName = "lockBuff"; CFBamServerProcBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SRVP"))) { buff = null; } return (buff); } public CFBamServerProcBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamServerProc.readAllBuff"; CFBamServerProcBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); CFBamServerProcBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SRVP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } public CFBamServerProcBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamScope.readBuffByIdIdx() "; CFBamServerProcBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("SCOP")) { return ((CFBamServerProcBuff) buff); } else { return (null); } } public CFBamServerProcBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamScope.readBuffByTenantIdx() "; CFBamServerProcBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); CFBamServerProcBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SCOP")) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } public CFBamServerProcBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, long TableId, String Name) { final String S_ProcName = "CFBamRamServerMethod.readBuffByUNameIdx() "; CFBamServerProcBuff buff = readDerivedByUNameIdx(Authorization, TenantId, TableId, Name); if ((buff != null) && buff.getClassCode().equals("SRVM")) { return ((CFBamServerProcBuff) buff); } else { return (null); } } public CFBamServerProcBuff[] readBuffByTableIdx(CFSecurityAuthorization Authorization, long TenantId, long TableId) { final String S_ProcName = "CFBamRamServerMethod.readBuffByTableIdx() "; CFBamServerProcBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); CFBamServerProcBuff[] buffList = readDerivedByTableIdx(Authorization, TenantId, TableId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SRVM")) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } public CFBamServerProcBuff[] readBuffByDefSchemaIdx(CFSecurityAuthorization Authorization, Long DefSchemaTenantId, Long DefSchemaId) { final String S_ProcName = "CFBamRamServerMethod.readBuffByDefSchemaIdx() "; CFBamServerProcBuff buff; ArrayList<CFBamServerProcBuff> filteredList = new ArrayList<CFBamServerProcBuff>(); CFBamServerProcBuff[] buffList = readDerivedByDefSchemaIdx(Authorization, DefSchemaTenantId, DefSchemaId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SRVM")) { filteredList.add((CFBamServerProcBuff) buff); } } return (filteredList.toArray(new CFBamServerProcBuff[0])); } public void updateServerProc(CFSecurityAuthorization Authorization, CFBamServerProcBuff Buff) { schema.getTableServerMethod().updateServerMethod(Authorization, Buff); CFBamScopePKey pkey = schema.getFactoryScope().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFBamServerProcBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateServerProc", "Existing record not found", "ServerProc", pkey); } // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableServerMethod().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateServerProc", "Superclass", "SuperClass", "ServerMethod", null); } } } // Update is valid Map<CFBamScopePKey, CFBamServerProcBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); } public void deleteServerProc(CFSecurityAuthorization Authorization, CFBamServerProcBuff Buff) { final String S_ProcName = "CFBamRamServerProcTable.deleteServerProc() "; String classCode; CFBamScopePKey pkey = schema.getFactoryScope().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFBamServerProcBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteServerProc", pkey); } schema.getTableParam().deleteParamByServerMethodIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); // Validate reverse foreign keys // Delete is valid Map<CFBamScopePKey, CFBamServerProcBuff> subdict; dictByPKey.remove(pkey); schema.getTableServerMethod().deleteServerMethod(Authorization, Buff); } public void deleteServerProcByUNameIdx(CFSecurityAuthorization Authorization, long argTenantId, long argTableId, String argName) { CFBamServerMethodByUNameIdxKey key = schema.getFactoryServerMethod().newUNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTableId(argTableId); key.setRequiredName(argName); deleteServerProcByUNameIdx(Authorization, key); } public void deleteServerProcByUNameIdx(CFSecurityAuthorization Authorization, CFBamServerMethodByUNameIdxKey argKey) { CFBamServerProcBuff cur; boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFBamServerProcBuff> matchSet = new LinkedList<CFBamServerProcBuff>(); Iterator<CFBamServerProcBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFBamServerProcBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableServerProc().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); deleteServerProc(Authorization, cur); } } public void deleteServerProcByTableIdx(CFSecurityAuthorization Authorization, long argTenantId, long argTableId) { CFBamServerMethodByTableIdxKey key = schema.getFactoryServerMethod().newTableIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTableId(argTableId); deleteServerProcByTableIdx(Authorization, key); } public void deleteServerProcByTableIdx(CFSecurityAuthorization Authorization, CFBamServerMethodByTableIdxKey argKey) { CFBamServerProcBuff cur; boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFBamServerProcBuff> matchSet = new LinkedList<CFBamServerProcBuff>(); Iterator<CFBamServerProcBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFBamServerProcBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableServerProc().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); deleteServerProc(Authorization, cur); } } public void deleteServerProcByDefSchemaIdx(CFSecurityAuthorization Authorization, Long argDefSchemaTenantId, Long argDefSchemaId) { CFBamServerMethodByDefSchemaIdxKey key = schema.getFactoryServerMethod().newDefSchemaIdxKey(); key.setOptionalDefSchemaTenantId(argDefSchemaTenantId); key.setOptionalDefSchemaId(argDefSchemaId); deleteServerProcByDefSchemaIdx(Authorization, key); } public void deleteServerProcByDefSchemaIdx(CFSecurityAuthorization Authorization, CFBamServerMethodByDefSchemaIdxKey argKey) { CFBamServerProcBuff cur; boolean anyNotNull = false; if (argKey.getOptionalDefSchemaTenantId() != null) { anyNotNull = true; } if (argKey.getOptionalDefSchemaId() != null) { anyNotNull = true; } if (!anyNotNull) { return; } LinkedList<CFBamServerProcBuff> matchSet = new LinkedList<CFBamServerProcBuff>(); Iterator<CFBamServerProcBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFBamServerProcBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableServerProc().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); deleteServerProc(Authorization, cur); } } public void deleteServerProcByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argId) { CFBamScopePKey key = schema.getFactoryScope().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteServerProcByIdIdx(Authorization, key); } public void deleteServerProcByIdIdx(CFSecurityAuthorization Authorization, CFBamScopePKey argKey) { boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } CFBamServerProcBuff cur; LinkedList<CFBamServerProcBuff> matchSet = new LinkedList<CFBamServerProcBuff>(); Iterator<CFBamServerProcBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFBamServerProcBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableServerProc().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); deleteServerProc(Authorization, cur); } } public void deleteServerProcByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFBamScopeByTenantIdxKey key = schema.getFactoryScope().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteServerProcByTenantIdx(Authorization, key); } public void deleteServerProcByTenantIdx(CFSecurityAuthorization Authorization, CFBamScopeByTenantIdxKey argKey) { CFBamServerProcBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFBamServerProcBuff> matchSet = new LinkedList<CFBamServerProcBuff>(); Iterator<CFBamServerProcBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFBamServerProcBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableServerProc().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); deleteServerProc(Authorization, cur); } } public CFSecurityCursor openServerProcCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamServerProcCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeServerProcCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFBamServerProcBuff nextServerProcCursor(CFSecurityCursor Cursor) { CFBamRamServerProcCursor cursor = (CFBamRamServerProcCursor) Cursor; CFBamServerProcBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFBamServerProcBuff prevServerProcCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFBamServerProcBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextServerProcCursor(Cursor); } return (rec); } public CFBamServerProcBuff firstServerProcCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFBamServerProcBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextServerProcCursor(Cursor); } return (rec); } public CFBamServerProcBuff lastServerProcCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastServerProcCursor"); } public CFBamServerProcBuff nthServerProcCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFBamServerProcBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextServerProcCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }