Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecGroup. /* * 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.*; /* * CFSecurityRamSecGroupTable in-memory RAM DbIO implementation * for SecGroup. */ public class CFSecurityRamSecGroupTable implements ICFSecuritySecGroupTable { private CFSecurityRamSchema schema; private Map<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> dictByPKey = new HashMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); private SortedMap<CFSecuritySecGroupByClusterIdxKey, SortedMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>> dictByClusterIdx = new TreeMap<CFSecuritySecGroupByClusterIdxKey, SortedMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>>(); private SortedMap<CFSecuritySecGroupByUNameIdxKey, CFSecuritySecGroupBuff> dictByUNameIdx = new TreeMap<CFSecuritySecGroupByUNameIdxKey, CFSecuritySecGroupBuff>(); public CFSecurityRamSecGroupTable(CFSecurityRamSchema argSchema) { schema = argSchema; } public void createSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(((CFSecurityRamClusterTable) schema.getTableCluster()) .nextSecGroupIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecGroupId(pkey.getRequiredSecGroupId()); CFSecuritySecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey 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<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFSecuritySecGroupBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamSecGroup.readDerived() "; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamSecGroup.readDerived() "; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecGroupId(PKey.getRequiredSecGroupId()); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamSecGroup.readAllDerived() "; CFSecuritySecGroupBuff[] retList = new CFSecuritySecGroupBuff[dictByPKey.values().size()]; Iterator<CFSecuritySecGroupBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySecGroupBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamSecGroup.readDerivedByClusterIdx() "; CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecuritySecGroupBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecuritySecGroupBuff[subdictClusterIdx.size()]; Iterator<CFSecuritySecGroupBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFSecuritySecGroupBuff[0]; } return (recArray); } public CFSecuritySecGroupBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFSecurityRamSecGroup.readDerivedByUNameIdx() "; CFSecuritySecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredName(Name); CFSecuritySecGroupBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFSecurityRamSecGroup.readDerivedByIdIdx() "; CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecGroupId(SecGroupId); CFSecuritySecGroupBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecGroupBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamSecGroup.readBuff() "; CFSecuritySecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFSecuritySecGroupBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey PKey) { final String S_ProcName = "CFSecurityRamSecGroup.readBuff() "; CFSecuritySecGroupBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SGRP"))) { buff = null; } return (buff); } public CFSecuritySecGroupBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamSecGroup.readAllBuff() "; CFSecuritySecGroupBuff buff; ArrayList<CFSecuritySecGroupBuff> filteredList = new ArrayList<CFSecuritySecGroupBuff>(); CFSecuritySecGroupBuff[] 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 CFSecuritySecGroupBuff[0])); } public CFSecuritySecGroupBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecGroupId) { final String S_ProcName = "CFSecurityRamSecGroup.readBuffByIdIdx() "; CFSecuritySecGroupBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecGroupId); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFSecuritySecGroupBuff) buff); } else { return (null); } } public CFSecuritySecGroupBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamSecGroup.readBuffByClusterIdx() "; CFSecuritySecGroupBuff buff; ArrayList<CFSecuritySecGroupBuff> filteredList = new ArrayList<CFSecuritySecGroupBuff>(); CFSecuritySecGroupBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SGRP")) { filteredList.add((CFSecuritySecGroupBuff) buff); } } return (filteredList.toArray(new CFSecuritySecGroupBuff[0])); } public CFSecuritySecGroupBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String Name) { final String S_ProcName = "CFSecurityRamSecGroup.readBuffByUNameIdx() "; CFSecuritySecGroupBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, Name); if ((buff != null) && buff.getClassCode().equals("SGRP")) { return ((CFSecuritySecGroupBuff) buff); } else { return (null); } } public void updateSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFSecuritySecGroupBuff 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); CFSecuritySecGroupByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecGroupByClusterIdxKey newKeyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey existingKeyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); CFSecuritySecGroupByUNameIdxKey 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<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> 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<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteSecGroup(CFSecurityAuthorization Authorization, CFSecuritySecGroupBuff Buff) { final String S_ProcName = "CFSecurityRamSecGroupTable.deleteSecGroup() "; CFSecuritySecGroupPKey pkey = schema.getFactorySecGroup().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecGroupId(Buff.getRequiredSecGroupId()); CFSecuritySecGroupBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecGroup", pkey); } CFSecuritySecGroupByClusterIdxKey keyClusterIdx = schema.getFactorySecGroup().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecGroupByUNameIdxKey keyUNameIdx = schema.getFactorySecGroup().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid schema.getTableSecGroupInclude().deleteSecGroupIncludeByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupMember().deleteSecGroupMemberByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupInclude().deleteSecGroupIncludeByIncludeIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); schema.getTableSecGroupForm().deleteSecGroupFormByGroupIdx(Authorization, Buff.getRequiredClusterId(), Buff.getRequiredSecGroupId()); SortedMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteSecGroupByIdIdx(CFSecurityAuthorization Authorization, long argClusterId, int argSecGroupId) { CFSecuritySecGroupPKey key = schema.getFactorySecGroup().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecGroupId(argSecGroupId); deleteSecGroupByIdIdx(Authorization, key); } public void deleteSecGroupByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupPKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecGroupByClusterIdx(Authorization, key); } public void deleteSecGroupByClusterIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupByClusterIdxKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public void deleteSecGroupByUNameIdx(CFSecurityAuthorization Authorization, long argClusterId, String argName) { CFSecuritySecGroupByUNameIdxKey key = schema.getFactorySecGroup().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredName(argName); deleteSecGroupByUNameIdx(Authorization, key); } public void deleteSecGroupByUNameIdx(CFSecurityAuthorization Authorization, CFSecuritySecGroupByUNameIdxKey argKey) { CFSecuritySecGroupBuff cur; LinkedList<CFSecuritySecGroupBuff> matchSet = new LinkedList<CFSecuritySecGroupBuff>(); Iterator<CFSecuritySecGroupBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecGroupBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecGroup(Authorization, cur); } } public CFSecurityCursor openSecGroupCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFSecurityRamSecGroupCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSecGroupCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecuritySecGroupByClusterIdxKey key = schema.getFactorySecGroup().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecuritySecGroupPKey, CFSecuritySecGroupBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFSecurityRamSecGroupCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFSecurityRamSecGroupCursor(Authorization, schema, new ArrayList<CFSecuritySecGroupBuff>()); } return (cursor); } public void closeSecGroupCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySecGroupBuff nextSecGroupCursor(CFSecurityCursor Cursor) { CFSecurityRamSecGroupCursor cursor = (CFSecurityRamSecGroupCursor) Cursor; CFSecuritySecGroupBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySecGroupBuff prevSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFSecuritySecGroupBuff firstSecGroupCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySecGroupBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public CFSecuritySecGroupBuff lastSecGroupCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecGroupCursor"); } public CFSecuritySecGroupBuff nthSecGroupCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySecGroupBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecGroupCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }