Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for TSecGroup. /* * 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.*; /* * CFAccRamTSecGroupTable in-memory RAM DbIO implementation * for TSecGroup. */ public class CFAccRamTSecGroupTable implements ICFAccTSecGroupTable { private CFAccRamSchema schema; private Map<CFAccTSecGroupPKey, CFAccTSecGroupBuff> dictByPKey = new HashMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff>(); private SortedMap<CFAccTSecGroupByTenantIdxKey, SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff>> dictByTenantIdx = new TreeMap<CFAccTSecGroupByTenantIdxKey, SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff>>(); private SortedMap<CFAccTSecGroupByUNameIdxKey, CFAccTSecGroupBuff> dictByUNameIdx = new TreeMap<CFAccTSecGroupByUNameIdxKey, CFAccTSecGroupBuff>(); public CFAccRamTSecGroupTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createTSecGroup(CFAccAuthorization Authorization, CFAccTSecGroupBuff Buff) { CFAccTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(((CFAccRamTenantTable) schema.getTableTenant()) .nextTSecGroupIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTSecGroupId(pkey.getRequiredTSecGroupId()); CFAccTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAccTSecGroupByUNameIdxKey keyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTSecGroup", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTSecGroup", "TSecGroupUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createTSecGroup", "Container", "TSecGroupTenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFAccTSecGroupBuff readDerived(CFAccAuthorization Authorization, CFAccTSecGroupPKey PKey) { final String S_ProcName = "CFAccRamTSecGroup.readDerived() "; CFAccTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFAccTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTSecGroupBuff lockDerived(CFAccAuthorization Authorization, CFAccTSecGroupPKey PKey) { final String S_ProcName = "CFAccRamTSecGroup.readDerived() "; CFAccTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFAccTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTSecGroupBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamTSecGroup.readAllDerived() "; CFAccTSecGroupBuff[] retList = new CFAccTSecGroupBuff[dictByPKey.values().size()]; Iterator<CFAccTSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccTSecGroupBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamTSecGroup.readDerivedByTenantIdx() "; CFAccTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFAccTSecGroupBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFAccTSecGroupBuff[subdictTenantIdx.size()]; Iterator<CFAccTSecGroupBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAccTSecGroupBuff[0]; } return (recArray); } public CFAccTSecGroupBuff readDerivedByUNameIdx(CFAccAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAccRamTSecGroup.readDerivedByUNameIdx() "; CFAccTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFAccTSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFAccTSecGroupBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAccRamTSecGroup.readDerivedByIdIdx() "; CFAccTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTSecGroupId(TSecGroupId); CFAccTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccTSecGroupBuff readBuff(CFAccAuthorization Authorization, CFAccTSecGroupPKey PKey) { final String S_ProcName = "CFAccRamTSecGroup.readBuff() "; CFAccTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFAccTSecGroupBuff lockBuff(CFAccAuthorization Authorization, CFAccTSecGroupPKey PKey) { final String S_ProcName = "CFAccRamTSecGroup.readBuff() "; CFAccTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFAccTSecGroupBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamTSecGroup.readAllBuff() "; CFAccTSecGroupBuff buff; ArrayList<CFAccTSecGroupBuff> filteredList = new ArrayList<CFAccTSecGroupBuff>(); CFAccTSecGroupBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAccTSecGroupBuff[0])); } public CFAccTSecGroupBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFAccRamTSecGroup.readBuffByIdIdx() "; CFAccTSecGroupBuff buff = readDerivedByIdIdx(Authorization, TenantId, TSecGroupId); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFAccTSecGroupBuff) buff); } else { return (null); } } public CFAccTSecGroupBuff[] readBuffByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamTSecGroup.readBuffByTenantIdx() "; CFAccTSecGroupBuff buff; ArrayList<CFAccTSecGroupBuff> filteredList = new ArrayList<CFAccTSecGroupBuff>(); CFAccTSecGroupBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add((CFAccTSecGroupBuff) buff); } } return (filteredList.toArray(new CFAccTSecGroupBuff[0])); } public CFAccTSecGroupBuff readBuffByUNameIdx(CFAccAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFAccRamTSecGroup.readBuffByUNameIdx() "; CFAccTSecGroupBuff buff = readDerivedByUNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFAccTSecGroupBuff) buff); } else { return (null); } } public void updateTSecGroup(CFAccAuthorization Authorization, CFAccTSecGroupBuff Buff) { CFAccTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFAccTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTSecGroup", "Existing record not found", "TSecGroup", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTSecGroup", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAccTSecGroupByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAccTSecGroupByTenantIdxKey newKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFAccTSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFAccTSecGroupByUNameIdxKey newKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); newKeyUNameIdx.setRequiredTenantId(Buff.getRequiredTenantId()); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTSecGroup", "TSecGroupUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTSecGroup", "Container", "TSecGroupTenant", "Tenant", null); } } } // Update is valid SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByTenantIdx.get(existingKeyTenantIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByTenantIdx.containsKey(newKeyTenantIdx)) { subdict = dictByTenantIdx.get(newKeyTenantIdx); } else { subdict = new TreeMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTSecGroup(CFAccAuthorization Authorization, CFAccTSecGroupBuff Buff) { final String S_ProcName = "CFAccRamTSecGroupTable.deleteTSecGroup() "; CFAccTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFAccTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTSecGroup", pkey); } CFAccTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAccTSecGroupByUNameIdxKey keyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableTSecGroupInclude().deleteTSecGroupIncludeByGroupIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); schema.getTableTSecGroupMember().deleteTSecGroupMemberByGroupIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); schema.getTableTSecGroupInclude().deleteTSecGroupIncludeByIncludeIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredTSecGroupId()); SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTSecGroupByIdIdx(CFAccAuthorization Authorization, long argTenantId, int argTSecGroupId) { CFAccTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTSecGroupId(argTSecGroupId); deleteTSecGroupByIdIdx(Authorization, key); } public void deleteTSecGroupByIdIdx(CFAccAuthorization Authorization, CFAccTSecGroupPKey argKey) { CFAccTSecGroupBuff cur; LinkedList<CFAccTSecGroupBuff> matchSet = new LinkedList<CFAccTSecGroupBuff>(); Iterator<CFAccTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByTenantIdx(CFAccAuthorization Authorization, long argTenantId) { CFAccTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTSecGroupByTenantIdx(Authorization, key); } public void deleteTSecGroupByTenantIdx(CFAccAuthorization Authorization, CFAccTSecGroupByTenantIdxKey argKey) { CFAccTSecGroupBuff cur; LinkedList<CFAccTSecGroupBuff> matchSet = new LinkedList<CFAccTSecGroupBuff>(); Iterator<CFAccTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByUNameIdx(CFAccAuthorization Authorization, long argTenantId, String argName) { CFAccTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTSecGroupByUNameIdx(Authorization, key); } public void deleteTSecGroupByUNameIdx(CFAccAuthorization Authorization, CFAccTSecGroupByUNameIdxKey argKey) { CFAccTSecGroupBuff cur; LinkedList<CFAccTSecGroupBuff> matchSet = new LinkedList<CFAccTSecGroupBuff>(); Iterator<CFAccTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public CFAccCursor openTSecGroupCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamTSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAccCursor openTSecGroupCursorByTenantIdx(CFAccAuthorization Authorization, long TenantId) { CFAccCursor cursor; CFAccTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAccTSecGroupPKey, CFAccTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAccRamTSecGroupCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAccRamTSecGroupCursor(Authorization, schema, new ArrayList<CFAccTSecGroupBuff>()); } return (cursor); } public void closeTSecGroupCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccTSecGroupBuff nextTSecGroupCursor(CFAccCursor Cursor) { CFAccRamTSecGroupCursor cursor = (CFAccRamTSecGroupCursor) Cursor; CFAccTSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccTSecGroupBuff prevTSecGroupCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFAccTSecGroupBuff firstTSecGroupCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccTSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFAccTSecGroupBuff lastTSecGroupCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTSecGroupCursor"); } public CFAccTSecGroupBuff nthTSecGroupCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }