View Javadoc

1   /*
2    * Copyright 1999-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.workflow.web;
18  
19  
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import javax.servlet.ServletRequest;
23  import org.apache.commons.beanutils.BeanUtils;
24  import org.apache.commons.workflow.Context;
25  import org.apache.commons.workflow.Descriptor;
26  import org.apache.commons.workflow.StepException;
27  import org.apache.commons.workflow.base.DescriptorStep;
28  
29  
30  /**
31   * <p>For each associated <code>Descriptor</code>, populate the properties
32   * of the bean specified by that descriptor from the request parameters of
33   * the current request.</p>
34   *
35   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
36   * @author Craig R. McClanahan
37   */
38  
39  public class PopulateStep extends DescriptorStep {
40  
41  
42      // ----------------------------------------------------------= Constructors
43  
44  
45      /**
46       * Construct a default instance of this Step.
47       */
48      public PopulateStep() {
49  
50          super();
51  
52      }
53  
54  
55      /**
56       * Construct an instance of this Step with the specified identifier.
57       *
58       * @param id Step identifier
59       */
60      public PopulateStep(String id) {
61  
62          super();
63          setId(id);
64  
65      }
66  
67  
68      /**
69       * Construct an instance of this Step with the specified identifier
70       * and associated Descriptor.
71       *
72       * @param id Step identifier
73       * @param descriptor Initial descriptor
74       */
75      public PopulateStep(String id, Descriptor descriptor) {
76  
77          super();
78          setId(id);
79          addDescriptor(descriptor);
80  
81      }
82  
83  
84      // --------------------------------------------------------- Public Methods
85  
86  
87      /**
88       * Perform the executable actions related to this Step, in the context of
89       * the specified Context.
90       *
91       * @param context The Context that is tracking our execution state
92       *
93       * @exception StepException if a processing error has occurred
94       */
95      public void execute(Context context) throws StepException {
96  
97          // Make sure our executing Context is a WebContext
98          if (!(context instanceof WebContext))
99              throw new StepException("Execution context is not a WebContext",
100                                     this);
101         WebContext webContext = (WebContext) context;
102         ServletRequest request = webContext.getServletRequest();
103 
104         // Prepare a Map of our request parameter names and values
105         // (in Servlet 2.3 we would just call request.getParameterMap())
106         HashMap map = new HashMap();
107         Enumeration names = request.getParameterNames();
108         while (names.hasMoreElements()) {
109             String name = (String) names.nextElement();
110             map.put(name, request.getParameterValues(name));
111         }
112 
113         // Process all associated descriptors
114         Descriptor descriptors[] = findDescriptors();
115         for (int i = 0; i < descriptors.length; i++) {
116             Object value = descriptors[i].get(context);
117             if (value == null)
118                 throw new StepException
119                     ("Cannot retrieve object for " + descriptors[i], this);
120             try {
121                 BeanUtils.populate(value, map);
122             } catch (Throwable t) {
123                 throw new StepException("Populate exception", t, this);
124             }
125         }
126 
127     }
128 
129 
130     /**
131      * Render a string representation of this Step.
132      */
133     public String toString() {
134 
135         StringBuffer sb = new StringBuffer("<web:populate");
136         if (getId() != null) {
137             sb.append(" id=\"");
138             sb.append(getId());
139             sb.append("\"");
140         }
141         sb.append(">");
142         Descriptor descriptors[] = findDescriptors();
143         for (int i = 0; i < descriptors.length; i++)
144             sb.append(descriptors[i].toString());
145         sb.append("</web:populate>");
146         return (sb.toString());
147 
148     }
149 
150 
151 }