Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SecApp. /* * 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.*; /* * CFSecurityRamSecAppTable in-memory RAM DbIO implementation * for SecApp. */ public class CFSecurityRamSecAppTable implements ICFSecuritySecAppTable { private CFSecurityRamSchema schema; private Map<CFSecuritySecAppPKey, CFSecuritySecAppBuff> dictByPKey = new HashMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); private SortedMap<CFSecuritySecAppByClusterIdxKey, SortedMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>> dictByClusterIdx = new TreeMap<CFSecuritySecAppByClusterIdxKey, SortedMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>>(); private SortedMap<CFSecuritySecAppByUJEEMountIdxKey, CFSecuritySecAppBuff> dictByUJEEMountIdx = new TreeMap<CFSecuritySecAppByUJEEMountIdxKey, CFSecuritySecAppBuff>(); public CFSecurityRamSecAppTable(CFSecurityRamSchema argSchema) { schema = argSchema; } public void createSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(((CFSecurityRamClusterTable) schema.getTableCluster()) .nextSecAppIdGen(Authorization, Buff.getRequiredClusterId())); Buff.setRequiredClusterId(pkey.getRequiredClusterId()); Buff.setRequiredSecAppId(pkey.getRequiredSecAppId()); CFSecuritySecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey 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<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUJEEMountIdx.put(keyUJEEMountIdx, Buff); } public CFSecuritySecAppBuff readDerived(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFSecurityRamSecApp.readDerived() "; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff lockDerived(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFSecurityRamSecApp.readDerived() "; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(PKey.getRequiredClusterId()); key.setRequiredSecAppId(PKey.getRequiredSecAppId()); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamSecApp.readAllDerived() "; CFSecuritySecAppBuff[] retList = new CFSecuritySecAppBuff[dictByPKey.values().size()]; Iterator<CFSecuritySecAppBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecuritySecAppBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamSecApp.readDerivedByClusterIdx() "; CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecuritySecAppBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecuritySecAppBuff[subdictClusterIdx.size()]; Iterator<CFSecuritySecAppBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFSecuritySecAppBuff[0]; } return (recArray); } public CFSecuritySecAppBuff readDerivedByUJEEMountIdx(CFSecurityAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFSecurityRamSecApp.readDerivedByUJEEMountIdx() "; CFSecuritySecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredJEEMountName(JEEMountName); CFSecuritySecAppBuff buff; if (dictByUJEEMountIdx.containsKey(key)) { buff = dictByUJEEMountIdx.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFSecurityRamSecApp.readDerivedByIdIdx() "; CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(ClusterId); key.setRequiredSecAppId(SecAppId); CFSecuritySecAppBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecuritySecAppBuff readBuff(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFSecurityRamSecApp.readBuff() "; CFSecuritySecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFSecuritySecAppBuff lockBuff(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey PKey) { final String S_ProcName = "CFSecurityRamSecApp.readBuff() "; CFSecuritySecAppBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SAPP"))) { buff = null; } return (buff); } public CFSecuritySecAppBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamSecApp.readAllBuff() "; CFSecuritySecAppBuff buff; ArrayList<CFSecuritySecAppBuff> filteredList = new ArrayList<CFSecuritySecAppBuff>(); CFSecuritySecAppBuff[] 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 CFSecuritySecAppBuff[0])); } public CFSecuritySecAppBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long ClusterId, int SecAppId) { final String S_ProcName = "CFSecurityRamSecApp.readBuffByIdIdx() "; CFSecuritySecAppBuff buff = readDerivedByIdIdx(Authorization, ClusterId, SecAppId); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFSecuritySecAppBuff) buff); } else { return (null); } } public CFSecuritySecAppBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamSecApp.readBuffByClusterIdx() "; CFSecuritySecAppBuff buff; ArrayList<CFSecuritySecAppBuff> filteredList = new ArrayList<CFSecuritySecAppBuff>(); CFSecuritySecAppBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SAPP")) { filteredList.add((CFSecuritySecAppBuff) buff); } } return (filteredList.toArray(new CFSecuritySecAppBuff[0])); } public CFSecuritySecAppBuff readBuffByUJEEMountIdx(CFSecurityAuthorization Authorization, long ClusterId, String JEEMountName) { final String S_ProcName = "CFSecurityRamSecApp.readBuffByUJEEMountIdx() "; CFSecuritySecAppBuff buff = readDerivedByUJEEMountIdx(Authorization, ClusterId, JEEMountName); if ((buff != null) && buff.getClassCode().equals("SAPP")) { return ((CFSecuritySecAppBuff) buff); } else { return (null); } } public void updateSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFSecuritySecAppBuff 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); CFSecuritySecAppByClusterIdxKey existingKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecAppByClusterIdxKey newKeyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey existingKeyUJEEMountIdx = schema.getFactorySecApp().newUJEEMountIdxKey(); existingKeyUJEEMountIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUJEEMountIdx.setRequiredJEEMountName(existing.getRequiredJEEMountName()); CFSecuritySecAppByUJEEMountIdxKey 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<CFSecuritySecAppPKey, CFSecuritySecAppBuff> 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<CFSecuritySecAppPKey, CFSecuritySecAppBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUJEEMountIdx.remove(existingKeyUJEEMountIdx); dictByUJEEMountIdx.put(newKeyUJEEMountIdx, Buff); } public void deleteSecApp(CFSecurityAuthorization Authorization, CFSecuritySecAppBuff Buff) { final String S_ProcName = "CFSecurityRamSecAppTable.deleteSecApp() "; CFSecuritySecAppPKey pkey = schema.getFactorySecApp().newPKey(); pkey.setRequiredClusterId(Buff.getRequiredClusterId()); pkey.setRequiredSecAppId(Buff.getRequiredSecAppId()); CFSecuritySecAppBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSecApp", pkey); } CFSecuritySecAppByClusterIdxKey keyClusterIdx = schema.getFactorySecApp().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecuritySecAppByUJEEMountIdxKey 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<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUJEEMountIdx.remove(keyUJEEMountIdx); } public void deleteSecAppByIdIdx(CFSecurityAuthorization Authorization, long argClusterId, int argSecAppId) { CFSecuritySecAppPKey key = schema.getFactorySecApp().newPKey(); key.setRequiredClusterId(argClusterId); key.setRequiredSecAppId(argSecAppId); deleteSecAppByIdIdx(Authorization, key); } public void deleteSecAppByIdIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppPKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSecAppByClusterIdx(Authorization, key); } public void deleteSecAppByClusterIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppByClusterIdxKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public void deleteSecAppByUJEEMountIdx(CFSecurityAuthorization Authorization, long argClusterId, String argJEEMountName) { CFSecuritySecAppByUJEEMountIdxKey key = schema.getFactorySecApp().newUJEEMountIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredJEEMountName(argJEEMountName); deleteSecAppByUJEEMountIdx(Authorization, key); } public void deleteSecAppByUJEEMountIdx(CFSecurityAuthorization Authorization, CFSecuritySecAppByUJEEMountIdxKey argKey) { CFSecuritySecAppBuff cur; LinkedList<CFSecuritySecAppBuff> matchSet = new LinkedList<CFSecuritySecAppBuff>(); Iterator<CFSecuritySecAppBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecuritySecAppBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSecApp(Authorization, cur); } } public CFSecurityCursor openSecAppCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFSecurityRamSecAppCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openSecAppCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecuritySecAppByClusterIdxKey key = schema.getFactorySecApp().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecuritySecAppPKey, CFSecuritySecAppBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFSecurityRamSecAppCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFSecurityRamSecAppCursor(Authorization, schema, new ArrayList<CFSecuritySecAppBuff>()); } return (cursor); } public void closeSecAppCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecuritySecAppBuff nextSecAppCursor(CFSecurityCursor Cursor) { CFSecurityRamSecAppCursor cursor = (CFSecurityRamSecAppCursor) Cursor; CFSecuritySecAppBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecuritySecAppBuff prevSecAppCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecuritySecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFSecuritySecAppBuff firstSecAppCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecuritySecAppBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public CFSecuritySecAppBuff lastSecAppCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSecAppCursor"); } public CFSecuritySecAppBuff nthSecAppCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecuritySecAppBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSecAppCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }