Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PopulateStep |
|
| 3.0;3 |
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 | 0 | super(); |
51 | ||
52 | 0 | } |
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 | 0 | super(); |
63 | 0 | setId(id); |
64 | ||
65 | 0 | } |
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 | 0 | super(); |
78 | 0 | setId(id); |
79 | 0 | addDescriptor(descriptor); |
80 | ||
81 | 0 | } |
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 | 0 | if (!(context instanceof WebContext)) |
99 | 0 | throw new StepException("Execution context is not a WebContext", |
100 | this); | |
101 | 0 | WebContext webContext = (WebContext) context; |
102 | 0 | 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 | 0 | HashMap map = new HashMap(); |
107 | 0 | Enumeration names = request.getParameterNames(); |
108 | 0 | while (names.hasMoreElements()) { |
109 | 0 | String name = (String) names.nextElement(); |
110 | 0 | map.put(name, request.getParameterValues(name)); |
111 | 0 | } |
112 | ||
113 | // Process all associated descriptors | |
114 | 0 | Descriptor descriptors[] = findDescriptors(); |
115 | 0 | for (int i = 0; i < descriptors.length; i++) { |
116 | 0 | Object value = descriptors[i].get(context); |
117 | 0 | if (value == null) |
118 | 0 | throw new StepException |
119 | ("Cannot retrieve object for " + descriptors[i], this); | |
120 | try { | |
121 | 0 | BeanUtils.populate(value, map); |
122 | 0 | } catch (Throwable t) { |
123 | 0 | throw new StepException("Populate exception", t, this); |
124 | 0 | } |
125 | } | |
126 | ||
127 | 0 | } |
128 | ||
129 | ||
130 | /** | |
131 | * Render a string representation of this Step. | |
132 | */ | |
133 | public String toString() { | |
134 | ||
135 | 0 | StringBuffer sb = new StringBuffer("<web:populate"); |
136 | 0 | if (getId() != null) { |
137 | 0 | sb.append(" id=\""); |
138 | 0 | sb.append(getId()); |
139 | 0 | sb.append("\""); |
140 | } | |
141 | 0 | sb.append(">"); |
142 | 0 | Descriptor descriptors[] = findDescriptors(); |
143 | 0 | for (int i = 0; i < descriptors.length; i++) |
144 | 0 | sb.append(descriptors[i].toString()); |
145 | 0 | sb.append("</web:populate>"); |
146 | 0 | return (sb.toString()); |
147 | ||
148 | } | |
149 | ||
150 | ||
151 | } |