1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39 public class PopulateStep extends DescriptorStep {
40
41
42
43
44
45
46
47
48 public PopulateStep() {
49
50 super();
51
52 }
53
54
55
56
57
58
59
60 public PopulateStep(String id) {
61
62 super();
63 setId(id);
64
65 }
66
67
68
69
70
71
72
73
74
75 public PopulateStep(String id, Descriptor descriptor) {
76
77 super();
78 setId(id);
79 addDescriptor(descriptor);
80
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public void execute(Context context) throws StepException {
96
97
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
105
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
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
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 }