1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44 public class ExpressionFunctions {
45
46
47
48
49
50
51
52
53
54 public static boolean isAssignableFrom(Class<?> assignableClass, Class<?> objectClass) {
55 return assignableClass.isAssignableFrom(objectClass);
56 }
57
58
59
60
61
62
63
64 public static boolean empty(Object value) {
65 return (value == null) || (StringUtils.isBlank(value.toString()));
66 }
67
68
69
70
71
72
73
74 public static boolean emptyList(List<?> list) {
75 return (list == null) || list.isEmpty();
76 }
77
78
79
80
81
82
83
84
85
86 public static <T extends Object> T getService(String serviceName) {
87 return KRADServiceLocatorWeb.getService(serviceName);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 public static boolean listContains(List<?> list, Object[] values) {
105 if (list != null && values != null && values.length > 0 && !list.isEmpty()) {
106
107 if (list.get(0) instanceof String && !(values[0] instanceof String)) {
108 String[] stringValues = new String[values.length];
109 for (int i = 0; i < values.length; i++) {
110 stringValues[i] = values[i].toString();
111 }
112 return list.containsAll(Arrays.asList(stringValues));
113 } else if (list.get(0) instanceof Date && values[0] instanceof String) {
114
115 return false;
116 } else if (!(list.get(0) instanceof String) && values[0] instanceof String) {
117
118 List<String> stringList = new ArrayList<String>();
119 for (Object value : list) {
120 stringList.add(value.toString());
121 }
122 return stringList.containsAll(Arrays.asList(values));
123 } else {
124
125 return list.containsAll(Arrays.asList(values));
126 }
127 }
128
129
130 return false;
131
132 }
133
134
135
136
137
138
139
140 public static String getName(Class<?> clazz) {
141 if (clazz == null) {
142 return "";
143 } else {
144 return clazz.getName();
145 }
146 }
147
148
149
150
151
152
153
154
155
156 public static String getParam(String namespaceCode, String componentCode, String parameterName) {
157 return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode, componentCode,
158 parameterName);
159 }
160
161
162
163
164
165
166
167
168
169
170 public static Boolean getParamAsBoolean(String namespaceCode, String componentCode, String parameterName) {
171 return CoreFrameworkServiceLocator.getParameterService().getParameterValueAsBoolean(namespaceCode,
172 componentCode, parameterName);
173 }
174
175
176
177
178
179
180
181
182
183
184 public static Integer getParamAsInteger(String namespaceCode, String componentCode, String parameterName) {
185 String param = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode,
186 componentCode, parameterName);
187 if (param == null) {
188 return null;
189 }
190
191 return Integer.valueOf(CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode,
192 componentCode, parameterName));
193 }
194
195
196
197
198
199
200
201
202
203
204 public static Double getParamAsDouble(String namespaceCode, String componentCode, String parameterName) {
205 String param = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode,
206 componentCode, parameterName);
207 if (param == null) {
208 return null;
209 }
210
211 return Double.valueOf(CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(namespaceCode,
212 componentCode, parameterName));
213 }
214
215
216
217
218
219
220
221
222 public static boolean hasPerm(String namespaceCode, String permissionName) {
223 Person user = GlobalVariables.getUserSession().getPerson();
224
225 return KimApiServiceLocator.getPermissionService().hasPermission(user.getPrincipalId(), namespaceCode,
226 permissionName);
227 }
228
229
230
231
232
233
234
235
236
237
238
239 public static boolean hasPermDtls(String namespaceCode, String permissionName,
240 Map<String, String> permissionDetails, Map<String, String> roleQualifiers) {
241 Person user = GlobalVariables.getUserSession().getPerson();
242
243 return KimApiServiceLocator.getPermissionService().isAuthorized(user.getPrincipalId(), namespaceCode,
244 permissionName, roleQualifiers);
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258 public static boolean hasPermTmpl(String namespaceCode, String templateName, Map<String, String> permissionDetails,
259 Map<String, String> roleQualifiers) {
260 Person user = GlobalVariables.getUserSession().getPerson();
261
262 return KimApiServiceLocator.getPermissionService().isAuthorizedByTemplate(user.getPrincipalId(), namespaceCode,
263 templateName, permissionDetails, roleQualifiers);
264 }
265
266
267
268
269
270
271
272 public static Long sequence(String sequenceName) {
273 DataSource dataSource = (DataSource) ConfigContext.getCurrentContextConfig().getObject(KRADConstants.KRAD_APPLICATION_DATASOURCE);
274 if (dataSource == null) {
275 dataSource = KRADServiceLocator.getKradApplicationDataSource();
276 }
277 return Long.valueOf(MaxValueIncrementerFactory.getIncrementer(dataSource, sequenceName).nextLongValue());
278 }
279
280
281
282
283
284
285
286 public static String getDataObjectKey(String dataObjectClassName) {
287
288 if (StringUtils.isBlank(dataObjectClassName)) {
289 throw new RuntimeException("getDataObjectKey SpringEL function failed because the class name was blank");
290 }
291
292 Class dataObjectClass = null;
293
294 try {
295 dataObjectClass = Class.forName(dataObjectClassName);
296 } catch (ClassNotFoundException e) {
297 throw new RuntimeException(
298 "getDataObjectKey SpringEL function failed when trying to find class " + dataObjectClassName, e);
299 }
300
301
302 List<String> pkPropertyNames = KRADServiceLocatorWeb.getLegacyDataAdapter().listPrimaryKeyFieldNames(dataObjectClass);
303
304
305 if (pkPropertyNames != null && !pkPropertyNames.isEmpty()) {
306 return pkPropertyNames.get(0);
307 }
308
309
310 KualiModuleService kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
311 ModuleService moduleService = kualiModuleService.getResponsibleModuleService(dataObjectClass);
312
313
314 List<List<String>> altKeys = null;
315 if (moduleService != null) {
316 altKeys = moduleService.listAlternatePrimaryKeyFieldNames(dataObjectClass);
317 }
318
319 if (altKeys != null && !altKeys.isEmpty()) {
320 for (List<String> list : altKeys) {
321 if (list != null && !list.isEmpty()) {
322
323 return list.get(0);
324 }
325 }
326 }
327
328 return null;
329 }
330
331
332
333
334
335
336 public static boolean isProductionEnvironment() {
337 return ConfigContext.getCurrentContextConfig().isProductionEnvironment();
338 }
339 }