Java tutorial
// Description: Java6 in-memory RAM DbIO implementation for Attachment. /* * 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.*; /* * MSSBamRamAttachmentTable in-memory RAM DbIO implementation * for Attachment. */ public class MSSBamRamAttachmentTable implements IMSSBamAttachmentTable { private MSSBamBLRamSchema schema; private Map<MSSBamAttachmentPKey, MSSBamAttachmentBuff> dictByPKey = new HashMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>(); private SortedMap<MSSBamAttachmentByContactIdxKey, SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>> dictByContactIdx = new TreeMap<MSSBamAttachmentByContactIdxKey, SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>>(); private SortedMap<MSSBamAttachmentByUDescrIdxKey, MSSBamAttachmentBuff> dictByUDescrIdx = new TreeMap<MSSBamAttachmentByUDescrIdxKey, MSSBamAttachmentBuff>(); private SortedMap<MSSBamAttachmentByMimeTypeIdxKey, SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>> dictByMimeTypeIdx = new TreeMap<MSSBamAttachmentByMimeTypeIdxKey, SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>>(); public MSSBamRamAttachmentTable(MSSBamBLRamSchema argSchema) { schema = argSchema; } public void createAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) { MSSBamAttachmentPKey pkey = schema.getFactoryAttachment().newPKey(); pkey.setRequiredAttachmentId(schema.nextAttachmentIdGen()); Buff.setRequiredAttachmentId(pkey.getRequiredAttachmentId()); MSSBamAttachmentByContactIdxKey keyContactIdx = schema.getFactoryAttachment().newContactIdxKey(); keyContactIdx.setRequiredContactId(Buff.getRequiredContactId()); MSSBamAttachmentByUDescrIdxKey keyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey(); keyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId()); keyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamAttachmentByMimeTypeIdxKey keyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey(); keyMimeTypeIdx.setOptionalMimeTypeId(Buff.getOptionalMimeTypeId()); // Validate unique indexes if (dictByPKey.containsKey(pkey)) { throw CFLib.getDefaultExceptionFactory().newPrimaryKeyNotNewException(getClass(), "createAttachment", pkey); } if (dictByUDescrIdx.containsKey(keyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "createAttachment", "AttachmentUDescrIdx", keyUDescrIdx); } // Validate foreign keys { boolean allNull = true; allNull = false; if (!allNull) { if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredContactId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createAttachment", "Container", "Contact", "Contact", null); } } } { boolean allNull = true; if (Buff.getOptionalMimeTypeId() != null) { allNull = false; } if (!allNull) { if (null == schema.getTableMimeType().readDerivedByIdIdx(Authorization, Buff.getOptionalMimeTypeId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "createAttachment", "Lookup", "MimeType", "MimeType", null); } } } // Proceed with adding the new record dictByPKey.put(pkey, Buff); SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictContactIdx; if (dictByContactIdx.containsKey(keyContactIdx)) { subdictContactIdx = dictByContactIdx.get(keyContactIdx); } else { subdictContactIdx = new TreeMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>(); dictByContactIdx.put(keyContactIdx, subdictContactIdx); } subdictContactIdx.put(pkey, Buff); dictByUDescrIdx.put(keyUDescrIdx, Buff); SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictMimeTypeIdx; if (dictByMimeTypeIdx.containsKey(keyMimeTypeIdx)) { subdictMimeTypeIdx = dictByMimeTypeIdx.get(keyMimeTypeIdx); } else { subdictMimeTypeIdx = new TreeMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>(); dictByMimeTypeIdx.put(keyMimeTypeIdx, subdictMimeTypeIdx); } subdictMimeTypeIdx.put(pkey, Buff); } public MSSBamAttachmentBuff readDerived(MSSBamAuthorization Authorization, MSSBamAttachmentPKey PKey) { final String S_ProcName = "MSSBamRamAttachment.readDerived() "; MSSBamAttachmentPKey key = schema.getFactoryAttachment().newPKey(); key.setRequiredAttachmentId(PKey.getRequiredAttachmentId()); MSSBamAttachmentBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamAttachmentBuff[] readAllDerived(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamAttachment.readAllDerived() "; MSSBamAttachmentBuff[] retList = new MSSBamAttachmentBuff[dictByPKey.values().size()]; Iterator<MSSBamAttachmentBuff> iter = dictByPKey.values().iterator(); int idx = 0; while (iter.hasNext()) { retList[idx++] = iter.next(); } return (retList); } public MSSBamAttachmentBuff[] readDerivedByContactIdx(MSSBamAuthorization Authorization, long ContactId) { final String S_ProcName = "MSSBamRamAttachment.readDerivedByContactIdx() "; MSSBamAttachmentByContactIdxKey key = schema.getFactoryAttachment().newContactIdxKey(); key.setRequiredContactId(ContactId); MSSBamAttachmentBuff[] recArray; if (dictByContactIdx.containsKey(key)) { SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictContactIdx = dictByContactIdx.get(key); recArray = new MSSBamAttachmentBuff[subdictContactIdx.size()]; Iterator<MSSBamAttachmentBuff> iter = subdictContactIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamAttachmentBuff[0]; } return (recArray); } public MSSBamAttachmentBuff readDerivedByUDescrIdx(MSSBamAuthorization Authorization, long ContactId, String Description) { final String S_ProcName = "MSSBamRamAttachment.readDerivedByUDescrIdx() "; MSSBamAttachmentByUDescrIdxKey key = schema.getFactoryAttachment().newUDescrIdxKey(); key.setRequiredContactId(ContactId); key.setRequiredDescription(Description); MSSBamAttachmentBuff buff; if (dictByUDescrIdx.containsKey(key)) { buff = dictByUDescrIdx.get(key); } else { buff = null; } return (buff); } public MSSBamAttachmentBuff[] readDerivedByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) { final String S_ProcName = "MSSBamRamAttachment.readDerivedByMimeTypeIdx() "; MSSBamAttachmentByMimeTypeIdxKey key = schema.getFactoryAttachment().newMimeTypeIdxKey(); key.setOptionalMimeTypeId(MimeTypeId); MSSBamAttachmentBuff[] recArray; if (dictByMimeTypeIdx.containsKey(key)) { SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictMimeTypeIdx = dictByMimeTypeIdx.get(key); recArray = new MSSBamAttachmentBuff[subdictMimeTypeIdx.size()]; Iterator<MSSBamAttachmentBuff> iter = subdictMimeTypeIdx.values().iterator(); int idx = 0; while (iter.hasNext()) { recArray[idx++] = iter.next(); } } else { recArray = new MSSBamAttachmentBuff[0]; } return (recArray); } public MSSBamAttachmentBuff readDerivedByIdIdx(MSSBamAuthorization Authorization, long AttachmentId) { final String S_ProcName = "MSSBamRamAttachment.readDerivedByIdIdx() "; MSSBamAttachmentPKey key = schema.getFactoryAttachment().newPKey(); key.setRequiredAttachmentId(AttachmentId); MSSBamAttachmentBuff buff; if (dictByPKey.containsKey(key)) { buff = dictByPKey.get(key); } else { buff = null; } return (buff); } public MSSBamAttachmentBuff readBuff(MSSBamAuthorization Authorization, MSSBamAttachmentPKey PKey) { final String S_ProcName = "MSSBamRamAttachment.readBuff() "; MSSBamAttachmentBuff buff = readDerived(Authorization, PKey); if ((buff != null) && (!buff.getClassCode().equals("ATTC"))) { buff = null; } return (buff); } public MSSBamAttachmentBuff[] readAllBuff(MSSBamAuthorization Authorization) { final String S_ProcName = "MSSBamRamAttachment.readAllBuff() "; MSSBamAttachmentBuff buff; ArrayList<MSSBamAttachmentBuff> filteredList = new ArrayList<MSSBamAttachmentBuff>(); MSSBamAttachmentBuff[] buffList = readAllDerived(Authorization); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ATTC")) { filteredList.add(buff); } } return (filteredList.toArray(new MSSBamAttachmentBuff[0])); } public MSSBamAttachmentBuff readBuffByIdIdx(MSSBamAuthorization Authorization, long AttachmentId) { final String S_ProcName = "MSSBamRamAttachment.readBuffByIdIdx() "; MSSBamAttachmentBuff buff = readDerivedByIdIdx(Authorization, AttachmentId); if ((buff != null) && buff.getClassCode().equals("ATTC")) { return ((MSSBamAttachmentBuff) buff); } else { return (null); } } public MSSBamAttachmentBuff[] readBuffByContactIdx(MSSBamAuthorization Authorization, long ContactId) { final String S_ProcName = "MSSBamRamAttachment.readBuffByContactIdx() "; MSSBamAttachmentBuff buff; ArrayList<MSSBamAttachmentBuff> filteredList = new ArrayList<MSSBamAttachmentBuff>(); MSSBamAttachmentBuff[] buffList = readDerivedByContactIdx(Authorization, ContactId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ATTC")) { filteredList.add((MSSBamAttachmentBuff) buff); } } return (filteredList.toArray(new MSSBamAttachmentBuff[0])); } public MSSBamAttachmentBuff readBuffByUDescrIdx(MSSBamAuthorization Authorization, long ContactId, String Description) { final String S_ProcName = "MSSBamRamAttachment.readBuffByUDescrIdx() "; MSSBamAttachmentBuff buff = readDerivedByUDescrIdx(Authorization, ContactId, Description); if ((buff != null) && buff.getClassCode().equals("ATTC")) { return ((MSSBamAttachmentBuff) buff); } else { return (null); } } public MSSBamAttachmentBuff[] readBuffByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) { final String S_ProcName = "MSSBamRamAttachment.readBuffByMimeTypeIdx() "; MSSBamAttachmentBuff buff; ArrayList<MSSBamAttachmentBuff> filteredList = new ArrayList<MSSBamAttachmentBuff>(); MSSBamAttachmentBuff[] buffList = readDerivedByMimeTypeIdx(Authorization, MimeTypeId); for (int idx = 0; idx < buffList.length; idx++) { buff = buffList[idx]; if ((buff != null) && buff.getClassCode().equals("ATTC")) { filteredList.add((MSSBamAttachmentBuff) buff); } } return (filteredList.toArray(new MSSBamAttachmentBuff[0])); } public void updateAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) { MSSBamAttachmentPKey pkey = schema.getFactoryAttachment().newPKey(); pkey.setRequiredAttachmentId(Buff.getRequiredAttachmentId()); MSSBamAttachmentBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "updateAttachment", "Existing record not found", "Attachment", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "updateAttachment", pkey); } Buff.setRequiredRevision(Buff.getRequiredRevision() + 1); MSSBamAttachmentByContactIdxKey existingKeyContactIdx = schema.getFactoryAttachment().newContactIdxKey(); existingKeyContactIdx.setRequiredContactId(existing.getRequiredContactId()); MSSBamAttachmentByContactIdxKey newKeyContactIdx = schema.getFactoryAttachment().newContactIdxKey(); newKeyContactIdx.setRequiredContactId(Buff.getRequiredContactId()); MSSBamAttachmentByUDescrIdxKey existingKeyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey(); existingKeyUDescrIdx.setRequiredContactId(existing.getRequiredContactId()); existingKeyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamAttachmentByUDescrIdxKey newKeyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey(); newKeyUDescrIdx.setRequiredContactId(Buff.getRequiredContactId()); newKeyUDescrIdx.setRequiredDescription(Buff.getRequiredDescription()); MSSBamAttachmentByMimeTypeIdxKey existingKeyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey(); existingKeyMimeTypeIdx.setOptionalMimeTypeId(existing.getOptionalMimeTypeId()); MSSBamAttachmentByMimeTypeIdxKey newKeyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey(); newKeyMimeTypeIdx.setOptionalMimeTypeId(Buff.getOptionalMimeTypeId()); // Check unique indexes if (!existingKeyUDescrIdx.equals(newKeyUDescrIdx)) { if (dictByUDescrIdx.containsKey(newKeyUDescrIdx)) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), "updateAttachment", "AttachmentUDescrIdx", newKeyUDescrIdx); } } // Validate foreign keys { boolean allNull = true; if (allNull) { if (null == schema.getTableContact().readDerivedByIdIdx(Authorization, Buff.getRequiredContactId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateAttachment", "Container", "Contact", "Contact", null); } } } { boolean allNull = true; if (Buff.getOptionalMimeTypeId() != null) { allNull = false; } if (allNull) { if (null == schema.getTableMimeType().readDerivedByIdIdx(Authorization, Buff.getOptionalMimeTypeId())) { throw CFLib.getDefaultExceptionFactory().newUnresolvedRelationException(getClass(), "updateAttachment", "Lookup", "MimeType", "MimeType", null); } } } // Update is valid SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> 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<MSSBamAttachmentPKey, MSSBamAttachmentBuff>(); dictByContactIdx.put(newKeyContactIdx, subdict); } subdict.put(pkey, Buff); dictByUDescrIdx.remove(existingKeyUDescrIdx); dictByUDescrIdx.put(newKeyUDescrIdx, Buff); subdict = dictByMimeTypeIdx.get(existingKeyMimeTypeIdx); if (subdict != null) { subdict.remove(pkey); } if (dictByMimeTypeIdx.containsKey(newKeyMimeTypeIdx)) { subdict = dictByMimeTypeIdx.get(newKeyMimeTypeIdx); } else { subdict = new TreeMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff>(); dictByMimeTypeIdx.put(newKeyMimeTypeIdx, subdict); } subdict.put(pkey, Buff); } public void deleteAttachment(MSSBamAuthorization Authorization, MSSBamAttachmentBuff Buff) { final String S_ProcName = "MSSBamRamAttachmentTable.deleteAttachment() "; MSSBamAttachmentPKey pkey = schema.getFactoryAttachment().newPKey(); pkey.setRequiredAttachmentId(schema.nextAttachmentIdGen()); MSSBamAttachmentBuff existing = dictByPKey.get(pkey); if (existing == null) { throw CFLib.getDefaultExceptionFactory().newStaleCacheDetectedException(getClass(), "deleteAttachment", "Existing record not found", "Attachment", pkey); } if (existing.getRequiredRevision() != Buff.getRequiredRevision()) { throw CFLib.getDefaultExceptionFactory().newCollisionDetectedException(getClass(), "deleteAttachment", pkey); } MSSBamAttachmentByContactIdxKey keyContactIdx = schema.getFactoryAttachment().newContactIdxKey(); keyContactIdx.setRequiredContactId(existing.getRequiredContactId()); MSSBamAttachmentByUDescrIdxKey keyUDescrIdx = schema.getFactoryAttachment().newUDescrIdxKey(); keyUDescrIdx.setRequiredContactId(existing.getRequiredContactId()); keyUDescrIdx.setRequiredDescription(existing.getRequiredDescription()); MSSBamAttachmentByMimeTypeIdxKey keyMimeTypeIdx = schema.getFactoryAttachment().newMimeTypeIdxKey(); keyMimeTypeIdx.setOptionalMimeTypeId(existing.getOptionalMimeTypeId()); // Validate reverse foreign keys if (schema.getTableAttachmentTag().readDerivedByAttachmentIdx(Authorization, existing.getRequiredAttachmentId()).length > 0) { throw CFLib.getDefaultExceptionFactory().newDependentsDetectedException(getClass(), "deleteAttachment", "Container", "AttachmentTagAttachment", "AttachmentTag", pkey); } // Delete is valid SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdict; dictByPKey.remove(pkey); subdict = dictByContactIdx.get(keyContactIdx); subdict.remove(pkey); dictByUDescrIdx.remove(keyUDescrIdx); subdict = dictByMimeTypeIdx.get(keyMimeTypeIdx); subdict.remove(pkey); } public MSSBamCursor openAttachmentCursorAll(MSSBamAuthorization Authorization) { MSSBamCursor cursor = new MSSBamRamAttachmentCursor(Authorization, schema, dictByPKey.values()); return (cursor); } public MSSBamCursor openAttachmentCursorByContactIdx(MSSBamAuthorization Authorization, long ContactId) { MSSBamCursor cursor; MSSBamAttachmentByContactIdxKey key = schema.getFactoryAttachment().newContactIdxKey(); key.setRequiredContactId(ContactId); if (dictByContactIdx.containsKey(key)) { SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictContactIdx = dictByContactIdx.get(key); cursor = new MSSBamRamAttachmentCursor(Authorization, schema, subdictContactIdx.values()); } else { cursor = new MSSBamRamAttachmentCursor(Authorization, schema, new ArrayList<MSSBamAttachmentBuff>()); } return (cursor); } public MSSBamCursor openAttachmentCursorByMimeTypeIdx(MSSBamAuthorization Authorization, Short MimeTypeId) { MSSBamCursor cursor; MSSBamAttachmentByMimeTypeIdxKey key = schema.getFactoryAttachment().newMimeTypeIdxKey(); key.setOptionalMimeTypeId(MimeTypeId); if (dictByMimeTypeIdx.containsKey(key)) { SortedMap<MSSBamAttachmentPKey, MSSBamAttachmentBuff> subdictMimeTypeIdx = dictByMimeTypeIdx.get(key); cursor = new MSSBamRamAttachmentCursor(Authorization, schema, subdictMimeTypeIdx.values()); } else { cursor = new MSSBamRamAttachmentCursor(Authorization, schema, new ArrayList<MSSBamAttachmentBuff>()); } return (cursor); } public void closeAttachmentCursor(MSSBamCursor Cursor) { // Cursor.DataReader.Close(); } public MSSBamAttachmentBuff nextAttachmentCursor(MSSBamCursor Cursor) { MSSBamRamAttachmentCursor cursor = (MSSBamRamAttachmentCursor) Cursor; MSSBamAttachmentBuff rec = cursor.getCursor().next(); cursor.setRowIdx(cursor.getRowIdx() + 1); return (rec); } public MSSBamAttachmentBuff prevAttachmentCursor(MSSBamCursor Cursor) { int targetRowIdx = (Cursor.getRowIdx() > 1) ? Cursor.getRowIdx() - 1 : 1; MSSBamAttachmentBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAttachmentCursor(Cursor); } return (rec); } public MSSBamAttachmentBuff firstAttachmentCursor(MSSBamCursor Cursor) { int targetRowIdx = 1; MSSBamAttachmentBuff rec = null; Cursor.reset(); while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAttachmentCursor(Cursor); } return (rec); } public MSSBamAttachmentBuff lastAttachmentCursor(MSSBamCursor Cursor) { throw CFLib.getDefaultExceptionFactory().newNotImplementedYetException(getClass(), "lastAttachmentCursor"); } public MSSBamAttachmentBuff nthAttachmentCursor(MSSBamCursor Cursor, int Idx) { int targetRowIdx = Idx; MSSBamAttachmentBuff rec = null; if (Cursor.getRowIdx() >= targetRowIdx) { Cursor.reset(); } while (Cursor.getRowIdx() < targetRowIdx) { rec = nextAttachmentCursor(Cursor); } return (rec); } }