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