1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.workflow;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Properties;
22
23 import javax.xml.xpath.XPath;
24 import javax.xml.xpath.XPathExpressionException;
25 import javax.xml.xpath.XPathFactory;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.kuali.rice.kew.engine.RouteContext;
29 import org.kuali.rice.kew.rule.xmlrouting.WorkflowFunctionResolver;
30 import org.kuali.rice.kew.rule.xmlrouting.WorkflowNamespaceContext;
31 import org.kuali.rice.kns.util.FieldUtils;
32 import org.kuali.rice.kns.web.ui.Field;
33 import org.kuali.rice.kns.web.ui.Row;
34 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
35 import org.kuali.rice.krad.util.KRADConstants;
36 import org.kuali.rice.krad.util.KRADPropertyConstants;
37 import org.kuali.rice.krad.util.UrlFactory;
38 import org.w3c.dom.Document;
39
40
41 public final class WorkflowUtils {
42 private static final String XPATH_ROUTE_CONTEXT_KEY = "_xpathKey";
43 public static final String XSTREAM_SAFE_PREFIX = "wf:xstreamsafe('";
44 public static final String XSTREAM_SAFE_SUFFIX = "')";
45 public static final String XSTREAM_MATCH_ANYWHERE_PREFIX = "//";
46 public static final String XSTREAM_MATCH_RELATIVE_PREFIX = "./";
47
48 private WorkflowUtils() {
49 throw new UnsupportedOperationException("do not call");
50 }
51
52
53
54
55
56
57
58
59
60
61 public final static XPath getXPath(Document document) {
62 XPath xpath = getXPath(RouteContext.getCurrentRouteContext());
63 xpath.setNamespaceContext(new WorkflowNamespaceContext());
64 WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
65 resolver.setXpath(xpath);
66 resolver.setRootNode(document);
67 xpath.setXPathFunctionResolver(resolver);
68 return xpath;
69 }
70
71 public final static XPath getXPath(RouteContext routeContext) {
72 if (routeContext == null) {
73 return XPathFactory.newInstance().newXPath();
74 }
75 if (!routeContext.getParameters().containsKey(XPATH_ROUTE_CONTEXT_KEY)) {
76 routeContext.getParameters().put(XPATH_ROUTE_CONTEXT_KEY, XPathFactory.newInstance().newXPath());
77 }
78 return (XPath) routeContext.getParameters().get(XPATH_ROUTE_CONTEXT_KEY);
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public static final String xstreamSafeEval(XPath xpath, String xpathExpression, Object item) {
92 String xstreamSafeXPath = xstreamSafeXPath(xpathExpression);
93 String evalResult = "";
94 try {
95 evalResult = xpath.evaluate(xstreamSafeXPath, item);
96 }
97 catch (XPathExpressionException e) {
98 throw new RuntimeException("XPathExpressionException occurred on xpath: " + xstreamSafeXPath, e);
99 }
100 return evalResult;
101 }
102
103
104
105
106
107
108
109
110 public static final String xstreamSafeXPath(String xpathExpression) {
111 return new StringBuilder(XSTREAM_SAFE_PREFIX).append(xpathExpression).append(XSTREAM_SAFE_SUFFIX).toString();
112 }
113
114
115
116
117
118
119
120
121 public static final String getBusinessObjectAttributeLabel(Class businessObjectClass, String attributeName) {
122 return KRADServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(businessObjectClass, attributeName);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 public static Row buildTextRow(Class propertyClass, String boPropertyName, String workflowPropertyKey) {
138 if (propertyClass == null) {
139 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
140 }
141 if (StringUtils.isBlank(boPropertyName)) {
142 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
143 }
144 if (StringUtils.isBlank(workflowPropertyKey)) {
145 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
146 }
147 List<Field> fields = new ArrayList<Field>();
148 Field field;
149 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
150 fields.add(field);
151 return new Row(fields);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165 public static Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey) {
166 return buildTextRowWithLookup(propertyClass, boPropertyName, workflowPropertyKey, null);
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map fieldConversionsByBoPropertyName) {
183 if (propertyClass == null) {
184 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
185 }
186 if (StringUtils.isBlank(boPropertyName)) {
187 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
188 }
189 if (StringUtils.isBlank(workflowPropertyKey)) {
190 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
191 }
192 Field field;
193 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
194
195 List<Field> fields = new ArrayList<Field>();
196 fields.add(field);
197 return new Row(fields);
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public static Row buildDropdownRow(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map<String, String> optionMap, boolean addBlankRow) {
213 if (propertyClass == null) {
214 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
215 }
216 if (StringUtils.isBlank(boPropertyName)) {
217 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
218 }
219 if (StringUtils.isBlank(workflowPropertyKey)) {
220 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
221 }
222 if (optionMap == null) {
223 throw new IllegalArgumentException("Method parameter 'optionMap' was passed a NULL value.");
224 }
225 List<Field> fields = new ArrayList<Field>();
226 Field field;
227 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
228 fields.add(field);
229 return new Row(fields);
230 }
231 }