Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/OldServer/src/org/litesoft/GWT/forms/server/support/InstanceHelper.java

Diff revisions: vs.
  @@ -1,201 +1,201 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.GWT.forms.server.support;
3 -
4 - import org.litesoft.GWT.forms.server.*;
5 - import org.litesoft.commonfoundation.typeutils.Objects;
6 - import org.litesoft.ui.def.nonpublic.support.*;
7 -
8 - import java.io.*;
9 - import java.util.*;
10 - import java.util.concurrent.atomic.*;
11 -
12 - public abstract class InstanceHelper {
13 - private AtomicBoolean mDisposed = new AtomicBoolean( false );
14 - protected TypeHelper mTypeHelper;
15 - private String mRowObjectKey;
16 - private FormDataRowKey mFormDataRowKey = null;
17 -
18 - private Set<AttributeUpdateFormData> mUpdates = new HashSet<AttributeUpdateFormData>();
19 - private Map<TypeHelper, List<String>> mRelSubFormRefToRowObjectKeys =
20 - new HashMap<TypeHelper, List<String>>();
21 -
22 - public InstanceHelper( TypeHelper pTypeHelper, String pRowObjectKey ) {
23 - mTypeHelper = pTypeHelper;
24 - if ( null != (mRowObjectKey = pRowObjectKey) ) {
25 - mFormDataRowKey = new FormDataRowKey( mTypeHelper.getFormUniqueID(), mRowObjectKey );
26 - }
27 - }
28 -
29 - public FormDataRowKey getFormDataRowKey() {
30 - return mFormDataRowKey;
31 - }
32 -
33 - public final boolean isDisposed() {
34 - return mDisposed.get();
35 - }
36 -
37 - public final void dispose() {
38 - if ( !mDisposed.getAndSet( true ) ) // set true and test for false
39 - {
40 - try {
41 - LLdispose();
42 - }
43 - catch ( RuntimeException e ) {
44 - FormServicePeer.LOGGER.error.log( e );
45 - }
46 - synchronized ( this ) {
47 - mRowObjectKey = null;
48 - mUpdates.clear();
49 - mRelSubFormRefToRowObjectKeys.clear();
50 - }
51 - }
52 - }
53 -
54 - protected void LLdispose() {
55 - }
56 -
57 - public AMDconverterPair findAttribute( Integer pUniqueID ) {
58 - return mTypeHelper.findAttribute( pUniqueID );
59 - }
60 -
61 - public synchronized void resetSavedAttibuteValues() {
62 - mUpdates.clear();
63 - mRelSubFormRefToRowObjectKeys.clear();
64 - }
65 -
66 - public synchronized void loadAllAttributeValues() {
67 - for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) {
68 - AttributeMetaData meta = pair.getAttributeMetaData();
69 - AttributeUpdateFormData data = new AttributeUpdateFormData( mFormDataRowKey, meta );
70 - String attributeRef = meta.getAttributeReference();
71 - Object value = getRawAttributeValue( attributeRef );
72 - data.value( pair.getDataConverter().convertToPassable( value ) );
73 - mTypeHelper.getCollectorProxy().add( data );
74 - mUpdates.add( data );
75 - }
76 - checkForChangesInSubForms();
77 - }
78 -
79 - public List<AttributeUpdateFormData> findAttributeUpdateFormData( String pFieldName ) {
80 -
81 - List<AttributeUpdateFormData> zData = new ArrayList<AttributeUpdateFormData>();
82 - for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) {
83 - AttributeMetaData meta = pair.getAttributeMetaData();
84 - if ( pFieldName.equals( meta.getAttributeReference() ) ) {
85 - zData.add( new AttributeUpdateFormData( mFormDataRowKey, meta ) );
86 - }
87 - }
88 - return zData;
89 - }
90 -
91 - public abstract boolean valueUpdated( String pAttributeReference, Object pValueForAttribute );
92 -
93 - abstract protected Object getRawAttributeValue( String pAttributeRef );
94 -
95 - abstract protected void setRawAttributeValue( String pAttributeRef, Object pValueForAttribute );
96 -
97 - public boolean actionRequest( String pActionID, boolean pInputAction ) {
98 - return (!mTypeHelper.isBaseForm() && "RemoveRow".equals( pActionID )) ? removeRow() :
99 - mTypeHelper.actionRequest( pActionID, pInputAction, this );
100 - }
101 -
102 - /**
103 - * Only called for Non-BaseForms
104 - *
105 - * @return true if successful
106 - */
107 - protected boolean removeRow() {
108 - return false;
109 - }
110 -
111 - public synchronized void checkForChanges( AMDconverterPair pChangingPair, Serializable pNewValue,
112 - String pResolvedError ) {
113 - if ( mTypeHelper != null ) {
114 - for ( AttributeUpdateFormData update : mUpdates ) {
115 - AMDconverterPair pair = mTypeHelper.findAttribute( update.getUniqueID() );
116 - // use of == okay, findAttribute will return the same obj for the same ref
117 - if ( pair != pChangingPair ) {
118 - checkForSideEffect( pair, update );
119 - } else if ( pResolvedError == null ) {
120 - // as this is the value we changed, change its update record now so we'll
121 - // only report it as a change if it has FURTHER mutated from what we did to it.
122 - update.value( pNewValue );
123 - checkForSideEffect( pair, update );
124 - } else {
125 - mTypeHelper.getCollectorProxy().add( update.copyForError( pResolvedError ) );
126 - }
127 - }
128 - checkForChangesInSubForms();
129 - }
130 - }
131 -
132 - private void checkForSideEffect( AMDconverterPair pConverterPair, AttributeUpdateFormData pUpdate ) {
133 - Object raw = getRawAttributeValue( pUpdate.getAttributeReference() );
134 - Serializable current = pConverterPair.getDataConverter().convertToPassable( raw );
135 - Serializable last = pUpdate.getValue();
136 -
137 - if ( !Objects.areNonArraysEqual( current, last ) ) {
138 - pUpdate.value( current );
139 - mTypeHelper.getCollectorProxy().add( pUpdate );
140 - }
141 - }
142 -
143 - private void checkForChangesInSubForms() {
144 - Map<String, TypeHelper[]> zRelRefsToTypes = mTypeHelper.getRelSubFormRefToTypes();
145 - if ( !zRelRefsToTypes.isEmpty() ) {
146 - for ( Map.Entry<String, TypeHelper[]> entry : zRelRefsToTypes.entrySet() ) {
147 - String zRelSubFormRef = entry.getKey();
148 - TypeHelper[] zHelpers = entry.getValue();
149 - for ( TypeHelper zHelper : zHelpers ) {
150 - checkForChangesInSubForms( zRelSubFormRef, zHelper );
151 - }
152 - }
153 - }
154 - }
155 -
156 - abstract protected List<String> getRowKeysFor( String pRelSubFormRef );
157 -
158 - private synchronized void checkForChangesInSubForms( String pRelSubFormRef, TypeHelper pTypeHelper ) {
159 - List<String> zActualRowKeys = getRowKeysFor( pRelSubFormRef );
160 - List<String> zReportedRowKeys = mRelSubFormRefToRowObjectKeys.get( pTypeHelper );
161 - boolean zRowsDifferent = false;
162 - if ( zReportedRowKeys == null ) {
163 - zReportedRowKeys = Collections.emptyList();
164 - zRowsDifferent = true;
165 - }
166 - List<String> zToDeleteRowKeys = new ArrayList<String>( zReportedRowKeys );
167 - List<String> zToAddRowKeys = new ArrayList<String>();
168 - for ( int i = 0; i < zActualRowKeys.size(); i++ ) {
169 - String zActualRowKey = zActualRowKeys.get( i );
170 - if ( !zReportedRowKeys.contains( zActualRowKey ) ) {
171 - zToAddRowKeys.add( zActualRowKey );
172 - } else {
173 - zToDeleteRowKeys.remove( zActualRowKey );
174 - if ( !zRowsDifferent ) {
175 - zRowsDifferent = (zReportedRowKeys.size() <= i) ||
176 - !zActualRowKey.equals( zReportedRowKeys.get( i ) );
177 - }
178 - }
179 - }
180 - for ( String zToDeleteRowKey : zToDeleteRowKeys ) {
181 - zRowsDifferent = true;
182 - pTypeHelper.removeInstanceHelper( zToDeleteRowKey );
183 - }
184 - for ( String zToAddRowKey : zToAddRowKeys ) {
185 - zRowsDifferent = true;
186 - InstanceHelper zNewHelper = pTypeHelper.createRelSubInstanceHelper( this, zToAddRowKey );
187 - zNewHelper.loadAllAttributeValues();
188 - }
189 - if ( zRowsDifferent ) {
190 - mRelSubFormRefToRowObjectKeys.put( pTypeHelper, zActualRowKeys );
191 - String[] zKeysAsArray = zActualRowKeys.toArray( new String[zActualRowKeys.size()] );
192 - mTypeHelper.getCollectorProxy().add(
193 - new RelSubFormRowKeys( mFormDataRowKey, mTypeHelper.getFormUniqueID(), //
194 - pRelSubFormRef, zKeysAsArray ) );
195 - }
196 - }
197 -
198 - public TypeHelper getTypeHelper() {
199 - return mTypeHelper;
200 - }
201 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.GWT.forms.server.support;
3 +
4 + import org.litesoft.GWT.forms.server.*;
5 + import org.litesoft.commonfoundation.base.*;
6 + import org.litesoft.ui.def.nonpublic.support.*;
7 +
8 + import java.io.*;
9 + import java.util.*;
10 + import java.util.concurrent.atomic.*;
11 +
12 + public abstract class InstanceHelper {
13 + private AtomicBoolean mDisposed = new AtomicBoolean( false );
14 + protected TypeHelper mTypeHelper;
15 + private String mRowObjectKey;
16 + private FormDataRowKey mFormDataRowKey = null;
17 +
18 + private Set<AttributeUpdateFormData> mUpdates = new HashSet<AttributeUpdateFormData>();
19 + private Map<TypeHelper, List<String>> mRelSubFormRefToRowObjectKeys =
20 + new HashMap<TypeHelper, List<String>>();
21 +
22 + public InstanceHelper( TypeHelper pTypeHelper, String pRowObjectKey ) {
23 + mTypeHelper = pTypeHelper;
24 + if ( null != (mRowObjectKey = pRowObjectKey) ) {
25 + mFormDataRowKey = new FormDataRowKey( mTypeHelper.getFormUniqueID(), mRowObjectKey );
26 + }
27 + }
28 +
29 + public FormDataRowKey getFormDataRowKey() {
30 + return mFormDataRowKey;
31 + }
32 +
33 + public final boolean isDisposed() {
34 + return mDisposed.get();
35 + }
36 +
37 + public final void dispose() {
38 + if ( !mDisposed.getAndSet( true ) ) // set true and test for false
39 + {
40 + try {
41 + LLdispose();
42 + }
43 + catch ( RuntimeException e ) {
44 + FormServicePeer.LOGGER.error.log( e );
45 + }
46 + synchronized ( this ) {
47 + mRowObjectKey = null;
48 + mUpdates.clear();
49 + mRelSubFormRefToRowObjectKeys.clear();
50 + }
51 + }
52 + }
53 +
54 + protected void LLdispose() {
55 + }
56 +
57 + public AMDconverterPair findAttribute( Integer pUniqueID ) {
58 + return mTypeHelper.findAttribute( pUniqueID );
59 + }
60 +
61 + public synchronized void resetSavedAttibuteValues() {
62 + mUpdates.clear();
63 + mRelSubFormRefToRowObjectKeys.clear();
64 + }
65 +
66 + public synchronized void loadAllAttributeValues() {
67 + for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) {
68 + AttributeMetaData meta = pair.getAttributeMetaData();
69 + AttributeUpdateFormData data = new AttributeUpdateFormData( mFormDataRowKey, meta );
70 + String attributeRef = meta.getAttributeReference();
71 + Object value = getRawAttributeValue( attributeRef );
72 + data.value( pair.getDataConverter().convertToPassable( value ) );
73 + mTypeHelper.getCollectorProxy().add( data );
74 + mUpdates.add( data );
75 + }
76 + checkForChangesInSubForms();
77 + }
78 +
79 + public List<AttributeUpdateFormData> findAttributeUpdateFormData( String pFieldName ) {
80 +
81 + List<AttributeUpdateFormData> zData = new ArrayList<AttributeUpdateFormData>();
82 + for ( AMDconverterPair pair : mTypeHelper.getAMDconvertPairs() ) {
83 + AttributeMetaData meta = pair.getAttributeMetaData();
84 + if ( pFieldName.equals( meta.getAttributeReference() ) ) {
85 + zData.add( new AttributeUpdateFormData( mFormDataRowKey, meta ) );
86 + }
87 + }
88 + return zData;
89 + }
90 +
91 + public abstract boolean valueUpdated( String pAttributeReference, Object pValueForAttribute );
92 +
93 + abstract protected Object getRawAttributeValue( String pAttributeRef );
94 +
95 + abstract protected void setRawAttributeValue( String pAttributeRef, Object pValueForAttribute );
96 +
97 + public boolean actionRequest( String pActionID, boolean pInputAction ) {
98 + return (!mTypeHelper.isBaseForm() && "RemoveRow".equals( pActionID )) ? removeRow() :
99 + mTypeHelper.actionRequest( pActionID, pInputAction, this );
100 + }
101 +
102 + /**
103 + * Only called for Non-BaseForms
104 + *
105 + * @return true if successful
106 + */
107 + protected boolean removeRow() {
108 + return false;
109 + }
110 +
111 + public synchronized void checkForChanges( AMDconverterPair pChangingPair, Serializable pNewValue,
112 + String pResolvedError ) {
113 + if ( mTypeHelper != null ) {
114 + for ( AttributeUpdateFormData update : mUpdates ) {
115 + AMDconverterPair pair = mTypeHelper.findAttribute( update.getUniqueID() );
116 + // use of == okay, findAttribute will return the same obj for the same ref
117 + if ( pair != pChangingPair ) {
118 + checkForSideEffect( pair, update );
119 + } else if ( pResolvedError == null ) {
120 + // as this is the value we changed, change its update record now so we'll
121 + // only report it as a change if it has FURTHER mutated from what we did to it.
122 + update.value( pNewValue );
123 + checkForSideEffect( pair, update );
124 + } else {
125 + mTypeHelper.getCollectorProxy().add( update.copyForError( pResolvedError ) );
126 + }
127 + }
128 + checkForChangesInSubForms();
129 + }
130 + }
131 +
132 + private void checkForSideEffect( AMDconverterPair pConverterPair, AttributeUpdateFormData pUpdate ) {
133 + Object raw = getRawAttributeValue( pUpdate.getAttributeReference() );
134 + Serializable current = pConverterPair.getDataConverter().convertToPassable( raw );
135 + Serializable last = pUpdate.getValue();
136 +
137 + if ( !Currently.areEqual( current, last ) ) {
138 + pUpdate.value( current );
139 + mTypeHelper.getCollectorProxy().add( pUpdate );
140 + }
141 + }
142 +
143 + private void checkForChangesInSubForms() {
144 + Map<String, TypeHelper[]> zRelRefsToTypes = mTypeHelper.getRelSubFormRefToTypes();
145 + if ( !zRelRefsToTypes.isEmpty() ) {
146 + for ( Map.Entry<String, TypeHelper[]> entry : zRelRefsToTypes.entrySet() ) {
147 + String zRelSubFormRef = entry.getKey();
148 + TypeHelper[] zHelpers = entry.getValue();
149 + for ( TypeHelper zHelper : zHelpers ) {
150 + checkForChangesInSubForms( zRelSubFormRef, zHelper );
151 + }
152 + }
153 + }
154 + }
155 +
156 + abstract protected List<String> getRowKeysFor( String pRelSubFormRef );
157 +
158 + private synchronized void checkForChangesInSubForms( String pRelSubFormRef, TypeHelper pTypeHelper ) {
159 + List<String> zActualRowKeys = getRowKeysFor( pRelSubFormRef );
160 + List<String> zReportedRowKeys = mRelSubFormRefToRowObjectKeys.get( pTypeHelper );
161 + boolean zRowsDifferent = false;
162 + if ( zReportedRowKeys == null ) {
163 + zReportedRowKeys = Collections.emptyList();
164 + zRowsDifferent = true;
165 + }
166 + List<String> zToDeleteRowKeys = new ArrayList<String>( zReportedRowKeys );
167 + List<String> zToAddRowKeys = new ArrayList<String>();
168 + for ( int i = 0; i < zActualRowKeys.size(); i++ ) {
169 + String zActualRowKey = zActualRowKeys.get( i );
170 + if ( !zReportedRowKeys.contains( zActualRowKey ) ) {
171 + zToAddRowKeys.add( zActualRowKey );
172 + } else {
173 + zToDeleteRowKeys.remove( zActualRowKey );
174 + if ( !zRowsDifferent ) {
175 + zRowsDifferent = (zReportedRowKeys.size() <= i) ||
176 + !zActualRowKey.equals( zReportedRowKeys.get( i ) );
177 + }
178 + }
179 + }
180 + for ( String zToDeleteRowKey : zToDeleteRowKeys ) {
181 + zRowsDifferent = true;
182 + pTypeHelper.removeInstanceHelper( zToDeleteRowKey );
183 + }
184 + for ( String zToAddRowKey : zToAddRowKeys ) {
185 + zRowsDifferent = true;
186 + InstanceHelper zNewHelper = pTypeHelper.createRelSubInstanceHelper( this, zToAddRowKey );
187 + zNewHelper.loadAllAttributeValues();
188 + }
189 + if ( zRowsDifferent ) {
190 + mRelSubFormRefToRowObjectKeys.put( pTypeHelper, zActualRowKeys );
191 + String[] zKeysAsArray = zActualRowKeys.toArray( new String[zActualRowKeys.size()] );
192 + mTypeHelper.getCollectorProxy().add(
193 + new RelSubFormRowKeys( mFormDataRowKey, mTypeHelper.getFormUniqueID(), //
194 + pRelSubFormRef, zKeysAsArray ) );
195 + }
196 + }
197 +
198 + public TypeHelper getTypeHelper() {
199 + return mTypeHelper;
200 + }
201 + }