Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for ContactList. /* * 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.*; /* * MSSBamRamContactListTable in-memory RAM DbIO implementation * for ContactList. */ public class MSSBamRamContactListTable implements IMSSBamContactListTable { private MSSBamBLRamSchema schema; private Map<MSSBamContactListPKey, MSSBamContactListBuff> dictByPKey = new HashMap<MSSBamContactListPKey, MSSBamContactListBuff>(); private SortedMap<MSSBamContactListByTenantIdxKey, SortedMap<MSSBamContactListPKey, MSSBamContactListBuff>> dictByTenantIdx = new TreeMap<MSSBamContactListByTenantIdxKey, SortedMap<MSSBamContactListPKey, MSSBamContactListBuff>>(); private SortedMap<MSSBamContactListByUDescrIdxKey, MSSBamContactListBuff> dictByUDescrIdx = new TreeMap<MSSBamContactListByUDescrIdxKey, MSSBamContactListBuff>(); public MSSBamRamContactListTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createContactList(MSSBamAuthorization Authorization, MSSBamContactListBuff Buff) { MSSBamContactListPKey pkey = schema.getFactoryContactList().newPKey(); pkey.setRequiredContactListId(schema.nextContactListIdGen()); Buff.setRequiredContactListId(pkey.getRequiredContactListId()); MSSBamContactListByTenantIdxKey keyTenantIdx = schema.getFactoryContactList().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); MSSBamContactListByUDescrIdxKey keyUDescrIdx = schema.getFactoryContactList().newUDescrIdxKey(); keyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId()); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createContactList", pkey); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createContactList", "ContactListUDescrIdx", keyUDescrIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createContactList", "Container", "ContactListTenant", "Tenant", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamContactListPKey, MSSBamContactListBuff> subdictTenantIdx; if (dictByTenantIdx.containsKey(keyTenantIdx)) { subdictTenantIdx = dictByTenantIdx.get(keyTenantIdx); } else { subdictTenantIdx = new TreeMap<MSSBamContactListPKey, MSSBamContactListBuff>(); dictByTenantIdx.put(keyTenantIdx, subdictTenantIdx); } subdictTenantIdx.put(pkey, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); } public MSSBamContactListBuff readDerived(MSSBamAuthorization Authorization, MSSBamContactListPKey PKey) { final String S_ProcName = "MSSBamRamContactList.readDerived() "; MSSBamContactListPKey key = schema.getFactoryContactList().newPKey(); key.setRequiredContactListId(PKey.getRequiredContactListId()); MSSBamContactListBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamContactListBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamContactList.readAllDerived() "; MSSBamContactListBuff[] retList = new MSSBamContactListBuff[dictByPKey.values().size()]; Iterator<MSSBamContactListBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamContactListBuff[] readDerivedByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { final String S_ProcName = "MSSBamRamContactList.readDerivedByTenantIdx() "; MSSBamContactListByTenantIdxKey key = schema.getFactoryContactList().newTenantIdxKey(); key.setRequiredTenantId(TenantId); MSSBamContactListBuff[] recArray; if (dictByTenantIdx.containsKey(key)) { SortedMap<MSSBamContactListPKey, MSSBamContactListBuff> subdictTenantIdx = dictByTenantIdx.get(key); recArray = new MSSBamContactListBuff[subdictTenantIdx.size()]; Iterator<MSSBamContactListBuff> iter = subdictTenantIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamContactListBuff[0]; } return (recArray); } public MSSBamContactListBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long TenantId, String Description) { final String S_ProcName = "MSSBamRamContactList.readDerivedByUDescrIdx() "; MSSBamContactListByUDescrIdxKey key = schema.getFactoryContactList().newUDescrIdxKey(); key.setRequiredTenantId(TenantId); key.setRequiredDescription(Description); MSSBamContactListBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public MSSBamContactListBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long ContactListId) { final String S_ProcName = "MSSBamRamContactList.readDerivedByIdIdx() "; MSSBamContactListPKey key = schema.getFactoryContactList().newPKey(); key.setRequiredContactListId(ContactListId); MSSBamContactListBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamContactListBuff readBuff(MSSBamAuthorization Authorization, MSSBamContactListPKey PKey) { final String S_ProcName = "MSSBamRamContactList.readBuff() "; MSSBamContactListBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("CTL"))) { buff = null; } return (buff); } public MSSBamContactListBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamContactList.readAllBuff() "; MSSBamContactListBuff buff; ArrayList<MSSBamContactListBuff> filteredList = new ArrayList<MSSBamContactListBuff>(); MSSBamContactListBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("CTL")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamContactListBuff[0])); } public MSSBamContactListBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long ContactListId) { final String S_ProcName = "MSSBamRamContactList.readBuffByIdIdx() "; MSSBamContactListBuff buff = readDerivedByIdIdx(Authorization, ContactListId); if ((buff != null) && buff.getClassCode().equals("CTL")) { return ((MSSBamContactListBuff) buff); } else { return (null); } } public MSSBamContactListBuff[] readBuffByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { final String S_ProcName = "MSSBamRamContactList.readBuffByTenantIdx() "; MSSBamContactListBuff buff; ArrayList<MSSBamContactListBuff> filteredList = new ArrayList<MSSBamContactListBuff>(); MSSBamContactListBuff[] buffList = readDerivedByTenantIdx(Authorization, TenantId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("CTL")) { filteredList.add((MSSBamContactListBuff) buff); } } return (filteredList.toArray(new MSSBamContactListBuff[0])); } public MSSBamContactListBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, long TenantId, String Description) { final String S_ProcName = "MSSBamRamContactList.readBuffByUDescrIdx() "; MSSBamContactListBuff buff = readDerivedByUDescrIdx(Authorization, TenantId, Description); if ((buff != null) && buff.getClassCode().equals("CTL")) { return ((MSSBamContactListBuff) buff); } else { return (null); } } public void updateContactList(MSSBamAuthorization Authorization, MSSBamContactListBuff Buff) { MSSBamContactListPKey pkey = schema.getFactoryContactList().newPKey(); pkey.setRequiredContactListId(Buff.getRequiredContactListId()); MSSBamContactListBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateContactList", "Existing record not found", "ContactList", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateContactList", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamContactListByTenantIdxKey existingKeyTenantIdx = schema.getFactoryContactList().newTenantIdxKey(); existingKeyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); MSSBamContactListByTenantIdxKey newKeyTenantIdx = schema.getFactoryContactList().newTenantIdxKey(); newKeyTenantIdx.setRequiredTenantId(Buff.getRequiredTenantId()); MSSBamContactListByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryContactList().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId()); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamContactListByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryContactList().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredTenantId(Buff.getRequiredTenantId()); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); // Check unique indexes if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateContactList", "ContactListUDescrIdx", newKeyUDescrIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableTenant().readDerivedByIdIdx(Authorization, Buff.getRequiredTenantId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateContactList", "Container", "ContactListTenant", "Tenant", null); } } } // Update is valid SortedMap<MSSBamContactListPKey, MSSBamContactListBuff> 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 TreeMap<MSSBamContactListPKey, MSSBamContactListBuff>(); dictByTenantIdx.put(newKeyTenantIdx, subdict); } subdict.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); } public void deleteContactList(MSSBamAuthorization Authorization, MSSBamContactListBuff Buff) { final String S_ProcName = "MSSBamRamContactListTable.deleteContactList() "; MSSBamContactListPKey pkey = schema.getFactoryContactList().newPKey(); pkey.setRequiredContactListId(schema.nextContactListIdGen()); MSSBamContactListBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteContactList", "Existing record not found", "ContactList", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteContactList", pkey); } MSSBamContactListByTenantIdxKey keyTenantIdx = schema.getFactoryContactList().newTenantIdxKey(); keyTenantIdx.setRequiredTenantId(existing.getRequiredTenantId()); MSSBamContactListByUDescrIdxKey keyUDescrIdx = schema.getFactoryContactList().newUDescrIdxKey(); keyUDescrIdx.setRequiredTenantId(existing.getRequiredTenantId()); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); // Validate reverse foreign keys if (schema.getTableContact().readDerivedByContactListIdx(Authorization, existing.getRequiredContactListId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteContactList", "Container", "ContactList", "Contact", pkey); } // Delete is valid SortedMap<MSSBamContactListPKey, MSSBamContactListBuff> subdict; dictByPKey.remove(pkey); subdict = dictByTenantIdx.get(keyTenantIdx); subdict.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); } public MSSBamCursor openContactListCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamContactListCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openContactListCursorByTenantIdx(MSSBamAuthorization Authorization, long TenantId) { MSSBamCursor cursor; MSSBamContactListByTenantIdxKey key = schema.getFactoryContactList().newTenantIdxKey(); key.setRequiredTenantId(TenantId); if (dictByTenantIdx.containsKey(key)) { SortedMap<MSSBamContactListPKey, MSSBamContactListBuff> subdictTenantIdx = dictByTenantIdx.get(key); cursor = new MSSBamRamContactListCursor(Authorization, schema, subdictTenantIdx.values()); } else { cursor = new MSSBamRamContactListCursor(Authorization, schema, new ArrayList<MSSBamContactListBuff>()); } return (cursor); } public void closeContactListCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamContactListBuff nextContactListCursor(MSSBamCursor Cursor) { MSSBamRamContactListCursor cursor = (MSSBamRamContactListCursor) Cursor; MSSBamContactListBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamContactListBuff prevContactListCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamContactListBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextContactListCursor(Cursor); } return (rec); } public MSSBamContactListBuff firstContactListCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamContactListBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextContactListCursor(Cursor); } return (rec); } public MSSBamContactListBuff lastContactListCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastContactListCursor"); } public MSSBamContactListBuff nthContactListCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamContactListBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextContactListCursor(Cursor); } return (rec); } }