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.kew.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.config.property.ConfigStrLookup;
020import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
021import org.kuali.rice.coreservice.api.parameter.ParameterKey;
022import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.krad.util.KRADConstants;
025
026/**
027 * Uses the KEW runtime parameters to locate a string for replacement, falling back to the deploy time configuration
028 * variables if necessary.
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class ParameterStrLookup extends ConfigStrLookup {
033        
034        private final String applicationId;
035
036    /**
037     * Creates a string locator to search for KEW runtime parameters for any {@code applicationId}.
038     */
039        public ParameterStrLookup() {
040        this(null);
041        }
042
043    /**
044     * Creates a string locator to search for KEW runtime parameters for a specific {@code applicationId}.
045     *
046     * @param applicationId the application to search for the KEW runtime parameter in.
047     */
048        public ParameterStrLookup(String applicationId) {
049                this.applicationId = applicationId;
050        }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    public String lookup(String propertyName) {
057        if (StringUtils.isBlank(propertyName)) {
058            return null;
059        }
060
061        String paramValue;
062
063        // check runtime parameters first
064        if (StringUtils.isBlank(applicationId)) {
065            paramValue = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(
066                    KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.ALL_DETAIL_TYPE, propertyName);
067        } else {
068            ParameterKey parameterKey = ParameterKey.create(applicationId, KewApiConstants.KEW_NAMESPACE,
069                    KRADConstants.DetailTypes.ALL_DETAIL_TYPE, propertyName);
070            paramValue = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameterValueAsString(parameterKey);
071        }
072
073        // fall back to deploy time configurations if empty
074        if (paramValue == null) {
075            paramValue = super.lookup(propertyName);
076        }
077
078        return paramValue;
079    }
080
081}