View Javadoc

1   /**
2    * Copyright 2005-2013 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.krad.uif.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.CoreApiServiceLocator;
20  import org.kuali.rice.core.api.encryption.EncryptionService;
21  import org.kuali.rice.core.web.format.Formatter;
22  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
23  import org.kuali.rice.krad.util.KRADConstants;
24  import org.kuali.rice.krad.web.form.UifFormBase;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import java.security.GeneralSecurityException;
28  import java.util.Map;
29  
30  /**
31   * Class for utility methods that pertain to UIF Lookup processing
32   *
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   */
35  public class LookupInquiryUtils {
36  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);
37  
38  	public static String retrieveLookupParameterValue(UifFormBase form, HttpServletRequest request,
39  			Class<?> lookupObjectClass, String propertyName, String propertyValueName) {
40  		String parameterValue = "";
41  
42  		// get literal parameter values first
43  		if (StringUtils.startsWith(propertyValueName, "'") && StringUtils.endsWith(propertyValueName, "'")) {
44              parameterValue = StringUtils.removeStart(propertyValueName, "'");
45  			parameterValue = StringUtils.removeEnd(propertyValueName, "'");
46  		}
47  		else if (parameterValue.startsWith(KRADConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
48  				+ KRADConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER)) {
49  			parameterValue = StringUtils.removeStart(parameterValue, KRADConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
50  					+ KRADConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER);
51  		}
52  		// check if parameter is in request
53  		else if (request.getParameterMap().containsKey(propertyValueName)) {
54  			parameterValue = request.getParameter(propertyValueName);
55  		}
56  		// get parameter value from form object
57  		else {
58  			Object value = ObjectPropertyUtils.getPropertyValue(form, propertyValueName);
59  			if (value != null) {
60  				if (value instanceof String) {
61  					parameterValue = (String) value;
62  				}
63  
64  				Formatter formatter = Formatter.getFormatter(value.getClass());
65  				parameterValue = (String) formatter.format(value);
66  			}
67  		}
68  
69  		if (parameterValue != null
70  				&& lookupObjectClass != null
71  				&& KRADServiceLocatorWeb.getDataObjectAuthorizationService()
72  						.attributeValueNeedsToBeEncryptedOnFormsAndLinks(lookupObjectClass, propertyName)) {
73  			try {
74                  if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
75  				    parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue)
76  					    	+ EncryptionService.ENCRYPTION_POST_PREFIX;
77                  }
78  			}
79  			catch (GeneralSecurityException e) {
80  				LOG.error("Unable to encrypt value for property name: " + propertyName);
81  				throw new RuntimeException(e);
82  			}
83  		}
84  
85  		return parameterValue;
86  	}
87  
88      public static String getBaseLookupUrl() {
89          return CoreApiServiceLocator.getKualiConfigurationService().
90                  getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
91      }
92  
93      /**
94  	 * Helper method for building the title text for an element and a map of
95  	 * key/value pairs
96  	 *
97  	 * @param prependText text to prepend to the title
98  	 * @param element element class the title is being generated for, used to as
99  	 *            the parent for getting the key labels
100 	 * @param keyValueMap map of key value pairs to add to the title text
101 	 * @return title text
102 	 */
103 	public static String getLinkTitleText(String prependText, Class<?> element, Map<String, String> keyValueMap) {
104 		StringBuffer titleText = new StringBuffer(prependText);
105 		for (String key : keyValueMap.keySet()) {
106 			String fieldVal = keyValueMap.get(key).toString();
107 
108 			titleText.append(" " + KRADServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(element, key) + "="
109 					+ fieldVal.toString());
110 		}
111 
112 		return titleText.toString();
113 	}
114 
115 }