Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for DomainBase. /* * CF Asterisk 11 Configuration Model * * Copyright (c) 2013-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/>. * * This source code incorporates modified modules originally licensed * under the Apache 2.0 license by MSS Code Factory including CFSecurity * (net-sourceforge-msscodefactory-2.0-cfsecurity.xml), * CFInternet (net-sourceforge-msscodefactory-2.0-cfinternet.xml), and * CFCrm 2.0 (net-sourceforge-msscodefactory-2.0-cfcrm.xml), with all of the * required models being available as part of the MSS Code Factory 1.11 * distribution source and install zips. * * You can download installations of MSS Code Factory 1.11 from * http://msscodefactory.sourceforge.net/ * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam; 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.cfasterisk.v2_0.CFAst.*; import net.sourceforge.msscodefactory.cfasterisk.v2_0.CFAstRam.*; /* * CFAstRamDomainBaseTable in-memory RAM DbIO implementation * for DomainBase. */ public class CFAstRamDomainBaseTable implements ICFAstDomainBaseTable { private CFAstRamSchema schema; private Map<CFAstDomainBasePKey, CFAstDomainBaseBuff> dictByPKey = new HashMap<CFAstDomainBasePKey, CFAstDomainBaseBuff>(); private SortedMap<CFAstDomainBaseByTenantIdxKey, SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff>> dictByTenantIdx = new TreeMap<CFAstDomainBaseByTenantIdxKey, SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff>>(); public CFAstRamDomainBaseTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createDomainBase(CFAstAuthorization Authorization, CFAstDomainBaseBuff Buff) { CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(((CFAstRamTenantTable) schema.getTableTenant()).nextDomainIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredId(pkey.getRequiredId()); CFAstDomainBaseByTenantIdxKey keyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createDomainBase", pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createDomainBase", "Owner", "Tenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<CFAstDomainBasePKey, CFAstDomainBaseBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); } public CFAstDomainBaseBuff readDerived(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) { final String S_ProcName = "CFAstRamDomainBase.readDerived() "; CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFAstDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstDomainBaseBuff lockDerived(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) { final String S_ProcName = "CFAstRamDomainBase.readDerived() "; CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFAstDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstDomainBaseBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamDomainBase.readAllDerived() "; CFAstDomainBaseBuff[] retList = new CFAstDomainBaseBuff[dictByPKey.values().size()]; Iterator<CFAstDomainBaseBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstDomainBaseBuff[] readDerivedByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamDomainBase.readDerivedByTenantIdx() "; CFAstDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFAstDomainBaseBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFAstDomainBaseBuff[subdictTenantIdx.size()]; Iterator<CFAstDomainBaseBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new CFAstDomainBaseBuff[0]; } return (recArray); } public CFAstDomainBaseBuff readDerivedByIdIdx(CFAstAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAstRamDomainBase.readDerivedByIdIdx() "; CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFAstDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstDomainBaseBuff readBuff(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) { final String S_ProcName = "CFAstRamDomainBase.readBuff() "; CFAstDomainBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BDOM"))) { buff = null; } return (buff); } public CFAstDomainBaseBuff lockBuff(CFAstAuthorization Authorization, CFAstDomainBasePKey PKey) { final String S_ProcName = "CFAstRamDomainBase.readBuff() "; CFAstDomainBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BDOM"))) { buff = null; } return (buff); } public CFAstDomainBaseBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamDomainBase.readAllBuff() "; CFAstDomainBaseBuff buff; ArrayList<CFAstDomainBaseBuff> filteredList = new ArrayList<CFAstDomainBaseBuff>(); CFAstDomainBaseBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstDomainBaseBuff[0])); } public CFAstDomainBaseBuff readBuffByIdIdx(CFAstAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAstRamDomainBase.readBuffByIdIdx() "; CFAstDomainBaseBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFAstDomainBaseBuff) buff); } else { return (null); } } public CFAstDomainBaseBuff[] readBuffByTenantIdx(CFAstAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAstRamDomainBase.readBuffByTenantIdx() "; CFAstDomainBaseBuff buff; ArrayList<CFAstDomainBaseBuff> filteredList = new ArrayList<CFAstDomainBaseBuff>(); CFAstDomainBaseBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFAstDomainBaseBuff) buff); } } return (filteredList.toArray(new CFAstDomainBaseBuff[0])); } public void updateDomainBase(CFAstAuthorization Authorization, CFAstDomainBaseBuff Buff) { CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFAstDomainBaseBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateDomainBase", "Existing record not found", "DomainBase", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateDomainBase", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstDomainBaseByTenantIdxKey existingKeyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFAstDomainBaseByTenantIdxKey newKeyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateDomainBase", "Owner", "Tenant", "Tenant", null); } } } // Update is valid SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByTenantIdx.get(existingKeyTenantIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByTenantIdx.containsKey(newKeyTenantIdx)) { subdict = dictByTenantIdx.get(newKeyTenantIdx); } else { subdict = new TreeMap<CFAstDomainBasePKey, CFAstDomainBaseBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); } public void deleteDomainBase(CFAstAuthorization Authorization, CFAstDomainBaseBuff Buff) { final String S_ProcName = "CFAstRamDomainBaseTable.deleteDomainBase() "; CFAstDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFAstDomainBaseBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteDomainBase", pkey); } CFAstDomainBaseByTenantIdxKey keyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); // Validate reverse foreign keys if (schema.getTableTopDomain().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteDomainBase", "Superclass", "SuperClass", "TopDomain", pkey); } if (schema.getTableDomain().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteDomainBase", "Superclass", "SuperClass", "Domain", pkey); } if (schema.getTableProjectBase().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteDomainBase", "Superclass", "SuperClass", "ProjectBase", pkey); } // Delete is valid SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); } public void deleteDomainBaseByIdIdx(CFAstAuthorization Authorization, long argTenantId, long argId) { CFAstDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteDomainBaseByIdIdx(Authorization, key); } public void deleteDomainBaseByIdIdx(CFAstAuthorization Authorization, CFAstDomainBasePKey argKey) { final String S_ProcName = "deleteDomainBaseByIdIdx"; CFAstDomainBaseBuff cur; LinkedList<CFAstDomainBaseBuff> matchSet = new LinkedList<CFAstDomainBaseBuff>(); Iterator<CFAstDomainBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstDomainBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); String subClassCode = cur.getClassCode(); if ("BDOM".equals(subClassCode)) { schema.getTableDomainBase().deleteDomainBase(Authorization, cur); } else if ("TDOM".equals(subClassCode)) { schema.getTableTopDomain().deleteTopDomain(Authorization, (CFAstTopDomainBuff) cur); } else if ("DOMN".equals(subClassCode)) { schema.getTableDomain().deleteDomain(Authorization, (CFAstDomainBuff) cur); } else if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, (CFAstProjectBaseBuff) cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFAstRealProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFAstTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFAstSubProjectBuff) cur); } else if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, (CFAstVersionBuff) cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFAstMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFAstMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of DomainBase must not be \"" + subClassCode + "\""); } } } public void deleteDomainBaseByTenantIdx(CFAstAuthorization Authorization, long argTenantId) { CFAstDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteDomainBaseByTenantIdx(Authorization, key); } public void deleteDomainBaseByTenantIdx(CFAstAuthorization Authorization, CFAstDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteDomainBaseByTenantIdx"; CFAstDomainBaseBuff cur; LinkedList<CFAstDomainBaseBuff> matchSet = new LinkedList<CFAstDomainBaseBuff>(); Iterator<CFAstDomainBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstDomainBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); String subClassCode = cur.getClassCode(); if ("BDOM".equals(subClassCode)) { schema.getTableDomainBase().deleteDomainBase(Authorization, cur); } else if ("TDOM".equals(subClassCode)) { schema.getTableTopDomain().deleteTopDomain(Authorization, (CFAstTopDomainBuff) cur); } else if ("DOMN".equals(subClassCode)) { schema.getTableDomain().deleteDomain(Authorization, (CFAstDomainBuff) cur); } else if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, (CFAstProjectBaseBuff) cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFAstRealProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFAstTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFAstSubProjectBuff) cur); } else if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, (CFAstVersionBuff) cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFAstMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFAstMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of DomainBase must not be \"" + subClassCode + "\""); } } } public CFAstCursor openDomainBaseCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamDomainBaseCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openDomainBaseCursorByTenantIdx(CFAstAuthorization Authorization, long TenantId) { CFAstCursor cursor; CFAstDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<CFAstDomainBasePKey, CFAstDomainBaseBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFAstRamDomainBaseCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFAstRamDomainBaseCursor(Authorization, schema, new ArrayList<CFAstDomainBaseBuff>()); } return (cursor); } public void closeDomainBaseCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstDomainBaseBuff nextDomainBaseCursor(CFAstCursor Cursor) { CFAstRamDomainBaseCursor cursor = (CFAstRamDomainBaseCursor) Cursor; CFAstDomainBaseBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstDomainBaseBuff prevDomainBaseCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstDomainBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public CFAstDomainBaseBuff firstDomainBaseCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstDomainBaseBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public CFAstDomainBaseBuff lastDomainBaseCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastDomainBaseCursor"); } public CFAstDomainBaseBuff nthDomainBaseCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstDomainBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }