001: package com.xoetrope.service;
002:
003: import java.io.StringReader;
004: import java.io.StringWriter;
005: import net.xoetrope.data.XDataSource;
006: import net.xoetrope.optional.data.XOptionalDataSource;
007: import net.xoetrope.optional.service.*;
008: import net.xoetrope.xml.XmlElement;
009: import net.xoetrope.xml.XmlSource;
010: import net.xoetrope.xui.XProject;
011: import net.xoetrope.xui.XProjectManager;
012: import net.xoetrope.xui.data.XBaseModel;
013: import net.xoetrope.xui.data.XModel;
014:
015: /**
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.7 $</p>
022: */
023: public class StateManager extends ServiceProxy {
024: public static final String ARG_NAME_MODELPATH = "modelpath";
025: public static final String ARG_NAME_OUTPUTPARAM = "outputparam";
026: public static final String ARG_NAME_OPERATION = "stateoperation";
027:
028: public static final String ARG_VALUE_MODELOUTPUT = "stateoutput";
029: public static final String ARG_VALUE_MODELRESTORE = "staterestore";
030:
031: public static final String ARG_ERROR_CANNOT_PARSE_DATA = "Cannot parse the provided data.";
032:
033: /**
034: * The owner project and the context in which this object operates.
035: */
036: protected XProject currentProject = XProjectManager
037: .getCurrentProject();
038: private XModel rootMdl = XProjectManager.getModel();
039:
040: /**
041: * The overloaded call for the ServiceProxy
042: * @param method the name of the service
043: * @param context the context object of the call
044: */
045: public Object call(String method, ServiceContext context) {
046: ServiceProxyArgs args = context.getArgs();
047: String mode = (String) args.getPassParam(ARG_NAME_OPERATION);
048: String modelPath = (String) args
049: .getPassParam(ARG_NAME_MODELPATH);
050: try {
051: Object ret = null;
052: String outputParam = (String) args
053: .getPassParam(ARG_NAME_OUTPUTPARAM);
054: if (side == XRouteManager.SERVER_SIDE) {
055: ret = callNextProxy(method, context, null);
056: if (mode.compareTo(ARG_VALUE_MODELRESTORE) == 0)
057: outputModel(context, outputParam, modelPath);
058: } else {
059: if (mode.compareTo(ARG_VALUE_MODELRESTORE) == 0) {
060: // The file is being opened so the returned content needs to populate the model
061: ((XBaseModel) rootMdl.get("xui_state/" + modelPath))
062: .clear();
063: ((XBaseModel) rootMdl.get(modelPath)).clear();
064:
065: ret = callNextProxy(method, context, null);
066: restoreModel(context, outputParam);
067: } else if (mode.compareTo(ARG_VALUE_MODELOUTPUT) == 0) {
068: //The file is being saved and the contents of the target model need to be output
069: outputModel(context, outputParam, modelPath);
070: ret = callNextProxy(method, context, null);
071: }
072: }
073: return ret;
074: } catch (ServiceProxyException e) {
075: return null;
076: }
077: }
078:
079: private void restoreModel(ServiceContext context, String outputParam) {
080: ServiceProxyArgs args = context.getArgs();
081: String contents = (String) args.getReturnParam(outputParam);
082: try {
083: StringReader sr = new StringReader(contents);
084: XmlElement ele = XmlSource.read(sr);
085:
086: if (ele != null) {
087: XOptionalDataSource ds = new XOptionalDataSource(
088: currentProject);
089: ds.loadTable(ele, rootMdl);
090: ds.loadTable(ele, (XModel) rootMdl.get("xui_state"));
091: }
092: } catch (Exception e) {
093: context.getArgs().addError(ARG_ERROR_CANNOT_PARSE_DATA);
094: }
095: }
096:
097: private void outputModel(ServiceContext context,
098: String outputParam, String modelPath) {
099: ServiceProxyArgs args = context.getArgs();
100: XBaseModel mdl = (XBaseModel) currentProject.getModel().get(
101: modelPath);
102: StringWriter sw = new StringWriter();
103: XDataSource.outputModel(sw, mdl);
104: if (side == XRouteManager.CLIENT_SIDE)
105: args.setPassParam(outputParam, "<Datasets>" + sw.toString()
106: + "</Datasets>");
107: else
108: args.setReturnParam(outputParam, "<Datasets>"
109: + sw.toString() + "</Datasets>");
110: }
111: }
|