litesoft
@ 804
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.GWT.forms.server.support; import java.util.*; import java.util.concurrent.atomic.*; import org.litesoft.GWT.forms.server.*; import java.io.*; import org.litesoft.core.typeutils.*; import org.litesoft.ui.def.nonpublic.support.*; public abstract class InstanceHelper { private AtomicBoolean mDisposed = new AtomicBoolean( false ); protected TypeHelper mTypeHelper; private String mRowObjectKey; private FormDataRowKey mFormDataRowKey = null; private Set<AttributeUpdateFormData> mUpdates = new HashSet<AttributeUpdateFormData>(); private Map<TypeHelper, List<String>> mRelSubFormRefToRowObjectKeys = new HashMap<TypeHelper, List<String>>(); public InstanceHelper( TypeHelper pTypeHelper, String pRowObjectKey ) { mTypeHelper = pTypeHelper; if ( null != (mRowObjectKey = pRowObjectKey) ) { mFormDataRowKey = new FormDataRowKey( mTypeHelper.getFormUniqueID(), mRowObjectKey ); } } public FormDataRowKey getFormDataRowKey() { return mFormDataRowKey; } public final boolean isDisposed() { return mDisposed.get(); } public final void dispose() { if ( !mDisposed.getAndSet( true ) ) // set true and test for false { try { LLdispose(); } catch ( RuntimeException e ) { FormServicePeer.LOGGER.error.log( e ); } synchronized ( this ) { mRowObjectKey = null; mUpdates.clear(); mRelSubFormRefToRowObjectKeys.clear(); } } } protected void LLdispose() { } public AMDconverterPair findAttribute( Integer pUniqueID ) { return mTypeHelper.findAttribute( pUniqueID ); } public synchronized void resetSavedAttibuteValues() { mUpdates.clear(); mRelSubFormRefToRowObjectKeys.clear(); } public synchronized void loadAllAttributeValues() { for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) { AttributeMetaData meta = pair.getAttributeMetaData(); AttributeUpdateFormData data = new AttributeUpdateFormData( mFormDataRowKey, meta ); String attributeRef = meta.getAttributeReference(); Object value = getRawAttributeValue( attributeRef ); data.value( pair.getDataConverter().convertToPassable( value ) ); mTypeHelper.getCollectorProxy().add( data ); mUpdates.add( data ); } checkForChangesInSubForms(); } public List<AttributeUpdateFormData> findAttributeUpdateFormData( String pFieldName ) { List<AttributeUpdateFormData> zData = new ArrayList<AttributeUpdateFormData>(); for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) { AttributeMetaData meta = pair.getAttributeMetaData(); if ( pFieldName.equals( meta.getAttributeReference() ) ) { zData.add( new AttributeUpdateFormData( mFormDataRowKey, meta ) ); } } return zData; } public abstract boolean valueUpdated( String pAttributeReference, Object pValueForAttribute ); abstract protected Object getRawAttributeValue( String pAttributeRef ); abstract protected void setRawAttributeValue( String pAttributeRef, Object pValueForAttribute ); public boolean actionRequest( String pActionID, boolean pInputAction ) { return (!mTypeHelper.isBaseForm() && "RemoveRow".equals( pActionID )) ? removeRow() : mTypeHelper.actionRequest( pActionID, pInputAction, this ); } /** * Only called for Non-BaseForms * * @return true if successful */ protected boolean removeRow() { return false; } public synchronized void checkForChanges( AMDconverterPair pChangingPair, Serializable pNewValue, String pResolvedError ) { if ( mTypeHelper != null ) { for ( AttributeUpdateFormData update : mUpdates ) { AMDconverterPair pair = mTypeHelper.findAttribute( update.getUniqueID() ); // use of == okay, findAttribute will return the same obj for the same ref if ( pair != pChangingPair ) { checkForSideEffect( pair, update ); } else if ( pResolvedError == null ) { // as this is the value we changed, change its update record now so we'll // only report it as a change if it has FURTHER mutated from what we did to it. update.value( pNewValue ); checkForSideEffect( pair, update ); } else { mTypeHelper.getCollectorProxy().add( update.copyForError( pResolvedError ) ); } } checkForChangesInSubForms(); } } private void checkForSideEffect( AMDconverterPair pConverterPair, AttributeUpdateFormData pUpdate ) { Object raw = getRawAttributeValue( pUpdate.getAttributeReference() ); Serializable current = pConverterPair.getDataConverter().convertToPassable( raw ); Serializable last = pUpdate.getValue(); if ( !Objects.areNonArraysEqual( current, last ) ) { pUpdate.value( current ); mTypeHelper.getCollectorProxy().add( pUpdate ); } } private void checkForChangesInSubForms() { Map<String, TypeHelper[]> zRelRefsToTypes = mTypeHelper.getRelSubFormRefToTypes(); if ( !zRelRefsToTypes.isEmpty() ) { for ( Map.Entry<String, TypeHelper[]> entry : zRelRefsToTypes.entrySet() ) { String zRelSubFormRef = entry.getKey(); TypeHelper[] zHelpers = entry.getValue(); for ( TypeHelper zHelper : zHelpers ) { checkForChangesInSubForms( zRelSubFormRef, zHelper ); } } } } abstract protected List<String> getRowKeysFor( String pRelSubFormRef ); private synchronized void checkForChangesInSubForms( String pRelSubFormRef, TypeHelper pTypeHelper ) { List<String> zActualRowKeys = getRowKeysFor( pRelSubFormRef ); List<String> zReportedRowKeys = mRelSubFormRefToRowObjectKeys.get( pTypeHelper ); boolean zRowsDifferent = false; if ( zReportedRowKeys == null ) { zReportedRowKeys = Collections.emptyList(); zRowsDifferent = true; } List<String> zToDeleteRowKeys = new ArrayList<String>( zReportedRowKeys ); List<String> zToAddRowKeys = new ArrayList<String>(); for ( int i = 0; i < zActualRowKeys.size(); i++ ) { String zActualRowKey = zActualRowKeys.get( i ); if ( !zReportedRowKeys.contains( zActualRowKey ) ) { zToAddRowKeys.add( zActualRowKey ); } else { zToDeleteRowKeys.remove( zActualRowKey ); if ( !zRowsDifferent ) { zRowsDifferent = (zReportedRowKeys.size() <= i) || !zActualRowKey.equals( zReportedRowKeys.get( i ) ); } } } for ( String zToDeleteRowKey : zToDeleteRowKeys ) { zRowsDifferent = true; pTypeHelper.removeInstanceHelper( zToDeleteRowKey ); } for ( String zToAddRowKey : zToAddRowKeys ) { zRowsDifferent = true; InstanceHelper zNewHelper = pTypeHelper.createRelSubInstanceHelper( this, zToAddRowKey ); zNewHelper.loadAllAttributeValues(); } if ( zRowsDifferent ) { mRelSubFormRefToRowObjectKeys.put( pTypeHelper, zActualRowKeys ); String[] zKeysAsArray = zActualRowKeys.toArray( new String[zActualRowKeys.size()] ); mTypeHelper.getCollectorProxy().add( new RelSubFormRowKeys( mFormDataRowKey, mTypeHelper.getFormUniqueID(), // pRelSubFormRef, zKeysAsArray ) ); } } public TypeHelper getTypeHelper() { return mTypeHelper; } } |