001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.web.popups;
016:
017: import java.util.HashMap;
018: import java.util.Map;
019: import org.apache.commons.lang.RandomStringUtils;
020: import org.araneaframework.Environment;
021: import org.araneaframework.EnvironmentAwareCallback;
022: import org.araneaframework.OutputData;
023: import org.araneaframework.Service;
024: import org.araneaframework.Widget;
025: import org.araneaframework.core.ApplicationWidget;
026: import org.araneaframework.core.Assert;
027: import org.araneaframework.core.BaseApplicationWidget;
028: import org.araneaframework.core.StandardEnvironment;
029: import org.araneaframework.core.util.ExceptionUtil;
030: import org.araneaframework.framework.FlowContext;
031: import org.araneaframework.framework.ThreadContext;
032: import org.araneaframework.framework.TopServiceContext;
033: import org.araneaframework.framework.TransactionContext;
034: import org.araneaframework.http.HttpInputData;
035: import org.araneaframework.http.HttpOutputData;
036: import org.araneaframework.http.util.URLUtil;
037: import org.araneaframework.uilib.core.BaseUIWidget;
038: import org.araneaframework.uilib.core.PopupFlowWrapperWidget;
039:
040: /**
041: * Similar to {@link PopupFlowWrapperWidget} but this never proxies {@link FlowContext} calls
042: * to opening thread's {@link FlowContext} and allows setting the {@link Service} that will
043: * be executed upon return from wrapped {@link Widget}.
044: *
045: * @author Taimo Peelo (taimo@araneaframework.org)
046: */
047: public class StandalonePopupFlowWrapperWidget extends
048: BaseApplicationWidget implements FlowContext {
049: private Widget widget;
050: private ClientSideReturnService finishingService;
051: private Service cancellingService;
052:
053: public StandalonePopupFlowWrapperWidget(BaseUIWidget widget) {
054: this .widget = widget;
055: }
056:
057: public void setFinishService(ClientSideReturnService service) {
058: this .finishingService = service;
059: }
060:
061: public ClientSideReturnService getFinishService() {
062: return finishingService;
063: }
064:
065: public void setCancelService(Service service) {
066: cancellingService = service;
067: }
068:
069: public Service getCancelService() {
070: return cancellingService;
071: }
072:
073: protected Environment getChildWidgetEnvironment() throws Exception {
074: return new StandardEnvironment(super
075: .getChildWidgetEnvironment(), FlowContext.class, this );
076: }
077:
078: protected void init() throws Exception {
079: addWidget("widget", widget);
080: }
081:
082: protected void render(OutputData output) throws Exception {
083: widget._getWidget().render(output);
084: }
085:
086: protected FlowContext getFlowCtx() {
087: return (FlowContext) getEnvironment().requireEntry(
088: FlowContext.class);
089: }
090:
091: public void addNestedEnvironmentEntry(ApplicationWidget scope,
092: Object entryId, Object envEntry) {
093: getFlowCtx()
094: .addNestedEnvironmentEntry(scope, entryId, envEntry);
095: }
096:
097: public void cancel() {
098: ThreadContext threadCtx = getThreadContext();
099: TopServiceContext topCtx = getTopServiceContext();
100: try {
101: // close the session-thread serving popupflow
102: threadCtx.close(threadCtx.getCurrentId());
103:
104: String rndThreadId = RandomStringUtils
105: .randomAlphanumeric(12);
106: Assert.notNull(cancellingService);
107: threadCtx.addService(rndThreadId, cancellingService);
108: ((HttpOutputData) getOutputData())
109: .sendRedirect(getResponseURL(
110: ((HttpInputData) getInputData())
111: .getContainerURL(), (String) topCtx
112: .getCurrentId(), rndThreadId));
113: } catch (Exception e) {
114: ExceptionUtil.uncheckException(e);
115: }
116: }
117:
118: public void finish(Object result) {
119: ThreadContext threadCtx = getThreadContext();
120: TopServiceContext topCtx = getTopServiceContext();
121: try {
122: threadCtx.close(threadCtx.getCurrentId());
123:
124: String rndThreadId = RandomStringUtils
125: .randomAlphanumeric(12);
126:
127: finishingService.setResult(result);
128: threadCtx.addService(rndThreadId, finishingService);
129: ((HttpOutputData) getOutputData())
130: .sendRedirect(getResponseURL(
131: ((HttpInputData) getInputData())
132: .getContainerURL(), (String) topCtx
133: .getCurrentId(), rndThreadId));
134: } catch (Exception e) {
135: ExceptionUtil.uncheckException(e);
136: }
137: }
138:
139: protected TopServiceContext getTopServiceContext() {
140: return (TopServiceContext) getEnvironment().getEntry(
141: TopServiceContext.class);
142: }
143:
144: protected ThreadContext getThreadContext() {
145: return (ThreadContext) getEnvironment().getEntry(
146: ThreadContext.class);
147: }
148:
149: public FlowReference getCurrentReference() {
150: return getFlowCtx().getCurrentReference();
151: }
152:
153: public boolean isNested() {
154: return getFlowCtx().isNested();
155: }
156:
157: public void replace(Widget flow) {
158: replace(flow, null);
159: }
160:
161: public void replace(Widget flow, Configurator configurator) {
162: }
163:
164: public void reset(EnvironmentAwareCallback callback) {
165: throw new IllegalStateException();
166: }
167:
168: public void start(Widget flow, Handler handler) {
169: start(flow, null, handler);
170: }
171:
172: public void start(Widget flow) {
173: start(flow, null, null);
174: }
175:
176: public void start(Widget flow, Configurator configurator,
177: Handler handler) {
178: getFlowCtx().start(flow, configurator, handler);
179: }
180:
181: protected String getResponseURL(String url, String topServiceId,
182: String threadServiceId) {
183: Map m = new HashMap();
184: m.put(TopServiceContext.TOP_SERVICE_KEY, topServiceId);
185: m.put(ThreadContext.THREAD_SERVICE_KEY, threadServiceId);
186: m.put(TransactionContext.TRANSACTION_ID_KEY,
187: TransactionContext.OVERRIDE_KEY);
188: return ((HttpOutputData) getOutputData()).encodeURL(URLUtil
189: .parametrizeURI(url, m));
190: }
191:
192: public void setTransitionHandler(TransitionHandler handler) {
193: getFlowCtx().setTransitionHandler(handler);
194: }
195:
196: public TransitionHandler getTransitionHandler() {
197: return getFlowCtx().getTransitionHandler();
198: }
199: }
|