001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreApiServiceLocator;
020import org.kuali.rice.core.api.encryption.EncryptionService;
021import org.kuali.rice.core.web.format.Formatter;
022import org.kuali.rice.krad.service.KRADServiceLocator;
023import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
024import org.kuali.rice.krad.util.KRADConstants;
025import org.kuali.rice.krad.web.form.UifFormBase;
026
027import javax.servlet.http.HttpServletRequest;
028import java.security.GeneralSecurityException;
029import java.util.Map;
030
031/**
032 * Class for utility methods that pertain to UIF Lookup processing
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class LookupInquiryUtils {
037        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupInquiryUtils.class);
038
039        public static String retrieveLookupParameterValue(UifFormBase form, HttpServletRequest request,
040                        Class<?> lookupObjectClass, String propertyName, String propertyValueName) {
041                String parameterValue = "";
042
043                // get literal parameter values first
044                if (StringUtils.startsWith(propertyValueName, "'") && StringUtils.endsWith(propertyValueName, "'")) {
045            parameterValue = StringUtils.removeStart(propertyValueName, "'");
046                        parameterValue = StringUtils.removeEnd(propertyValueName, "'");
047                }
048                else if (parameterValue.startsWith(KRADConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
049                                + KRADConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER)) {
050                        parameterValue = StringUtils.removeStart(parameterValue, KRADConstants.LOOKUP_PARAMETER_LITERAL_PREFIX
051                                        + KRADConstants.LOOKUP_PARAMETER_LITERAL_DELIMITER);
052                }
053                // check if parameter is in request
054                else if (request.getParameterMap().containsKey(propertyValueName)) {
055                        parameterValue = request.getParameter(propertyValueName);
056                }
057                // get parameter value from form object
058                else {
059                        Object value = ObjectPropertyUtils.getPropertyValue(form, propertyValueName);
060                        if (value != null) {
061                                if (value instanceof String) {
062                                        parameterValue = (String) value;
063                                }
064
065                                Formatter formatter = Formatter.getFormatter(value.getClass());
066                                parameterValue = (String) formatter.format(value);
067                        }
068                }
069
070                if (parameterValue != null
071                                && lookupObjectClass != null
072                                && KRADServiceLocatorWeb.getDataObjectAuthorizationService()
073                                                .attributeValueNeedsToBeEncryptedOnFormsAndLinks(lookupObjectClass, propertyName)) {
074                        try {
075                if(CoreApiServiceLocator.getEncryptionService().isEnabled()) {
076                                    parameterValue = CoreApiServiceLocator.getEncryptionService().encrypt(parameterValue)
077                                                + EncryptionService.ENCRYPTION_POST_PREFIX;
078                }
079                        }
080                        catch (GeneralSecurityException e) {
081                                LOG.error("Unable to encrypt value for property name: " + propertyName);
082                                throw new RuntimeException(e);
083                        }
084                }
085
086                return parameterValue;
087        }
088
089    public static String getBaseLookupUrl() {
090        return KRADServiceLocator.getKualiConfigurationService().
091                getPropertyValueAsString(KRADConstants.KRAD_LOOKUP_URL_KEY);
092    }
093
094    /**
095         * Helper method for building the title text for an element and a map of
096         * key/value pairs
097         * 
098         * @param prependText
099         *            - 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}