Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/OldClient/src/org/litesoft/GWT/forms/client/rpc/BusFormServicePeerChannel.java

Diff revisions: vs.
  @@ -1,164 +1,164 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.GWT.forms.client.rpc;
3 -
4 - import org.litesoft.GWT.client.*;
5 - import org.litesoft.GWT.eventbus.client.*;
6 - import org.litesoft.GWT.forms.client.*;
7 - import org.litesoft.commonfoundation.typeutils.*;
8 - import org.litesoft.core.simpletypes.*;
9 - import org.litesoft.ui.def.nonpublic.support.*;
10 - import org.litesoft.ui.support.*;
11 -
12 - import java.io.*;
13 - import java.util.*;
14 -
15 - public class BusFormServicePeerChannel implements AsyncFormServicePeerChannel,
16 - FormComponentBusConstants {
17 - private EventBus mEventBus;
18 - private String mUniqueName;
19 - private String mFormServicePeerFactoryName;
20 -
21 - private InitializationListener mInitializationListener = null;
22 - private OperationalListener mOperationalListener;
23 - private EventPackageSubscriber mEventSubscription = null;
24 - private String mServerPeer = null; // id of the server peer
25 -
26 - private Map<AttributeResourceOptionsRequestData, ResourceKeyNameURLsAvailableCallback> mCallBacks =
27 - new HashMap<AttributeResourceOptionsRequestData, ResourceKeyNameURLsAvailableCallback>();
28 -
29 - /**
30 - * @param pTraceNameBase an identifier which will form the base of the
31 - * channel's subscriber id, i.e. the VIS_ID or unique id
32 - */
33 - public BusFormServicePeerChannel( EventBus pEventBus, String pTraceNameBase,
34 - String pFormServicePeerFactoryName ) {
35 - mEventBus = pEventBus;
36 - mUniqueName = Strings.assertNotNullNotEmpty( "TraceNameBase", pTraceNameBase ) +
37 - "-BusFormServicePeerChannel-" + pEventBus.getNextUniqueID();
38 - mFormServicePeerFactoryName = pFormServicePeerFactoryName;
39 - }
40 -
41 - public void init( FormUsage pFormUsage, Serializable pInitializationPayload,
42 - OperationalListener pOperationalListener,
43 - InitializationListener pInitializationListener ) {
44 - mInitializationListener = pInitializationListener;
45 - mOperationalListener = pOperationalListener;
46 - mEventBus.subscribeAndPublish( mEventSubscription = getEventSubscription(), //
47 - new FormCreationRequestPackage( mUniqueName, //
48 - mFormServicePeerFactoryName, //
49 - pFormUsage, pInitializationPayload ) );
50 - }
51 -
52 - public void requestValues( Integer pAsyncMessageNumber, String pRootTypeKeyOrNullForNew ) {
53 - chkMethodOK( "requestValues" );
54 - if ( pAsyncMessageNumber != null ) {
55 - FormValuesRequestPackage zPackage = new FormValuesRequestPackage( mUniqueName, mServerPeer, //
56 - pAsyncMessageNumber,
57 - pRootTypeKeyOrNullForNew );
58 - mEventBus.publish( zPackage );
59 - }
60 - }
61 -
62 - public void requestResources( ResourceKeyNameURLsAvailableCallback pCallback,
63 - AttributeResourceOptionsRequestData pRequest ) {
64 - chkMethodOK( "requestResources" );
65 - if ( (pRequest != null) && (pCallback != null) ) {
66 - mCallBacks.put( pRequest, pCallback );
67 - FormResourceRequestPackage zPackage = new FormResourceRequestPackage( mUniqueName, mServerPeer, //
68 - pRequest );
69 - mEventBus.publish( zPackage );
70 - }
71 - }
72 -
73 - public void formEvents( FormToServicePeerData pFormToServicePeerData ) {
74 - chkMethodOK( "formEvents" );
75 - if ( pFormToServicePeerData != null ) {
76 - FormToServicePeerPackage zPackage = new FormToServicePeerPackage( mUniqueName, mServerPeer, //
77 - pFormToServicePeerData );
78 - mEventBus.publish( zPackage );
79 - }
80 - }
81 -
82 - public void dispose() {
83 - if ( (mServerPeer != null) && !isDisposed() ) {
84 - if ( mEventSubscription != null ) {
85 - mEventBus.unsubscribe( mEventSubscription );
86 - mEventBus.publish( new FormDisposePackage( mUniqueName, mServerPeer ) );
87 - mEventSubscription = null;
88 - }
89 - mEventBus = null;
90 - mInitializationListener = null;
91 - mOperationalListener = null;
92 - mUniqueName = mFormServicePeerFactoryName = mServerPeer = null;
93 - }
94 - }
95 -
96 - private boolean isDisposed() {
97 - return (mEventBus == null);
98 - }
99 -
100 - private void chkMethodOK( String pMethod ) {
101 - if ( isDisposed() ) {
102 - throw new IllegalStateException( pMethod + " unavailable. This form has been disposed" );
103 - }
104 - if ( mEventSubscription == null ) {
105 - throw new IllegalStateException( pMethod + " unavailable. This form has NOT been init()'d" );
106 - }
107 - if ( mServerPeer == null ) {
108 - throw new IllegalStateException( pMethod + " unavailable. " + mFormServicePeerFactoryName +
109 - " has not replied with a ServerPeer ID" );
110 - }
111 - }
112 -
113 - private EventPackageSubscriber getEventSubscription() {
114 - return new EventPackageSubscriber() {
115 - public String subscribeWith() {
116 - return mUniqueName;
117 - }
118 -
119 - public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) {
120 - String type = pEventPackage.getType();
121 - if ( ServicePeerToFormPackage.UNSOLICITED_DATA.equals( type ) ) {
122 - if ( mOperationalListener != null ) {
123 - mOperationalListener.unsolicitedData(
124 - ((ServicePeerToFormPackage) pEventPackage).getToFormData() );
125 - }
126 - } else if ( ServicePeerToFormPackage.RESPONSE_FROM_REQUEST_VALUES.equals( type ) ) {
127 - if ( mOperationalListener != null ) {
128 - mOperationalListener.responseFromRequestValues(
129 - ((ServicePeerToFormPackage) pEventPackage).getToFormData() );
130 - }
131 - } else if ( FormCreationResponsePackage.EVENT_TYPE.equals( type ) ) {
132 - mServerPeer = pEventPackage.getSourceName();
133 - FormCreationResponsePackage zEvent = (FormCreationResponsePackage) pEventPackage;
134 - if ( mInitializationListener != null ) {
135 - ServicePeerToFormCreationData data = zEvent.getFormCreationData();
136 - if ( data != null ) {
137 - mInitializationListener.metaDataAvailable( data.getFormMetaData(),
138 - data.getServicePeerXtra() );
139 - }
140 - String zExceptionText = zEvent.getExceptionText();
141 - if ( zExceptionText != null ) {
142 - AlertManager.alert( "FormServicePeer", "Initialization Error", zExceptionText );
143 - }
144 - }
145 - } else if ( FormResourceResponsePackage.EVENT_TYPE.equals( type ) ) {
146 - FormResourceResponsePackage zEvent = (FormResourceResponsePackage) pEventPackage;
147 - ResourceKeyNameURLsAvailableCallback zCallback = mCallBacks.get( zEvent.getRequest() );
148 - if ( zCallback == null ) {
149 - AlertManager.alert( "FormServicePeer", "Resourse Request Error",
150 - "No Call Back for: " + zEvent.getRequest() );
151 - return;
152 - }
153 - AttributeResourceOptionsResponseData zResponseData = zEvent.getResponse();
154 - ResourceOptionsErrorType zErrorType = zResponseData.getErrorType();
155 - if ( zErrorType != null ) {
156 - zCallback.resourceRequestFailed( zErrorType, zResponseData.getErrorText() );
157 - return;
158 - }
159 - zCallback.resourceKeyNameURLsAvailable( zResponseData.getResourceKeyNameURLs() );
160 - }
161 - }
162 - };
163 - }
164 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.GWT.forms.client.rpc;
3 +
4 + import org.litesoft.GWT.client.*;
5 + import org.litesoft.GWT.eventbus.client.*;
6 + import org.litesoft.GWT.forms.client.*;
7 + import org.litesoft.commonfoundation.base.*;
8 + import org.litesoft.core.simpletypes.*;
9 + import org.litesoft.ui.def.nonpublic.support.*;
10 + import org.litesoft.ui.support.*;
11 +
12 + import java.io.*;
13 + import java.util.*;
14 +
15 + public class BusFormServicePeerChannel implements AsyncFormServicePeerChannel,
16 + FormComponentBusConstants {
17 + private EventBus mEventBus;
18 + private String mUniqueName;
19 + private String mFormServicePeerFactoryName;
20 +
21 + private InitializationListener mInitializationListener = null;
22 + private OperationalListener mOperationalListener;
23 + private EventPackageSubscriber mEventSubscription = null;
24 + private String mServerPeer = null; // id of the server peer
25 +
26 + private Map<AttributeResourceOptionsRequestData, ResourceKeyNameURLsAvailableCallback> mCallBacks =
27 + new HashMap<AttributeResourceOptionsRequestData, ResourceKeyNameURLsAvailableCallback>();
28 +
29 + /**
30 + * @param pTraceNameBase an identifier which will form the base of the
31 + * channel's subscriber id, i.e. the VIS_ID or unique id
32 + */
33 + public BusFormServicePeerChannel( EventBus pEventBus, String pTraceNameBase,
34 + String pFormServicePeerFactoryName ) {
35 + mEventBus = pEventBus;
36 + mUniqueName = Confirm.significant( "TraceNameBase", pTraceNameBase ) +
37 + "-BusFormServicePeerChannel-" + pEventBus.getNextUniqueID();
38 + mFormServicePeerFactoryName = pFormServicePeerFactoryName;
39 + }
40 +
41 + public void init( FormUsage pFormUsage, Serializable pInitializationPayload,
42 + OperationalListener pOperationalListener,
43 + InitializationListener pInitializationListener ) {
44 + mInitializationListener = pInitializationListener;
45 + mOperationalListener = pOperationalListener;
46 + mEventBus.subscribeAndPublish( mEventSubscription = getEventSubscription(), //
47 + new FormCreationRequestPackage( mUniqueName, //
48 + mFormServicePeerFactoryName, //
49 + pFormUsage, pInitializationPayload ) );
50 + }
51 +
52 + public void requestValues( Integer pAsyncMessageNumber, String pRootTypeKeyOrNullForNew ) {
53 + chkMethodOK( "requestValues" );
54 + if ( pAsyncMessageNumber != null ) {
55 + FormValuesRequestPackage zPackage = new FormValuesRequestPackage( mUniqueName, mServerPeer, //
56 + pAsyncMessageNumber,
57 + pRootTypeKeyOrNullForNew );
58 + mEventBus.publish( zPackage );
59 + }
60 + }
61 +
62 + public void requestResources( ResourceKeyNameURLsAvailableCallback pCallback,
63 + AttributeResourceOptionsRequestData pRequest ) {
64 + chkMethodOK( "requestResources" );
65 + if ( (pRequest != null) && (pCallback != null) ) {
66 + mCallBacks.put( pRequest, pCallback );
67 + FormResourceRequestPackage zPackage = new FormResourceRequestPackage( mUniqueName, mServerPeer, //
68 + pRequest );
69 + mEventBus.publish( zPackage );
70 + }
71 + }
72 +
73 + public void formEvents( FormToServicePeerData pFormToServicePeerData ) {
74 + chkMethodOK( "formEvents" );
75 + if ( pFormToServicePeerData != null ) {
76 + FormToServicePeerPackage zPackage = new FormToServicePeerPackage( mUniqueName, mServerPeer, //
77 + pFormToServicePeerData );
78 + mEventBus.publish( zPackage );
79 + }
80 + }
81 +
82 + public void dispose() {
83 + if ( (mServerPeer != null) && !isDisposed() ) {
84 + if ( mEventSubscription != null ) {
85 + mEventBus.unsubscribe( mEventSubscription );
86 + mEventBus.publish( new FormDisposePackage( mUniqueName, mServerPeer ) );
87 + mEventSubscription = null;
88 + }
89 + mEventBus = null;
90 + mInitializationListener = null;
91 + mOperationalListener = null;
92 + mUniqueName = mFormServicePeerFactoryName = mServerPeer = null;
93 + }
94 + }
95 +
96 + private boolean isDisposed() {
97 + return (mEventBus == null);
98 + }
99 +
100 + private void chkMethodOK( String pMethod ) {
101 + if ( isDisposed() ) {
102 + throw new IllegalStateException( pMethod + " unavailable. This form has been disposed" );
103 + }
104 + if ( mEventSubscription == null ) {
105 + throw new IllegalStateException( pMethod + " unavailable. This form has NOT been init()'d" );
106 + }
107 + if ( mServerPeer == null ) {
108 + throw new IllegalStateException( pMethod + " unavailable. " + mFormServicePeerFactoryName +
109 + " has not replied with a ServerPeer ID" );
110 + }
111 + }
112 +
113 + private EventPackageSubscriber getEventSubscription() {
114 + return new EventPackageSubscriber() {
115 + public String subscribeWith() {
116 + return mUniqueName;
117 + }
118 +
119 + public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) {
120 + String type = pEventPackage.getType();
121 + if ( ServicePeerToFormPackage.UNSOLICITED_DATA.equals( type ) ) {
122 + if ( mOperationalListener != null ) {
123 + mOperationalListener.unsolicitedData(
124 + ((ServicePeerToFormPackage) pEventPackage).getToFormData() );
125 + }
126 + } else if ( ServicePeerToFormPackage.RESPONSE_FROM_REQUEST_VALUES.equals( type ) ) {
127 + if ( mOperationalListener != null ) {
128 + mOperationalListener.responseFromRequestValues(
129 + ((ServicePeerToFormPackage) pEventPackage).getToFormData() );
130 + }
131 + } else if ( FormCreationResponsePackage.EVENT_TYPE.equals( type ) ) {
132 + mServerPeer = pEventPackage.getSourceName();
133 + FormCreationResponsePackage zEvent = (FormCreationResponsePackage) pEventPackage;
134 + if ( mInitializationListener != null ) {
135 + ServicePeerToFormCreationData data = zEvent.getFormCreationData();
136 + if ( data != null ) {
137 + mInitializationListener.metaDataAvailable( data.getFormMetaData(),
138 + data.getServicePeerXtra() );
139 + }
140 + String zExceptionText = zEvent.getExceptionText();
141 + if ( zExceptionText != null ) {
142 + AlertManager.alert( "FormServicePeer", "Initialization Error", zExceptionText );
143 + }
144 + }
145 + } else if ( FormResourceResponsePackage.EVENT_TYPE.equals( type ) ) {
146 + FormResourceResponsePackage zEvent = (FormResourceResponsePackage) pEventPackage;
147 + ResourceKeyNameURLsAvailableCallback zCallback = mCallBacks.get( zEvent.getRequest() );
148 + if ( zCallback == null ) {
149 + AlertManager.alert( "FormServicePeer", "Resourse Request Error",
150 + "No Call Back for: " + zEvent.getRequest() );
151 + return;
152 + }
153 + AttributeResourceOptionsResponseData zResponseData = zEvent.getResponse();
154 + ResourceOptionsErrorType zErrorType = zResponseData.getErrorType();
155 + if ( zErrorType != null ) {
156 + zCallback.resourceRequestFailed( zErrorType, zResponseData.getErrorText() );
157 + return;
158 + }
159 + zCallback.resourceKeyNameURLsAvailable( zResponseData.getResourceKeyNameURLs() );
160 + }
161 + }
162 + };
163 + }
164 + }