litesoft
@ 151
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.orsup.nonpublic; import java.io.*; import java.util.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.lazyload.*; import org.litesoft.orsup.otherattributeaccessors.*; import org.litesoft.util.*; public class BlobHandler<Owner extends PersistentObjectImpl> extends AbstractLazyLoadToOne<Owner, Blob> implements SizeableStreamAccessor { protected AttributeAccessorSCDtoBlob<Owner> mSCDtoOne; public BlobHandler( Owner pOwner, AttributeAccessorSCDtoBlob<Owner> pSCDtoOne ) { super( pOwner, pSCDtoOne ); mSCDtoOne = pSCDtoOne; } @Override public final AttributeAccessorSCDtoBlob<Owner> getSCDtoOne() { return mSCDtoOne; } @SuppressWarnings({"unchecked"}) private Blob getBlob() { return mOwner.processLazyLoadAccess( this ); } @SuppressWarnings({"unchecked"}) private Blob createNewBlob() { Blob zBlob = new Blob( mOwner.getTransaction() ); setBlob( zBlob ); return zBlob; } @SuppressWarnings({"unchecked"}) public void setBlob( Blob pBlob ) { mOwner.processLazyLoadMutation( this, pBlob ); } /** * @param pNewThem !null */ public final Object extract_db_value( Blob pNewThem ) { return LL_extract_db_value( pNewThem, null ); } /** * @param pDBValue !null */ @Override protected final PersistentObject<?> LL_loadValueWithNonNull( Object pDBValue ) { //noinspection unchecked return mOwner.getFinder().findOne( Blob.class, new UniqueKeySingle( Blob.CD_ID, pDBValue ) ); } @Override protected String getBackRefOnUnWire( String pBackRefAttributeNameOnNewThem ) { return null; } /** * @param pNewThem !null */ @Override protected final Object LL_extract_db_value( PersistentObjectImpl pNewThem, String pBackRefAttributeNameOnNewThem ) { return pNewThem.getAttributeValue( Blob.CD_ID.getName() ); } // SizeableStreamAccessor /** * @return Actual Size or -1 if not readable */ @Override public long getSizeInBytes() { return isReadable() ? getBlob().getSizeInBytes() : -1; } @Override public boolean isReadable() { Blob zBlob = getBlob(); return (zBlob != null) && zBlob.isReadable(); } @Override public boolean isWritable() { if ( mOwner.getTransaction() != null ) { Blob zBlob = getBlob(); if ( (zBlob == null) || !zBlob.isStreamOpen() ) { return true; } } return false; } /** * @return !null * * @throws IllegalStateException - if not Readable * @throws MissingResourceException - if resource not found * @throws IOException - any other IOException */ @Override public InputStream readOpen() throws IllegalStateException, MissingResourceException, IOException { Blob zBlob = getBlob(); if ( zBlob == null ) { throw new IllegalStateException( "Not Readable: No Data ever written" ); } return zBlob.readOpen(); } /** * @return !null * * @throws UnsupportedOperationException - if not Writable (ie already written) * @throws IllegalStateException - if something required for writing is missing * @throws IOException - any other IOException */ @Override public OutputStream writeOpen() throws UnsupportedOperationException, IllegalStateException, IOException { if ( mOwner.getTransaction() == null ) { throw new UnsupportedOperationException( "Not in a transaction" ); } Blob zBlob = getBlob(); if ( (zBlob != null) && zBlob.isStreamOpen() ) { throw new UnsupportedOperationException( "Already Writing" ); } return createNewBlob().writeOpen(); } } |