001: /*
002: *
003: */
004: package org.enhydra.util.chiba;
005:
006: import java.util.Locale;
007: import java.util.ResourceBundle;
008:
009: import javax.servlet.http.HttpSession;
010:
011: import org.chiba.adapter.ChibaAdapter;
012: import org.chiba.adapter.ChibaEvent;
013: import org.chiba.adapter.DefaultChibaEventImpl;
014: import org.chiba.xml.xforms.exception.XFormsException;
015: import org.w3c.dom.Element;
016:
017: import uk.ltd.getahead.dwr.ExecutionContext;
018:
019: import com.lutris.appserver.server.session.Session;
020:
021: /**
022: * AJAX Facade class to hide the full functionality from the web-client.
023: *
024: * @author Slobodan Vujasinovic
025: *
026: */
027: public class ScriptFacade {
028: // this is a custom event to activate a trigger in XForms.
029: public static final String FLUX_ACTIVATE_EVENT = "flux-action-event";
030:
031: private static final String SESSION_ERROR = "SESSION_ERROR";
032:
033: private static final String SESSION_ERROR_DEFAULT = "Session expired! Please start again.";
034:
035: private static final String INFO = "INFO";
036:
037: private static final String INFO_DEFAULT = "FluxFacade using";
038:
039: private BaseAdapter adapter = null;
040:
041: private HttpSession session;
042:
043: private ResourceBundle rb = null;
044:
045: /**
046: * grabs the actual adapter from the session.
047: */
048: public ScriptFacade() {
049: session = ExecutionContext.get().getSession(); // get current session
050: try {
051: rb = ResourceBundle
052: .getBundle("org.enhydra.util.chiba.message");
053: } catch (Exception ex) {
054: rb = ResourceBundle.getBundle(
055: "org.enhydra.util.chiba.message", new Locale(""));
056: }
057:
058: try {
059: adapter = ((ChibaAdapterContainer) ((Session) session
060: .getAttribute("session")).getSessionData().get(
061: "chiba.adapter.container")).getFirstChibaAdapter();
062: } catch (Exception e) {
063: adapter = null;
064: // e.printStackTrace();
065: }
066: }
067:
068: /**
069: * executes a trigger
070: *
071: * @param id
072: * the id of the trigger to execute
073: * @return the list of events that may result through this action
074: * @throws FluxException
075: */
076: public org.w3c.dom.Element fireAction(String id) throws Exception {
077: ChibaEvent chibaActivateEvent = new DefaultChibaEventImpl();
078: chibaActivateEvent.initEvent(FLUX_ACTIVATE_EVENT, id, null);
079: return dispatch(chibaActivateEvent);
080: }
081:
082: /**
083: * sets the value of a control in the processor.
084: *
085: * @param id
086: * the id of the control in the host document
087: * @param value
088: * the new value
089: * @return the list of events that may result through this action
090: * @throws FluxException
091: */
092: public org.w3c.dom.Element setXFormsValue(String id, String value)
093: throws Exception {
094: ChibaEvent event = new DefaultChibaEventImpl();
095: event.initEvent("SETVALUE", id, value);
096: return dispatch(event);
097: }
098:
099: public org.w3c.dom.Element setRepeatIndex(String id, String position)
100: throws Exception {
101: ChibaEvent event = new DefaultChibaEventImpl();
102: event.initEvent("SETINDEX", id, position);
103: return dispatch(event);
104: }
105:
106: /**
107: * fetches the progress of a running upload.
108: *
109: * @param id
110: * id of the upload control in use
111: * @param filename
112: * filename for uploaded data
113: * @return a array containing two elements for evaluation in browser. First
114: * param is the upload control id and second will be the current
115: * progress of the upload.
116: */
117: public org.w3c.dom.Element fetchProgress(String id, String filename) {
118: String progress;
119:
120: if (session.getAttribute(filename) != null) {
121: progress = ((Integer) session.getAttribute(filename))
122: .toString();
123: } else {
124: progress = "0";
125: }
126: EventLog eventLog = null;
127: try {
128: eventLog = (EventLog) adapter.getContextParam("EVENT-LOG");
129: } catch (Exception ex) {
130: // ex.printStackTrace();
131: }
132: if (eventLog == null) {
133: eventLog = new EventLog();
134: }
135: Element eventlogElement = eventLog.getLog();
136: eventLog.flush();
137:
138: Element progressEvent = eventLog.add("upload-progress-event",
139: id, "upload");
140: eventLog.addProperty(progressEvent, "progress", progress);
141: return eventlogElement;
142: }
143:
144: private org.w3c.dom.Element dispatch(ChibaEvent event)
145: throws Exception {
146:
147: if (adapter != null && !adapter.isClean()) {
148: session.setAttribute("chiba.ScriptFacade.errorMessage",
149: new Boolean(true));
150: try {
151: adapter.dispatch(event);
152: } catch (Exception e) {
153: // throw new Exception(e);
154: return null;
155: }
156:
157: EventLog eventLog = (EventLog) adapter
158: .getContextParam("EVENT-LOG");
159: if (eventLog == null) {
160: eventLog = new EventLog();
161: }
162: Element eventlogElement = eventLog.getLog();
163: return eventlogElement;
164: } else {
165: // session expired or cookie got lost
166: // check if we have already shown error message
167: boolean errorMessage = false;
168: Boolean errorMessageObject = (Boolean) session
169: .getAttribute("chiba.ScriptFacade.errorMessage");
170: if (errorMessageObject == null) {
171: errorMessage = true;
172: } else {
173: errorMessage = errorMessageObject.booleanValue();
174: }
175: if (adapter == null && errorMessage) {
176: String message = SESSION_ERROR_DEFAULT;
177: try {
178: message = rb.getString(SESSION_ERROR);
179: } catch (Exception ex) {
180: ex.printStackTrace();
181: }
182: session.setAttribute("chiba.ScriptFacade.errorMessage",
183: new Boolean(false));
184:
185: throw new Exception(message);
186: }
187: return null;
188: }
189:
190: }
191:
192: public String getInfo() {
193: String message = INFO_DEFAULT;
194: try {
195: message = rb.getString(INFO);
196: } catch (Exception ex) {
197: ex.printStackTrace();
198: }
199:
200: return message + " " + adapter.toString();
201: }
202: }
|