Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for DomainBase. /* * 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.*; /* * CFBamRamDomainBaseTable in-memory RAM DbIO implementation * for DomainBase. */ public class CFBamRamDomainBaseTable implements ICFBamDomainBaseTable { private ICFBamSchema schema; private Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> dictByPKey = new HashMap<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>(); private Map<CFInternetDomainBaseByTenantIdxKey, Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>> dictByTenantIdx = new HashMap<CFInternetDomainBaseByTenantIdxKey, Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>>(); public CFBamRamDomainBaseTable(ICFBamSchema argSchema) { schema = argSchema; } public void createDomainBase(CFSecurityAuthorization Authorization, CFInternetDomainBaseBuff Buff) { final String S_ProcName = "createDomainBase"; CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(((CFBamRamTenantTable) schema.getTableTenant()).nextDomainIdGen(Authorization, Buff.getRequiredTenantId())); Buff.setRequiredTenantId(pkey.getRequiredTenantId()); Buff.setRequiredId(pkey.getRequiredId()); CFInternetDomainBaseByTenantIdxKey keyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), S_ProcName, pkey); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Owner", "Tenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new HashMap<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); } public CFInternetDomainBaseBuff readDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamDomainBase.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetDomainBaseBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamDomainBase.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetDomainBaseBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamDomainBase.readAllDerived"; CFInternetDomainBaseBuff[] retList = new CFInternetDomainBaseBuff[dictByPKey.values().size()]; Iterator<CFInternetDomainBaseBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetDomainBaseBuff[] readDerivedByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamDomainBase.readDerivedByTenantIdx"; CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(TenantId); CFInternetDomainBaseBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new CFInternetDomainBaseBuff[subdictTenantIdx.size()]; Iterator<CFInternetDomainBaseBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> subdictTenantIdx = new HashMap<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>(); dictByTenantIdx.put(key, subdictTenantIdx); recArray = new CFInternetDomainBaseBuff[0]; } return (recArray); } public CFInternetDomainBaseBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readDerivedByIdIdx() "; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFInternetDomainBaseBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetDomainBaseBuff readBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFBamRamDomainBase.readBuff"; CFInternetDomainBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BDOM"))) { buff = null; } return (buff); } public CFInternetDomainBaseBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "lockBuff"; CFInternetDomainBaseBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("BDOM"))) { buff = null; } return (buff); } public CFInternetDomainBaseBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFBamRamDomainBase.readAllBuff"; CFInternetDomainBaseBuff buff; ArrayList<CFInternetDomainBaseBuff> filteredList = new ArrayList<CFInternetDomainBaseBuff>(); CFInternetDomainBaseBuff[] 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 CFInternetDomainBaseBuff[0])); } public CFInternetDomainBaseBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFBamRamDomainBase.readBuffByIdIdx() "; CFInternetDomainBaseBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFInternetDomainBaseBuff) buff); } else { return (null); } } public CFInternetDomainBaseBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFBamRamDomainBase.readBuffByTenantIdx() "; CFInternetDomainBaseBuff buff; ArrayList<CFInternetDomainBaseBuff> filteredList = new ArrayList<CFInternetDomainBaseBuff>(); CFInternetDomainBaseBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFInternetDomainBaseBuff) buff); } } return (filteredList.toArray(new CFInternetDomainBaseBuff[0])); } public void updateDomainBase(CFSecurityAuthorization Authorization, CFInternetDomainBaseBuff Buff) { CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetDomainBaseBuff 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); CFInternetDomainBaseByTenantIdxKey existingKeyTenantIdx = schema.getFactoryDomainBase().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); CFInternetDomainBaseByTenantIdxKey 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 Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> 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 HashMap<CFInternetDomainBasePKey, CFInternetDomainBaseBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); } public void deleteDomainBase(CFSecurityAuthorization Authorization, CFInternetDomainBaseBuff Buff) { final String S_ProcName = "CFBamRamDomainBaseTable.deleteDomainBase() "; String classCode; CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setClassCode(Buff.getClassCode()); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetDomainBaseBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteDomainBase", pkey); } schema.getTableSchemaDef().deleteSchemaDefByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); schema.getTableLicense().deleteLicenseByDomainIdx(Authorization, existing.getRequiredTenantId(), existing.getRequiredId()); CFInternetDomainBaseByTenantIdxKey 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 Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); } public void deleteDomainBaseByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argId) { CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteDomainBaseByIdIdx(Authorization, key); } public void deleteDomainBaseByIdIdx(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey argKey) { final String S_ProcName = "deleteDomainBaseByIdIdx"; boolean anyNotNull = false; anyNotNull = true; anyNotNull = true; if (!anyNotNull) { return; } CFInternetDomainBaseBuff cur; LinkedList<CFInternetDomainBaseBuff> matchSet = new LinkedList<CFInternetDomainBaseBuff>(); Iterator<CFInternetDomainBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetDomainBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableDomainBase().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("BDOM".equals(subClassCode)) { schema.getTableDomainBase().deleteDomainBase(Authorization, cur); } else if ("TDOM".equals(subClassCode)) { schema.getTableTopDomain().deleteTopDomain(Authorization, (CFInternetTopDomainBuff) cur); } else if ("DOMN".equals(subClassCode)) { schema.getTableDomain().deleteDomain(Authorization, (CFInternetDomainBuff) cur); } else if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, (CFInternetProjectBaseBuff) cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFInternetRealProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) 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 DomainBase must not be \"" + subClassCode + "\""); } } } public void deleteDomainBaseByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteDomainBaseByTenantIdx(Authorization, key); } public void deleteDomainBaseByTenantIdx(CFSecurityAuthorization Authorization, CFInternetDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteDomainBaseByTenantIdx"; CFInternetDomainBaseBuff cur; boolean anyNotNull = false; anyNotNull = true; if (!anyNotNull) { return; } LinkedList<CFInternetDomainBaseBuff> matchSet = new LinkedList<CFInternetDomainBaseBuff>(); Iterator<CFInternetDomainBaseBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetDomainBaseBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); cur = schema.getTableDomainBase().readDerivedByIdIdx(Authorization, cur.getRequiredTenantId(), cur.getRequiredId()); String subClassCode = cur.getClassCode(); if ("BDOM".equals(subClassCode)) { schema.getTableDomainBase().deleteDomainBase(Authorization, cur); } else if ("TDOM".equals(subClassCode)) { schema.getTableTopDomain().deleteTopDomain(Authorization, (CFInternetTopDomainBuff) cur); } else if ("DOMN".equals(subClassCode)) { schema.getTableDomain().deleteDomain(Authorization, (CFInternetDomainBuff) cur); } else if ("BPRJ".equals(subClassCode)) { schema.getTableProjectBase().deleteProjectBase(Authorization, (CFInternetProjectBaseBuff) cur); } else if ("RPRJ".equals(subClassCode)) { schema.getTableRealProject().deleteRealProject(Authorization, (CFInternetRealProjectBuff) cur); } else if ("TPRJ".equals(subClassCode)) { schema.getTableTopProject().deleteTopProject(Authorization, (CFInternetTopProjectBuff) cur); } else if ("SPRJ".equals(subClassCode)) { schema.getTableSubProject().deleteSubProject(Authorization, (CFInternetSubProjectBuff) 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 DomainBase must not be \"" + subClassCode + "\""); } } } public CFSecurityCursor openDomainBaseCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFBamRamDomainBaseCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFSecurityCursor openDomainBaseCursorByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { CFSecurityCursor cursor; CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { Map<CFInternetDomainBasePKey, CFInternetDomainBaseBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new CFBamRamDomainBaseCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new CFBamRamDomainBaseCursor(Authorization, schema, new ArrayList<CFInternetDomainBaseBuff>()); } return (cursor); } public void closeDomainBaseCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetDomainBaseBuff nextDomainBaseCursor(CFSecurityCursor Cursor) { CFBamRamDomainBaseCursor cursor = (CFBamRamDomainBaseCursor) Cursor; CFInternetDomainBaseBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetDomainBaseBuff prevDomainBaseCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetDomainBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public CFInternetDomainBaseBuff firstDomainBaseCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetDomainBaseBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public CFInternetDomainBaseBuff lastDomainBaseCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastDomainBaseCursor"); } public CFInternetDomainBaseBuff nthDomainBaseCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetDomainBaseBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextDomainBaseCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }