Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for DataScope. /* * 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.*; /* * MSSBamRamDataScopeTable in-memory RAM DbIO implementation * for DataScope. */ public class MSSBamRamDataScopeTable implements IMSSBamDataScopeTable { private MSSBamBLRamSchema schema; private Map<MSSBamDataScopePKey, MSSBamDataScopeBuff> dictByPKey = new HashMap<MSSBamDataScopePKey, MSSBamDataScopeBuff>(); private SortedMap<MSSBamDataScopeByUNameIdxKey, SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff>> dictByUNameIdx = new TreeMap<MSSBamDataScopeByUNameIdxKey, SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff>>(); public MSSBamRamDataScopeTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createDataScope(MSSBamAuthorization Authorization, MSSBamDataScopeBuff Buff) { MSSBamDataScopePKey pkey = schema.getFactoryDataScope().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); Buff.setRequiredId(pkey.getRequiredId()); MSSBamDataScopeByUNameIdxKey keyUNameIdx = schema.getFactoryDataScope().newUNameIdxKey(); keyUNameIdx.setRequiredName(Buff.getRequiredName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createDataScope", pkey); } // Validate foreign keys // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff> subdictUNameIdx; if (dictByUNameIdx.containsKey(keyUNameIdx)) { subdictUNameIdx = dictByUNameIdx.get(keyUNameIdx); } else { subdictUNameIdx = new TreeMap<MSSBamDataScopePKey, MSSBamDataScopeBuff>(); dictByUNameIdx.put(keyUNameIdx, subdictUNameIdx); } subdictUNameIdx.put(pkey, Buff); } public MSSBamDataScopeBuff readDerived(MSSBamAuthorization Authorization, MSSBamDataScopePKey PKey) { final String S_ProcName = "MSSBamRamDataScope.readDerived() "; MSSBamDataScopePKey key = schema.getFactoryDataScope().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamDataScopeBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamDataScopeBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamDataScope.readAllDerived() "; MSSBamDataScopeBuff[] retList = new MSSBamDataScopeBuff[dictByPKey.values().size()]; Iterator<MSSBamDataScopeBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamDataScopeBuff[] readDerivedByUNameIdx(MSSBamAuthorization Authorization, String Name) { final String S_ProcName = "MSSBamRamDataScope.readDerivedByUNameIdx() "; MSSBamDataScopeByUNameIdxKey key = schema.getFactoryDataScope().newUNameIdxKey(); key.setRequiredName(Name); MSSBamDataScopeBuff[] recArray; if (dictByUNameIdx.containsKey(key)) { SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff> subdictUNameIdx = dictByUNameIdx.get(key); recArray = new MSSBamDataScopeBuff[subdictUNameIdx.size()]; Iterator<MSSBamDataScopeBuff> iter = subdictUNameIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamDataScopeBuff[0]; } return (recArray); } public MSSBamDataScopeBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, short Id) { final String S_ProcName = "MSSBamRamDataScope.readDerivedByIdIdx() "; MSSBamDataScopePKey key = schema.getFactoryDataScope().newPKey(); key.setRequiredId(Id); MSSBamDataScopeBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamDataScopeBuff readBuff(MSSBamAuthorization Authorization, MSSBamDataScopePKey PKey) { final String S_ProcName = "MSSBamRamDataScope.readBuff() "; MSSBamDataScopeBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("DSCP"))) { buff = null; } return (buff); } public MSSBamDataScopeBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamDataScope.readAllBuff() "; MSSBamDataScopeBuff buff; ArrayList<MSSBamDataScopeBuff> filteredList = new ArrayList<MSSBamDataScopeBuff>(); MSSBamDataScopeBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("DSCP")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamDataScopeBuff[0])); } public MSSBamDataScopeBuff readBuffByIdIdx(MSSBamAuthorization Authorization, short Id) { final String S_ProcName = "MSSBamRamDataScope.readBuffByIdIdx() "; MSSBamDataScopeBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("DSCP")) { return ((MSSBamDataScopeBuff) buff); } else { return (null); } } public MSSBamDataScopeBuff[] readBuffByUNameIdx(MSSBamAuthorization Authorization, String Name) { final String S_ProcName = "MSSBamRamDataScope.readBuffByUNameIdx() "; MSSBamDataScopeBuff buff; ArrayList<MSSBamDataScopeBuff> filteredList = new ArrayList<MSSBamDataScopeBuff>(); MSSBamDataScopeBuff[] buffList = readDerivedByUNameIdx(Authorization, Name); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("DSCP")) { filteredList.add((MSSBamDataScopeBuff) buff); } } return (filteredList.toArray(new MSSBamDataScopeBuff[0])); } public void updateDataScope(MSSBamAuthorization Authorization, MSSBamDataScopeBuff Buff) { MSSBamDataScopePKey pkey = schema.getFactoryDataScope().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); MSSBamDataScopeBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateDataScope", "Existing record not found", "DataScope", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateDataScope", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamDataScopeByUNameIdxKey existingKeyUNameIdx = schema.getFactoryDataScope().newUNameIdxKey(); existingKeyUNameIdx.setRequiredName(existing.getRequiredName()); MSSBamDataScopeByUNameIdxKey newKeyUNameIdx = schema.getFactoryDataScope().newUNameIdxKey(); newKeyUNameIdx.setRequiredName(Buff.getRequiredName()); // Check unique indexes // Validate foreign keys // Update is valid SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByUNameIdx.get(existingKeyUNameIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByUNameIdx.containsKey(newKeyUNameIdx)) { subdict = dictByUNameIdx.get(newKeyUNameIdx); } else { subdict = new TreeMap<MSSBamDataScopePKey, MSSBamDataScopeBuff>(); dictByUNameIdx.put(newKeyUNameIdx, subdict); } subdict.put(pkey, Buff); } public void deleteDataScope(MSSBamAuthorization Authorization, MSSBamDataScopeBuff Buff) { final String S_ProcName = "MSSBamRamDataScopeTable.deleteDataScope() "; MSSBamDataScopePKey pkey = schema.getFactoryDataScope().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); MSSBamDataScopeBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteDataScope", "Existing record not found", "DataScope", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteDataScope", pkey); } MSSBamDataScopeByUNameIdxKey keyUNameIdx = schema.getFactoryDataScope().newUNameIdxKey(); keyUNameIdx.setRequiredName(existing.getRequiredName()); // Validate reverse foreign keys // Delete is valid SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff> subdict; dictByPKey.remove(pkey); subdict = dictByUNameIdx.get(keyUNameIdx); subdict.remove(pkey); } public MSSBamCursor openDataScopeCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamDataScopeCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openDataScopeCursorByUNameIdx(MSSBamAuthorization Authorization, String Name) { MSSBamCursor cursor; MSSBamDataScopeByUNameIdxKey key = schema.getFactoryDataScope().newUNameIdxKey(); key.setRequiredName(Name); if (dictByUNameIdx.containsKey(key)) { SortedMap<MSSBamDataScopePKey, MSSBamDataScopeBuff> subdictUNameIdx = dictByUNameIdx.get(key); cursor = new MSSBamRamDataScopeCursor(Authorization, schema, subdictUNameIdx.values()); } else { cursor = new MSSBamRamDataScopeCursor(Authorization, schema, new ArrayList<MSSBamDataScopeBuff>()); } return (cursor); } public void closeDataScopeCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamDataScopeBuff nextDataScopeCursor(MSSBamCursor Cursor) { MSSBamRamDataScopeCursor cursor = (MSSBamRamDataScopeCursor) Cursor; MSSBamDataScopeBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamDataScopeBuff prevDataScopeCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamDataScopeBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDataScopeCursor(Cursor); } return (rec); } public MSSBamDataScopeBuff firstDataScopeCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamDataScopeBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDataScopeCursor(Cursor); } return (rec); } public MSSBamDataScopeBuff lastDataScopeCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastDataScopeCursor"); } public MSSBamDataScopeBuff nthDataScopeCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamDataScopeBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDataScopeCursor(Cursor); } return (rec); } }