/*
*
*/
package org.enhydra.util.chiba;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpSession;
import org.chiba.adapter.ChibaAdapter;
import org.chiba.adapter.ChibaEvent;
import org.chiba.adapter.DefaultChibaEventImpl;
import org.chiba.xml.xforms.exception.XFormsException;
import org.w3c.dom.Element;
import uk.ltd.getahead.dwr.ExecutionContext;
import com.lutris.appserver.server.session.Session;
/**
* AJAX Facade class to hide the full functionality from the web-client.
*
* @author Slobodan Vujasinovic
*
*/
public class ScriptFacade {
// this is a custom event to activate a trigger in XForms.
public static final String FLUX_ACTIVATE_EVENT = "flux-action-event";
private static final String SESSION_ERROR = "SESSION_ERROR";
private static final String SESSION_ERROR_DEFAULT = "Session expired! Please start again.";
private static final String INFO = "INFO";
private static final String INFO_DEFAULT = "FluxFacade using";
private BaseAdapter adapter = null;
private HttpSession session;
private ResourceBundle rb = null;
/**
* grabs the actual adapter from the session.
*/
public ScriptFacade() {
session = ExecutionContext.get().getSession(); // get current session
try {
rb = ResourceBundle.getBundle("org.enhydra.util.chiba.message");
} catch (Exception ex) {
rb = ResourceBundle.getBundle("org.enhydra.util.chiba.message",
new Locale(""));
}
try {
adapter = ((ChibaAdapterContainer) ((Session) session
.getAttribute("session")).getSessionData().get(
"chiba.adapter.container")).getFirstChibaAdapter();
} catch (Exception e) {
adapter = null;
// e.printStackTrace();
}
}
/**
* executes a trigger
*
* @param id
* the id of the trigger to execute
* @return the list of events that may result through this action
* @throws FluxException
*/
public org.w3c.dom.Element fireAction(String id) throws Exception {
ChibaEvent chibaActivateEvent = new DefaultChibaEventImpl();
chibaActivateEvent.initEvent(FLUX_ACTIVATE_EVENT, id, null);
return dispatch(chibaActivateEvent);
}
/**
* sets the value of a control in the processor.
*
* @param id
* the id of the control in the host document
* @param value
* the new value
* @return the list of events that may result through this action
* @throws FluxException
*/
public org.w3c.dom.Element setXFormsValue(String id, String value)
throws Exception {
ChibaEvent event = new DefaultChibaEventImpl();
event.initEvent("SETVALUE", id, value);
return dispatch(event);
}
public org.w3c.dom.Element setRepeatIndex(String id, String position)
throws Exception {
ChibaEvent event = new DefaultChibaEventImpl();
event.initEvent("SETINDEX", id, position);
return dispatch(event);
}
/**
* fetches the progress of a running upload.
*
* @param id
* id of the upload control in use
* @param filename
* filename for uploaded data
* @return a array containing two elements for evaluation in browser. First
* param is the upload control id and second will be the current
* progress of the upload.
*/
public org.w3c.dom.Element fetchProgress(String id, String filename) {
String progress;
if (session.getAttribute(filename) != null) {
progress = ((Integer) session.getAttribute(filename)).toString();
} else {
progress = "0";
}
EventLog eventLog = null;
try {
eventLog = (EventLog) adapter.getContextParam("EVENT-LOG");
} catch (Exception ex) {
// ex.printStackTrace();
}
if (eventLog == null) {
eventLog = new EventLog();
}
Element eventlogElement = eventLog.getLog();
eventLog.flush();
Element progressEvent = eventLog.add("upload-progress-event", id,
"upload");
eventLog.addProperty(progressEvent, "progress", progress);
return eventlogElement;
}
private org.w3c.dom.Element dispatch(ChibaEvent event) throws Exception {
if (adapter != null && !adapter.isClean()) {
session.setAttribute("chiba.ScriptFacade.errorMessage", new Boolean(true));
try {
adapter.dispatch(event);
} catch (Exception e) {
// throw new Exception(e);
return null;
}
EventLog eventLog = (EventLog) adapter.getContextParam("EVENT-LOG");
if (eventLog == null) {
eventLog = new EventLog();
}
Element eventlogElement = eventLog.getLog();
return eventlogElement;
} else {
// session expired or cookie got lost
// check if we have already shown error message
boolean errorMessage = false;
Boolean errorMessageObject = (Boolean)session.getAttribute("chiba.ScriptFacade.errorMessage");
if(errorMessageObject == null)
{
errorMessage = true;
}
else
{
errorMessage = errorMessageObject.booleanValue();
}
if (adapter == null && errorMessage) {
String message = SESSION_ERROR_DEFAULT;
try {
message = rb.getString(SESSION_ERROR);
} catch (Exception ex) {
ex.printStackTrace();
}
session.setAttribute("chiba.ScriptFacade.errorMessage", new Boolean(false));
throw new Exception(message);
}
return null;
}
}
public String getInfo() {
String message = INFO_DEFAULT;
try {
message = rb.getString(INFO);
} catch (Exception ex) {
ex.printStackTrace();
}
return message + " " + adapter.toString();
}
}
|