View Javadoc

1   /**
2    * Copyright 2005-2014 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                  if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
76  				    parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue)
77  					    	+ EncryptionService.ENCRYPTION_POST_PREFIX;
78                  }
79  			}
80  			catch (GeneralSecurityException e) {
81  				LOG.error("Unable to encrypt value for property name: " + propertyName);
82  				throw new RuntimeException(e);
83  			}
84  		}
85  
86  		return parameterValue;
87  	}
88  
89      public static String getBaseLookupUrl() {
90          return KRADServiceLocator.getKualiConfigurationService().
91                  getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
92      }
93  
94      /**
95  	 * Helper method for building the title text for an element and a map of
96  	 * key/value pairs
97  	 *
98  	 * @param prependText
99  	 *            - text to prepend to the title
100 	 * @param element
101 	 *            - element class the title is being generated for, used to as
102 	 *            the parent for getting the key labels
103 	 * @param keyValueMap
104 	 *            - map of key value pairs to add to the title text
105 	 * @return String title text
106 	 */
107 	public static String getLinkTitleText(String prependText, Class<?> element, Map<String, String> keyValueMap) {
108 		StringBuffer titleText = new StringBuffer(prependText);
109 		for (String key : keyValueMap.keySet()) {
110 			String fieldVal = keyValueMap.get(key).toString();
111 
112 			titleText.append(" " + KRADServiceLocatorWeb.getDataDictionaryService().getAttributeLabel(element, key) + "="
113 					+ fieldVal.toString());
114 		}
115 
116 		return titleText.toString();
117 	}
118 
119 }