View Javadoc

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