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