View Javadoc

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