001/**
002 * Copyright 2005-2015 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.coreservice.framework.CoreFrameworkServiceLocator;
020import org.kuali.rice.kim.api.identity.Person;
021import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022import org.kuali.rice.krad.util.GlobalVariables;
023
024import java.util.Map;
025
026/**
027 * Defines functions that can be used in el expressions within
028 * the UIF dictionary files
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class ExpressionFunctions {
033
034    /**
035     * Checks whether the given class parameter is assignable from the given object class
036     * parameter
037     *
038     * @param assignableClass - class to use for assignable to
039     * @param objectClass - class to use for assignable from
040     * @return boolean true if the object class is of type assignable class, false if not
041     */
042    public static boolean isAssignableFrom(Class<?> assignableClass, Class<?> objectClass) {
043        return assignableClass.isAssignableFrom(objectClass);
044    }
045
046    /**
047     * Checks whether the given value is null or blank string
048     *
049     * @param value - property value to check
050     * @return boolean true if value is null or blank, false if not
051     */
052    public static boolean empty(Object value) {
053        return (value == null) || (StringUtils.isBlank(value.toString()));
054    }
055
056    /**
057     * Returns the name for the given class
058     *
059     * @param clazz - class object to return name for
060     * @return String class name or empty string if class is null
061     */
062    public static String getName(Class<?> clazz) {
063        if (clazz == null) {
064            return "";
065        } else {
066            return clazz.getName();
067        }
068    }
069
070    /**
071     * Retrieves the value of the parameter identified with the given namespace, component, and name
072     *
073     * @param namespaceCode - namespace code for the parameter to retrieve
074     * @param componentCode - component code for the parameter to retrieve
075     * @param parameterName - name of the parameter to retrieve
076     * @return String value of parameter as a string or null if parameter does not exist
077     */
078    public static String getParm(String namespaceCode, String componentCode, String parameterName) {
079        return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode, componentCode,
080                parameterName);
081    }
082
083    /**
084     * Retrieves the value of the parameter identified with the given namespace, component, and name and converts
085     * to a Boolean
086     *
087     * @param namespaceCode - namespace code for the parameter to retrieve
088     * @param componentCode - component code for the parameter to retrieve
089     * @param parameterName - name of the parameter to retrieve
090     * @return Boolean value of parameter as a boolean or null if parameter does not exist
091     */
092    public static Boolean getParmInd(String namespaceCode, String componentCode, String parameterName) {
093        return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(namespaceCode,
094                componentCode, parameterName);
095    }
096
097    /**
098     * Indicates whether the current user has the permission identified by the given namespace and permission name
099     *
100     * @param namespaceCode - namespace code for the permission to check
101     * @param permissionName - name of the permission to check
102     * @return boolean true if the current user has the permission, false if not or the permission does not exist
103     */
104    public static boolean hasPerm(String namespaceCode, String permissionName) {
105        Person user = GlobalVariables.getUserSession().getPerson();
106
107        return KimApiServiceLocator.getPermissionService().hasPermission(user.getPrincipalId(), namespaceCode,
108                permissionName);
109    }
110
111    /**
112     * Indicates whether the current user has the permission identified by the given namespace and permission name
113     * and with the given details and role qualification
114     *
115     * @param namespaceCode - namespace code for the permission to check
116     * @param permissionName - name of the permission to check
117     * @param permissionDetails - details for the permission check
118     * @param roleQualifiers - qualification for assigned roles
119     * @return boolean true if the current user has the permission, false if not or the permission does not exist
120     */
121    public static boolean hasPermDtls(String namespaceCode, String permissionName, Map<String, String> permissionDetails,
122            Map<String, String> roleQualifiers) {
123        Person user = GlobalVariables.getUserSession().getPerson();
124
125        return KimApiServiceLocator.getPermissionService().isAuthorized(user.getPrincipalId(), namespaceCode,
126                permissionName, roleQualifiers);
127    }
128
129    /**
130     * Indicates whether the current user has the permission identified by the given namespace and template name
131     * and with the given details and role qualification
132     *
133     * @param namespaceCode - namespace code for the permission to check
134     * @param templateName - name of the permission template to find permissions for
135     * @param permissionDetails - details for the permission check
136     * @param roleQualifiers - qualification for assigned roles
137     * @return boolean true if the current user has a permission with the given template, false if not or
138     * the permission does not exist
139     */
140    public static boolean hasPermTmpl(String namespaceCode, String templateName, Map<String, String> permissionDetails,
141            Map<String, String> roleQualifiers) {
142        Person user = GlobalVariables.getUserSession().getPerson();
143
144        return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(user.getPrincipalId(), namespaceCode,
145                templateName, permissionDetails, roleQualifiers);
146    }
147}