Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for PopDep. /* * 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.*; /* * MSSBamRamPopDepTable in-memory RAM DbIO implementation * for PopDep. */ public class MSSBamRamPopDepTable implements IMSSBamPopDepTable { private MSSBamBLRamSchema schema; private Map<MSSBamAnyObjPKey, MSSBamPopDepBuff> dictByPKey = new HashMap<MSSBamAnyObjPKey, MSSBamPopDepBuff>(); private SortedMap<MSSBamPopDepByRelationIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff>> dictByRelationIdx = new TreeMap<MSSBamPopDepByRelationIdxKey, SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff>>(); public MSSBamRamPopDepTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createPopDep(MSSBamAuthorization Authorization, MSSBamPopDepBuff Buff) { MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredId(Buff.getRequiredId()); MSSBamPopDepByRelationIdxKey keyRelationIdx = schema.getFactoryPopDep().newRelationIdxKey(); keyRelationIdx.setRequiredRelationId(Buff.getRequiredRelationId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createPopDep", pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableScope().readDerivedByIdIdx(Authorization, Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createPopDep", "Superclass", "SuperClass", "Scope", null); } } } { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization, Buff.getRequiredRelationId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createPopDep", "Lookup", "Relation", "Relation", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff> subdictRelationIdx; if (dictByRelationIdx.containsKey(keyRelationIdx)) { subdictRelationIdx = dictByRelationIdx.get(keyRelationIdx); } else { subdictRelationIdx = new TreeMap<MSSBamAnyObjPKey, MSSBamPopDepBuff>(); dictByRelationIdx.put(keyRelationIdx, subdictRelationIdx); } subdictRelationIdx.put(pkey, Buff); } public MSSBamPopDepBuff readDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamPopDep.readDerived() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamPopDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamPopDepBuff lockDerived(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamPopDep.readDerived() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(PKey.getRequiredId()); MSSBamPopDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamPopDepBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamPopDep.readAllDerived() "; MSSBamPopDepBuff[] retList = new MSSBamPopDepBuff[dictByPKey.values().size()]; Iterator<MSSBamPopDepBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamPopDepBuff[] 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<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamPopDepBuff)) { filteredList.add((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } } public MSSBamPopDepBuff[] 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<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamPopDepBuff)) { filteredList.add((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } } public MSSBamPopDepBuff[] 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<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamPopDepBuff)) { filteredList.add((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } } public MSSBamPopDepBuff[] 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<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof MSSBamPopDepBuff)) { filteredList.add((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } } public MSSBamPopDepBuff 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 MSSBamPopDepBuff) { return ((MSSBamPopDepBuff) buff); } else { return (null); } } public MSSBamPopDepBuff[] readDerivedByRelationIdx(MSSBamAuthorization Authorization, long RelationId) { final String S_ProcName = "MSSBamRamPopDep.readDerivedByRelationIdx() "; MSSBamPopDepByRelationIdxKey key = schema.getFactoryPopDep().newRelationIdxKey(); key.setRequiredRelationId(RelationId); MSSBamPopDepBuff[] recArray; if (dictByRelationIdx.containsKey(key)) { SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff> subdictRelationIdx = dictByRelationIdx.get(key); recArray = new MSSBamPopDepBuff[subdictRelationIdx.size()]; Iterator<MSSBamPopDepBuff> iter = subdictRelationIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamPopDepBuff[0]; } return (recArray); } public MSSBamPopDepBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamAnyObj.readDerivedByIdIdx() "; MSSBamAnyObjPKey key = schema.getFactoryAnyObj().newPKey(); key.setRequiredId(Id); MSSBamPopDepBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamPopDepBuff readBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamPopDep.readBuff() "; MSSBamPopDepBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("POPD"))) { buff = null; } return (buff); } public MSSBamPopDepBuff lockBuff(MSSBamAuthorization Authorization, MSSBamAnyObjPKey PKey) { final String S_ProcName = "MSSBamRamPopDep.readBuff() "; MSSBamPopDepBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("POPD"))) { buff = null; } return (buff); } public MSSBamPopDepBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamPopDep.readAllBuff() "; MSSBamPopDepBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); MSSBamPopDepBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("POPD")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public MSSBamPopDepBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long Id) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByIdIdx() "; MSSBamAnyObjBuff buff = readDerivedByIdIdx(Authorization, Id); if ((buff != null) && buff.getClassCode().equals("ANYO")) { return ((MSSBamPopDepBuff) buff); } else { return (null); } } public MSSBamPopDepBuff[] readBuffByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByTenantIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); 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((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public MSSBamPopDepBuff[] readBuffByScopeIdx(MSSBamAuthorization Authorization, Long ScopeId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByScopeIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); 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((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public MSSBamPopDepBuff[] readBuffByDefSchemaIdx(MSSBamAuthorization Authorization, Long DefSchemaId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByDefSchemaIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); 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((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public MSSBamPopDepBuff[] readBuffByAuthorIdx(MSSBamAuthorization Authorization, Long AuthorId) { final String S_ProcName = "MSSBamRamAnyObj.readBuffByAuthorIdx() "; MSSBamAnyObjBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); 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((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public MSSBamPopDepBuff 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 ((MSSBamPopDepBuff) buff); } else { return (null); } } public MSSBamPopDepBuff[] readBuffByRelationIdx(MSSBamAuthorization Authorization, long RelationId) { final String S_ProcName = "MSSBamRamPopDep.readBuffByRelationIdx() "; MSSBamPopDepBuff buff; ArrayList<MSSBamPopDepBuff> filteredList = new ArrayList<MSSBamPopDepBuff>(); MSSBamPopDepBuff[] buffList = readDerivedByRelationIdx(Authorization, RelationId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("POPD")) { filteredList.add((MSSBamPopDepBuff) buff); } } return (filteredList.toArray(new MSSBamPopDepBuff[0])); } public void updatePopDep(MSSBamAuthorization Authorization, MSSBamPopDepBuff Buff) { MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setRequiredId(Buff.getRequiredId()); MSSBamPopDepBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updatePopDep", "Existing record not found", "PopDep", pkey); } MSSBamPopDepByRelationIdxKey existingKeyRelationIdx = schema.getFactoryPopDep().newRelationIdxKey(); existingKeyRelationIdx.setRequiredRelationId(existing.getRequiredRelationId()); MSSBamPopDepByRelationIdxKey newKeyRelationIdx = schema.getFactoryPopDep().newRelationIdxKey(); newKeyRelationIdx.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(), "updatePopDep", "Superclass", "SuperClass", "Scope", null); } } } { boolean allNull = true; if (allNull) { if (null == schema.getTableRelation().readDerivedByIdIdx(Authorization, Buff.getRequiredRelationId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updatePopDep", "Lookup", "Relation", "Relation", null); } } } // Update is valid SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByRelationIdx.get(existingKeyRelationIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByRelationIdx.containsKey(newKeyRelationIdx)) { subdict = dictByRelationIdx.get(newKeyRelationIdx); } else { subdict = new TreeMap<MSSBamAnyObjPKey, MSSBamPopDepBuff>(); dictByRelationIdx.put(newKeyRelationIdx, subdict); } subdict.put(pkey, Buff); } public void deletePopDep(MSSBamAuthorization Authorization, MSSBamPopDepBuff Buff) { final String S_ProcName = "MSSBamRamPopDepTable.deletePopDep() "; MSSBamAnyObjPKey pkey = schema.getFactoryAnyObj().newPKey(); pkey.setRequiredId(schema.nextAnyObjIdGen()); MSSBamPopDepBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deletePopDep", "Existing record not found", "PopDep", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deletePopDep", pkey); } MSSBamPopDepByRelationIdxKey keyRelationIdx = schema.getFactoryPopDep().newRelationIdxKey(); keyRelationIdx.setRequiredRelationId(existing.getRequiredRelationId()); // Validate reverse foreign keys if (schema.getTablePopTopDep().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deletePopDep", "Superclass", "SuperClass", "PopTopDep", pkey); } if (schema.getTablePopSubDep1().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deletePopDep", "Superclass", "SuperClass", "PopSubDep1", pkey); } if (schema.getTablePopSubDep2().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deletePopDep", "Superclass", "SuperClass", "PopSubDep2", pkey); } if (schema.getTablePopSubDep3().readDerivedByIdIdx(Authorization, existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deletePopDep", "Superclass", "SuperClass", "PopSubDep3", pkey); } // Delete is valid SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff> subdict; dictByPKey.remove(pkey); subdict = dictByRelationIdx.get(keyRelationIdx); subdict.remove(pkey); } public MSSBamCursor openPopDepCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamPopDepCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openPopDepCursorByRelationIdx(MSSBamAuthorization Authorization, long RelationId) { MSSBamCursor cursor; MSSBamPopDepByRelationIdxKey key = schema.getFactoryPopDep().newRelationIdxKey(); key.setRequiredRelationId(RelationId); if (dictByRelationIdx.containsKey(key)) { SortedMap<MSSBamAnyObjPKey, MSSBamPopDepBuff> subdictRelationIdx = dictByRelationIdx.get(key); cursor = new MSSBamRamPopDepCursor(Authorization, schema, subdictRelationIdx.values()); } else { cursor = new MSSBamRamPopDepCursor(Authorization, schema, new ArrayList<MSSBamPopDepBuff>()); } return (cursor); } public void closePopDepCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamPopDepBuff nextPopDepCursor(MSSBamCursor Cursor) { MSSBamRamPopDepCursor cursor = (MSSBamRamPopDepCursor) Cursor; MSSBamPopDepBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamPopDepBuff prevPopDepCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamPopDepBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPopDepCursor(Cursor); } return (rec); } public MSSBamPopDepBuff firstPopDepCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamPopDepBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPopDepCursor(Cursor); } return (rec); } public MSSBamPopDepBuff lastPopDepCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastPopDepCursor"); } public MSSBamPopDepBuff nthPopDepCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamPopDepBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPopDepCursor(Cursor); } return (rec); } }