View Javadoc
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.core.api.config.property.ConfigContext;
20  import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
21  import org.kuali.rice.kim.api.identity.Person;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
24  import org.kuali.rice.krad.service.KRADServiceLocator;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  import org.kuali.rice.krad.service.KualiModuleService;
27  import org.kuali.rice.krad.service.ModuleService;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.KRADConstants;
30  
31  import javax.sql.DataSource;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Date;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * Defines functions that can be used in el expressions within
40   * the UIF dictionary files
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class ExpressionFunctions {
45  
46      /**
47       * Checks whether the given class parameter is assignable from the given object class
48       * parameter
49       *
50       * @param assignableClass class to use for assignable to
51       * @param objectClass class to use for assignable from
52       * @return true if the object class is of type assignable class, false if not
53       */
54      public static boolean isAssignableFrom(Class<?> assignableClass, Class<?> objectClass) {
55          return assignableClass.isAssignableFrom(objectClass);
56      }
57  
58      /**
59       * Checks whether the given value is null or blank string
60       *
61       * @param value property value to check
62       * @return true if value is null or blank, false if not
63       */
64      public static boolean empty(Object value) {
65          return (value == null) || (StringUtils.isBlank(value.toString()));
66      }
67  
68      /**
69       * Checks to see if the list is empty.  Throws a RuntimeException if list is not a List.
70       *
71       * @param list the list
72       * @return true if the list is null or empty, false otherwise
73       */
74      public static boolean emptyList(List<?> list) {
75          return (list == null) || list.isEmpty();
76      }
77  
78      /**
79       * Check to see if the list contains the values passed in.
80       *
81       * <p>In the SpringEL call values can be single item or array due to the way the EL converts values.
82       * The values can be string or numeric and should match
83       * the content type being stored in the list.  If the list is String and the values passed in are not string,
84       * toString() conversion will be used.  Returns true if the values are in the list and both lists are non-empty,
85       * false otherwise.
86       * </p>
87       *
88       * @param list the list to be evaluated
89       * @param values the values to be to check for in the list
90       * @return true if all values exist in the list and both values and list are non-null/not-empty, false otherwise
91       */
92      public static boolean listContains(List<?> list, Object[] values) {
93          if (list != null && values != null && values.length > 0 && !list.isEmpty()) {
94              //conversion for if the values are non-string but the list is string (special case)
95              if (list.get(0) instanceof String && !(values[0] instanceof String)) {
96                  String[] stringValues = new String[values.length];
97                  for (int i = 0; i < values.length; i++) {
98                      stringValues[i] = values[i].toString();
99                  }
100                 return list.containsAll(Arrays.asList(stringValues));
101             } else if (list.get(0) instanceof Date && values[0] instanceof String) {
102                 //TODO date conversion
103                 return false;
104             } else if (!(list.get(0) instanceof String) && values[0] instanceof String) {
105                 //values passed in are string but the list is of objects, use object's toString method
106                 List<String> stringList = new ArrayList<String>();
107                 for (Object value : list) {
108                     stringList.add(value.toString());
109                 }
110                 return stringList.containsAll(Arrays.asList(values));
111             } else {
112                 //no conversion for if neither list is String, assume matching types (numeric case)
113                 return list.containsAll(Arrays.asList(values));
114             }
115         }
116 
117         //no cases satisfied, return false
118         return false;
119 
120     }
121 
122     /**
123      * Returns the name for the given class
124      *
125      * @param clazz class object to return name for
126      * @return class name or empty string if class is null
127      */
128     public static String getName(Class<?> clazz) {
129         if (clazz == null) {
130             return "";
131         } else {
132             return clazz.getName();
133         }
134     }
135 
136     /**
137      * Retrieves the value of the parameter identified with the given namespace, component, and name
138      *
139      * @param namespaceCode namespace code for the parameter to retrieve
140      * @param componentCode component code for the parameter to retrieve
141      * @param parameterName name of the parameter to retrieve
142      * @return String value of parameter as a string or null if parameter does not exist
143      */
144     public static String getParam(String namespaceCode, String componentCode, String parameterName) {
145         return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode, componentCode,
146                 parameterName);
147     }
148 
149     /**
150      * Retrieves the value of the parameter identified with the given namespace, component, and name and converts
151      * to a Boolean
152      *
153      * @param namespaceCode namespace code for the parameter to retrieve
154      * @param componentCode component code for the parameter to retrieve
155      * @param parameterName name of the parameter to retrieve
156      * @return Boolean value of parameter as a boolean or null if parameter does not exist
157      */
158     public static Boolean getParamAsBoolean(String namespaceCode, String componentCode, String parameterName) {
159         return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(namespaceCode,
160                 componentCode, parameterName);
161     }
162 
163     /**
164      * Indicates whether the current user has the permission identified by the given namespace and permission name
165      *
166      * @param namespaceCode namespace code for the permission to check
167      * @param permissionName name of the permission to check
168      * @return true if the current user has the permission, false if not or the permission does not exist
169      */
170     public static boolean hasPerm(String namespaceCode, String permissionName) {
171         Person user = GlobalVariables.getUserSession().getPerson();
172 
173         return KimApiServiceLocator.getPermissionService().hasPermission(user.getPrincipalId(), namespaceCode,
174                 permissionName);
175     }
176 
177     /**
178      * Indicates whether the current user has the permission identified by the given namespace and permission name
179      * and with the given details and role qualification
180      *
181      * @param namespaceCode namespace code for the permission to check
182      * @param permissionName name of the permission to check
183      * @param permissionDetails details for the permission check
184      * @param roleQualifiers qualification for assigned roles
185      * @return true if the current user has the permission, false if not or the permission does not exist
186      */
187     public static boolean hasPermDtls(String namespaceCode, String permissionName,
188             Map<String, String> permissionDetails, Map<String, String> roleQualifiers) {
189         Person user = GlobalVariables.getUserSession().getPerson();
190 
191         return KimApiServiceLocator.getPermissionService().isAuthorized(user.getPrincipalId(), namespaceCode,
192                 permissionName, roleQualifiers);
193     }
194 
195     /**
196      * Indicates whether the current user has the permission identified by the given namespace and template name
197      * and with the given details and role qualification
198      *
199      * @param namespaceCode namespace code for the permission to check
200      * @param templateName name of the permission template to find permissions for
201      * @param permissionDetails details for the permission check
202      * @param roleQualifiers qualification for assigned roles
203      * @return true if the current user has a permission with the given template, false if not or
204      *         the permission does not exist
205      */
206     public static boolean hasPermTmpl(String namespaceCode, String templateName, Map<String, String> permissionDetails,
207             Map<String, String> roleQualifiers) {
208         Person user = GlobalVariables.getUserSession().getPerson();
209 
210         return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(user.getPrincipalId(), namespaceCode,
211                 templateName, permissionDetails, roleQualifiers);
212     }
213 
214     /**
215      * Gets the next available number from a sequence
216      *
217      * @param sequenceName name of the sequence to retrieve from
218      * @return next sequence value
219      */
220     public static Long sequence(String sequenceName) {
221         DataSource dataSource = (DataSource) ConfigContext.getCurrentContextConfig().getObject(KRADConstants.KRAD_APPLICATION_DATASOURCE);
222         if (dataSource == null) {
223             dataSource = KRADServiceLocator.getKradApplicationDataSource();
224         }
225         return Long.valueOf(MaxValueIncrementerFactory.getIncrementer(dataSource, sequenceName).nextLongValue());
226 
227     }
228 
229     /**
230      * Get the a primary key (valid for inquiry/maintenance view retrieval) for the dataObject by class name passed in
231      *
232      * @param dataObjectClassName the class name to get the key for
233      * @return a key valid for use as a request parameter for retrieving an inquiry or maintenance doc
234      */
235     public static String getDataObjectKey(String dataObjectClassName) {
236 
237         if (StringUtils.isBlank(dataObjectClassName)) {
238             throw new RuntimeException("getDataObjectKey SpringEL function failed because the class name was blank");
239         }
240 
241         Class dataObjectClass = null;
242 
243         try {
244             dataObjectClass = Class.forName(dataObjectClassName);
245         } catch (ClassNotFoundException e) {
246             throw new RuntimeException(
247                     "getDataObjectKey SpringEL function failed when trying to find class " + dataObjectClassName, e);
248         }
249 
250         // build list of key values from the map parameters
251         List<String> pkPropertyNames = KRADServiceLocatorWeb.getLegacyDataAdapter().listPrimaryKeyFieldNames(dataObjectClass);
252 
253         //return first primary key found
254         if (pkPropertyNames != null && !pkPropertyNames.isEmpty()) {
255             return pkPropertyNames.get(0);
256         }
257 
258         //this likely won't be reached, as most should have a primary key (assumption)
259         KualiModuleService kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
260         ModuleService moduleService = kualiModuleService.getResponsibleModuleService(dataObjectClass);
261 
262         // some classes might have alternate keys defined for retrieving
263         List<List<String>> altKeys = null;
264         if (moduleService != null) {
265             altKeys = moduleService.listAlternatePrimaryKeyFieldNames(dataObjectClass);
266         }
267 
268         if (altKeys != null && !altKeys.isEmpty()) {
269             for (List<String> list : altKeys) {
270                 if (list != null && !list.isEmpty()) {
271                     //return any key first found
272                     return list.get(0);
273                 }
274             }
275         }
276 
277         return null;
278     }
279 }