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