Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for HostNode. /* * MSS Code Factory 1.10 * * Copyright (c) 2012 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/>. * * *********************************************************************** * * Code manufactured * by MSS Code Factory version 1.9.3294 * * $Revision: 26 $ */ package net.sourceforge.msscodefactory.v1_10.MSSBamRam; import java.sql.*; import java.util.*; import net.sourceforge.msscodefactory.cflib.v1_9.CFLib.*; import org.apache.commons.codec.binary.Base64; import net.sourceforge.msscodefactory.v1_10.MSSBam.*; import net.sourceforge.msscodefactory.v1_10.MSSBamBL.*; import net.sourceforge.msscodefactory.v1_10.MSSBamBLRam.*; /* * MSSBamRamHostNodeTable in-memory RAM DbIO implementation * for HostNode. */ public class MSSBamRamHostNodeTable implements IMSSBamHostNodeTable { private MSSBamBLRamSchema schema; private Map<MSSBamHostNodePKey, MSSBamHostNodeBuff> dictByPKey = new HashMap<MSSBamHostNodePKey, MSSBamHostNodeBuff>(); private SortedMap<MSSBamHostNodeByClusterIdxKey, SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff>> dictByClusterIdx = new TreeMap<MSSBamHostNodeByClusterIdxKey, SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff>>(); private SortedMap<MSSBamHostNodeByUDescrIdxKey, MSSBamHostNodeBuff> dictByUDescrIdx = new TreeMap<MSSBamHostNodeByUDescrIdxKey, MSSBamHostNodeBuff>(); private SortedMap<MSSBamHostNodeByHostNameIdxKey, MSSBamHostNodeBuff> dictByHostNameIdx = new TreeMap<MSSBamHostNodeByHostNameIdxKey, MSSBamHostNodeBuff>(); public MSSBamRamHostNodeTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createHostNode(MSSBamAuthorization Authorization, MSSBamHostNodeBuff Buff) { MSSBamHostNodePKey pkey = schema.getFactoryHostNode().newPKey(); pkey.setRequiredHostNodeId(schema.nextHostNodeIdGen()); Buff.setRequiredHostNodeId(pkey.getRequiredHostNodeId()); MSSBamHostNodeByClusterIdxKey keyClusterIdx = schema.getFactoryHostNode().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamHostNodeByUDescrIdxKey keyUDescrIdx = schema.getFactoryHostNode().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamHostNodeByHostNameIdxKey keyHostNameIdx = schema.getFactoryHostNode().newHostNameIdxKey(); keyHostNameIdx.setRequiredHostName(Buff.getRequiredHostName()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createHostNode", pkey); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createHostNode", "HostNodeUDescrIdx", keyUDescrIdx); } if (dictByHostNameIdx.containsKey(keyHostNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createHostNode", "HostNodeUHostNameIdx", keyHostNameIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createHostNode", "Container", "HostNodeCluster", "Cluster", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff> subdictClusterIdx; if (dictByClusterIdx.containsKey(keyClusterIdx)) { subdictClusterIdx = dictByClusterIdx.get(keyClusterIdx); } else { subdictClusterIdx = new TreeMap<MSSBamHostNodePKey, MSSBamHostNodeBuff>(); dictByClusterIdx.put(keyClusterIdx, subdictClusterIdx); } subdictClusterIdx.put(pkey, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); dictByHostNameIdx.put(keyHostNameIdx, Buff); } public MSSBamHostNodeBuff readDerived(MSSBamAuthorization Authorization, MSSBamHostNodePKey PKey) { final String S_ProcName = "MSSBamRamHostNode.readDerived() "; MSSBamHostNodePKey key = schema.getFactoryHostNode().newPKey(); key.setRequiredHostNodeId(PKey.getRequiredHostNodeId()); MSSBamHostNodeBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamHostNodeBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamHostNode.readAllDerived() "; MSSBamHostNodeBuff[] retList = new MSSBamHostNodeBuff[dictByPKey.values().size()]; Iterator<MSSBamHostNodeBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamHostNodeBuff[] readDerivedByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamHostNode.readDerivedByClusterIdx() "; MSSBamHostNodeByClusterIdxKey key = schema.getFactoryHostNode().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); MSSBamHostNodeBuff[] recArray; if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff> subdictClusterIdx = dictByClusterIdx.get(key); recArray = new MSSBamHostNodeBuff[subdictClusterIdx.size()]; Iterator<MSSBamHostNodeBuff> iter = subdictClusterIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamHostNodeBuff[0]; } return (recArray); } public MSSBamHostNodeBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, String Description) { final String S_ProcName = "MSSBamRamHostNode.readDerivedByUDescrIdx() "; MSSBamHostNodeByUDescrIdxKey key = schema.getFactoryHostNode().newUDescrIdxKey(); key.setRequiredDescription(Description); MSSBamHostNodeBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public MSSBamHostNodeBuff readDerivedByHostNameIdx(MSSBamAuthorization Authorization, String HostName) { final String S_ProcName = "MSSBamRamHostNode.readDerivedByHostNameIdx() "; MSSBamHostNodeByHostNameIdxKey key = schema.getFactoryHostNode().newHostNameIdxKey(); key.setRequiredHostName(HostName); MSSBamHostNodeBuff buff; if (dictByHostNameIdx.containsKey(key)) { buff = dictByHostNameIdx.get(key); } else { buff = null; } return (buff); } public MSSBamHostNodeBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long HostNodeId) { final String S_ProcName = "MSSBamRamHostNode.readDerivedByIdIdx() "; MSSBamHostNodePKey key = schema.getFactoryHostNode().newPKey(); key.setRequiredHostNodeId(HostNodeId); MSSBamHostNodeBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamHostNodeBuff readBuff(MSSBamAuthorization Authorization, MSSBamHostNodePKey PKey) { final String S_ProcName = "MSSBamRamHostNode.readBuff() "; MSSBamHostNodeBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("HSND"))) { buff = null; } return (buff); } public MSSBamHostNodeBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamHostNode.readAllBuff() "; MSSBamHostNodeBuff buff; ArrayList<MSSBamHostNodeBuff> filteredList = new ArrayList<MSSBamHostNodeBuff>(); MSSBamHostNodeBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("HSND")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamHostNodeBuff[0])); } public MSSBamHostNodeBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long HostNodeId) { final String S_ProcName = "MSSBamRamHostNode.readBuffByIdIdx() "; MSSBamHostNodeBuff buff = readDerivedByIdIdx(Authorization, HostNodeId); if ((buff != null) && buff.getClassCode().equals("HSND")) { return ((MSSBamHostNodeBuff) buff); } else { return (null); } } public MSSBamHostNodeBuff[] readBuffByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { final String S_ProcName = "MSSBamRamHostNode.readBuffByClusterIdx() "; MSSBamHostNodeBuff buff; ArrayList<MSSBamHostNodeBuff> filteredList = new ArrayList<MSSBamHostNodeBuff>(); MSSBamHostNodeBuff[] buffList = readDerivedByClusterIdx(Authorization, ClusterId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("HSND")) { filteredList.add((MSSBamHostNodeBuff) buff); } } return (filteredList.toArray(new MSSBamHostNodeBuff[0])); } public MSSBamHostNodeBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, String Description) { final String S_ProcName = "MSSBamRamHostNode.readBuffByUDescrIdx() "; MSSBamHostNodeBuff buff = readDerivedByUDescrIdx(Authorization, Description); if ((buff != null) && buff.getClassCode().equals("HSND")) { return ((MSSBamHostNodeBuff) buff); } else { return (null); } } public MSSBamHostNodeBuff readBuffByHostNameIdx(MSSBamAuthorization Authorization, String HostName) { final String S_ProcName = "MSSBamRamHostNode.readBuffByHostNameIdx() "; MSSBamHostNodeBuff buff = readDerivedByHostNameIdx(Authorization, HostName); if ((buff != null) && buff.getClassCode().equals("HSND")) { return ((MSSBamHostNodeBuff) buff); } else { return (null); } } public void updateHostNode(MSSBamAuthorization Authorization, MSSBamHostNodeBuff Buff) { MSSBamHostNodePKey pkey = schema.getFactoryHostNode().newPKey(); pkey.setRequiredHostNodeId(Buff.getRequiredHostNodeId()); MSSBamHostNodeBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateHostNode", "Existing record not found", "HostNode", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateHostNode", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamHostNodeByClusterIdxKey existingKeyClusterIdx = schema.getFactoryHostNode().newClusterIdxKey(); existingKeyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamHostNodeByClusterIdxKey newKeyClusterIdx = schema.getFactoryHostNode().newClusterIdxKey(); newKeyClusterIdx.setRequiredClusterId(Buff.getRequiredClusterId()); MSSBamHostNodeByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryHostNode().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamHostNodeByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryHostNode().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamHostNodeByHostNameIdxKey existingKeyHostNameIdx = schema.getFactoryHostNode().newHostNameIdxKey(); existingKeyHostNameIdx.setRequiredHostName(existing.getRequiredHostName()); MSSBamHostNodeByHostNameIdxKey newKeyHostNameIdx = schema.getFactoryHostNode().newHostNameIdxKey(); newKeyHostNameIdx.setRequiredHostName(Buff.getRequiredHostName()); // Check unique indexes if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateHostNode", "HostNodeUDescrIdx", newKeyUDescrIdx); } } if (!existingKeyHostNameIdx.equals(newKeyHostNameIdx)) { if (dictByHostNameIdx.containsKey(newKeyHostNameIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateHostNode", "HostNodeUHostNameIdx", newKeyHostNameIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableCluster().readDerivedByIdIdx(Authorization, Buff.getRequiredClusterId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateHostNode", "Container", "HostNodeCluster", "Cluster", null); } } } // Update is valid SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff> 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 TreeMap<MSSBamHostNodePKey, MSSBamHostNodeBuff>(); dictByClusterIdx.put(newKeyClusterIdx, subdict); } subdict.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); dictByHostNameIdx.remove(existingKeyHostNameIdx); dictByHostNameIdx.put(newKeyHostNameIdx, Buff); } public void deleteHostNode(MSSBamAuthorization Authorization, MSSBamHostNodeBuff Buff) { final String S_ProcName = "MSSBamRamHostNodeTable.deleteHostNode() "; MSSBamHostNodePKey pkey = schema.getFactoryHostNode().newPKey(); pkey.setRequiredHostNodeId(schema.nextHostNodeIdGen()); MSSBamHostNodeBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteHostNode", "Existing record not found", "HostNode", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteHostNode", pkey); } MSSBamHostNodeByClusterIdxKey keyClusterIdx = schema.getFactoryHostNode().newClusterIdxKey(); keyClusterIdx.setRequiredClusterId(existing.getRequiredClusterId()); MSSBamHostNodeByUDescrIdxKey keyUDescrIdx = schema.getFactoryHostNode().newUDescrIdxKey(); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamHostNodeByHostNameIdxKey keyHostNameIdx = schema.getFactoryHostNode().newHostNameIdxKey(); keyHostNameIdx.setRequiredHostName(existing.getRequiredHostName()); // Validate reverse foreign keys if (schema.getTableService().readDerivedByHostIdx(Authorization, existing.getRequiredHostNodeId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteHostNode", "Container", "ServiceHost", "Service", pkey); } // Delete is valid SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff> subdict; dictByPKey.remove(pkey); subdict = dictByClusterIdx.get(keyClusterIdx); subdict.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); dictByHostNameIdx.remove(keyHostNameIdx); } public MSSBamCursor openHostNodeCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamHostNodeCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openHostNodeCursorByClusterIdx(MSSBamAuthorization Authorization, long ClusterId) { MSSBamCursor cursor; MSSBamHostNodeByClusterIdxKey key = schema.getFactoryHostNode().newClusterIdxKey(); key.setRequiredClusterId(ClusterId); if (dictByClusterIdx.containsKey(key)) { SortedMap<MSSBamHostNodePKey, MSSBamHostNodeBuff> subdictClusterIdx = dictByClusterIdx.get(key); cursor = new MSSBamRamHostNodeCursor(Authorization, schema, subdictClusterIdx.values()); } else { cursor = new MSSBamRamHostNodeCursor(Authorization, schema, new ArrayList<MSSBamHostNodeBuff>()); } return (cursor); } public void closeHostNodeCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamHostNodeBuff nextHostNodeCursor(MSSBamCursor Cursor) { MSSBamRamHostNodeCursor cursor = (MSSBamRamHostNodeCursor) Cursor; MSSBamHostNodeBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamHostNodeBuff prevHostNodeCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamHostNodeBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextHostNodeCursor(Cursor); } return (rec); } public MSSBamHostNodeBuff firstHostNodeCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamHostNodeBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextHostNodeCursor(Cursor); } return (rec); } public MSSBamHostNodeBuff lastHostNodeCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastHostNodeCursor"); } public MSSBamHostNodeBuff nthHostNodeCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamHostNodeBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextHostNodeCursor(Cursor); } return (rec); } }