You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2009/06/03 14:49:11 UTC

svn commit: r781354 - in /myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow: FlowTypeConverter.java NullFlowTypeConverter.java config/FlowParamAccept.java config/FlowReturnAccept.java

Author: skitching
Date: Wed Jun  3 12:49:10 2009
New Revision: 781354

URL: http://svn.apache.org/viewvc?rev=781354&view=rev
Log:
Add ability to do data conversions on params passed into and out of a flow.

Added:
    myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java   (with props)
    myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java   (with props)
Modified:
    myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowParamAccept.java
    myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowReturnAccept.java

Added: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java?rev=781354&view=auto
==============================================================================
--- myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java (added)
+++ myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java Wed Jun  3 12:49:10 2009
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.orchestra.flow;
+
+/**
+ * Used to type-convert params on input to and output from a flow. This allows a flow
+ * definition to specify a converter for a "flowAccept/param" or "flowCall/param"
+ * definition which will transform the value being passed into or out of the flow.
+ * <p>
+ * See property "converter" on the FlowParamAccept and FlowReturnAccept classes.
+ */
+public interface FlowTypeConverter
+{
+    Object convert(Object in);
+}

Propchange: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/FlowTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java?rev=781354&view=auto
==============================================================================
--- myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java (added)
+++ myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java Wed Jun  3 12:49:10 2009
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.orchestra.flow;
+
+import java.io.Serializable;
+
+/**
+ * An implementation of the FlowTypeConverter interface that just passes the object
+ * through unmodified. This can be used when a FlowTypeConverter object is required,
+ * but no actual conversion needs to be done.
+ */
+public class NullFlowTypeConverter implements FlowTypeConverter, Serializable
+{
+    private static final long serialVersionUID = 1L;
+
+    public Object convert(Object in)
+    {
+        return in;
+    }
+}

Propchange: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/NullFlowTypeConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowParamAccept.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowParamAccept.java?rev=781354&r1=781353&r2=781354&view=diff
==============================================================================
--- myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowParamAccept.java (original)
+++ myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowParamAccept.java Wed Jun  3 12:49:10 2009
@@ -20,9 +20,11 @@
 
 import java.io.Serializable;
 
+import javax.el.ELContext;
 import javax.el.ValueExpression;
 import javax.faces.context.FacesContext;
 
+import org.apache.myfaces.orchestra.flow.FlowTypeConverter;
 import org.apache.myfaces.orchestra.flow.utils._ExpressionFactory;
 import org.apache.myfaces.orchestra.lib.OrchestraException;
 
@@ -41,6 +43,8 @@
     private ValueExpression dstExpr;
     private String dflt;
     private ValueExpression dfltExpr;
+    private String converter;
+    private ValueExpression converterExpr;
 
     /** Constructor. */
     public FlowParamAccept()
@@ -111,7 +115,24 @@
         if (dstExpr != null)
         {
             // note: dst is null when the caller doesn't want to use the returned value.
-            dstExpr.setValue(facesContext.getELContext(), newValue);
+            ELContext elc = facesContext.getELContext();
+            if (converterExpr != null)
+            {
+                FlowTypeConverter conv = (FlowTypeConverter) converterExpr.getValue(elc);
+                if (conv != null)
+                {
+                    newValue = conv.convert(newValue);
+                }
+                else
+                {
+                    // There is an expression for converter, but the expression returns
+                    // null, or the expression could not be evaluated. We don't treat
+                    // this as an error here, because a flow may well need a converter
+                    // in some circumstances and not in others. Ignoring a missing
+                    // converter here makes those use-cases easier.
+                }
+            }
+            dstExpr.setValue(elc, newValue);
         }
     }
 
@@ -146,4 +167,25 @@
     {
         return dfltExpr.getValue(facesContext.getELContext());
     }
+
+    /**
+     * An optional EL expression which maps to a FlowTypeConverter instance that will
+     * be used to transform the input parameter before it is assigned to dst.
+     * <p>
+     * Note that this EL expression is evaluated within the environment of the
+     * called flow, not the caller.
+     */
+    public String getConverter()
+    {
+        return converter;
+    }
+
+    /** For use only during object initialization. */
+    public void setConverter(String converter)
+    {
+        this.converter = converter;
+
+        FacesContext fc = FacesContext.getCurrentInstance();
+        converterExpr = _ExpressionFactory.createValueExpression(fc, converter);
+    }
 }

Modified: myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowReturnAccept.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowReturnAccept.java?rev=781354&r1=781353&r2=781354&view=diff
==============================================================================
--- myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowReturnAccept.java (original)
+++ myfaces/orchestra/trunk/flow/src/main/java/org/apache/myfaces/orchestra/flow/config/FlowReturnAccept.java Wed Jun  3 12:49:10 2009
@@ -20,9 +20,11 @@
 
 import java.io.Serializable;
 
+import javax.el.ELContext;
 import javax.el.ValueExpression;
 import javax.faces.context.FacesContext;
 
+import org.apache.myfaces.orchestra.flow.FlowTypeConverter;
 import org.apache.myfaces.orchestra.flow.utils._ExpressionFactory;
 import org.apache.myfaces.orchestra.lib.OrchestraException;
 
@@ -38,6 +40,8 @@
     private String name;
     private String dst;
     private ValueExpression dstExpr;
+    private String converter;
+    private ValueExpression converterExpr;
     
     /** Constructor. */
     public FlowReturnAccept()
@@ -108,7 +112,47 @@
         if (dstExpr != null)
         {
             // note: dst is null when the caller doesn't want to use the returned value.
-            dstExpr.setValue(facesContext.getELContext(), newValue);
+
+            ELContext elc = facesContext.getELContext();
+            if (converterExpr != null)
+            {
+                FlowTypeConverter conv = (FlowTypeConverter) converterExpr.getValue(elc);
+                if (conv != null)
+                {
+                    newValue = conv.convert(newValue);
+                }
+                else
+                {
+                    // There is an expression for converter, but the expression returns
+                    // null, or the expression could not be evaluated. We don't treat
+                    // this as an error here, because a flow may well need a converter
+                    // in some circumstances and not in others. Ignoring a missing
+                    // converter here makes those use-cases easier.
+                }
+            }
+
+            dstExpr.setValue(elc, newValue);
         }
     }
+
+    /**
+     * An optional EL expression which maps to a FlowTypeConverter instance that will
+     * be used to transform the input parameter before it is assigned to dst.
+     * <p>
+     * Note that this EL expression is evaluated within the environment of the
+     * called flow, not the caller.
+     */
+    public String getConverter()
+    {
+        return converter;
+    }
+
+    /** For use only during object initialization. */
+    public void setConverter(String converter)
+    {
+        this.converter = converter;
+
+        FacesContext fc = FacesContext.getCurrentInstance();
+        converterExpr = _ExpressionFactory.createValueExpression(fc, converter);
+    }
 }