Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for RealProject. /* * CFBam * * Copyright (c) 2014-2016 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_7.CFBamRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_7.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternet.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBam.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_7.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_7.CFInternetObj.*; import net.sourceforge.msscodefactory.cfbam.v2_7.CFBamObj.*; /* * CFBamRamRealProjectTable in-memory RAM DbIO implementation * for RealProject. */ public class CFBamRamRealProjectTable implements ICFBamRealProjectTable { private ICFBamSchema schema; private Map<CFInternetDomainBasePKey, CFInternetRealProjectBuff> dictByPKey = new HashMap<CFInternetDomainBasePKey, CFInternetRealProjectBuff>(); public CFBamRamRealProjectTable(ICFBamSchema argSchema) { schema = argSchema; } public void createRealProject(CFSecurityAuthorization Authorization, CFInternetRealProjectBuff Buff) { final String S_ProcName = "createRealProject"; schema.getTableProjectBase().createProjectBase(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.getTableProjectBase().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Superclass", "SuperClass", "ProjectBase", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); } public CFInternetRealProjectBuff readDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamRealProject.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetRealProjectBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetRealProjectBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamRealProject.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetRealProjectBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetRealProjectBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamRealProject.readAllDerived"; CFInternetRealProjectBuff[] retList = new CFInternetRealProjectBuff[dictByPKey.values().size()]; Iterator<CFInternetRealProjectBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetRealProjectBuff[] 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<CFInternetRealProjectBuff> filteredList = new ArrayList<CFInternetRealProjectBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFInternetRealProjectBuff)) { filteredList.add((CFInternetRealProjectBuff) buff); } } return (filteredList.toArray(new CFInternetRealProjectBuff[0])); } } public CFInternetRealProjectBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readDerivedByIdIdx() "; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFInternetRealProjectBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetRealProjectBuff readBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamRealProject.readBuff"; CFInternetRealProjectBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("RPRJ"))) { buff = null; } return (buff); } public CFInternetRealProjectBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "lockBuff"; CFInternetRealProjectBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("RPRJ"))) { buff = null; } return (buff); } public CFInternetRealProjectBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamRealProject.readAllBuff"; CFInternetRealProjectBuff buff; ArrayList<CFInternetRealProjectBuff> filteredList = new ArrayList<CFInternetRealProjectBuff>(); CFInternetRealProjectBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("RPRJ")) { filteredList.add(buff); } } return (filteredList.toArray(new CFInternetRealProjectBuff[0])); } public CFInternetRealProjectBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readBuffByIdIdx() "; CFInternetRealProjectBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFInternetRealProjectBuff) buff); } else { return (null); } } public CFInternetRealProjectBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamDomainBase.readBuffByTenantIdx() "; CFInternetRealProjectBuff buff; ArrayList<CFInternetRealProjectBuff> filteredList = new ArrayList<CFInternetRealProjectBuff>(); CFInternetRealProjectBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFInternetRealProjectBuff) buff); } } return (filteredList.toArray(new CFInternetRealProjectBuff[0])); } public void updateRealProject(CFSecurityAuthorization Authorization, CFInternetRealProjectBuff Buff) { schema.getTableProjectBase().updateProjectBase(Authorization, Buff); CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetRealProjectBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateRealProject", "Existing record not found", "RealProject", 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(), "updateRealProject", "Superclass", "SuperClass", "ProjectBase", null); } } } // Update is valid Map<CFInternetDomainBasePKey, CFInternetRealProjectBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); } public void deleteRealProject(CFSecurityAuthorization Authorization, CFInternetRealProjectBuff Buff) { final String S_ProcName = "CFBamRamRealProjectTable.deleteRealProject() "; String classCode; CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetRealProjectBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteRealProject", pkey); } schema.getTableSchemaDef().deleteSchemaDefByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); schema.getTableLicense().deleteLicenseByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); schema.getTableSchemaDef().deleteSchemaDefByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); schema.getTableLicense().deleteLicenseByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); schema.getTableMajorVersion().deleteMajorVersionByPPrjIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); // Validate reverse foreign keys if (schema.getTableTopProject().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteRealProject", "Superclass", "SuperClass", "TopProject", pkey); } if (schema.getTableSubProject().readDerivedByIdIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()) != null) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteRealProject", "Superclass", "SuperClass", "SubProject", pkey); } // Delete is valid Map<CFInternetDomainBasePKey, CFInternetRealProjectBuff> subdict; dictByPKey.remove(pkey); schema.getTableProjectBase().deleteProjectBase(Authorization, Buff); } public void deleteRealProjectByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argId) { CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteRealProjectByIdIdx(Authorization, key); } public void deleteRealProjectByIdIdx(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey argKey) { final String S_ProcName = "deleteRealProjectByIdIdx"; boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } CFInternetRealProjectBuff cur; LinkedList<CFInternetRealProjectBuff> matchSet = new LinkedList<CFInternetRealProjectBuff>(); Iterator<CFInternetRealProjectBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetRealProjectBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableRealProject().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of RealProject must not be \"" + subClassCode + "\""); } } } public void deleteRealProjectByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteRealProjectByTenantIdx(Authorization, key); } public void deleteRealProjectByTenantIdx(CFSecurityAuthorization Authorization, CFInternetDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteRealProjectByTenantIdx"; CFInternetRealProjectBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFInternetRealProjectBuff> matchSet = new LinkedList<CFInternetRealProjectBuff>(); Iterator<CFInternetRealProjectBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetRealProjectBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableRealProject().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) cur); } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "subClassCode", cur, "Instance of or subclass of RealProject must not be \"" + subClassCode + "\""); } } } public CFSecurityCursor openRealProjectCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamRealProjectCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeRealProjectCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetRealProjectBuff nextRealProjectCursor(CFSecurityCursor Cursor) { CFBamRamRealProjectCursor cursor = (CFBamRamRealProjectCursor) Cursor; CFInternetRealProjectBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetRealProjectBuff prevRealProjectCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetRealProjectBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextRealProjectCursor(Cursor); } return (rec); } public CFInternetRealProjectBuff firstRealProjectCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetRealProjectBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextRealProjectCursor(Cursor); } return (rec); } public CFInternetRealProjectBuff lastRealProjectCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastRealProjectCursor"); } public CFInternetRealProjectBuff nthRealProjectCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetRealProjectBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextRealProjectCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }