Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for TSecGroup. /* * CF Security Core Implementation * * Copyright (c) 2010-2014 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. * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfsecurity.v2_0.CFSecurityRam; 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.cfsecurity.v2_0.CFSecurity.*; import net.sourceforge.msscodefactory.cfsecurity.v2_0.CFSecurityRam.*; /* * CFSecurityRamTSecGroupTable in-memory RAM DbIO implementation * for TSecGroup. */ public class CFSecurityRamTSecGroupTable implements ICFSecurityTSecGroupTable { private CFSecurityRamSchema schema; private Map<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> dictByPKey = new HashMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); private SortedMap<CFSecurityTSecGroupByTenantIdxKey, SortedMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>> dictByTenantIdx = new TreeMap<CFSecurityTSecGroupByTenantIdxKey, SortedMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>>(); private SortedMap<CFSecurityTSecGroupByUNameIdxKey, CFSecurityTSecGroupBuff> dictByUNameIdx = new TreeMap<CFSecurityTSecGroupByUNameIdxKey, CFSecurityTSecGroupBuff>(); public CFSecurityRamTSecGroupTable(CFSecurityRamSchema argSchema) { schema = argSchema; } public void createTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(((CFSecurityRamTenantTable) schema.getTableTenant()) .nextTSecGroupIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredTSecGroupId(pkey.getRequiredTSecGroupId()); CFSecurityTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey 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<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFSecurityTSecGroupBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamTSecGroup.readDerived() "; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamTSecGroup.readDerived() "; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredTSecGroupId(PKey.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamTSecGroup.readAllDerived() "; CFSecurityTSecGroupBuff[] retList = new CFSecurityTSecGroupBuff[dictByPKey.values().size()]; Iterator<CFSecurityTSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityTSecGroupBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFSecurityRamTSecGroup.readDerivedByTenantIdx() "; CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFSecurityTSecGroupBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFSecurityTSecGroupBuff[subdictTenantIdx.size()]; Iterator<CFSecurityTSecGroupBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFSecurityTSecGroupBuff[0]; } return (recArray); } public CFSecurityTSecGroupBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFSecurityRamTSecGroup.readDerivedByUNameIdx() "; CFSecurityTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredName(Name); CFSecurityTSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFSecurityRamTSecGroup.readDerivedByIdIdx() "; CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredTSecGroupId(TSecGroupId); CFSecurityTSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTSecGroupBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamTSecGroup.readBuff() "; CFSecurityTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFSecurityTSecGroupBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamTSecGroup.readBuff() "; CFSecurityTSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TGRP"))) { buff = null; } return (buff); } public CFSecurityTSecGroupBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamTSecGroup.readAllBuff() "; CFSecurityTSecGroupBuff buff; ArrayList<CFSecurityTSecGroupBuff> filteredList = new ArrayList<CFSecurityTSecGroupBuff>(); CFSecurityTSecGroupBuff[] 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 CFSecurityTSecGroupBuff[0])); } public CFSecurityTSecGroupBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, int TSecGroupId) { final String S_ProcName = "CFSecurityRamTSecGroup.readBuffByIdIdx() "; CFSecurityTSecGroupBuff buff = readDerivedByIdIdx(Authorization, TenantId, TSecGroupId); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFSecurityTSecGroupBuff) buff); } else { return (null); } } public CFSecurityTSecGroupBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFSecurityRamTSecGroup.readBuffByTenantIdx() "; CFSecurityTSecGroupBuff buff; ArrayList<CFSecurityTSecGroupBuff> filteredList = new ArrayList<CFSecurityTSecGroupBuff>(); CFSecurityTSecGroupBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TGRP")) { filteredList.add((CFSecurityTSecGroupBuff) buff); } } return (filteredList.toArray(new CFSecurityTSecGroupBuff[0])); } public CFSecurityTSecGroupBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long TenantId, String Name) { final String S_ProcName = "CFSecurityRamTSecGroup.readBuffByUNameIdx() "; CFSecurityTSecGroupBuff buff = readDerivedByUNameIdx(Authorization, TenantId, Name); if ((buff != null) && buff.getClassCode().equals("TGRP")) { return ((CFSecurityTSecGroupBuff) buff); } else { return (null); } } public void updateTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff 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); CFSecurityTSecGroupByTenantIdxKey existingKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFSecurityTSecGroupByTenantIdxKey newKeyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTSecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFSecurityTSecGroupByUNameIdxKey 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<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> 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<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTSecGroup(CFSecurityAuthorization Authorization, CFSecurityTSecGroupBuff Buff) { final String S_ProcName = "CFSecurityRamTSecGroupTable.deleteTSecGroup() "; CFSecurityTSecGroupPKey pkey = schema.getFactoryTSecGroup().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredTSecGroupId(Buff.getRequiredTSecGroupId()); CFSecurityTSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTSecGroup", pkey); } CFSecurityTSecGroupByTenantIdxKey keyTenantIdx = schema.getFactoryTSecGroup().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFSecurityTSecGroupByUNameIdxKey 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<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTSecGroupByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, int argTSecGroupId) { CFSecurityTSecGroupPKey key = schema.getFactoryTSecGroup().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredTSecGroupId(argTSecGroupId); deleteTSecGroupByIdIdx(Authorization, key); } public void deleteTSecGroupByIdIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupPKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteTSecGroupByTenantIdx(Authorization, key); } public void deleteTSecGroupByTenantIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupByTenantIdxKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public void deleteTSecGroupByUNameIdx(CFSecurityAuthorization Authorization, long argTenantId, String argName) { CFSecurityTSecGroupByUNameIdxKey key = schema.getFactoryTSecGroup().newUNameIdxKey(); key.setRequiredTenantId(argTenantId); key.setRequiredName(argName); deleteTSecGroupByUNameIdx(Authorization, key); } public void deleteTSecGroupByUNameIdx(CFSecurityAuthorization Authorization, CFSecurityTSecGroupByUNameIdxKey argKey) { CFSecurityTSecGroupBuff cur; LinkedList<CFSecurityTSecGroupBuff> matchSet = new LinkedList<CFSecurityTSecGroupBuff>(); Iterator<CFSecurityTSecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTSecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTSecGroup(Authorization, cur); } } public CFSecurityCursor openTSecGroupCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFSecurityRamTSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openTSecGroupCursorByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { CFSecurityCursor cursor; CFSecurityTSecGroupByTenantIdxKey key = schema.getFactoryTSecGroup().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFSecurityTSecGroupPKey, CFSecurityTSecGroupBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFSecurityRamTSecGroupCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFSecurityRamTSecGroupCursor(Authorization, schema, new ArrayList<CFSecurityTSecGroupBuff>()); } return (cursor); } public void closeTSecGroupCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityTSecGroupBuff nextTSecGroupCursor(CFSecurityCursor Cursor) { CFSecurityRamTSecGroupCursor cursor = (CFSecurityRamTSecGroupCursor) Cursor; CFSecurityTSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityTSecGroupBuff prevTSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFSecurityTSecGroupBuff firstTSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityTSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public CFSecurityTSecGroupBuff lastTSecGroupCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTSecGroupCursor"); } public CFSecurityTSecGroupBuff nthTSecGroupCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityTSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }