Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for ProjectBase. /* * net.sourceforge.MSSCodeFactory.CFBam * * Copyright (c) 2014-2017 Mark Sobkow * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sourceforge.msscodefactory.cfbam.v2_9.CFBamRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_9.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_9.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_9.CFInternet.*; import net.sourceforge.msscodefactory.cfbam.v2_9.CFBam.*; import net.sourceforge.msscodefactory.cfbam.v2_9.CFBamObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_9.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_9.CFInternetObj.*; import net.sourceforge.msscodefactory.cfbam.v2_9.CFBamObj.*; /* * CFBamRamProjectBaseTable in-memory RAM DbIO implementation * for ProjectBase. */ public class CFBamRamProjectBaseTable implements ICFBamProjectBaseTable { private ICFBamSchema schema; private Map<CFInternetDomainBasePKey, CFInternetProjectBaseBuff> dictByPKey = new HashMap<CFInternetDomainBasePKey, CFInternetProjectBaseBuff>(); public CFBamRamProjectBaseTable(ICFBamSchema argSchema) { schema = argSchema; } public void createProjectBase(CFSecurityAuthorization Authorization, CFInternetProjectBaseBuff Buff) { final String S_ProcName = "createProjectBase"; schema.getTableDomainBase().createDomainBase(Authorization, Buff); CFInternetDomainBasePKey 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(), S_ProcName, pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; allNull = false; if (!allNull) { if (null == schema.getTableDomainBase().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Superclass", "SuperClass", "DomainBase", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); } public CFInternetProjectBaseBuff readDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamProjectBase.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetProjectBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetProjectBaseBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamProjectBase.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetProjectBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetProjectBaseBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamProjectBase.readAllDerived"; CFInternetProjectBaseBuff[] retList = new CFInternetProjectBaseBuff[dictByPKey.values().size()]; Iterator<CFInternetProjectBaseBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetProjectBaseBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamDomainBase.readDerivedByTenantIdx"; CFInternetDomainBaseBuff buffList[] = schema.getTableDomainBase().readDerivedByTenantIdx(Authorization, TenantId); if (buffList == null) { return (null); } else { CFInternetDomainBaseBuff buff; ArrayList<CFInternetProjectBaseBuff> filteredList = new ArrayList<CFInternetProjectBaseBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFInternetProjectBaseBuff)) { filteredList.add((CFInternetProjectBaseBuff) buff); } } return (filteredList.toArray(new CFInternetProjectBaseBuff[0])); } } public CFInternetProjectBaseBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readDerivedByIdIdx() "; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFInternetProjectBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetProjectBaseBuff readBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamProjectBase.readBuff"; CFInternetProjectBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BPRJ"))) { buff = null; } return (buff); } public CFInternetProjectBaseBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "lockBuff"; CFInternetProjectBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BPRJ"))) { buff = null; } return (buff); } public CFInternetProjectBaseBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamProjectBase.readAllBuff"; CFInternetProjectBaseBuff buff; ArrayList<CFInternetProjectBaseBuff> filteredList = new ArrayList<CFInternetProjectBaseBuff>(); CFInternetProjectBaseBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BPRJ")) { filteredList.add(buff); } } return (filteredList.toArray(new CFInternetProjectBaseBuff[0])); } public CFInternetProjectBaseBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readBuffByIdIdx() "; CFInternetProjectBaseBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFInternetProjectBaseBuff) buff); } else { return (null); } } public CFInternetProjectBaseBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamDomainBase.readBuffByTenantIdx() "; CFInternetProjectBaseBuff buff; ArrayList<CFInternetProjectBaseBuff> filteredList = new ArrayList<CFInternetProjectBaseBuff>(); CFInternetProjectBaseBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFInternetProjectBaseBuff) buff); } } return (filteredList.toArray(new CFInternetProjectBaseBuff[0])); } public void updateProjectBase(CFSecurityAuthorization Authorization, CFInternetProjectBaseBuff Buff) { schema.getTableDomainBase().updateDomainBase(Authorization, Buff); CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetProjectBaseBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateProjectBase", "Existing record not found", "ProjectBase", pkey); } // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableDomainBase().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateProjectBase", "Superclass", "SuperClass", "DomainBase", null); } } } // Update is valid Map<CFInternetDomainBasePKey, CFInternetProjectBaseBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); } public void deleteProjectBase(CFSecurityAuthorization Authorization, CFInternetProjectBaseBuff Buff) { final String S_ProcName = "CFBamRamProjectBaseTable.deleteProjectBase() "; String classCode; CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetProjectBaseBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteProjectBase", pkey); } // Short circuit self-referential code to prevent stack overflows Object arrCheckLicenses[] = schema.getTableLicense().readDerivedByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); if (arrCheckLicenses.length > 0) { schema.getTableLicense().deleteLicenseByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); } // Short circuit self-referential code to prevent stack overflows Object arrCheckSchemaDef[] = schema.getTableSchemaDef().readDerivedByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); if (arrCheckSchemaDef.length > 0) { schema.getTableSchemaDef().deleteSchemaDefByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); } // Validate reverse foreign keys if (schema.getTableRealProject().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteProjectBase", "Superclass", "SuperClass", "RealProject", pkey); } if (schema.getTableVersion().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteProjectBase", "Superclass", "SuperClass", "Version", pkey); } // Delete is valid Map<CFInternetDomainBasePKey, CFInternetProjectBaseBuff> subdict; dictByPKey.remove(pkey); schema.getTableDomainBase().deleteDomainBase(Authorization, Buff); } public void deleteProjectBaseByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argId) { CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteProjectBaseByIdIdx(Authorization, key); } public void deleteProjectBaseByIdIdx(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey argKey) { final String S_ProcName = "deleteProjectBaseByIdIdx"; boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } CFInternetProjectBaseBuff cur; LinkedList<CFInternetProjectBaseBuff> matchSet = new LinkedList<CFInternetProjectBaseBuff>(); Iterator<CFInternetProjectBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetProjectBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableProjectBase().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFInternetRealProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, (CFInternetVersionBuff) cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFInternetMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFInternetMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of ProjectBase must not be \"" + subClassCode + "\""); } } } public void deleteProjectBaseByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteProjectBaseByTenantIdx(Authorization, key); } public void deleteProjectBaseByTenantIdx(CFSecurityAuthorization Authorization, CFInternetDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteProjectBaseByTenantIdx"; CFInternetProjectBaseBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFInternetProjectBaseBuff> matchSet = new LinkedList<CFInternetProjectBaseBuff>(); Iterator<CFInternetProjectBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetProjectBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableProjectBase().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFInternetRealProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("VERN".equals(subClassCode)) { schema.getTableVersion().deleteVersion(Authorization, (CFInternetVersionBuff) cur); } else if ("MJVR".equals(subClassCode)) { schema.getTableMajorVersion().deleteMajorVersion(Authorization, (CFInternetMajorVersionBuff) cur); } else if ("MNVR".equals(subClassCode)) { schema.getTableMinorVersion().deleteMinorVersion(Authorization, (CFInternetMinorVersionBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of ProjectBase must not be \"" + subClassCode + "\""); } } } public CFSecurityCursor openProjectBaseCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamProjectBaseCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeProjectBaseCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetProjectBaseBuff nextProjectBaseCursor(CFSecurityCursor Cursor) { CFBamRamProjectBaseCursor cursor = (CFBamRamProjectBaseCursor) Cursor; CFInternetProjectBaseBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetProjectBaseBuff prevProjectBaseCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetProjectBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextProjectBaseCursor(Cursor); } return (rec); } public CFInternetProjectBaseBuff firstProjectBaseCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetProjectBaseBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextProjectBaseCursor(Cursor); } return (rec); } public CFInternetProjectBaseBuff lastProjectBaseCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastProjectBaseCursor"); } public CFInternetProjectBaseBuff nthProjectBaseCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetProjectBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextProjectBaseCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }