Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for Phone. /* * 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.*; /* * MSSBamRamPhoneTable in-memory RAM DbIO implementation * for Phone. */ public class MSSBamRamPhoneTable implements IMSSBamPhoneTable { private MSSBamBLRamSchema schema; private Map<MSSBamPhonePKey, MSSBamPhoneBuff> dictByPKey = new HashMap<MSSBamPhonePKey, MSSBamPhoneBuff>(); private SortedMap<MSSBamPhoneByContactIdxKey, SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff>> dictByContactIdx = new TreeMap<MSSBamPhoneByContactIdxKey, SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff>>(); private SortedMap<MSSBamPhoneByUDescrIdxKey, MSSBamPhoneBuff> dictByUDescrIdx = new TreeMap<MSSBamPhoneByUDescrIdxKey, MSSBamPhoneBuff>(); private SortedMap<MSSBamPhoneByUPhoneNumberIdxKey, MSSBamPhoneBuff> dictByUPhoneNumberIdx = new TreeMap<MSSBamPhoneByUPhoneNumberIdxKey, MSSBamPhoneBuff>(); public MSSBamRamPhoneTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createPhone(MSSBamAuthorization Authorization, MSSBamPhoneBuff Buff) { MSSBamPhonePKey pkey = schema.getFactoryPhone().newPKey(); pkey.setRequiredPhoneId(schema.nextPhoneIdGen()); Buff.setRequiredPhoneId(pkey.getRequiredPhoneId()); MSSBamPhoneByContactIdxKey keyContactIdx = schema.getFactoryPhone().newContactIdxKey(); keyContactIdx.setRequiredContactId(Buff.getRequiredContactId()); MSSBamPhoneByUDescrIdxKey keyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey(); keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId()); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamPhoneByUPhoneNumberIdxKey keyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey(); keyUPhoneNumberIdx.setRequiredContactId(Buff.getRequiredContactId()); keyUPhoneNumberIdx.setRequiredPhoneNumber(Buff.getRequiredPhoneNumber()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createPhone", pkey); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createPhone", "PhoneUDescrIdx", keyUDescrIdx); } if (dictByUPhoneNumberIdx.containsKey(keyUPhoneNumberIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createPhone", "PhoneUPhoneNumberIdx", keyUPhoneNumberIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredContactId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createPhone", "Container", "Contact", "Contact", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff> subdictContactIdx; if (dictByContactIdx.containsKey(keyContactIdx)) { subdictContactIdx = dictByContactIdx.get(keyContactIdx); } else { subdictContactIdx = new TreeMap<MSSBamPhonePKey, MSSBamPhoneBuff>(); dictByContactIdx.put(keyContactIdx, subdictContactIdx); } subdictContactIdx.put(pkey, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); dictByUPhoneNumberIdx.put(keyUPhoneNumberIdx, Buff); } public MSSBamPhoneBuff readDerived(MSSBamAuthorization Authorization, MSSBamPhonePKey PKey) { final String S_ProcName = "MSSBamRamPhone.readDerived() "; MSSBamPhonePKey key = schema.getFactoryPhone().newPKey(); key.setRequiredPhoneId(PKey.getRequiredPhoneId()); MSSBamPhoneBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamPhoneBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamPhone.readAllDerived() "; MSSBamPhoneBuff[] retList = new MSSBamPhoneBuff[dictByPKey.values().size()]; Iterator<MSSBamPhoneBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamPhoneBuff[] readDerivedByContactIdx(MSSBamAuthorization Authorization, long ContactId) { final String S_ProcName = "MSSBamRamPhone.readDerivedByContactIdx() "; MSSBamPhoneByContactIdxKey key = schema.getFactoryPhone().newContactIdxKey(); key.setRequiredContactId(ContactId); MSSBamPhoneBuff[] recArray; if (dictByContactIdx.containsKey(key)) { SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff> subdictContactIdx = dictByContactIdx.get(key); recArray = new MSSBamPhoneBuff[subdictContactIdx.size()]; Iterator<MSSBamPhoneBuff> iter = subdictContactIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamPhoneBuff[0]; } return (recArray); } public MSSBamPhoneBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long ContactId, String Description) { final String S_ProcName = "MSSBamRamPhone.readDerivedByUDescrIdx() "; MSSBamPhoneByUDescrIdxKey key = schema.getFactoryPhone().newUDescrIdxKey(); key.setRequiredContactId(ContactId); key.setRequiredDescription(Description); MSSBamPhoneBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public MSSBamPhoneBuff readDerivedByUPhoneNumberIdx(MSSBamAuthorization Authorization, long ContactId, String PhoneNumber) { final String S_ProcName = "MSSBamRamPhone.readDerivedByUPhoneNumberIdx() "; MSSBamPhoneByUPhoneNumberIdxKey key = schema.getFactoryPhone().newUPhoneNumberIdxKey(); key.setRequiredContactId(ContactId); key.setRequiredPhoneNumber(PhoneNumber); MSSBamPhoneBuff buff; if (dictByUPhoneNumberIdx.containsKey(key)) { buff = dictByUPhoneNumberIdx.get(key); } else { buff = null; } return (buff); } public MSSBamPhoneBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long PhoneId) { final String S_ProcName = "MSSBamRamPhone.readDerivedByIdIdx() "; MSSBamPhonePKey key = schema.getFactoryPhone().newPKey(); key.setRequiredPhoneId(PhoneId); MSSBamPhoneBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamPhoneBuff readBuff(MSSBamAuthorization Authorization, MSSBamPhonePKey PKey) { final String S_ProcName = "MSSBamRamPhone.readBuff() "; MSSBamPhoneBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("PH"))) { buff = null; } return (buff); } public MSSBamPhoneBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamPhone.readAllBuff() "; MSSBamPhoneBuff buff; ArrayList<MSSBamPhoneBuff> filteredList = new ArrayList<MSSBamPhoneBuff>(); MSSBamPhoneBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("PH")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamPhoneBuff[0])); } public MSSBamPhoneBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long PhoneId) { final String S_ProcName = "MSSBamRamPhone.readBuffByIdIdx() "; MSSBamPhoneBuff buff = readDerivedByIdIdx(Authorization, PhoneId); if ((buff != null) && buff.getClassCode().equals("PH")) { return ((MSSBamPhoneBuff) buff); } else { return (null); } } public MSSBamPhoneBuff[] readBuffByContactIdx(MSSBamAuthorization Authorization, long ContactId) { final String S_ProcName = "MSSBamRamPhone.readBuffByContactIdx() "; MSSBamPhoneBuff buff; ArrayList<MSSBamPhoneBuff> filteredList = new ArrayList<MSSBamPhoneBuff>(); MSSBamPhoneBuff[] buffList = readDerivedByContactIdx(Authorization, ContactId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("PH")) { filteredList.add((MSSBamPhoneBuff) buff); } } return (filteredList.toArray(new MSSBamPhoneBuff[0])); } public MSSBamPhoneBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, long ContactId, String Description) { final String S_ProcName = "MSSBamRamPhone.readBuffByUDescrIdx() "; MSSBamPhoneBuff buff = readDerivedByUDescrIdx(Authorization, ContactId, Description); if ((buff != null) && buff.getClassCode().equals("PH")) { return ((MSSBamPhoneBuff) buff); } else { return (null); } } public MSSBamPhoneBuff readBuffByUPhoneNumberIdx(MSSBamAuthorization Authorization, long ContactId, String PhoneNumber) { final String S_ProcName = "MSSBamRamPhone.readBuffByUPhoneNumberIdx() "; MSSBamPhoneBuff buff = readDerivedByUPhoneNumberIdx(Authorization, ContactId, PhoneNumber); if ((buff != null) && buff.getClassCode().equals("PH")) { return ((MSSBamPhoneBuff) buff); } else { return (null); } } public void updatePhone(MSSBamAuthorization Authorization, MSSBamPhoneBuff Buff) { MSSBamPhonePKey pkey = schema.getFactoryPhone().newPKey(); pkey.setRequiredPhoneId(Buff.getRequiredPhoneId()); MSSBamPhoneBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updatePhone", "Existing record not found", "Phone", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updatePhone", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamPhoneByContactIdxKey existingKeyContactIdx = schema.getFactoryPhone().newContactIdxKey(); existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId()); MSSBamPhoneByContactIdxKey newKeyContactIdx = schema.getFactoryPhone().newContactIdxKey(); newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId()); MSSBamPhoneByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId()); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamPhoneByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId()); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamPhoneByUPhoneNumberIdxKey existingKeyUPhoneNumberIdx = schema.getFactoryPhone() .newUPhoneNumberIdxKey(); existingKeyUPhoneNumberIdx.setRequiredContactId(existing.getRequiredContactId()); existingKeyUPhoneNumberIdx.setRequiredPhoneNumber(existing.getRequiredPhoneNumber()); MSSBamPhoneByUPhoneNumberIdxKey newKeyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey(); newKeyUPhoneNumberIdx.setRequiredContactId(Buff.getRequiredContactId()); newKeyUPhoneNumberIdx.setRequiredPhoneNumber(Buff.getRequiredPhoneNumber()); // Check unique indexes if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updatePhone", "PhoneUDescrIdx", newKeyUDescrIdx); } } if (!existingKeyUPhoneNumberIdx.equals(newKeyUPhoneNumberIdx)) { if (dictByUPhoneNumberIdx.containsKey(newKeyUPhoneNumberIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updatePhone", "PhoneUPhoneNumberIdx", newKeyUPhoneNumberIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredContactId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updatePhone", "Container", "Contact", "Contact", null); } } } // Update is valid SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff> subdict; dictByPKey.remove(pkey); dictByPKey.put(pkey, Buff); subdict = dictByContactIdx.get(existingKeyContactIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByContactIdx.containsKey(newKeyContactIdx)) { subdict = dictByContactIdx.get(newKeyContactIdx); } else { subdict = new TreeMap<MSSBamPhonePKey, MSSBamPhoneBuff>(); dictByContactIdx.put(newKeyContactIdx, subdict); } subdict.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); dictByUPhoneNumberIdx.remove(existingKeyUPhoneNumberIdx); dictByUPhoneNumberIdx.put(newKeyUPhoneNumberIdx, Buff); } public void deletePhone(MSSBamAuthorization Authorization, MSSBamPhoneBuff Buff) { final String S_ProcName = "MSSBamRamPhoneTable.deletePhone() "; MSSBamPhonePKey pkey = schema.getFactoryPhone().newPKey(); pkey.setRequiredPhoneId(schema.nextPhoneIdGen()); MSSBamPhoneBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deletePhone", "Existing record not found", "Phone", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deletePhone", pkey); } MSSBamPhoneByContactIdxKey keyContactIdx = schema.getFactoryPhone().newContactIdxKey(); keyContactIdx.setRequiredContactId(existing.getRequiredContactId()); MSSBamPhoneByUDescrIdxKey keyUDescrIdx = schema.getFactoryPhone().newUDescrIdxKey(); keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId()); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamPhoneByUPhoneNumberIdxKey keyUPhoneNumberIdx = schema.getFactoryPhone().newUPhoneNumberIdxKey(); keyUPhoneNumberIdx.setRequiredContactId(existing.getRequiredContactId()); keyUPhoneNumberIdx.setRequiredPhoneNumber(existing.getRequiredPhoneNumber()); // Validate reverse foreign keys if (schema.getTablePhoneTag().readDerivedByPhoneIdx(Authorization, existing.getRequiredPhoneId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deletePhone", "Container", "ContactPhone", "PhoneTag", pkey); } // Delete is valid SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff> subdict; dictByPKey.remove(pkey); subdict = dictByContactIdx.get(keyContactIdx); subdict.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); dictByUPhoneNumberIdx.remove(keyUPhoneNumberIdx); } public MSSBamCursor openPhoneCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamPhoneCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openPhoneCursorByContactIdx(MSSBamAuthorization Authorization, long ContactId) { MSSBamCursor cursor; MSSBamPhoneByContactIdxKey key = schema.getFactoryPhone().newContactIdxKey(); key.setRequiredContactId(ContactId); if (dictByContactIdx.containsKey(key)) { SortedMap<MSSBamPhonePKey, MSSBamPhoneBuff> subdictContactIdx = dictByContactIdx.get(key); cursor = new MSSBamRamPhoneCursor(Authorization, schema, subdictContactIdx.values()); } else { cursor = new MSSBamRamPhoneCursor(Authorization, schema, new ArrayList<MSSBamPhoneBuff>()); } return (cursor); } public void closePhoneCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamPhoneBuff nextPhoneCursor(MSSBamCursor Cursor) { MSSBamRamPhoneCursor cursor = (MSSBamRamPhoneCursor) Cursor; MSSBamPhoneBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamPhoneBuff prevPhoneCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamPhoneBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPhoneCursor(Cursor); } return (rec); } public MSSBamPhoneBuff firstPhoneCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamPhoneBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPhoneCursor(Cursor); } return (rec); } public MSSBamPhoneBuff lastPhoneCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastPhoneCursor"); } public MSSBamPhoneBuff nthPhoneCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamPhoneBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextPhoneCursor(Cursor); } return (rec); } }