Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WorkflowUtils |
|
| 3.6;3.6 |
1 | /** | |
2 | * Copyright 2005-2011 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
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 | 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 | 0 | private WorkflowUtils() { |
49 | 0 | throw new UnsupportedOperationException("do not call"); |
50 | } | |
51 | ||
52 | /** | |
53 | * | |
54 | * This method sets up the XPath with the correct workflow namespace and resolver initialized. This ensures that the XPath | |
55 | * statements can use required workflow functions as part of the XPath statements. | |
56 | * | |
57 | * @param document - document | |
58 | * @return a fully initialized XPath instance that has access to the workflow resolver and namespace. | |
59 | * | |
60 | */ | |
61 | public final static XPath getXPath(Document document) { | |
62 | 0 | XPath xpath = getXPath(RouteContext.getCurrentRouteContext()); |
63 | 0 | xpath.setNamespaceContext(new WorkflowNamespaceContext()); |
64 | 0 | WorkflowFunctionResolver resolver = new WorkflowFunctionResolver(); |
65 | 0 | resolver.setXpath(xpath); |
66 | 0 | resolver.setRootNode(document); |
67 | 0 | xpath.setXPathFunctionResolver(resolver); |
68 | 0 | return xpath; |
69 | } | |
70 | ||
71 | public final static XPath getXPath(RouteContext routeContext) { | |
72 | 0 | if (routeContext == null) { |
73 | 0 | return XPathFactory.newInstance().newXPath(); |
74 | } | |
75 | 0 | if (!routeContext.getParameters().containsKey(XPATH_ROUTE_CONTEXT_KEY)) { |
76 | 0 | routeContext.getParameters().put(XPATH_ROUTE_CONTEXT_KEY, XPathFactory.newInstance().newXPath()); |
77 | } | |
78 | 0 | return (XPath) routeContext.getParameters().get(XPATH_ROUTE_CONTEXT_KEY); |
79 | } | |
80 | ||
81 | /** | |
82 | * This method will do a simple XPath.evaluate, while wrapping your xpathExpression with the xstreamSafe function. It assumes a | |
83 | * String result, and will return such. If an XPathExpressionException is thrown, this will be re-thrown within a | |
84 | * RuntimeException. | |
85 | * | |
86 | * @param xpath A correctly initialized XPath instance. | |
87 | * @param xpathExpression Your XPath Expression that needs to be wrapped in an xstreamSafe wrapper and run. | |
88 | * @param item The document contents you will be searching within. | |
89 | * @return The string value of the xpath.evaluate(). | |
90 | */ | |
91 | public static final String xstreamSafeEval(XPath xpath, String xpathExpression, Object item) { | |
92 | 0 | String xstreamSafeXPath = xstreamSafeXPath(xpathExpression); |
93 | 0 | String evalResult = ""; |
94 | try { | |
95 | 0 | evalResult = xpath.evaluate(xstreamSafeXPath, item); |
96 | } | |
97 | 0 | catch (XPathExpressionException e) { |
98 | 0 | throw new RuntimeException("XPathExpressionException occurred on xpath: " + xstreamSafeXPath, e); |
99 | 0 | } |
100 | 0 | return evalResult; |
101 | } | |
102 | ||
103 | /** | |
104 | * This method wraps the passed-in XPath expression in XStream Safe wrappers, so that XStream generated reference links will be | |
105 | * handled correctly. | |
106 | * | |
107 | * @param xpathExpression The XPath Expression you wish to use. | |
108 | * @return Your XPath Expression wrapped in the XStreamSafe wrapper. | |
109 | */ | |
110 | public static final String xstreamSafeXPath(String xpathExpression) { | |
111 | 0 | return new StringBuilder(XSTREAM_SAFE_PREFIX).append(xpathExpression).append(XSTREAM_SAFE_SUFFIX).toString(); |
112 | } | |
113 | ||
114 | /** | |
115 | * This method returns a label from the data dictionary service | |
116 | * | |
117 | * @param businessObjectClass - class where the label should come from | |
118 | * @param attributeName - name of the attribute you need the label for | |
119 | * @return the label from the data dictionary for the given Class and attributeName or null if not found | |
120 | */ | |
121 | public static final String getBusinessObjectAttributeLabel(Class businessObjectClass, String attributeName) { | |
122 | 0 | return KRADServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(businessObjectClass, attributeName); |
123 | } | |
124 | ||
125 | ||
126 | /** | |
127 | * This method builds a workflow-lookup-screen Row of type TEXT, with no quickfinder/lookup. | |
128 | * | |
129 | * @param propertyClass The Class of the BO that this row is based on. For example, Account.class for accountNumber. | |
130 | * @param boPropertyName The property name on the BO that this row is based on. For example, accountNumber for | |
131 | * Account.accountNumber. | |
132 | * @param workflowPropertyKey The workflow-lookup-screen property key. For example, account_nbr for Account.accountNumber. This | |
133 | * key can be anything, but needs to be consistent with what is used for the row/field key on the java attribute, so | |
134 | * everything links up correctly. | |
135 | * @return A populated and ready-to-use workflow lookupable.Row. | |
136 | */ | |
137 | public static Row buildTextRow(Class propertyClass, String boPropertyName, String workflowPropertyKey) { | |
138 | 0 | if (propertyClass == null) { |
139 | 0 | throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value."); |
140 | } | |
141 | 0 | if (StringUtils.isBlank(boPropertyName)) { |
142 | 0 | throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value."); |
143 | } | |
144 | 0 | if (StringUtils.isBlank(workflowPropertyKey)) { |
145 | 0 | throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value."); |
146 | } | |
147 | 0 | List<Field> fields = new ArrayList<Field>(); |
148 | Field field; | |
149 | 0 | field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false); |
150 | 0 | fields.add(field); |
151 | 0 | return new Row(fields); |
152 | } | |
153 | ||
154 | /** | |
155 | * This method builds a workflow-lookup-screen Row of type TEXT, with the attached lookup icon and functionality. | |
156 | * | |
157 | * @param propertyClass The Class of the BO that this row is based on. For example, Account.class for accountNumber. | |
158 | * @param boPropertyName The property name on the BO that this row is based on. For example, accountNumber for | |
159 | * Account.accountNumber. | |
160 | * @param workflowPropertyKey The workflow-lookup-screen property key. For example, account_nbr for Account.accountNumber. This | |
161 | * key can be anything, but needs to be consistent with what is used for the row/field key on the java attribute, so | |
162 | * everything links up correctly. | |
163 | * @return A populated and ready-to-use workflow lookupable.Row, which includes both the property field and the lookup icon. | |
164 | */ | |
165 | public static Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey) { | |
166 | 0 | return buildTextRowWithLookup(propertyClass, boPropertyName, workflowPropertyKey, null); |
167 | } | |
168 | ||
169 | /** | |
170 | * This method builds a workflow-lookup-screen Row of type TEXT, with the attached lookup icon and functionality. | |
171 | * | |
172 | * @param propertyClass The Class of the BO that this row is based on. For example, Account.class for accountNumber. | |
173 | * @param boPropertyName The property name on the BO that this row is based on. For example, accountNumber for | |
174 | * Account.accountNumber. | |
175 | * @param workflowPropertyKey The workflow-lookup-screen property key. For example, account_nbr for Account.accountNumber. This | |
176 | * key can be anything, but needs to be consistent with what is used for the row/field key on the java attribute, so | |
177 | * everything links up correctly. | |
178 | * @param fieldConversionsByBoPropertyName A list of extra field conversions where the key is the business object property name | |
179 | * and the value is the workflow property key | |
180 | * @return A populated and ready-to-use workflow lookupable.Row, which includes both the property field and the lookup icon. | |
181 | */ | |
182 | public static Row buildTextRowWithLookup(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map fieldConversionsByBoPropertyName) { | |
183 | 0 | if (propertyClass == null) { |
184 | 0 | throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value."); |
185 | } | |
186 | 0 | if (StringUtils.isBlank(boPropertyName)) { |
187 | 0 | throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value."); |
188 | } | |
189 | 0 | if (StringUtils.isBlank(workflowPropertyKey)) { |
190 | 0 | throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value."); |
191 | } | |
192 | Field field; | |
193 | 0 | field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false); |
194 | ||
195 | 0 | List<Field> fields = new ArrayList<Field>(); |
196 | 0 | fields.add(field); |
197 | 0 | return new Row(fields); |
198 | } | |
199 | ||
200 | /** | |
201 | * This method builds a workflow-lookup-screen Row of type DROPDOWN. | |
202 | * | |
203 | * @param propertyClass The Class of the BO that this row is based on. For example, Account.class for accountNumber. | |
204 | * @param boPropertyName The property name on the BO that this row is based on. For example, accountNumber for | |
205 | * Account.accountNumber. | |
206 | * @param workflowPropertyKey The workflow-lookup-screen property key. For example, account_nbr for Account.accountNumber. This | |
207 | * key can be anything, but needs to be consistent with what is used for the row/field key on the java attribute, so | |
208 | * everything links up correctly. | |
209 | * @param optionMap The map of value, text pairs that will be used to constuct the dropdown list. | |
210 | * @return A populated and ready-to-use workflow lookupable.Row. | |
211 | */ | |
212 | public static Row buildDropdownRow(Class propertyClass, String boPropertyName, String workflowPropertyKey, Map<String, String> optionMap, boolean addBlankRow) { | |
213 | 0 | if (propertyClass == null) { |
214 | 0 | throw new IllegalArgumentException("Method parameter 'propertyClass' was passed a NULL value."); |
215 | } | |
216 | 0 | if (StringUtils.isBlank(boPropertyName)) { |
217 | 0 | throw new IllegalArgumentException("Method parameter 'boPropertyName' was passed a NULL or blank value."); |
218 | } | |
219 | 0 | if (StringUtils.isBlank(workflowPropertyKey)) { |
220 | 0 | throw new IllegalArgumentException("Method parameter 'workflowPropertyKey' was passed a NULL or blank value."); |
221 | } | |
222 | 0 | if (optionMap == null) { |
223 | 0 | throw new IllegalArgumentException("Method parameter 'optionMap' was passed a NULL value."); |
224 | } | |
225 | 0 | List<Field> fields = new ArrayList<Field>(); |
226 | Field field; | |
227 | 0 | field = FieldUtils.getPropertyField(propertyClass, boPropertyName, false); |
228 | 0 | fields.add(field); |
229 | 0 | return new Row(fields); |
230 | } | |
231 | } |