View Javadoc

1   /**
2    * Copyright 2005-2012 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.coreservice.framework.CoreFrameworkServiceLocator;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  
24  import java.util.Map;
25  
26  /**
27   * Defines functions that can be used in el expressions within
28   * the UIF dictionary files
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class ExpressionFunctions {
33  
34      /**
35       * Checks whether the given class parameter is assignable from the given object class
36       * parameter
37       *
38       * @param assignableClass - class to use for assignable to
39       * @param objectClass - class to use for assignable from
40       * @return boolean true if the object class is of type assignable class, false if not
41       */
42      public static boolean isAssignableFrom(Class<?> assignableClass, Class<?> objectClass) {
43          return assignableClass.isAssignableFrom(objectClass);
44      }
45  
46      /**
47       * Checks whether the given value is null or blank string
48       *
49       * @param value - property value to check
50       * @return boolean true if value is null or blank, false if not
51       */
52      public static boolean empty(Object value) {
53          return (value == null) || (StringUtils.isBlank(value.toString()));
54      }
55  
56      /**
57       * Returns the name for the given class
58       *
59       * @param clazz - class object to return name for
60       * @return String class name or empty string if class is null
61       */
62      public static String getName(Class<?> clazz) {
63          if (clazz == null) {
64              return "";
65          } else {
66              return clazz.getName();
67          }
68      }
69  
70      /**
71       * Retrieves the value of the parameter identified with the given namespace, component, and name
72       *
73       * @param namespaceCode - namespace code for the parameter to retrieve
74       * @param componentCode - component code for the parameter to retrieve
75       * @param parameterName - name of the parameter to retrieve
76       * @return String value of parameter as a string or null if parameter does not exist
77       */
78      public static String getParm(String namespaceCode, String componentCode, String parameterName) {
79          return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode, componentCode,
80                  parameterName);
81      }
82  
83      /**
84       * Retrieves the value of the parameter identified with the given namespace, component, and name and converts
85       * to a Boolean
86       *
87       * @param namespaceCode - namespace code for the parameter to retrieve
88       * @param componentCode - component code for the parameter to retrieve
89       * @param parameterName - name of the parameter to retrieve
90       * @return Boolean value of parameter as a boolean or null if parameter does not exist
91       */
92      public static Boolean getParmInd(String namespaceCode, String componentCode, String parameterName) {
93          return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(namespaceCode,
94                  componentCode, parameterName);
95      }
96  
97      /**
98       * Indicates whether the current user has the permission identified by the given namespace and permission name
99       *
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 }