Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for Tenant. /* * 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.*; /* * MSSBamRamTenantTable in-memory RAM DbIO implementation * for Tenant. */ public class MSSBamRamTenantTable implements IMSSBamTenantTable { private MSSBamBLRamSchema schema; private Map<MSSBamTenantPKey, MSSBamTenantBuff> dictByPKey = new HashMap<MSSBamTenantPKey, MSSBamTenantBuff>(); private SortedMap<MSSBamTenantByClusterIdxKey, SortedMap<MSSBamTenantPKey, MSSBamTenantBuff>> dictByClusterIdx = new TreeMap<MSSBamTenantByClusterIdxKey, SortedMap<MSSBamTenantPKey, MSSBamTenantBuff>>(); private SortedMap<MSSBamTenantByUNameIdxKey, MSSBamTenantBuff> dictByUNameIdx = new TreeMap<MSSBamTenantByUNameIdxKey, MSSBamTenantBuff>(); public MSSBamRamTenantTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createTenant(MSSBamAuthorization Authorization, MSSBamTenantBuff Buff) { MSSBamTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(schema.nextTenantIdGen()); Buff.setRequiredId(pkey.getRequiredId()); MSSBamTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamTenantByUNameIdxKey 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<MSSBamTenantPKey, MSSBamTenantBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<MSSBamTenantPKey, MSSBamTenantBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUNameIdx.put(keyUNameIdx, Buff); } public MSSBamTenantBuff readDerived(MSSBamAuthorization Authorization, MSSBamTenantPKey PKey) { final String S_ProcName = "MSSBamRamTenant.readDerived() "; MSSBamTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamTenantBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamTenant.readAllDerived() "; MSSBamTenantBuff[] retList = new MSSBamTenantBuff[dictByPKey.values().size()]; Iterator<MSSBamTenantBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamTenantBuff[] readDerivedByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamTenant.readDerivedByClusterIdx() "; MSSBamTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); MSSBamTenantBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamTenantPKey, MSSBamTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new MSSBamTenantBuff[subdictClusterIdx.size()]; Iterator<MSSBamTenantBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamTenantBuff[0]; } return (recArray); } public MSSBamTenantBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "MSSBamRamTenant.readDerivedByUNameIdx() "; MSSBamTenantByUNameIdxKey key = schema.getFactoryTenant().newUNameIdxKey(); key.setRequiredClusterId(ClusterId); key.setRequiredTenantName(TenantName); MSSBamTenantBuff buff; if (dictByUNameIdx.containsKey(key)) { buff = dictByUNameIdx.get(key); } else { buff = null; } return (buff); } public MSSBamTenantBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamTenant.readDerivedByIdIdx() "; MSSBamTenantPKey key = schema.getFactoryTenant().newPKey(); key.setRequiredId(Id); MSSBamTenantBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamTenantBuff readBuff(MSSBamAuthorization Authorization, MSSBamTenantPKey PKey) { final String S_ProcName = "MSSBamRamTenant.readBuff() "; MSSBamTenantBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("TENT"))) { buff = null; } return (buff); } public MSSBamTenantBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamTenant.readAllBuff() "; MSSBamTenantBuff buff; ArrayList<MSSBamTenantBuff> filteredList = new ArrayList<MSSBamTenantBuff>(); MSSBamTenantBuff[] 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 MSSBamTenantBuff[0])); } public MSSBamTenantBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamTenant.readBuffByIdIdx() "; MSSBamTenantBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((MSSBamTenantBuff) buff); } else { return (null); } } public MSSBamTenantBuff[] readBuffByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamTenant.readBuffByClusterIdx() "; MSSBamTenantBuff buff; ArrayList<MSSBamTenantBuff> filteredList = new ArrayList<MSSBamTenantBuff>(); MSSBamTenantBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("TENT")) { filteredList.add((MSSBamTenantBuff) buff); } } return (filteredList.toArray(new MSSBamTenantBuff[0])); } public MSSBamTenantBuff readBuffByUNameIdx(MSSBamAuthorization Authorization, long ClusterId, String TenantName) { final String S_ProcName = "MSSBamRamTenant.readBuffByUNameIdx() "; MSSBamTenantBuff buff = readDerivedByUNameIdx(Authorization, ClusterId, TenantName); if ((buff != null) && buff.getClassCode().equals("TENT")) { return ((MSSBamTenantBuff) buff); } else { return (null); } } public void updateTenant(MSSBamAuthorization Authorization, MSSBamTenantBuff Buff) { MSSBamTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); MSSBamTenantBuff 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); MSSBamTenantByClusterIdxKey existingKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamTenantByClusterIdxKey newKeyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamTenantByUNameIdxKey existingKeyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); existingKeyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); existingKeyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); MSSBamTenantByUNameIdxKey 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<MSSBamTenantPKey, MSSBamTenantBuff> 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<MSSBamTenantPKey, MSSBamTenantBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUNameIdx.remove(existingKeyUNameIdx); dictByUNameIdx.put(newKeyUNameIdx, Buff); } public void deleteTenant(MSSBamAuthorization Authorization, MSSBamTenantBuff Buff) { final String S_ProcName = "MSSBamRamTenantTable.deleteTenant() "; MSSBamTenantPKey pkey = schema.getFactoryTenant().newPKey(); pkey.setRequiredId(schema.nextTenantIdGen()); MSSBamTenantBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteTenant", "Existing record not found", "Tenant", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteTenant", pkey); } MSSBamTenantByClusterIdxKey keyClusterIdx = schema.getFactoryTenant().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamTenantByUNameIdxKey keyUNameIdx = schema.getFactoryTenant().newUNameIdxKey(); keyUNameIdx.setRequiredClusterId(existing.getRequiredClusterId()); keyUNameIdx.setRequiredTenantName(existing.getRequiredTenantName()); // Validate reverse foreign keys if (schema.getTableContactList().readDerivedByTenantIdx(Authorization, existing.getRequiredId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteTenant", "Container", "ContactListTenant", "ContactList", pkey); } if (schema.getTableTag().readDerivedByTenantIdx(Authorization, existing.getRequiredId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteTenant", "Container", "TagTenant", "Tag", pkey); } // Delete is valid SortedMap<MSSBamTenantPKey, MSSBamTenantBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUNameIdx.remove(keyUNameIdx); } public MSSBamCursor openTenantCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamTenantCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openTenantCursorByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { MSSBamCursor cursor; MSSBamTenantByClusterIdxKey key = schema.getFactoryTenant().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamTenantPKey, MSSBamTenantBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new MSSBamRamTenantCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new MSSBamRamTenantCursor(Authorization, schema, new ArrayList<MSSBamTenantBuff>()); } return (cursor); } public void closeTenantCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamTenantBuff nextTenantCursor(MSSBamCursor Cursor) { MSSBamRamTenantCursor cursor = (MSSBamRamTenantCursor) Cursor; MSSBamTenantBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamTenantBuff prevTenantCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public MSSBamTenantBuff firstTenantCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamTenantBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } public MSSBamTenantBuff lastTenantCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastTenantCursor"); } public MSSBamTenantBuff nthTenantCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamTenantBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextTenantCursor(Cursor); } return (rec); } }