1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.edl.impl.components; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
21 | |
import org.kuali.rice.edl.impl.EDLContext; |
22 | |
import org.kuali.rice.edl.impl.EDLModelComponent; |
23 | |
import org.kuali.rice.kew.api.KEWPropertyConstants; |
24 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
25 | |
import org.kuali.rice.krad.service.KRADServiceLocator; |
26 | |
import org.kuali.rice.krad.util.KRADConstants; |
27 | |
import org.kuali.rice.krad.util.UrlFactory; |
28 | |
import org.w3c.dom.Document; |
29 | |
import org.w3c.dom.Element; |
30 | |
|
31 | |
import javax.xml.xpath.XPath; |
32 | |
import javax.xml.xpath.XPathExpressionException; |
33 | |
import javax.xml.xpath.XPathFactory; |
34 | |
import java.util.Collections; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.Map; |
37 | |
import java.util.Properties; |
38 | |
import java.util.StringTokenizer; |
39 | |
|
40 | 0 | public class PerformLookupComponent implements EDLModelComponent { |
41 | |
|
42 | 0 | private static final Logger LOG = Logger.getLogger(PerformLookupComponent.class); |
43 | |
|
44 | |
@Override |
45 | |
public void updateDOM(Document dom, Element configElement, EDLContext edlContext) { |
46 | 0 | String userAction = edlContext.getUserAction().getAction(); |
47 | 0 | if (userAction != null && userAction.startsWith("performLookup")) { |
48 | 0 | edlContext.setRedirectUrl(constructRedirectUrl(dom, configElement, edlContext)); |
49 | |
} |
50 | 0 | } |
51 | |
|
52 | |
protected String constructRedirectUrl(Document dom, Element configElement, EDLContext edlContext) { |
53 | 0 | StringBuilder buf = new StringBuilder(30); |
54 | 0 | buf.append(KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString( |
55 | |
KRADConstants.APPLICATION_URL_KEY)); |
56 | 0 | buf.append("/kr/").append(KRADConstants.LOOKUP_ACTION); |
57 | |
|
58 | 0 | Properties parameters = new Properties(); |
59 | 0 | parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, getBusinessObjectClassName(dom, configElement, edlContext)); |
60 | 0 | parameters.put(KEWPropertyConstants.DOC_FORM_KEY, edlContext.getUserSession().addObjectWithGeneratedKey(dom)); |
61 | 0 | parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, constructReturnUrl(dom, configElement, edlContext)); |
62 | 0 | parameters.putAll(getLookupParameters(dom, configElement, edlContext)); |
63 | 0 | parameters.put(KRADConstants.CONVERSION_FIELDS_PARAMETER, getFieldConversions(dom, configElement, edlContext)); |
64 | |
|
65 | 0 | String url = UrlFactory.parameterizeUrl(buf.toString(), parameters); |
66 | |
|
67 | 0 | return url; |
68 | |
} |
69 | |
|
70 | |
protected String getBusinessObjectClassName(Document dom, Element configElement, EDLContext edlContext) { |
71 | 0 | String userAction = edlContext.getUserAction().getAction(); |
72 | 0 | String lookupField = StringUtils.substringAfter(userAction, "."); |
73 | 0 | if (StringUtils.isBlank(lookupField)) { |
74 | 0 | LOG.error("Cannot find lookup field parameters definition for field " + lookupField); |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | 0 | XPath xPath = XPathFactory.newInstance().newXPath(); |
79 | |
try { |
80 | 0 | String businessObjectClassName = xPath.evaluate("//fieldDef[@name='" + lookupField + "']/lookup/businessObjectClassName", dom); |
81 | 0 | return businessObjectClassName; |
82 | 0 | } catch (XPathExpressionException e) { |
83 | 0 | throw new WorkflowRuntimeException(e); |
84 | |
} |
85 | |
} |
86 | |
|
87 | |
protected String getFieldConversions(Document dom, Element configElement, EDLContext edlContext) { |
88 | 0 | String userAction = edlContext.getUserAction().getAction(); |
89 | 0 | String lookupField = StringUtils.substringAfter(userAction, "."); |
90 | 0 | if (StringUtils.isBlank(lookupField)) { |
91 | 0 | LOG.error("Cannot find lookup field parameters definition for field " + lookupField); |
92 | 0 | return null; |
93 | |
} |
94 | |
|
95 | 0 | XPath xPath = XPathFactory.newInstance().newXPath(); |
96 | |
try { |
97 | 0 | String lookupParameters = xPath.evaluate("//fieldDef[@name='" + lookupField + "']/lookup/fieldConversions", dom); |
98 | 0 | return lookupParameters; |
99 | 0 | } catch (XPathExpressionException e) { |
100 | 0 | throw new WorkflowRuntimeException(e); |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
protected Map<String, String> getLookupParameters(Document dom, Element configElement, EDLContext edlContext) { |
105 | 0 | String lookupParameterDefinition = retrieveLookupParametersString(dom, configElement, edlContext); |
106 | 0 | if (StringUtils.isBlank(lookupParameterDefinition)) { |
107 | 0 | return Collections.emptyMap(); |
108 | |
} |
109 | 0 | StringTokenizer tok = new StringTokenizer(lookupParameterDefinition, ","); |
110 | 0 | Map<String, String> lookupParameters = new HashMap<String, String>(); |
111 | |
|
112 | |
|
113 | 0 | Element currentVersion = VersioningPreprocessor.findCurrentVersion(dom); |
114 | |
|
115 | 0 | while (tok.hasMoreTokens()) { |
116 | 0 | String parameterDefinition = tok.nextToken(); |
117 | 0 | int colonInd = parameterDefinition.indexOf(':'); |
118 | 0 | if (colonInd == -1) { |
119 | 0 | throw new WorkflowRuntimeException("Lookup definition string improperly formatted " + lookupParameterDefinition); |
120 | |
} |
121 | |
|
122 | 0 | String parameterName = parameterDefinition.substring(colonInd + 1); |
123 | 0 | String parameterValuePropertyName = parameterDefinition.substring(0, colonInd); |
124 | |
|
125 | 0 | XPath xPath = XPathFactory.newInstance().newXPath(); |
126 | |
try { |
127 | 0 | String parameterValue = xPath.evaluate("//field[@name='" + parameterValuePropertyName + "']/value", currentVersion); |
128 | 0 | if (LOG.isDebugEnabled()) { |
129 | 0 | LOG.debug(XmlJotter.jotNode(currentVersion, true)); |
130 | |
} |
131 | 0 | if (StringUtils.isNotBlank(parameterValue)) { |
132 | 0 | lookupParameters.put(parameterName, parameterValue); |
133 | |
} |
134 | 0 | } catch (XPathExpressionException e) { |
135 | 0 | throw new WorkflowRuntimeException(e); |
136 | 0 | } |
137 | 0 | } |
138 | 0 | return lookupParameters; |
139 | |
} |
140 | |
|
141 | |
protected String retrieveLookupParametersString(Document dom, Element configElement, EDLContext edlContext) { |
142 | 0 | String userAction = edlContext.getUserAction().getAction(); |
143 | 0 | String lookupField = StringUtils.substringAfter(userAction, "."); |
144 | 0 | if (StringUtils.isBlank(lookupField)) { |
145 | 0 | LOG.error("Cannot find lookup field parameters definition for field " + lookupField); |
146 | 0 | return null; |
147 | |
} |
148 | |
|
149 | 0 | XPath xPath = XPathFactory.newInstance().newXPath(); |
150 | |
try { |
151 | 0 | String lookupParameters = xPath.evaluate("//fieldDef[@name='" + lookupField + "']/lookup/lookupParameters", dom); |
152 | 0 | return lookupParameters; |
153 | 0 | } catch (XPathExpressionException e) { |
154 | 0 | throw new WorkflowRuntimeException(e); |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
protected String constructReturnUrl(Document dom, Element configElement, EDLContext edlContext) { |
159 | 0 | StringBuilder baseUrl = new StringBuilder(30); |
160 | 0 | baseUrl.append(KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString( |
161 | |
KRADConstants.APPLICATION_URL_KEY)); |
162 | 0 | baseUrl.append("/kew/EDocLite"); |
163 | |
|
164 | 0 | Properties parameters = new Properties(); |
165 | |
|
166 | 0 | String url = UrlFactory.parameterizeUrl(baseUrl.toString(), parameters); |
167 | 0 | return url; |
168 | |
} |
169 | |
} |