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