Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for Version. /* * MSS Code Factory Accounting Business Application Model * * Copyright (c) 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.cfacc.v2_0.CFAccRam; 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.cfacc.v2_0.CFAcc.*; import net.sourceforge.msscodefactory.cfacc.v2_0.CFAccRam.*; /* * CFAccRamVersionTable in-memory RAM DbIO implementation * for Version. */ public class CFAccRamVersionTable implements ICFAccVersionTable { private CFAccRamSchema schema; private Map<CFAccDomainBasePKey, CFAccVersionBuff> dictByPKey = new HashMap<CFAccDomainBasePKey, CFAccVersionBuff>(); public CFAccRamVersionTable(CFAccRamSchema argSchema) { schema = argSchema; } public void createVersion(CFAccAuthorization Authorization, CFAccVersionBuff Buff) { schema.getTableProjectBase().createProjectBase(Authorization, Buff); CFAccDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createVersion", pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; allNull = false; if (!allNull) { if (null == schema.getTableProjectBase().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createVersion", "Superclass", "SuperClass", "ProjectBase", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); } public CFAccVersionBuff readDerived(CFAccAuthorization Authorization, CFAccDomainBasePKey PKey) { final String S_ProcName = "CFAccRamVersion.readDerived() "; CFAccDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFAccVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccVersionBuff lockDerived(CFAccAuthorization Authorization, CFAccDomainBasePKey PKey) { final String S_ProcName = "CFAccRamVersion.readDerived() "; CFAccDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFAccVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccVersionBuff[] readAllDerived(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamVersion.readAllDerived() "; CFAccVersionBuff[] retList = new CFAccVersionBuff[dictByPKey.values().size()]; Iterator<CFAccVersionBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAccVersionBuff[] readDerivedByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamDomainBase.readDerivedByTenantIdx() "; CFAccDomainBaseBuff buffList[] = schema.getTableDomainBase().readDerivedByTenantIdx(Authorization, TenantId); if (buffList == null) { return (null); } else { CFAccDomainBaseBuff buff; ArrayList<CFAccVersionBuff> filteredList = new ArrayList<CFAccVersionBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFAccVersionBuff)) { filteredList.add((CFAccVersionBuff) buff); } } return (filteredList.toArray(new CFAccVersionBuff[0])); } } public CFAccVersionBuff readDerivedByIdIdx(CFAccAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAccRamDomainBase.readDerivedByIdIdx() "; CFAccDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFAccVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAccVersionBuff readBuff(CFAccAuthorization Authorization, CFAccDomainBasePKey PKey) { final String S_ProcName = "CFAccRamVersion.readBuff() "; CFAccVersionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("VERN"))) { buff = null; } return (buff); } public CFAccVersionBuff lockBuff(CFAccAuthorization Authorization, CFAccDomainBasePKey PKey) { final String S_ProcName = "CFAccRamVersion.readBuff() "; CFAccVersionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("VERN"))) { buff = null; } return (buff); } public CFAccVersionBuff[] readAllBuff(CFAccAuthorization Authorization) { final String S_ProcName = "CFAccRamVersion.readAllBuff() "; CFAccVersionBuff buff; ArrayList<CFAccVersionBuff> filteredList = new ArrayList<CFAccVersionBuff>(); CFAccVersionBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("VERN")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAccVersionBuff[0])); } public CFAccVersionBuff readBuffByIdIdx(CFAccAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAccRamDomainBase.readBuffByIdIdx() "; CFAccDomainBaseBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFAccVersionBuff) buff); } else { return (null); } } public CFAccVersionBuff[] readBuffByTenantIdx(CFAccAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAccRamDomainBase.readBuffByTenantIdx() "; CFAccDomainBaseBuff buff; ArrayList<CFAccVersionBuff> filteredList = new ArrayList<CFAccVersionBuff>(); CFAccDomainBaseBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFAccVersionBuff) buff); } } return (filteredList.toArray(new CFAccVersionBuff[0])); } public void updateVersion(CFAccAuthorization Authorization, CFAccVersionBuff Buff) { schema.getTableProjectBase().updateProjectBase(Authorization, Buff); CFAccDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFAccVersionBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateVersion", "Existing record not found", "Version", pkey); } // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableProjectBase().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateVersion", "Superclass", "SuperClass", "ProjectBase", null); } } } // Update is valid SortedMap<CFAccDomainBasePKey, CFAccVersionBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); } public void deleteVersion(CFAccAuthorization Authorization, CFAccVersionBuff Buff) { final String S_ProcName = "CFAccRamVersionTable.deleteVersion() "; CFAccDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFAccVersionBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteVersion", pkey); } // Validate reverse foreign keys if (schema.getTableMajorVersion().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteVersion", "Superclass", "SuperClass", "MajorVersion", pkey); } if (schema.getTableMinorVersion().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteVersion", "Superclass", "SuperClass", "MinorVersion", pkey); } // Delete is valid SortedMap<CFAccDomainBasePKey, CFAccVersionBuff> subdict; dictByPKey.remove(pkey); schema.getTableProjectBase().deleteProjectBase(Authorization, Buff); } public void deleteVersionByIdIdx(CFAccAuthorization Authorization, long argTenantId, long argId) { CFAccDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteVersionByIdIdx(Authorization, key); } public void deleteVersionByIdIdx(CFAccAuthorization Authorization, CFAccDomainBasePKey argKey) { final String S_ProcName = "deleteVersionByIdIdx"; CFAccVersionBuff cur; LinkedList<CFAccVersionBuff> matchSet = new LinkedList<CFAccVersionBuff>(); Iterator<CFAccVersionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccVersionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); String subClassCode = cur.getClassCode(); if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFAccMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFAccMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of Version must not be \"" + subClassCode + "\""); } } } public void deleteVersionByTenantIdx(CFAccAuthorization Authorization, long argTenantId) { CFAccDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteVersionByTenantIdx(Authorization, key); } public void deleteVersionByTenantIdx(CFAccAuthorization Authorization, CFAccDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteVersionByTenantIdx"; CFAccVersionBuff cur; LinkedList<CFAccVersionBuff> matchSet = new LinkedList<CFAccVersionBuff>(); Iterator<CFAccVersionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAccVersionBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); String subClassCode = cur.getClassCode(); if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFAccMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFAccMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of Version must not be \"" + subClassCode + "\""); } } } public CFAccCursor openVersionCursorAll(CFAccAuthorization Authorization) { CFAccCursor cursor = new CFAccRamVersionCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeVersionCursor(CFAccCursor Cursor) { // Cursor.DataReader.Close(); } public CFAccVersionBuff nextVersionCursor(CFAccCursor Cursor) { CFAccRamVersionCursor cursor = (CFAccRamVersionCursor) Cursor; CFAccVersionBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAccVersionBuff prevVersionCursor(CFAccCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAccVersionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public CFAccVersionBuff firstVersionCursor(CFAccCursor Cursor) { int targetRowIdx = 1; CFAccVersionBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public CFAccVersionBuff lastVersionCursor(CFAccCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastVersionCursor"); } public CFAccVersionBuff nthVersionCursor(CFAccCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAccVersionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }