Author: rahul
Date: Tue Sep 12 14:17:21 2006
New Revision: 442708

URL: http://svn.apache.org/viewvc?view=rev&rev=442708
Log:
Support previously unsupported operations ;-)

Should take care of:
 * dialog data class name business
 * parent dialog ids
 * most of the recent dialog2 API changes

See code for TODOs and FIXMEs.

Modified:
    
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogContext.java
    
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogManager.java

Modified: 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogContext.java
URL: 
http://svn.apache.org/viewvc/shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogContext.java?view=diff&rev=442708&r1=442707&r2=442708
==============================================================================
--- 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogContext.java
 (original)
+++ 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogContext.java
 Tue Sep 12 14:17:21 2006
@@ -19,6 +19,9 @@
 import java.io.Serializable;
 import java.util.Iterator;
 
+import javax.faces.FacesException;
+import javax.faces.application.ViewHandler;
+import javax.faces.component.UIViewRoot;
 import javax.faces.context.FacesContext;
 
 import org.apache.commons.scxml.SCXMLExecutor;
@@ -31,6 +34,7 @@
 import org.apache.commons.scxml.model.State;
 import org.apache.shale.dialog2.DialogContext;
 import org.apache.shale.dialog2.DialogContextManager;
+import org.apache.shale.dialog2.scxml.config.DialogMetadata;
 
 /**
  * <p>Implementation of [EMAIL PROTECTED] DialogContextManager} for integrating
@@ -49,31 +53,31 @@
      * <p>Construct a new instance.</p>
      * 
      * @param manager [EMAIL PROTECTED] DialogContextManager} instance that 
owns us
-     * @param dialog The dialog's state machine (whose executable instance 
needs
+     * @param dialog The dialog's metadata (whose executable instance needs
      *               to be created)
      * @param name The dialog's logical name
      * @param id Dialog identifier assigned to this instance
      */
-    SCXMLDialogContext(DialogContextManager manager, SCXML dialog, String 
name, String id) {
+    SCXMLDialogContext(DialogContextManager manager, DialogMetadata dialog, 
String id,
+                       String parentDialogId) {
         this.manager = manager;
-        this.name = name;
+        this.name = dialog.getName();
+        this.dataClassName = dialog.getDataclassname();
         this.id = id;
+        this.parentDialogId = parentDialogId;
 
+        // Create a working instance of the state machine for this dialog, but 
do not
+        // set it in motion
         this.executor = new SCXMLExecutor(new ShaleDialogELEvaluator(),
                         new SimpleDispatcher(), new SimpleErrorReporter());
-        this.executor.setStateMachine(dialog);
-        this.executor.addListener(dialog, new SimpleSCXMLListener());
+        SCXML statemachine = dialog.getStateMachine();
+        this.executor.setStateMachine(statemachine);
+        this.executor.addListener(statemachine, new SimpleSCXMLListener());
 
         // TODO - Consider adding an explicit root context backed by either the
         // request or session map for greater EL capacities in the SCXML
         // document describing this dialog
 
-        try {
-            this.executor.go();
-        } catch (ModelException me) {
-            // FIXME - Better exception
-            throw new IllegalArgumentException("SCXML dialog cannot start:" + 
name);
-        }
     }
     
 
@@ -93,6 +97,19 @@
 
 
     /**
+     * <p>Type of data object (FQCN to be instantiated).</p>
+     */
+    private String dataClassName = null;
+
+
+    /**
+     * <p>Identifier of the parent [EMAIL PROTECTED] DialogContext} associated 
with
+     * this [EMAIL PROTECTED] DialogContext}, if any.  If there is no such 
parent,
+     * this value is set to <code>null</code>.</p>
+     */
+    private String parentDialogId = null;
+
+    /**
      * <p>Dialog identifier for this instance.</p>
      */
     private String id = null;
@@ -153,7 +170,19 @@
 
     /** [EMAIL PROTECTED] */
     public DialogContext getParent() {
-        throw new UnsupportedOperationException(); // FIXME - implement this
+
+        if (this.parentDialogId != null) {
+            DialogContext parent = manager.get(this.parentDialogId);
+            if (parent == null) {
+                throw new IllegalStateException("Dialog instance '"
+                        + parentDialogId + "' was associated with this 
instance '"
+                        + getId() + "' but is no longer available");
+            }
+            return parent;
+        } else {
+            return null;
+        }
+
     }
 
 
@@ -163,8 +192,6 @@
     /** [EMAIL PROTECTED] */
     public void advance(FacesContext context, String outcome) {
 
-        throw new UnsupportedOperationException(); // FIXME - implement 
revised contract for this
-/*
         ((ShaleDialogELEvaluator) this.executor.getEvaluator()).
                     setFacesContext(context);
         this.executor.getRootContext().setLocal(Globals.POSTBACK_OUTCOME, 
outcome);
@@ -174,28 +201,69 @@
                                 TriggerEvent.SIGNAL_EVENT));
         } catch (ModelException me) {
             // FIXME - Not an IAE
-                throw new IllegalArgumentException(me.getMessage());
+            throw new IllegalArgumentException(me.getMessage());
         }
 
         // TODO - Re-evaluate before leaving sandbox
         // Using C/C, "View" state ID is JSF viewId, which is an acceptable
         // approach with Commons SCXML v0.5
         Iterator iterator = 
this.executor.getCurrentStatus().getStates().iterator();
-        return ((State) iterator.next()).getId();
-*/
+        navigateTo(((State) iterator.next()).getId(), context);
 
     }
 
 
     /** [EMAIL PROTECTED] */
     public void start(FacesContext context) {
-        throw new UnsupportedOperationException(); // FIXME - implement this
+        
+        // Construct an appropriate data object for the specified dialog
+        ClassLoader loader = Thread.currentThread().getContextClassLoader();
+        if (loader == null) {
+            loader = SCXMLDialogContext.class.getClassLoader();
+        }
+        Class dataClass = null;
+        try {
+            dataClass = loader.loadClass(dataClassName);
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new IllegalArgumentException(e.toString());
+        }
+
+        try {
+            this.data = dataClass.newInstance();
+        } catch (RuntimeException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new FacesException(e);
+        }
+
+        // set state machine in motion
+        ((ShaleDialogELEvaluator) this.executor.getEvaluator()).
+            setFacesContext(context);
+        try {
+            this.executor.go();
+        } catch (ModelException me) {
+            // FIXME - Better exception, for extra credit:
+                // TODO - need DialogListener
+            throw new IllegalArgumentException("SCXML dialog cannot start:" + 
name);
+        }
+
+        // Using C/C, "View" state ID is JSF viewId, which is an acceptable
+        // approach with Commons SCXML v0.5 since it accepts spaces etc.
+        Iterator iterator = 
this.executor.getCurrentStatus().getStates().iterator();
+        navigateTo(((State) iterator.next()).getId(), context);
+
     }
 
 
     /** [EMAIL PROTECTED] */
     public void stop(FacesContext context) {
-        throw new UnsupportedOperationException(); // FIXME - implement this
+        
+        // TODO: Complete (add started flag like legacy impl, here and in 
other places)
+        deactivate();
+        manager.remove(this);
+
     }
 
 
@@ -212,4 +280,27 @@
     }
 
 
+    //  ------------------------------------------------- Private Methods
+
+
+    /**
+     * <p>Convention over configuration approach.</p>
+     */
+    private void navigateTo(String viewId, FacesContext context) {
+        // TODO: Convention needs to be configurable ;-)
+        // Navigate to the requested view identifier (if any)
+        if (viewId == null) {
+            return;
+        }
+        if (!viewId.startsWith("/")) {
+            viewId = "/" + viewId;
+        }
+        ViewHandler vh = context.getApplication().getViewHandler();
+        UIViewRoot view = vh.createView(context, viewId);
+        view.setViewId(viewId);
+        context.setViewRoot(view);
+        context.renderResponse();
+    }
+
 }
+

Modified: 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogManager.java
URL: 
http://svn.apache.org/viewvc/shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogManager.java?view=diff&rev=442708&r1=442707&r2=442708
==============================================================================
--- 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogManager.java
 (original)
+++ 
shale/sandbox/shale-dialog2-scxml/src/main/java/org/apache/shale/dialog2/scxml/SCXMLDialogManager.java
 Tue Sep 12 14:17:21 2006
@@ -32,6 +32,7 @@
 import org.apache.shale.dialog2.DialogContext;
 import org.apache.shale.dialog2.DialogContextManager;
 import org.apache.shale.dialog2.scxml.config.ConfigurationParser;
+import org.apache.shale.dialog2.scxml.config.DialogMetadata;
 
 /**
  * <p>Implementation of [EMAIL PROTECTED] DialogContextManager} for integrating
@@ -94,26 +95,39 @@
     /** [EMAIL PROTECTED] */
     public DialogContext create(FacesContext context, String name) {
 
+        return create(context, name, null);
+
+    }
+
+
+    /** [EMAIL PROTECTED] */
+    public DialogContext create(FacesContext context, String name, 
DialogContext parent) {
+
         // Look up the specified dialog configuration
         Map dialogs = dialogs(context);
-        SCXML dialog = (SCXML) dialogs.get(name);
-        if (dialog == null) {
+        DialogMetadata dialog = (DialogMetadata) dialogs.get(name);
+        if (dialog == null || dialog.getStateMachine() == null) {
             throw new IllegalArgumentException("No definition for dialog name 
'"
                                                + name + "' can be found");
         }
 
+        // Validate the specified parent (if any)
+        String parentDialogId = null;
+        if (parent != null) {
+            parentDialogId = parent.getId();
+            if (parent != get(parentDialogId)) {
+                throw new IllegalStateException("The specified parent 
DialogContext '"
+                        + parentDialogId + "' is not managed by this 
DialogContextManager");
+            }
+        }
+
         // Configure a new SCXMLDialogContext instance
-        SCXMLDialogContext instance = new SCXMLDialogContext(this, dialog, 
name, generateId());
+        SCXMLDialogContext instance = new SCXMLDialogContext(this, dialog, 
generateId(),
+                parentDialogId);
         instance.setData(new HashMap());
         map.put(instance.getId(), instance);
         
context.getExternalContext().getRequestMap().put(Constants.CONTEXT_BEAN, 
instance);
         return instance;
-    }
-
-
-    /** [EMAIL PROTECTED] */
-    public DialogContext create(FacesContext context, String name, 
DialogContext parent) {
-        throw new UnsupportedOperationException(); // FIXME - implement this
     }
 
 


Reply via email to