Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for ClearDep. /* * MSS Code Factory 1.11 * * Copyright (c) 2012-2014 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/>. * * *********************************************************************** * * $Revision$ */ package net.sourceforge.msscodefactory.v1_11.MSSBamRam; 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.v1_11.MSSBam.*; import net.sourceforge.msscodefactory.v1_11.MSSBamBL.*; import net.sourceforge.msscodefactory.v1_11.MSSBamBLRam.*; /* * MSSBamRamClearDepTable in-memory RAM DbIO implementation * for ClearDep. */ public class MSSBamRamClearDepTable implements IMSSBamClearDepTable { private MSSBamBLRamSchema schema; private Map<MSSBamAnyObjPKey, MSSBamClearDepBuff> dictByPKey = new HashMap<MSSBamAnyObjPKey, MSSBamClearDepBuff>(); private SortedMap<MSSBamClearDepByTableIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff>> dictByTableIdx = new TreeMap<MSSBamClearDepByTableIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff>>(); public MSSBamRamClearDepTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createClearDep(MSSBamAuthorization Authorization, MSSBamClearDepBuff Buff) { MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredId(Buff.getRequiredId()); MSSBamClearDepByTableIdxKey keyTableIdx = schema.getFactoryClearDep().newTableIdxKey(); keyTableIdx.setRequiredRelationId(Buff.getRequiredRelationId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createClearDep", pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableScope().readDerivedByIdIdx(Authorization, Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createClearDep", "Superclass", "SuperClass", "Scope", null); } } } { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization, Buff.getRequiredRelationId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createClearDep", "Lookup", "Relation", "Relation", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff> subdictTableIdx; if (dictByTableIdx.containsKey(keyTableIdx)) { subdictTableIdx = dictByTableIdx.get(keyTableIdx); } else { subdictTableIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamClearDepBuff>(); dictByTableIdx.put(keyTableIdx, subdictTableIdx); } subdictTableIdx.put(pkey, Buff); } public MSSBamClearDepBuff readDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamClearDep.readDerived() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamClearDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamClearDepBuff lockDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamClearDep.readDerived() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamClearDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamClearDepBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamClearDep.readAllDerived() "; MSSBamClearDepBuff[] retList = new MSSBamClearDepBuff[dictByPKey.values().size()]; Iterator<MSSBamClearDepBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamClearDepBuff[] readDerivedByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByTenantIdx() "; MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByTenantIdx(Authorization, TenantId); if (buffList == null) { return (null); } else { MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamClearDepBuff)) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } } public MSSBamClearDepBuff[] readDerivedByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByScopeIdx() "; MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByScopeIdx(Authorization, ScopeId); if (buffList == null) { return (null); } else { MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamClearDepBuff)) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } } public MSSBamClearDepBuff[] readDerivedByDefSchemaIdx(MSSBamAuthorization Authorization, Long DefSchemaId) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByDefSchemaIdx() "; MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByDefSchemaIdx(Authorization, DefSchemaId); if (buffList == null) { return (null); } else { MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamClearDepBuff)) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } } public MSSBamClearDepBuff[] readDerivedByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByAuthorIdx() "; MSSBamAnyObjBuff buffList[] = schema.getTableAnyObj().readDerivedByAuthorIdx(Authorization, AuthorId); if (buffList == null) { return (null); } else { MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamClearDepBuff)) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } } public MSSBamClearDepBuff readDerivedByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByUNameIdx() "; MSSBamAnyObjBuff buff = schema.getTableAnyObj().readDerivedByUNameIdx(Authorization, ScopeId, Name); if (buff == null) { return (null); } else if (buff instanceof MSSBamClearDepBuff) { return ((MSSBamClearDepBuff) buff); } else { return (null); } } public MSSBamClearDepBuff[] readDerivedByTableIdx(MSSBamAuthorization Authorization, long RelationId) { final String S_ProcName = "MSSBamRamClearDep.readDerivedByTableIdx() "; MSSBamClearDepByTableIdxKey key = schema.getFactoryClearDep().newTableIdxKey(); key.setRequiredRelationId(RelationId); MSSBamClearDepBuff[] recArray; if (dictByTableIdx.containsKey(key)) { SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff> subdictTableIdx = dictByTableIdx.get(key); recArray = new MSSBamClearDepBuff[subdictTableIdx.size()]; Iterator<MSSBamClearDepBuff> iter = subdictTableIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamClearDepBuff[0]; } return (recArray); } public MSSBamClearDepBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByIdIdx() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(Id); MSSBamClearDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamClearDepBuff readBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamClearDep.readBuff() "; MSSBamClearDepBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("CLRD"))) { buff = null; } return (buff); } public MSSBamClearDepBuff lockBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamClearDep.readBuff() "; MSSBamClearDepBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("CLRD"))) { buff = null; } return (buff); } public MSSBamClearDepBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamClearDep.readAllBuff() "; MSSBamClearDepBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamClearDepBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("CLRD")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public MSSBamClearDepBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByIdIdx() "; MSSBamAnyObjBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ANYO")) { return ((MSSBamClearDepBuff) buff); } else { return (null); } } public MSSBamClearDepBuff[] readBuffByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByTenantIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamAnyObjBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ANYO")) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public MSSBamClearDepBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByScopeIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamAnyObjBuff[] buffList = readDerivedByScopeIdx(Authorization, ScopeId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ANYO")) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public MSSBamClearDepBuff[] readBuffByDefSchemaIdx(MSSBamAuthorization Authorization, Long DefSchemaId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByDefSchemaIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamAnyObjBuff[] buffList = readDerivedByDefSchemaIdx(Authorization, DefSchemaId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ANYO")) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public MSSBamClearDepBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByAuthorIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamAnyObjBuff[] buffList = readDerivedByAuthorIdx(Authorization, AuthorId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ANYO")) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public MSSBamClearDepBuff readBuffByUNameIdx(MSSBamAuthorization Authorization, Long ScopeId, String Name) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByUNameIdx() "; MSSBamAnyObjBuff buff = readDerivedByUNameIdx(Authorization, ScopeId, Name); if ((buff != null) && buff.getClassCode().equals("ANYO")) { return ((MSSBamClearDepBuff) buff); } else { return (null); } } public MSSBamClearDepBuff[] readBuffByTableIdx(MSSBamAuthorization Authorization, long RelationId) { final String S_ProcName = "MSSBamRamClearDep.readBuffByTableIdx() "; MSSBamClearDepBuff buff; ArrayList<MSSBamClearDepBuff> filteredList = new ArrayList<MSSBamClearDepBuff>(); MSSBamClearDepBuff[] buffList = readDerivedByTableIdx(Authorization, RelationId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("CLRD")) { filteredList.add((MSSBamClearDepBuff) buff); } } return (filteredList.toArray(new MSSBamClearDepBuff[0])); } public void updateClearDep(MSSBamAuthorization Authorization, MSSBamClearDepBuff Buff) { MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); MSSBamClearDepBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateClearDep", "Existing record not found", "ClearDep", pkey); } MSSBamClearDepByTableIdxKey existingKeyTableIdx = schema.getFactoryClearDep().newTableIdxKey(); existingKeyTableIdx.setRequiredRelationId(existing.getRequiredRelationId()); MSSBamClearDepByTableIdxKey newKeyTableIdx = schema.getFactoryClearDep().newTableIdxKey(); newKeyTableIdx.setRequiredRelationId(Buff.getRequiredRelationId()); // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableScope().readDerivedByIdIdx(Authorization, Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateClearDep", "Superclass", "SuperClass", "Scope", null); } } } { boolean allNull = true; if (allNull) { if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization, Buff.getRequiredRelationId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateClearDep", "Lookup", "Relation", "Relation", null); } } } // Update is valid SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByTableIdx.get(existingKeyTableIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByTableIdx.containsKey(newKeyTableIdx)) { subdict = dictByTableIdx.get(newKeyTableIdx); } else { subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamClearDepBuff>(); dictByTableIdx.put(newKeyTableIdx, subdict); } subdict.put(pkey, Buff); } public void deleteClearDep(MSSBamAuthorization Authorization, MSSBamClearDepBuff Buff) { final String S_ProcName = "MSSBamRamClearDepTable.deleteClearDep() "; MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setRequiredId(schema.nextAnyObjIdGen()); MSSBamClearDepBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteClearDep", "Existing record not found", "ClearDep", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteClearDep", pkey); } MSSBamClearDepByTableIdxKey keyTableIdx = schema.getFactoryClearDep().newTableIdxKey(); keyTableIdx.setRequiredRelationId(existing.getRequiredRelationId()); // Validate reverse foreign keys if (schema.getTableClearTopDep().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteClearDep", "Superclass", "SuperClass", "ClearTopDep", pkey); } if (schema.getTableClearSubDep1().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteClearDep", "Superclass", "SuperClass", "ClearSubDep1", pkey); } if (schema.getTableClearSubDep2().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteClearDep", "Superclass", "SuperClass", "ClearSubDep2", pkey); } if (schema.getTableClearSubDep3().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteClearDep", "Superclass", "SuperClass", "ClearSubDep3", pkey); } // Delete is valid SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTableIdx.get(keyTableIdx); subdict.remove(pkey); } public MSSBamCursor openClearDepCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamClearDepCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openClearDepCursorByTableIdx(MSSBamAuthorization Authorization, long RelationId) { MSSBamCursor cursor; MSSBamClearDepByTableIdxKey key = schema.getFactoryClearDep().newTableIdxKey(); key.setRequiredRelationId(RelationId); if (dictByTableIdx.containsKey(key)) { SortedMap<MSSBamAnyObjPKey, MSSBamClearDepBuff> subdictTableIdx = dictByTableIdx.get(key); cursor = new MSSBamRamClearDepCursor(Authorization, schema, subdictTableIdx.values()); } else { cursor = new MSSBamRamClearDepCursor(Authorization, schema, new ArrayList<MSSBamClearDepBuff>()); } return (cursor); } public void closeClearDepCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamClearDepBuff nextClearDepCursor(MSSBamCursor Cursor) { MSSBamRamClearDepCursor cursor = (MSSBamRamClearDepCursor) Cursor; MSSBamClearDepBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamClearDepBuff prevClearDepCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamClearDepBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClearDepCursor(Cursor); } return (rec); } public MSSBamClearDepBuff firstClearDepCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamClearDepBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClearDepCursor(Cursor); } return (rec); } public MSSBamClearDepBuff lastClearDepCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastClearDepCursor"); } public MSSBamClearDepBuff nthClearDepCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamClearDepBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextClearDepCursor(Cursor); } return (rec); } }