Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for Tenant. /* * 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.*; /* * CFSecurityRamTenantTable in-memory RAM DbIO implementation * for Tenant. */ public class CFSecurityRamTenantTable implements ICFSecurityTenantTable { private CFSecurityRamSchema schema; private Map<CFSecurityTenantPKey, CFSecurityTenantBuff> dictByPKey = new HashMap<CFSecurityTenantPKey, CFSecurityTenantBuff>(); private SortedMap<CFSecurityTenantByClusterIdxKey, SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff>> dictByClusterIdx = new TreeMap<CFSecurityTenantByClusterIdxKey, SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff>>(); private SortedMap<CFSecurityTenantByUNameIdxKey, CFSecurityTenantBuff> dictByUNameIdx = new TreeMap<CFSecurityTenantByUNameIdxKey, CFSecurityTenantBuff>(); private Map<CFSecurityRamTenantId32Gen, CFSecurityRamTenantId32Gen> id32Generator = new HashMap<CFSecurityRamTenantId32Gen, CFSecurityRamTenantId32Gen>(); private Map<CFSecurityRamTenantId64Gen, CFSecurityRamTenantId64Gen> id64Generator = new HashMap<CFSecurityRamTenantId64Gen, CFSecurityRamTenantId64Gen>(); public CFSecurityRamTenantTable(CFSecurityRamSchema argSchema) { schema = argSchema; } protected int generateNextId32(long argId, short argSliceId) { CFSecurityRamTenantId32Gen key = new CFSecurityRamTenantId32Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFSecurityRamTenantId32Gen useGen = id32Generator.get(key); if (useGen == null) { id32Generator.put(key, key); useGen = key; } int retNext = useGen.getNextId(); return (retNext); } protected long generateNextId64(long argId, short argSliceId) { CFSecurityRamTenantId64Gen key = new CFSecurityRamTenantId64Gen(); key.setRequiredId(argId); key.setRequiredSliceId(argSliceId); CFSecurityRamTenantId64Gen useGen = id64Generator.get(key); if (useGen == null) { id64Generator.put(key, key); useGen = key; } long retNext = useGen.getNextId(); return (retNext); } public int nextTSecGroupIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) { int retval = nextTSecGroupIdGen(Authorization, pkey.getRequiredId()); return (retval); } public int nextTSecGroupIdGen(CFSecurityAuthorization Authorization, long argId) { int retNext = generateNextId32(argId, (short) 113); return (retNext); } public long nextTSecGroupMemberIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) { long retval = nextTSecGroupMemberIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextTSecGroupMemberIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 114); return (retNext); } public long nextTSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, CFSecurityTenantPKey pkey) { long retval = nextTSecGroupIncludeIdGen(Authorization, pkey.getRequiredId()); return (retval); } public long nextTSecGroupIncludeIdGen(CFSecurityAuthorization Authorization, long argId) { long retNext = generateNextId64(argId, (short) 115); return (retNext); } public void createTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) { CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(schema.nextTenantIdGen()); Buff.setRequiredId(pkey.getRequiredId()); CFSecurityTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecurityTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); keyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createTenant", pkey); } if (dictByUNameIdx.containsKey(keyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createTenant", "TenantUNameIdx", keyUNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createTenant", "Container", "TenantCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<CFSecurityTenantPKey, CFSecurityTenantBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public CFSecurityTenantBuff readDerived(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) { final String S_ProcName = "CFSecurityRamTenant.readDerived() "; CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTenantBuff lockDerived(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) { final String S_ProcName = "CFSecurityRamTenant.readDerived() "; CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(PKey.getRequiredId()); CFSecurityTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTenantBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamTenant.readAllDerived() "; CFSecurityTenantBuff[] retList = new CFSecurityTenantBuff[dictByPKey.values().size()]; Iterator<CFSecurityTenantBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFSecurityTenantBuff[] readDerivedByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamTenant.readDerivedByClusterIdx() "; CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFSecurityTenantBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFSecurityTenantBuff[subdictClusterIdx.size()]; Iterator<CFSecurityTenantBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFSecurityTenantBuff[0]; } return (recArray); } public CFSecurityTenantBuff readDerivedByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "CFSecurityRamTenant.readDerivedByUNameIdx() "; CFSecurityTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredTenantName(TenantName); CFSecurityTenantBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public CFSecurityTenantBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long Id) { final String S_ProcName = "CFSecurityRamTenant.readDerivedByIdIdx() "; CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(Id); CFSecurityTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFSecurityTenantBuff readBuff(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) { final String S_ProcName = "CFSecurityRamTenant.readBuff() "; CFSecurityTenantBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TENT"))) { buff = null; } return (buff); } public CFSecurityTenantBuff lockBuff(CFSecurityAuthorization Authorization, CFSecurityTenantPKey PKey) { final String S_ProcName = "CFSecurityRamTenant.readBuff() "; CFSecurityTenantBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TENT"))) { buff = null; } return (buff); } public CFSecurityTenantBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFSecurityRamTenant.readAllBuff() "; CFSecurityTenantBuff buff; ArrayList<CFSecurityTenantBuff> filteredList = new ArrayList<CFSecurityTenantBuff>(); CFSecurityTenantBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TENT")) { filteredList.add(buff); } } return (filteredList.toArray(new CFSecurityTenantBuff[0])); } public CFSecurityTenantBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long Id) { final String S_ProcName = "CFSecurityRamTenant.readBuffByIdIdx() "; CFSecurityTenantBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((CFSecurityTenantBuff) buff); } else { return (null); } } public CFSecurityTenantBuff[] readBuffByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFSecurityRamTenant.readBuffByClusterIdx() "; CFSecurityTenantBuff buff; ArrayList<CFSecurityTenantBuff> filteredList = new ArrayList<CFSecurityTenantBuff>(); CFSecurityTenantBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TENT")) { filteredList.add((CFSecurityTenantBuff) buff); } } return (filteredList.toArray(new CFSecurityTenantBuff[0])); } public CFSecurityTenantBuff readBuffByUNameIdx(CFSecurityAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "CFSecurityRamTenant.readBuffByUNameIdx() "; CFSecurityTenantBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, TenantName); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((CFSecurityTenantBuff) buff); } else { return (null); } } public void updateTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) { CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityTenantBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateTenant", "Existing record not found", "Tenant", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateTenant", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFSecurityTenantByClusterIdxKey existingKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecurityTenantByClusterIdxKey newKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); CFSecurityTenantByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); CFSecurityTenantByUNameIdxKey newKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); newKeyUNameIdx.setRequiredClusterId(Buff.getRequiredClusterId()); newKeyUNameIdx.setRequiredTenantName(Buff.getRequiredTenantName()); // Check unique indexes if (!existingKeyUNameIdx.equals(newKeyUNameIdx)) { if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateTenant", "TenantUNameIdx", newKeyUNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateTenant", "Container", "TenantCluster", "Cluster", null); } } } // Update is valid SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff> 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<CFSecurityTenantPKey, CFSecurityTenantBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTenant(CFSecurityAuthorization Authorization, CFSecurityTenantBuff Buff) { final String S_ProcName = "CFSecurityRamTenantTable.deleteTenant() "; CFSecurityTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); CFSecurityTenantBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTenant", pkey); } CFSecurityTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFSecurityTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); // Validate reverse foreign keys // Delete is valid schema.getTableTSecGroup().deleteTSecGroupByTenantIdx(Authorization, Buff.getRequiredId()); SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public void deleteTenantByIdIdx(CFSecurityAuthorization Authorization, long argId) { CFSecurityTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(argId); deleteTenantByIdIdx(Authorization, key); } public void deleteTenantByIdIdx(CFSecurityAuthorization Authorization, CFSecurityTenantPKey argKey) { CFSecurityTenantBuff cur; LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>(); Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public void deleteTenantByClusterIdx(CFSecurityAuthorization Authorization, long argClusterId) { CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteTenantByClusterIdx(Authorization, key); } public void deleteTenantByClusterIdx(CFSecurityAuthorization Authorization, CFSecurityTenantByClusterIdxKey argKey) { CFSecurityTenantBuff cur; LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>(); Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public void deleteTenantByUNameIdx(CFSecurityAuthorization Authorization, long argClusterId, String argTenantName) { CFSecurityTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey(); key.setRequiredClusterId(argClusterId); key.setRequiredTenantName(argTenantName); deleteTenantByUNameIdx(Authorization, key); } public void deleteTenantByUNameIdx(CFSecurityAuthorization Authorization, CFSecurityTenantByUNameIdxKey argKey) { CFSecurityTenantBuff cur; LinkedList<CFSecurityTenantBuff> matchSet = new LinkedList<CFSecurityTenantBuff>(); Iterator<CFSecurityTenantBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFSecurityTenantBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteTenant(Authorization, cur); } } public CFSecurityCursor openTenantCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFSecurityRamTenantCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openTenantCursorByClusterIdx(CFSecurityAuthorization Authorization, long ClusterId) { CFSecurityCursor cursor; CFSecurityTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<CFSecurityTenantPKey, CFSecurityTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFSecurityRamTenantCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFSecurityRamTenantCursor(Authorization, schema, new ArrayList<CFSecurityTenantBuff>()); } return (cursor); } public void closeTenantCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFSecurityTenantBuff nextTenantCursor(CFSecurityCursor Cursor) { CFSecurityRamTenantCursor cursor = (CFSecurityRamTenantCursor) Cursor; CFSecurityTenantBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFSecurityTenantBuff prevTenantCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFSecurityTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public CFSecurityTenantBuff firstTenantCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFSecurityTenantBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public CFSecurityTenantBuff lastTenantCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTenantCursor"); } public CFSecurityTenantBuff nthTenantCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFSecurityTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }