Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for SecGroup. /* * MSS Code Factory 1.10 * * Copyright (c) 2012 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/>. * * *********************************************************************** * * Code manufactured * by MSS Code Factory version 1.9.3294 * * $Revision: 26 $ */ package net.sourceforge.msscodefactory.v1_10.MSSBamRam; import java.sql.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_9.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.v1_10.MSSBam.*; import net.sourceforge.msscodefactory.v1_10.MSSBamBL.*; import net.sourceforge.msscodefactory.v1_10.MSSBamBLRam.*; /* * MSSBamRamSecGroupTable in-memory RAM DbIO implementation * for SecGroup. */ public class MSSBamRamSecGroupTable implements IMSSBamSecGroupTable { private MSSBamBLRamSchema schema; private Map<MSSBamSecGroupPKey, MSSBamSecGroupBuff> dictByPKey = new HashMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff>(); private SortedMap<MSSBamSecGroupByClusterIdxKey, SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff>> dictByClusterIdx = new TreeMap<MSSBamSecGroupByClusterIdxKey, SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff>>(); private SortedMap<MSSBamSecGroupByUNameIdxKey, MSSBamSecGroupBuff> dictByUNameIdx = new TreeMap<MSSBamSecGroupByUNameIdxKey, MSSBamSecGroupBuff>(); public MSSBamRamSecGroupTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createSecGroup(MSSBamAuthorization Authorization, MSSBamSecGroupBuff Buff) { MSSBamSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredSecGroupId(schema.nextSecGroupIdGen()); Buff.setRequiredSecGroupId(pkey.getRequiredSecGroupId()); MSSBamSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamSecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createSecGroup", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createSecGroup", "SecGroupUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createSecGroup", "Container", "SecGroupCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public MSSBamSecGroupBuff readDerived(MSSBamAuthorization Authorization, MSSBamSecGroupPKey PKey) { final String S_ProcName = "MSSBamRamSecGroup.readDerived() "; MSSBamSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); MSSBamSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamSecGroupBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamSecGroup.readAllDerived() "; MSSBamSecGroupBuff[] retList = new MSSBamSecGroupBuff[dictByPKey.values().size()]; Iterator<MSSBamSecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamSecGroupBuff[] readDerivedByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamSecGroup.readDerivedByClusterIdx() "; MSSBamSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); MSSBamSecGroupBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new MSSBamSecGroupBuff[subdictClusterIdx.size()]; Iterator<MSSBamSecGroupBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamSecGroupBuff[0]; } return (recArray); } public MSSBamSecGroupBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "MSSBamRamSecGroup.readDerivedByUNameIdx() "; MSSBamSecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredName(Name); MSSBamSecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public MSSBamSecGroupBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, int SecGroupId) { final String S_ProcName = "MSSBamRamSecGroup.readDerivedByIdIdx() "; MSSBamSecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredSecGroupId(SecGroupId); MSSBamSecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamSecGroupBuff readBuff(MSSBamAuthorization Authorization, MSSBamSecGroupPKey PKey) { final String S_ProcName = "MSSBamRamSecGroup.readBuff() "; MSSBamSecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public MSSBamSecGroupBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamSecGroup.readAllBuff() "; MSSBamSecGroupBuff buff; ArrayList<MSSBamSecGroupBuff> filteredList = new ArrayList<MSSBamSecGroupBuff>(); MSSBamSecGroupBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamSecGroupBuff[0])); } public MSSBamSecGroupBuff readBuffByIdIdx(MSSBamAuthorization Authorization, int SecGroupId) { final String S_ProcName = "MSSBamRamSecGroup.readBuffByIdIdx() "; MSSBamSecGroupBuff buff = readDerivedByIdIdx(Authorization, SecGroupId); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((MSSBamSecGroupBuff) buff); } else { return (null); } } public MSSBamSecGroupBuff[] readBuffByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamSecGroup.readBuffByClusterIdx() "; MSSBamSecGroupBuff buff; ArrayList<MSSBamSecGroupBuff> filteredList = new ArrayList<MSSBamSecGroupBuff>(); MSSBamSecGroupBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add((MSSBamSecGroupBuff) buff); } } return (filteredList.toArray(new MSSBamSecGroupBuff[0])); } public MSSBamSecGroupBuff readBuffByUNameIdx(MSSBamAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "MSSBamRamSecGroup.readBuffByUNameIdx() "; MSSBamSecGroupBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, Name); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((MSSBamSecGroupBuff) buff); } else { return (null); } } public void updateSecGroup(MSSBamAuthorization Authorization, MSSBamSecGroupBuff Buff) { MSSBamSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); MSSBamSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSecGroup", "Existing record not found", "SecGroup", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSecGroup", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamSecGroupByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamSecGroupByClusterIdxKey newKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamSecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); MSSBamSecGroupByUNameIdxKey newKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); newKeyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateSecGroup", "SecGroupUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSecGroup", "Container", "SecGroupCluster", "Cluster", null); } } } // Update is valid SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff> 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<MSSBamSecGroupPKey, MSSBamSecGroupBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteSecGroup(MSSBamAuthorization Authorization, MSSBamSecGroupBuff Buff) { final String S_ProcName = "MSSBamRamSecGroupTable.deleteSecGroup() "; MSSBamSecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredSecGroupId(schema.nextSecGroupIdGen()); MSSBamSecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteSecGroup", "Existing record not found", "SecGroup", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecGroup", pkey); } MSSBamSecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamSecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys if (schema.getTableSecGroupForm().readDerivedByGroupIdx(Authorization, existing.getRequiredSecGroupId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecGroup", "Container", "SecGroupFormGroup", "SecGroupForm", pkey); } if (schema.getTableSecGroupInclude().readDerivedByGroupIdx(Authorization, existing.getRequiredSecGroupId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecGroup", "Container", "SecGroupIncludeGroup", "SecGroupInclude", pkey); } if (schema.getTableSecGroupInclude().readDerivedByIncludeIdx(Authorization, existing.getRequiredSecGroupId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecGroup", "Lookup", "SecGroupIncludeSubGroup", "SecGroupInclude", pkey); } if (schema.getTableSecGroupMember().readDerivedByGroupIdx(Authorization, existing.getRequiredSecGroupId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteSecGroup", "Container", "SecGroupMemberGroup", "SecGroupMember", pkey); } // Delete is valid SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public MSSBamCursor openSecGroupCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openSecGroupCursorByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { MSSBamCursor cursor; MSSBamSecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamSecGroupPKey, MSSBamSecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new MSSBamRamSecGroupCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new MSSBamRamSecGroupCursor(Authorization, schema, new ArrayList<MSSBamSecGroupBuff>()); } return (cursor); } public void closeSecGroupCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamSecGroupBuff nextSecGroupCursor(MSSBamCursor Cursor) { MSSBamRamSecGroupCursor cursor = (MSSBamRamSecGroupCursor) Cursor; MSSBamSecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamSecGroupBuff prevSecGroupCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public MSSBamSecGroupBuff firstSecGroupCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamSecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public MSSBamSecGroupBuff lastSecGroupCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecGroupCursor"); } public MSSBamSecGroupBuff nthSecGroupCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamSecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } }