Java tutorial
// Description: Java7 in-memory RAM DbIO implementation for SysCluster. /* * Code Factory Asterisk 11 Configuration Model * * Copyright (c) 2014-2015 Mark Sobkow * * This program is available as free software under the GNU GPL v3, or * under a commercial license from Mark Sobkow. For commercial licensing * details, please contact msobkow@sasktel.net. * * Under the terms of the GPL: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * This software incorporates code originally Copyright (c) Mark Sobkow 2014-2015 * and licensed under the BSD 3-Clause license as written by xfree86.org: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * (1) Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * (2) Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * (3)The name of the author may not be used to * endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * *********************************************************************** * * Code manufactured by MSS Code Factory */ package net.sourceforge.msscodefactory.cfasterisk.v2_2.CFAstRam; import java.sql.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v2_1.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.cfasterisk.v2_2.CFAst.*; import net.sourceforge.msscodefactory.cfasterisk.v2_2.CFAstRam.*; /* * CFAstRamSysClusterTable in-memory RAM DbIO implementation * for SysCluster. */ public class CFAstRamSysClusterTable implements ICFAstSysClusterTable { private CFAstRamSchema schema; private Map<CFAstSysClusterPKey, CFAstSysClusterBuff> dictByPKey = new HashMap<CFAstSysClusterPKey, CFAstSysClusterBuff>(); private Map<CFAstSysClusterByClusterIdxKey, Map<CFAstSysClusterPKey, CFAstSysClusterBuff>> dictByClusterIdx = new HashMap<CFAstSysClusterByClusterIdxKey, Map<CFAstSysClusterPKey, CFAstSysClusterBuff>>(); public CFAstRamSysClusterTable(CFAstRamSchema argSchema) { schema = argSchema; } public void createSysCluster(CFAstAuthorization Authorization, CFAstSysClusterBuff Buff) { final String S_ProcName = "createSysCluster"; CFAstSysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); Buff.setRequiredSingletonId(pkey.getRequiredSingletonId()); CFAstSysClusterByClusterIdxKey keyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); // 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.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), S_ProcName, "Container", "SysClusterCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new HashMap<CFAstSysClusterPKey, CFAstSysClusterBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); } public CFAstSysClusterBuff readDerived(CFAstAuthorization Authorization, CFAstSysClusterPKey PKey) { final String S_ProcName = "CFAstRamSysCluster.readDerived() "; CFAstSysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(PKey.getRequiredSingletonId()); CFAstSysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSysClusterBuff lockDerived(CFAstAuthorization Authorization, CFAstSysClusterPKey PKey) { final String S_ProcName = "CFAstRamSysCluster.readDerived() "; CFAstSysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(PKey.getRequiredSingletonId()); CFAstSysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSysClusterBuff[] readAllDerived(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSysCluster.readAllDerived() "; CFAstSysClusterBuff[] retList = new CFAstSysClusterBuff[dictByPKey.values().size()]; Iterator<CFAstSysClusterBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public CFAstSysClusterBuff[] readDerivedByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSysCluster.readDerivedByClusterIdx() "; CFAstSysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); CFAstSysClusterBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new CFAstSysClusterBuff[subdictClusterIdx.size()]; Iterator<CFAstSysClusterBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdictClusterIdx = new HashMap<CFAstSysClusterPKey, CFAstSysClusterBuff>(); dictByClusterIdx.put(key, subdictClusterIdx); recArray = new CFAstSysClusterBuff[0]; } return (recArray); } public CFAstSysClusterBuff readDerivedByIdIdx(CFAstAuthorization Authorization, int SingletonId) { final String S_ProcName = "CFAstRamSysCluster.readDerivedByIdIdx() "; CFAstSysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(SingletonId); CFAstSysClusterBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public CFAstSysClusterBuff readBuff(CFAstAuthorization Authorization, CFAstSysClusterPKey PKey) { final String S_ProcName = "CFAstRamSysCluster.readBuff() "; CFAstSysClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SYSC"))) { buff = null; } return (buff); } public CFAstSysClusterBuff lockBuff(CFAstAuthorization Authorization, CFAstSysClusterPKey PKey) { final String S_ProcName = "CFAstRamSysCluster.readBuff() "; CFAstSysClusterBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("SYSC"))) { buff = null; } return (buff); } public CFAstSysClusterBuff[] readAllBuff(CFAstAuthorization Authorization) { final String S_ProcName = "CFAstRamSysCluster.readAllBuff() "; CFAstSysClusterBuff buff; ArrayList<CFAstSysClusterBuff> filteredList = new ArrayList<CFAstSysClusterBuff>(); CFAstSysClusterBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SYSC")) { filteredList.add(buff); } } return (filteredList.toArray(new CFAstSysClusterBuff[0])); } public CFAstSysClusterBuff readBuffByIdIdx(CFAstAuthorization Authorization, int SingletonId) { final String S_ProcName = "CFAstRamSysCluster.readBuffByIdIdx() "; CFAstSysClusterBuff buff = readDerivedByIdIdx(Authorization, SingletonId); if ((buff != null) && buff.getClassCode().equals("SYSC")) { return ((CFAstSysClusterBuff) buff); } else { return (null); } } public CFAstSysClusterBuff[] readBuffByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { final String S_ProcName = "CFAstRamSysCluster.readBuffByClusterIdx() "; CFAstSysClusterBuff buff; ArrayList<CFAstSysClusterBuff> filteredList = new ArrayList<CFAstSysClusterBuff>(); CFAstSysClusterBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("SYSC")) { filteredList.add((CFAstSysClusterBuff) buff); } } return (filteredList.toArray(new CFAstSysClusterBuff[0])); } public void updateSysCluster(CFAstAuthorization Authorization, CFAstSysClusterBuff Buff) { CFAstSysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); CFAstSysClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateSysCluster", "Existing record not found", "SysCluster", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateSysCluster", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); CFAstSysClusterByClusterIdxKey existingKeyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); CFAstSysClusterByClusterIdxKey newKeyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); // Check unique indexes // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateSysCluster", "Container", "SysClusterCluster", "Cluster", null); } } } // Update is valid Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByClusterIdx.get(existingKeyClusterIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByClusterIdx.containsKey(newKeyClusterIdx)) { subdict = dictByClusterIdx.get(newKeyClusterIdx); } else { subdict = new HashMap<CFAstSysClusterPKey, CFAstSysClusterBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); } public void deleteSysCluster(CFAstAuthorization Authorization, CFAstSysClusterBuff Buff) { final String S_ProcName = "CFAstRamSysClusterTable.deleteSysCluster() "; CFAstSysClusterPKey pkey = schema.getFactorySysCluster().newPKey(); pkey.setRequiredSingletonId(Buff.getRequiredSingletonId()); CFAstSysClusterBuff existing = dictByPKey.get(pkey); if (existing == null) { return; } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteSysCluster", pkey); } CFAstSysClusterByClusterIdxKey keyClusterIdx = schema.getFactorySysCluster().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); // Validate reverse foreign keys // Delete is valid Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); } public void deleteSysClusterByIdIdx(CFAstAuthorization Authorization, int argSingletonId) { CFAstSysClusterPKey key = schema.getFactorySysCluster().newPKey(); key.setRequiredSingletonId(argSingletonId); deleteSysClusterByIdIdx(Authorization, key); } public void deleteSysClusterByIdIdx(CFAstAuthorization Authorization, CFAstSysClusterPKey argKey) { CFAstSysClusterBuff cur; LinkedList<CFAstSysClusterBuff> matchSet = new LinkedList<CFAstSysClusterBuff>(); Iterator<CFAstSysClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSysClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSysCluster(Authorization, cur); } } public void deleteSysClusterByClusterIdx(CFAstAuthorization Authorization, long argClusterId) { CFAstSysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(argClusterId); deleteSysClusterByClusterIdx(Authorization, key); } public void deleteSysClusterByClusterIdx(CFAstAuthorization Authorization, CFAstSysClusterByClusterIdxKey argKey) { CFAstSysClusterBuff cur; LinkedList<CFAstSysClusterBuff> matchSet = new LinkedList<CFAstSysClusterBuff>(); Iterator<CFAstSysClusterBuff> values = dictByPKey.values().iterator(); while (values.hasNext()) { cur = values.next(); if (argKey.equals(cur)) { matchSet.add(cur); } } Iterator<CFAstSysClusterBuff> iterMatch = matchSet.iterator(); while (iterMatch.hasNext()) { cur = iterMatch.next(); deleteSysCluster(Authorization, cur); } } public CFAstCursor openSysClusterCursorAll(CFAstAuthorization Authorization) { CFAstCursor cursor = new CFAstRamSysClusterCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public CFAstCursor openSysClusterCursorByClusterIdx(CFAstAuthorization Authorization, long ClusterId) { CFAstCursor cursor; CFAstSysClusterByClusterIdxKey key = schema.getFactorySysCluster().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { Map<CFAstSysClusterPKey, CFAstSysClusterBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new CFAstRamSysClusterCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new CFAstRamSysClusterCursor(Authorization, schema, new ArrayList<CFAstSysClusterBuff>()); } return (cursor); } public void closeSysClusterCursor(CFAstCursor Cursor) { // Cursor.DataReader.Close(); } public CFAstSysClusterBuff nextSysClusterCursor(CFAstCursor Cursor) { CFAstRamSysClusterCursor cursor = (CFAstRamSysClusterCursor) Cursor; CFAstSysClusterBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public CFAstSysClusterBuff prevSysClusterCursor(CFAstCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; CFAstSysClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public CFAstSysClusterBuff firstSysClusterCursor(CFAstCursor Cursor) { int targetRowIdx = 1; CFAstSysClusterBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public CFAstSysClusterBuff lastSysClusterCursor(CFAstCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastSysClusterCursor"); } public CFAstSysClusterBuff nthSysClusterCursor(CFAstCursor Cursor, int Idx) { int targetRowIdx = Idx; CFAstSysClusterBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextSysClusterCursor(Cursor); } return (rec); } public void releasePreparedStatements() { } }