Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for RealProject. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 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.cfasterisk.v2_4.CFAsteriskRam; import java.sql.*; import java.util.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cflib.v2_3.CFLib.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurity.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternet.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsterisk.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; import net.sourceforge.msscodefactory.cfsecurity.v2_4.CFSecurityObj.*; import net.sourceforge.msscodefactory.cfinternet.v2_4.CFInternetObj.*; import net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskObj.*; /* * CFAsteriskRamRealProjectTable in-memory RAM DbIO implementation * for RealProject. */ public class CFAsteriskRamRealProjectTable implements ICFAsteriskRealProjectTable { private ICFAsteriskSchema schema; private Map<CFInternetDomainBasePKey, CFInternetRealProjectBuff> dictByPKey = new HashMap<CFInternetDomainBasePKey, CFInternetRealProjectBuff>(); public CFAsteriskRamRealProjectTable(ICFAsteriskSchema 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 = "CFAsteriskRamRealProject.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 = "CFAsteriskRamRealProject.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 = "CFAsteriskRamRealProject.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 = "CFAsteriskRamDomainBase.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 = "CFAsteriskRamDomainBase.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 = "CFAsteriskRamRealProject.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 = "CFAsteriskRamRealProject.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 = "CFAsteriskRamDomainBase.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 = "CFAsteriskRamDomainBase.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 = "CFAsteriskRamRealProjectTable.deleteRealProject() "; 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); } // 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 schema.getTableMajorVersion().deleteMajorVersionByPPrjIdx(Authorization, Buff.getRequiredTenantId(), Buff.getRequiredId()); 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"; 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(); 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; 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(); 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 CFAsteriskRamRealProjectCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeRealProjectCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetRealProjectBuff nextRealProjectCursor(CFSecurityCursor Cursor) { CFAsteriskRamRealProjectCursor cursor = (CFAsteriskRamRealProjectCursor) 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() { } }