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.krms.impl.ui;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
020import org.kuali.rice.krms.api.repository.operator.CustomOperator;
021import org.kuali.rice.krms.impl.util.KrmsImplConstants;
022
023import javax.xml.namespace.QName;
024
025/**
026 * Utility service used by the KRMS agenda editing UI for display and access to custom operators.
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class CustomOperatorUiTranslator {
031
032
033    /**
034     * Parses the {@link QName} for the custom operator service from the form value string, which has the format
035     * {@code customOperator:<Namespace>:<serviceName>}
036     *
037     * @param customOperatorFormValue
038     * @return the QName for the custom operator service
039     */
040    public QName parseCustomOperatorServiceQName(String customOperatorFormValue) {
041        if (customOperatorFormValue == null ||
042                !isCustomOperatorFormValue(customOperatorFormValue)) {
043            throw new IllegalArgumentException("custom operator form value (" + customOperatorFormValue +
044                    ") must be formatted as " +
045                    KrmsImplConstants.CUSTOM_OPERATOR_PREFIX + "namespace:serviceName");
046        }
047
048        String [] customOperatorServiceInfo = customOperatorFormValue.split(":");
049
050        if (customOperatorServiceInfo == null || customOperatorServiceInfo.length != 3) {
051            throw new IllegalArgumentException("custom operator form value (" + customOperatorFormValue +
052                    ") must be formatted as " +
053                    KrmsImplConstants.CUSTOM_OPERATOR_PREFIX + "namespace:serviceName");
054        }
055
056        QName customOperatorServiceQName = new QName(customOperatorServiceInfo[1], customOperatorServiceInfo[2]);
057
058        return customOperatorServiceQName;
059    }
060
061    /**
062     * Gets the custom operator function name which is used for display purposes
063     *
064     * @param customOperatorFormValue the form value representing the custom operator
065     * @return
066     */
067    public String getCustomOperatorName(String customOperatorFormValue) {
068        return getCustomOperator(customOperatorFormValue).getOperatorFunctionDefinition().getName();
069    }
070
071    /**
072     * Gets the service instance given the specially formatted form value.
073     *
074     * <p>The form value contains the namespace and name of the service, which is used internally for retrieval.</p>
075     *
076     * @param customOperatorFormValue the custom operator form value
077     * @return the custom operator service instance
078     * @throws IllegalArgumentException if the customOperatorFormValue is null or is formatted incorrectly
079     */
080    public CustomOperator getCustomOperator(String customOperatorFormValue) {
081        return GlobalResourceLoader.getService(parseCustomOperatorServiceQName(customOperatorFormValue));
082    }
083
084    /**
085     * Checks if a form value represents a custom operator.
086     *
087     * <p>The determination is made be checking for a special prefix value that is used by convention.</p>
088     *
089     * @param formValue the form value to check
090     * @return true if the form value represents a custom operator.
091     */
092    public boolean isCustomOperatorFormValue(String formValue) {
093        if (StringUtils.isEmpty(formValue)) {
094            return false;
095        }
096
097        return formValue.startsWith(KrmsImplConstants.CUSTOM_OPERATOR_PREFIX);
098    }
099
100}