Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecApp. /* * MSS Code Factory Accounting Business Application Model * * Copyright (c) 2014 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/>. * * This source code incorporates modified modules originally licensed * under the Apache 2.0 license by MSS Code Factory including CFSecurity * (net-sourceforge-msscodefactory-2.0-cfsecurity.xml), * CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and * CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the * required models being available as part of the MSS Code Factory 1.11 * distribution source and install zips. * * You can download installations of MSS Code Factory 1.11 from * http://msscodefactory.sourceforge.net/ * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam; import java.sql.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAcc.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*; /* * CFAccRamSecAppTable in-memory RAM DbIO implementation * for SecApp. */ public class CFAccRamSecAppTable implements ICFAccSecAppTable { private CFAccRamSchema schema; private Map<CFAccSecAppPKey, CFAccSecAppBuff> dictByPKey = new HashMap<CFAccSecAppPKey, CFAccSecAppBuff>(); private SortedMap<CFAccSecAppByClusterIdxKey, SortedMap<CFAccSecAppPKey, CFAccSecAppBuff>> dictByClusterIdx = new TreeMap<CFAccSecAppByClusterIdxKey, SortedMap<CFAccSecAppPKey, CFAccSecAppBuff>>(); private SortedMap<CFAccSecAppByUJEEMountIdxKey, CFAccSecAppBuff> dictByUJEEMountIdx = new TreeMap<CFAccSecAppByUJEEMountIdxKey, CFAccSecAppBuff>(); public CFAccRamSecAppTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createSecApp(CFAccAuthorization Authorization, CFAccSecAppBuff Buff) { CFAccSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(((CFAccRamClusterTable) schema.getTableCluster()).nextSecAppIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecAppId(pkey.getRequiredSecAppId()); CFAccSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAccSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); keyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createSecApp", pkey); } if (dictByUJEEMountIdx.containsKey(keyUJEEMountIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecApp", "SecAppUJEEMountIdx", keyUJEEMountIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createSecApp", "Container", "SecAppCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAccSecAppPKey, CFAccSecAppBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFAccSecAppPKey, CFAccSecAppBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUJEEMountIdx.put(keyUJEEMountIdx, Buff); } public CFAccSecAppBuff readDerived(CFAccAuthorization Authorization, CFAccSecAppPKey PKey) { final String S_ProcName = "CFAccRamSecApp.readDerived() "; CFAccSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFAccSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecAppBuff lockDerived(CFAccAuthorization Authorization, CFAccSecAppPKey PKey) { final String S_ProcName = "CFAccRamSecApp.readDerived() "; CFAccSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFAccSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecAppBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecApp.readAllDerived() "; CFAccSecAppBuff[] retList = new CFAccSecAppBuff[dictByPKey.values().size()]; Iterator<CFAccSecAppBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccSecAppBuff[] readDerivedByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAccRamSecApp.readDerivedByClusterIdx() "; CFAccSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAccSecAppBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAccSecAppPKey, CFAccSecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAccSecAppBuff[subdictClusterIdx.size()]; Iterator<CFAccSecAppBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAccSecAppBuff[0]; } return (recArray); } public CFAccSecAppBuff readDerivedByUJEEMountIdx(CFAccAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAccRamSecApp.readDerivedByUJEEMountIdx() "; CFAccSecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredJEEMountName(JEEMountName); CFAccSecAppBuff buff; if (dictByUJEEMountIdx.containsKey(key)) { buff = dictByUJEEMountIdx.get(key); } else { buff = null; } return (buff); } public CFAccSecAppBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAccRamSecApp.readDerivedByIdIdx() "; CFAccSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecAppId(SecAppId); CFAccSecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccSecAppBuff readBuff(CFAccAuthorization Authorization, CFAccSecAppPKey PKey) { final String S_ProcName = "CFAccRamSecApp.readBuff() "; CFAccSecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFAccSecAppBuff lockBuff(CFAccAuthorization Authorization, CFAccSecAppPKey PKey) { final String S_ProcName = "CFAccRamSecApp.readBuff() "; CFAccSecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFAccSecAppBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamSecApp.readAllBuff() "; CFAccSecAppBuff buff; ArrayList<CFAccSecAppBuff> filteredList = new ArrayList<CFAccSecAppBuff>(); CFAccSecAppBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAccSecAppBuff[0])); } public CFAccSecAppBuff readBuffByIdIdx(CFAccAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFAccRamSecApp.readBuffByIdIdx() "; CFAccSecAppBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecAppId); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFAccSecAppBuff) buff); } else { return (null); } } public CFAccSecAppBuff[] readBuffByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAccRamSecApp.readBuffByClusterIdx() "; CFAccSecAppBuff buff; ArrayList<CFAccSecAppBuff> filteredList = new ArrayList<CFAccSecAppBuff>(); CFAccSecAppBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add((CFAccSecAppBuff) buff); } } return (filteredList.toArray(new CFAccSecAppBuff[0])); } public CFAccSecAppBuff readBuffByUJEEMountIdx(CFAccAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFAccRamSecApp.readBuffByUJEEMountIdx() "; CFAccSecAppBuff buff = readDerivedByUJEEMountIdx(Authorization, ClusterId, JEEMountName); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFAccSecAppBuff) buff); } else { return (null); } } public void updateSecApp(CFAccAuthorization Authorization, CFAccSecAppBuff Buff) { CFAccSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFAccSecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecApp", "Existing record not found", "SecApp", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecApp", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAccSecAppByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAccSecAppByClusterIdxKey newKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFAccSecAppByUJEEMountIdxKey existingKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); existingKeyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); CFAccSecAppByUJEEMountIdxKey newKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); newKeyUJEEMountIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUJEEMountIdx.setRequiredJEEMountName(Buff.getRequiredJEEMountName()); // Check unique indexes if (!existingKeyUJEEMountIdx.equals(newKeyUJEEMountIdx)) { if (dictByUJEEMountIdx.containsKey(newKeyUJEEMountIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateSecApp", "SecAppUJEEMountIdx", newKeyUJEEMountIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSecApp", "Container", "SecAppCluster", "Cluster", null); } } } // Update is valid SortedMap<CFAccSecAppPKey, CFAccSecAppBuff> 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 TreeMap<CFAccSecAppPKey, CFAccSecAppBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUJEEMountIdx.remove(existingKeyUJEEMountIdx); dictByUJEEMountIdx.put(newKeyUJEEMountIdx, Buff); } public void deleteSecApp(CFAccAuthorization Authorization, CFAccSecAppBuff Buff) { final String S_ProcName = "CFAccRamSecAppTable.deleteSecApp() "; CFAccSecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFAccSecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecApp", pkey); } CFAccSecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAccSecAppByUJEEMountIdxKey keyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); keyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); // Validate reverse foreign keys // Delete is valid schema.getTableSecForm().deleteSecFormBySecAppIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecAppId()); SortedMap<CFAccSecAppPKey, CFAccSecAppBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUJEEMountIdx.remove(keyUJEEMountIdx); } public void deleteSecAppByIdIdx(CFAccAuthorization Authorization, long argClusterId, int argSecAppId) { CFAccSecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecAppId(argSecAppId); deleteSecAppByIdIdx(Authorization, key); } public void deleteSecAppByIdIdx(CFAccAuthorization Authorization, CFAccSecAppPKey argKey) { CFAccSecAppBuff cur; LinkedList<CFAccSecAppBuff> matchSet = new LinkedList<CFAccSecAppBuff>(); Iterator<CFAccSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByClusterIdx(CFAccAuthorization Authorization, long argClusterId) { CFAccSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecAppByClusterIdx(Authorization, key); } public void deleteSecAppByClusterIdx(CFAccAuthorization Authorization, CFAccSecAppByClusterIdxKey argKey) { CFAccSecAppBuff cur; LinkedList<CFAccSecAppBuff> matchSet = new LinkedList<CFAccSecAppBuff>(); Iterator<CFAccSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByUJEEMountIdx(CFAccAuthorization Authorization, long argClusterId, String argJEEMountName) { CFAccSecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredJEEMountName(argJEEMountName); deleteSecAppByUJEEMountIdx(Authorization, key); } public void deleteSecAppByUJEEMountIdx(CFAccAuthorization Authorization, CFAccSecAppByUJEEMountIdxKey argKey) { CFAccSecAppBuff cur; LinkedList<CFAccSecAppBuff> matchSet = new LinkedList<CFAccSecAppBuff>(); Iterator<CFAccSecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccSecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public CFAccCursor openSecAppCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamSecAppCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAccCursor openSecAppCursorByClusterIdx(CFAccAuthorization Authorization, long ClusterId) { CFAccCursor cursor; CFAccSecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFAccSecAppPKey, CFAccSecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAccRamSecAppCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAccRamSecAppCursor(Authorization, schema, new ArrayList<CFAccSecAppBuff>()); } return (cursor); } public void closeSecAppCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccSecAppBuff nextSecAppCursor(CFAccCursor Cursor) { CFAccRamSecAppCursor cursor = (CFAccRamSecAppCursor) Cursor; CFAccSecAppBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccSecAppBuff prevSecAppCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccSecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFAccSecAppBuff firstSecAppCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccSecAppBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFAccSecAppBuff lastSecAppCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecAppCursor"); } public CFAccSecAppBuff nthSecAppCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccSecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }