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
87
88
89
90
91
92 public static boolean listContains(List<?> list, Object[] values) {
93 if (list != null && values != null && values.length > 0 && !list.isEmpty()) {
94
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
103 return false;
104 } else if (!(list.get(0) instanceof String) && values[0] instanceof String) {
105
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
113 return list.containsAll(Arrays.asList(values));
114 }
115 }
116
117
118 return false;
119
120 }
121
122
123
124
125
126
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
138
139
140
141
142
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
151
152
153
154
155
156
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
165
166
167
168
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
179
180
181
182
183
184
185
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
197
198
199
200
201
202
203
204
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
216
217
218
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
231
232
233
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
251 List<String> pkPropertyNames = KRADServiceLocatorWeb.getLegacyDataAdapter().listPrimaryKeyFieldNames(dataObjectClass);
252
253
254 if (pkPropertyNames != null && !pkPropertyNames.isEmpty()) {
255 return pkPropertyNames.get(0);
256 }
257
258
259 KualiModuleService kualiModuleService = KRADServiceLocatorWeb.getKualiModuleService();
260 ModuleService moduleService = kualiModuleService.getResponsibleModuleService(dataObjectClass);
261
262
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
272 return list.get(0);
273 }
274 }
275 }
276
277 return null;
278 }
279 }