1 /**
2 * Copyright 2005-2014 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.service.KRADServiceLocator;
23 import org.kuali.rice.krad.util.GlobalVariables;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.Map;
30
31 /**
32 * Defines functions that can be used in el expressions within
33 * the UIF dictionary files
34 *
35 * @author Kuali Rice Team (rice.collab@kuali.org)
36 */
37 public class ExpressionFunctions {
38
39 /**
40 * Checks whether the given class parameter is assignable from the given object class
41 * parameter
42 *
43 * @param assignableClass - class to use for assignable to
44 * @param objectClass - class to use for assignable from
45 * @return boolean true if the object class is of type assignable class, false if not
46 */
47 public static boolean isAssignableFrom(Class<?> assignableClass, Class<?> objectClass) {
48 return assignableClass.isAssignableFrom(objectClass);
49 }
50
51 /**
52 * Checks whether the given value is null or blank string
53 *
54 * @param value - property value to check
55 * @return boolean true if value is null or blank, false if not
56 */
57 public static boolean empty(Object value) {
58 return (value == null) || (StringUtils.isBlank(value.toString()));
59 }
60
61 /**
62 * Checks to see if the list is empty. Throws a RuntimeException if list is not a List.
63 *
64 * @param value the list
65 * @return true if the list is null or empty, false otherwise
66 */
67 public static boolean emptyList(List<?> list){
68 return (list == null) || list.isEmpty();
69 }
70
71 /**
72 * Check to see if the list contains the values passed in.
73 *
74 * <p>In the SpringEL call values can be single item or array due to the way the EL converts values.
75 * The values can be string or numeric and should match
76 * the content type being stored in the list. If the list is String and the values passed in are not string,
77 * toString() conversion will be used. Returns true if the values are in the list and both lists are non-empty,
78 * false otherwise.
79 * </p>
80 *
81 * @param list the list to be evaluated
82 * @param values the values to be to check for in the list
83 * @return true if all values exist in the list and both values and list are non-null/not-empty, false otherwise
84 */
85 public static boolean listContains(List<?> list, Object[] values){
86 if(list != null && values != null && values.length > 0 && !list.isEmpty()){
87 //conversion for if the values are non-string but the list is string (special case)
88 if(list.get(0) instanceof String && !(values[0] instanceof String)){
89 String[] stringValues = new String[values.length];
90 for(int i = 0; i < values.length; i++){
91 stringValues[i] = values[i].toString();
92 }
93 return list.containsAll(Arrays.asList(stringValues));
94 }
95 else if(list.get(0) instanceof Date && values[0] instanceof String){
96 //TODO date conversion
97 return false;
98 }
99 else if(!(list.get(0) instanceof String) && values[0] instanceof String){
100 //values passed in are string but the list is of objects, use object's toString method
101 List<String> stringList = new ArrayList<String>();
102 for(Object value: list){
103 stringList.add(value.toString());
104 }
105 return stringList.containsAll(Arrays.asList(values));
106 }
107 else{
108 //no conversion for if neither list is String, assume matching types (numeric case)
109 return list.containsAll(Arrays.asList(values));
110 }
111 }
112
113 //no cases satisfied, return false
114 return false;
115
116 }
117
118 /**
119 * Returns the name for the given class
120 *
121 * @param clazz - class object to return name for
122 * @return String class name or empty string if class is null
123 */
124 public static String getName(Class<?> clazz) {
125 if (clazz == null) {
126 return "";
127 } else {
128 return clazz.getName();
129 }
130 }
131
132 /**
133 * Retrieves the value of the parameter identified with the given namespace, component, and name
134 *
135 * @param namespaceCode - namespace code for the parameter to retrieve
136 * @param componentCode - component code for the parameter to retrieve
137 * @param parameterName - name of the parameter to retrieve
138 * @return String value of parameter as a string or null if parameter does not exist
139 */
140 public static String getParm(String namespaceCode, String componentCode, String parameterName) {
141 return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode, componentCode,
142 parameterName);
143 }
144
145 /**
146 * Retrieves the value of the parameter identified with the given namespace, component, and name and converts
147 * to a Boolean
148 *
149 * @param namespaceCode - namespace code for the parameter to retrieve
150 * @param componentCode - component code for the parameter to retrieve
151 * @param parameterName - name of the parameter to retrieve
152 * @return Boolean value of parameter as a boolean or null if parameter does not exist
153 */
154 public static Boolean getParmInd(String namespaceCode, String componentCode, String parameterName) {
155 return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(namespaceCode,
156 componentCode, parameterName);
157 }
158
159 /**
160 * Indicates whether the current user has the permission identified by the given namespace and permission name
161 *
162 * @param namespaceCode - namespace code for the permission to check
163 * @param permissionName - name of the permission to check
164 * @return boolean true if the current user has the permission, false if not or the permission does not exist
165 */
166 public static boolean hasPerm(String namespaceCode, String permissionName) {
167 Person user = GlobalVariables.getUserSession().getPerson();
168
169 return KimApiServiceLocator.getPermissionService().hasPermission(user.getPrincipalId(), namespaceCode,
170 permissionName);
171 }
172
173 /**
174 * Indicates whether the current user has the permission identified by the given namespace and permission name
175 * and with the given details and role qualification
176 *
177 * @param namespaceCode - namespace code for the permission to check
178 * @param permissionName - name of the permission to check
179 * @param permissionDetails - details for the permission check
180 * @param roleQualifiers - qualification for assigned roles
181 * @return boolean true if the current user has the permission, false if not or the permission does not exist
182 */
183 public static boolean hasPermDtls(String namespaceCode, String permissionName, Map<String, String> permissionDetails,
184 Map<String, String> roleQualifiers) {
185 Person user = GlobalVariables.getUserSession().getPerson();
186
187 return KimApiServiceLocator.getPermissionService().isAuthorized(user.getPrincipalId(), namespaceCode,
188 permissionName, roleQualifiers);
189 }
190
191 /**
192 * Indicates whether the current user has the permission identified by the given namespace and template name
193 * and with the given details and role qualification
194 *
195 * @param namespaceCode - namespace code for the permission to check
196 * @param templateName - name of the permission template to find permissions for
197 * @param permissionDetails - details for the permission check
198 * @param roleQualifiers - qualification for assigned roles
199 * @return boolean true if the current user has a permission with the given template, false if not or
200 * the permission does not exist
201 */
202 public static boolean hasPermTmpl(String namespaceCode, String templateName, Map<String, String> permissionDetails,
203 Map<String, String> roleQualifiers) {
204 Person user = GlobalVariables.getUserSession().getPerson();
205
206 return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(user.getPrincipalId(), namespaceCode,
207 templateName, permissionDetails, roleQualifiers);
208 }
209
210 /**
211 * Gets the next available number from a sequence
212 *
213 * @param sequenceName name of the sequence to retrieve from
214 * @return Long next sequence value
215 */
216 public static Long sequence(String sequenceName) {
217 return KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(sequenceName);
218 }
219 }