Java tutorial
// Description: Java 8 in-memory RAM DbIO implementation for Version. /* * 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.*; /* * CFAsteriskRamVersionTable in-memory RAM DbIO implementation * for Version. */ public class CFAsteriskRamVersionTable implements ICFAsteriskVersionTable { private ICFAsteriskSchema schema; private Map<CFInternetDomainBasePKey, CFInternetVersionBuff> dictByPKey = new HashMap<CFInternetDomainBasePKey, CFInternetVersionBuff>(); public CFAsteriskRamVersionTable(ICFAsteriskSchema argSchema) { schema = argSchema; } public void createVersion(CFSecurityAuthorization Authorization, CFInternetVersionBuff Buff) { final String S_ProcName = "createVersion"; 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 CFInternetVersionBuff readDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFAsteriskRamVersion.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetVersionBuff lockDerived(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFAsteriskRamVersion.readDerived"; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(PKey.getRequiredTenantId()); key.setRequiredId(PKey.getRequiredId()); CFInternetVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetVersionBuff[] readAllDerived(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamVersion.readAllDerived"; CFInternetVersionBuff[] retList = new CFInternetVersionBuff[dictByPKey.values().size()]; Iterator<CFInternetVersionBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFInternetVersionBuff[] 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<CFInternetVersionBuff> filteredList = new ArrayList<CFInternetVersionBuff>(); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && (buff instanceof CFInternetVersionBuff)) { filteredList.add((CFInternetVersionBuff) buff); } } return (filteredList.toArray(new CFInternetVersionBuff[0])); } } public CFInternetVersionBuff readDerivedByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAsteriskRamDomainBase.readDerivedByIdIdx() "; CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(TenantId); key.setRequiredId(Id); CFInternetVersionBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFInternetVersionBuff readBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "CFAsteriskRamVersion.readBuff"; CFInternetVersionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("VERN"))) { buff = null; } return (buff); } public CFInternetVersionBuff lockBuff(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey PKey) { final String S_ProcName = "lockBuff"; CFInternetVersionBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("VERN"))) { buff = null; } return (buff); } public CFInternetVersionBuff[] readAllBuff(CFSecurityAuthorization Authorization) { final String S_ProcName = "CFAsteriskRamVersion.readAllBuff"; CFInternetVersionBuff buff; ArrayList<CFInternetVersionBuff> filteredList = new ArrayList<CFInternetVersionBuff>(); CFInternetVersionBuff[] 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 CFInternetVersionBuff[0])); } public CFInternetVersionBuff readBuffByIdIdx(CFSecurityAuthorization Authorization, long TenantId, long Id) { final String S_ProcName = "CFAsteriskRamDomainBase.readBuffByIdIdx() "; CFInternetVersionBuff buff = readDerivedByIdIdx(Authorization, TenantId, Id); if ((buff != null) && buff.getClassCode().equals("BDOM")) { return ((CFInternetVersionBuff) buff); } else { return (null); } } public CFInternetVersionBuff[] readBuffByTenantIdx(CFSecurityAuthorization Authorization, long TenantId) { final String S_ProcName = "CFAsteriskRamDomainBase.readBuffByTenantIdx() "; CFInternetVersionBuff buff; ArrayList<CFInternetVersionBuff> filteredList = new ArrayList<CFInternetVersionBuff>(); CFInternetVersionBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("BDOM")) { filteredList.add((CFInternetVersionBuff) buff); } } return (filteredList.toArray(new CFInternetVersionBuff[0])); } public void updateVersion(CFSecurityAuthorization Authorization, CFInternetVersionBuff Buff) { schema.getTableProjectBase().updateProjectBase(Authorization, Buff); CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetVersionBuff 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 Map<CFInternetDomainBasePKey, CFInternetVersionBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); } public void deleteVersion(CFSecurityAuthorization Authorization, CFInternetVersionBuff Buff) { final String S_ProcName = "CFAsteriskRamVersionTable.deleteVersion() "; CFInternetDomainBasePKey pkey = schema.getFactoryDomainBase().newPKey(); pkey.setRequiredTenantId(Buff.getRequiredTenantId()); pkey.setRequiredId(Buff.getRequiredId()); CFInternetVersionBuff 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 Map<CFInternetDomainBasePKey, CFInternetVersionBuff> subdict; dictByPKey.remove(pkey); schema.getTableProjectBase().deleteProjectBase(Authorization, Buff); } public void deleteVersionByIdIdx(CFSecurityAuthorization Authorization, long argTenantId, long argId) { CFInternetDomainBasePKey key = schema.getFactoryDomainBase().newPKey(); key.setRequiredTenantId(argTenantId); key.setRequiredId(argId); deleteVersionByIdIdx(Authorization, key); } public void deleteVersionByIdIdx(CFSecurityAuthorization Authorization, CFInternetDomainBasePKey argKey) { final String S_ProcName = "deleteVersionByIdIdx"; CFInternetVersionBuff cur; LinkedList<CFInternetVersionBuff> matchSet = new LinkedList<CFInternetVersionBuff>(); Iterator<CFInternetVersionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetVersionBuff> 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, (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 Version must not be \"" + subClassCode + "\""); } } } public void deleteVersionByTenantIdx(CFSecurityAuthorization Authorization, long argTenantId) { CFInternetDomainBaseByTenantIdxKey key = schema.getFactoryDomainBase().newTenantIdxKey(); key.setRequiredTenantId(argTenantId); deleteVersionByTenantIdx(Authorization, key); } public void deleteVersionByTenantIdx(CFSecurityAuthorization Authorization, CFInternetDomainBaseByTenantIdxKey argKey) { final String S_ProcName = "deleteVersionByTenantIdx"; CFInternetVersionBuff cur; LinkedList<CFInternetVersionBuff> matchSet = new LinkedList<CFInternetVersionBuff>(); Iterator<CFInternetVersionBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFInternetVersionBuff> 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, (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 Version must not be \"" + subClassCode + "\""); } } } public CFSecurityCursor openVersionCursorAll(CFSecurityAuthorization Authorization) { CFSecurityCursor cursor = new CFAsteriskRamVersionCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public void closeVersionCursor(CFSecurityCursor Cursor) { // Cursor.DataReader.Close(); } public CFInternetVersionBuff nextVersionCursor(CFSecurityCursor Cursor) { CFAsteriskRamVersionCursor cursor = (CFAsteriskRamVersionCursor) Cursor; CFInternetVersionBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFInternetVersionBuff prevVersionCursor(CFSecurityCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFInternetVersionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public CFInternetVersionBuff firstVersionCursor(CFSecurityCursor Cursor) { int targetRowIdx = 1; CFInternetVersionBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public CFInternetVersionBuff lastVersionCursor(CFSecurityCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastVersionCursor"); } public CFInternetVersionBuff nthVersionCursor(CFSecurityCursor Cursor, int Idx) { int targetRowIdx = Idx; CFInternetVersionBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextVersionCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }