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.service.KNSServiceLocator;
32 import org.kuali.rice.kns.util.FieldUtils;
33 import org.kuali.rice.kns.util.KNSConstants;
34 import org.kuali.rice.kns.util.KNSPropertyConstants;
35 import org.kuali.rice.kns.util.UrlFactory;
36 import org.kuali.rice.kns.web.ui.Field;
37 import org.kuali.rice.kns.web.ui.Row;
38 import org.w3c.dom.Document;
39
40
41 public 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
49
50
51
52
53
54
55
56
57 public final static XPath getXPath(Document document) {
58 XPath xpath = getXPath(RouteContext.getCurrentRouteContext());
59 xpath.setNamespaceContext(new WorkflowNamespaceContext());
60 WorkflowFunctionResolver resolver = new WorkflowFunctionResolver();
61 resolver.setXpath(xpath);
62 resolver.setRootNode(document);
63 xpath.setXPathFunctionResolver(resolver);
64 return xpath;
65 }
66
67 public final static XPath getXPath(RouteContext routeContext) {
68 if (routeContext == null) {
69 return XPathFactory.newInstance().newXPath();
70 }
71 if (!routeContext.getParameters().containsKey(XPATH_ROUTE_CONTEXT_KEY)) {
72 routeContext.getParameters().put(XPATH_ROUTE_CONTEXT_KEY, XPathFactory.newInstance().newXPath());
73 }
74 return (XPath) routeContext.getParameters().get(XPATH_ROUTE_CONTEXT_KEY);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static final String xstreamSafeEval(XPath xpath, String xpathExpression, Object item) {
88 String xstreamSafeXPath = xstreamSafeXPath(xpathExpression);
89 String evalResult = "";
90 try {
91 evalResult = xpath.evaluate(xstreamSafeXPath, item);
92 }
93 catch (XPathExpressionException e) {
94 throw new RuntimeException("XPathExpressionException occurred on xpath: " + xstreamSafeXPath, e);
95 }
96 return evalResult;
97 }
98
99
100
101
102
103
104
105
106 public static final String xstreamSafeXPath(String xpathExpression) {
107 return new StringBuilder(XSTREAM_SAFE_PREFIX).append(xpathExpression).append(XSTREAM_SAFE_SUFFIX).toString();
108 }
109
110
111
112
113
114
115
116 public static String getHelpUrl(org.kuali.rice.kns.web.ui.Field field) {
117 Properties params = new Properties();
118 params.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, "getAttributeHelpText");
119 params.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, field.getBusinessObjectClassName());
120 params.put(KNSPropertyConstants.ATTRIBUTE_NAME, field.getPropertyName());
121 String baseUrl = KNSServiceLocator.getRiceApplicationConfigurationMediationService().getBaseHelpUrl(field.getBusinessObjectClassName());
122 if (baseUrl == null) {
123 return null;
124 }
125 return UrlFactory.parameterizeUrl(baseUrl, params);
126 }
127
128
129
130
131
132
133
134
135 public static final String getBusinessObjectAttributeLabel(Class businessObjectClass, String attributeName) {
136 return KNSServiceLocator.getDataDictionaryService().getAttributeLabel(businessObjectClass, attributeName);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public static org.kuali.rice.kns.web.ui.Row buildTextRow(Class propertyClass, String boPropertyName, String workflowPropertyKey) {
152 if (propertyClass == null) {
153 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
154 }
155 if (StringUtils.isBlank(boPropertyName)) {
156 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
157 }
158 if (StringUtils.isBlank(workflowPropertyKey)) {
159 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
160 }
161 List<Field> fields = new ArrayList<Field>();
162 org.kuali.rice.kns.web.ui.Field field;
163 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
164 fields.add(field);
165 return new Row(fields);
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179 public static org.kuali.rice.kns.web.ui.Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey) {
180 return buildTextRowWithLookup(propertyClass, boPropertyName, workflowPropertyKey, null);
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 public static org.kuali.rice.kns.web.ui.Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map fieldConversionsByBoPropertyName) {
197 if (propertyClass == null) {
198 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
199 }
200 if (StringUtils.isBlank(boPropertyName)) {
201 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
202 }
203 if (StringUtils.isBlank(workflowPropertyKey)) {
204 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
205 }
206 org.kuali.rice.kns.web.ui.Field field;
207 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
208
209 List<Field> fields = new ArrayList<Field>();
210 fields.add(field);
211 return new Row(fields);
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public static org.kuali.rice.kns.web.ui.Row buildDropdownRow(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map<String, String> optionMap, boolean addBlankRow) {
227 if (propertyClass == null) {
228 throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value.");
229 }
230 if (StringUtils.isBlank(boPropertyName)) {
231 throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value.");
232 }
233 if (StringUtils.isBlank(workflowPropertyKey)) {
234 throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value.");
235 }
236 if (optionMap == null) {
237 throw new IllegalArgumentException("Method parameter 'optionMap' was passed a NULL value.");
238 }
239 List<Field> fields = new ArrayList<Field>();
240 org.kuali.rice.kns.web.ui.Field field;
241 field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false);
242 fields.add(field);
243 return new Row(fields);
244 }
245 }