1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchRoleException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.ArrayUtil;
28 import com.liferay.portal.kernel.util.OrderByComparator;
29 import com.liferay.portal.kernel.util.StringMaker;
30 import com.liferay.portal.kernel.util.StringPool;
31 import com.liferay.portal.kernel.util.StringUtil;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.model.Group;
34 import com.liferay.portal.model.Role;
35 import com.liferay.portal.model.impl.GroupModelImpl;
36 import com.liferay.portal.model.impl.RoleImpl;
37 import com.liferay.portal.model.impl.RoleModelImpl;
38 import com.liferay.portal.model.impl.UserModelImpl;
39 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
40 import com.liferay.portal.spring.hibernate.FinderCache;
41 import com.liferay.portal.spring.hibernate.HibernateUtil;
42 import com.liferay.util.dao.hibernate.QueryPos;
43 import com.liferay.util.dao.hibernate.QueryUtil;
44
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.LinkedHashMap;
49 import java.util.List;
50 import java.util.Map;
51
52 import org.hibernate.Hibernate;
53 import org.hibernate.SQLQuery;
54 import org.hibernate.Session;
55
56
62 public class RoleFinderImpl implements RoleFinder {
63
64 public static String COUNT_BY_C_N_D_T =
65 RoleFinder.class.getName() + ".countByC_N_D_T";
66
67 public static String COUNT_BY_COMMUNITY =
68 RoleFinder.class.getName() + ".countByCommunity";
69
70 public static String COUNT_BY_ORGANIZATION =
71 RoleFinder.class.getName() + ".countByOrganization";
72
73 public static String COUNT_BY_USER =
74 RoleFinder.class.getName() + ".countByUser";
75
76 public static String COUNT_BY_USER_GROUP =
77 RoleFinder.class.getName() + ".countByUserGroup";
78
79 public static String FIND_BY_USER_GROUP_ROLE =
80 RoleFinder.class.getName() + ".findByUserGroupRole";
81
82 public static String FIND_BY_C_N =
83 RoleFinder.class.getName() + ".findByC_N";
84
85 public static String FIND_BY_U_G =
86 RoleFinder.class.getName() + ".findByU_G";
87
88 public static String FIND_BY_C_N_D_T =
89 RoleFinder.class.getName() + ".findByC_N_D_T";
90
91 public static String FIND_BY_C_N_S_P =
92 RoleFinder.class.getName() + ".findByC_N_S_P";
93
94 public static String JOIN_BY_ROLES_PERMISSIONS =
95 RoleFinder.class.getName() + ".joinByRolesPermissions";
96
97 public static String JOIN_BY_USERS_ROLES =
98 RoleFinder.class.getName() + ".joinByUsersRoles";
99
100 public int countByR_U(long roleId, long userId) throws SystemException {
101 String finderSQL = Role.class.getName();
102 boolean[] finderClassNamesCacheEnabled = new boolean[] {
103 GroupModelImpl.CACHE_ENABLED, RoleModelImpl.CACHE_ENABLED,
104 GroupModelImpl.CACHE_ENABLED_GROUPS_ROLES,
105 UserModelImpl.CACHE_ENABLED_USERS_GROUPS,
106 UserModelImpl.CACHE_ENABLED_USERS_ORGS,
107 UserModelImpl.CACHE_ENABLED_USERS_ROLES,
108 UserModelImpl.CACHE_ENABLED_USERS_USERGROUPS
109 };
110 String[] finderClassNames = new String[] {
111 Group.class.getName(), Role.class.getName(), "Groups_Roles",
112 "Users_Groups", "Users_Orgs", "Users_Roles", "Users_UserGroups"
113 };
114 String finderMethodName = "customCountByR_U";
115 String finderParams[] = new String[] {
116 Long.class.getName(), Long.class.getName()
117 };
118 Object finderArgs[] = new Object[] {new Long(roleId), new Long(userId)};
119
120 Object result = null;
121
122 if (!ArrayUtil.contains(finderClassNamesCacheEnabled, false)) {
123 result = FinderCache.getResult(
124 finderSQL, finderClassNames, finderMethodName, finderParams,
125 finderArgs);
126 }
127
128 if (result == null) {
129 Session session = null;
130
131 try {
132 session = HibernateUtil.openSession();
133
134 StringMaker sm = new StringMaker();
135
136 sm.append("(");
137 sm.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
138 sm.append(") UNION (");
139 sm.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
140 sm.append(") UNION (");
141 sm.append(CustomSQLUtil.get(COUNT_BY_USER));
142 sm.append(") UNION (");
143 sm.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
144 sm.append(")");
145
146 SQLQuery q = session.createSQLQuery(sm.toString());
147
148 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
149
150 QueryPos qPos = QueryPos.getInstance(q);
151
152 for (int i = 0; i < 4; i++) {
153 qPos.add(roleId);
154 qPos.add(userId);
155 }
156
157 int count = 0;
158
159 Iterator itr = q.list().iterator();
160
161 while (itr.hasNext()) {
162 Long l = (Long)itr.next();
163
164 if (l != null) {
165 count += l.intValue();
166 }
167 }
168
169 FinderCache.putResult(
170 finderSQL, finderClassNamesCacheEnabled, finderClassNames,
171 finderMethodName, finderParams, finderArgs,
172 new Long(count));
173
174 return count;
175 }
176 catch (Exception e) {
177 throw new SystemException(e);
178 }
179 finally {
180 HibernateUtil.closeSession(session);
181 }
182 }
183 else {
184 return ((Long)result).intValue();
185 }
186 }
187
188 public int countByC_N_D_T(
189 long companyId, String name, String description, Integer type,
190 LinkedHashMap params)
191 throws SystemException {
192
193 name = StringUtil.lowerCase(name);
194 description = StringUtil.lowerCase(description);
195
196 Session session = null;
197
198 try {
199 session = HibernateUtil.openSession();
200
201 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
202
203 if (type == null) {
204 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
205 }
206
207 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
208 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
209
210 SQLQuery q = session.createSQLQuery(sql);
211
212 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
213
214 QueryPos qPos = QueryPos.getInstance(q);
215
216 setJoin(qPos, params);
217 qPos.add(companyId);
218 qPos.add(name);
219 qPos.add(name);
220 qPos.add(description);
221 qPos.add(description);
222
223 if (type != null) {
224 qPos.add(type);
225 }
226
227 Iterator itr = q.list().iterator();
228
229 if (itr.hasNext()) {
230 Long count = (Long)itr.next();
231
232 if (count != null) {
233 return count.intValue();
234 }
235 }
236
237 return 0;
238 }
239 catch (Exception e) {
240 throw new SystemException(e);
241 }
242 finally {
243 HibernateUtil.closeSession(session);
244 }
245 }
246
247 public List findByUserGroupRole(long userId, long groupId)
248 throws SystemException {
249
250 Session session = null;
251
252 try {
253 session = HibernateUtil.openSession();
254
255 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
256
257 SQLQuery q = session.createSQLQuery(sql);
258
259 q.addEntity("Role_", RoleImpl.class);
260
261 QueryPos qPos = QueryPos.getInstance(q);
262
263 qPos.add(userId);
264 qPos.add(groupId);
265
266 return q.list();
267 }
268 catch (Exception e) {
269 throw new SystemException(e);
270 }
271 finally {
272 HibernateUtil.closeSession(session);
273 }
274 }
275
276 public Role findByC_N(long companyId, String name)
277 throws NoSuchRoleException, SystemException {
278
279 name = StringUtil.lowerCase(name);
280
281 boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
282 String finderClassName = Role.class.getName();
283 String finderMethodName = "customFindByC_N";
284 String finderParams[] = new String[] {
285 Long.class.getName(), String.class.getName()
286 };
287 Object finderArgs[] = new Object[] {new Long(companyId), name};
288
289 Object result = FinderCache.getResult(
290 finderClassName, finderMethodName, finderParams, finderArgs);
291
292 if (result == null) {
293 Session session = null;
294
295 try {
296 session = HibernateUtil.openSession();
297
298 String sql = CustomSQLUtil.get(FIND_BY_C_N);
299
300 SQLQuery q = session.createSQLQuery(sql);
301
302 q.addEntity("Role_", RoleImpl.class);
303
304 QueryPos qPos = QueryPos.getInstance(q);
305
306 qPos.add(companyId);
307 qPos.add(name);
308
309 Iterator itr = q.list().iterator();
310
311 if (itr.hasNext()) {
312 Role role = (Role)itr.next();
313
314 FinderCache.putResult(
315 finderClassNameCacheEnabled, finderClassName,
316 finderMethodName, finderParams, finderArgs, role);
317
318 return role;
319 }
320 }
321 catch (Exception e) {
322 throw new SystemException(e);
323 }
324 finally {
325 HibernateUtil.closeSession(session);
326 }
327
328 throw new NoSuchRoleException(
329 "No Role exists with the key {companyId=" + companyId +
330 ", name=" + name + "}");
331 }
332 else {
333 return (Role)result;
334 }
335 }
336
337 public List findByU_G(long userId, long groupId) throws SystemException {
338 return findByU_G(userId, new long[] {groupId});
339 }
340
341 public List findByU_G(long userId, long[] groupIds) throws SystemException {
342 Session session = null;
343
344 try {
345 session = HibernateUtil.openSession();
346
347 String sql = CustomSQLUtil.get(FIND_BY_U_G);
348
349 sql = StringUtil.replace(
350 sql, "[$GROUP_IDS$]", getGroupIds(groupIds, "Groups_Roles"));
351
352 SQLQuery q = session.createSQLQuery(sql);
353
354 q.addEntity("Role_", RoleImpl.class);
355
356 QueryPos qPos = QueryPos.getInstance(q);
357
358 qPos.add(userId);
359 setGroupIds(qPos, groupIds);
360
361 return q.list();
362 }
363 catch (Exception e) {
364 throw new SystemException(e);
365 }
366 finally {
367 HibernateUtil.closeSession(session);
368 }
369 }
370
371 public List findByU_G(long userId, List groups) throws SystemException {
372 long[] groupIds = new long[groups.size()];
373
374 for (int i = 0; i < groups.size(); i++) {
375 Group group = (Group)groups.get(i);
376
377 groupIds[i] = group.getGroupId();
378 }
379
380 return findByU_G(userId, groupIds);
381 }
382
383 public List findByC_N_D_T(
384 long companyId, String name, String description, Integer type,
385 LinkedHashMap params, int begin, int end, OrderByComparator obc)
386 throws SystemException {
387
388 name = StringUtil.lowerCase(name);
389 description = StringUtil.lowerCase(description);
390
391 Session session = null;
392
393 try {
394 session = HibernateUtil.openSession();
395
396 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
397
398 if (type == null) {
399 sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)", "");
400 }
401
402 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
403 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
404 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
405
406 SQLQuery q = session.createSQLQuery(sql);
407
408 q.addEntity("Role_", RoleImpl.class);
409
410 QueryPos qPos = QueryPos.getInstance(q);
411
412 setJoin(qPos, params);
413 qPos.add(companyId);
414 qPos.add(name);
415 qPos.add(name);
416 qPos.add(description);
417 qPos.add(description);
418
419 if (type != null) {
420 qPos.add(type);
421 }
422
423 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
424 }
425 catch (Exception e) {
426 throw new SystemException(e);
427 }
428 finally {
429 HibernateUtil.closeSession(session);
430 }
431 }
432
433 public Map findByC_N_S_P(
434 long companyId, String name, int scope, String primKey)
435 throws SystemException {
436
437 Session session = null;
438
439 try {
440 session = HibernateUtil.openSession();
441
442 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
443
444 SQLQuery q = session.createSQLQuery(sql);
445
446 q.addScalar("roleName", Hibernate.STRING);
447 q.addScalar("actionId", Hibernate.STRING);
448
449 QueryPos qPos = QueryPos.getInstance(q);
450
451 qPos.add(companyId);
452 qPos.add(name);
453 qPos.add(scope);
454 qPos.add(primKey);
455
456 Map roleMap = new HashMap();
457
458 Iterator itr = q.list().iterator();
459
460 while (itr.hasNext()) {
461 Object[] array = (Object[])itr.next();
462
463 String roleName = (String)array[0];
464 String actionId = (String)array[1];
465
466 List roleList = (List)roleMap.get(roleName);
467
468 if (roleList == null) {
469 roleList = new ArrayList();
470 }
471
472 roleList.add(actionId);
473
474 roleMap.put(roleName, roleList);
475 }
476
477 return roleMap;
478 }
479 catch (Exception e) {
480 throw new SystemException(e);
481 }
482 finally {
483 HibernateUtil.closeSession(session);
484 }
485 }
486
487 protected String getGroupIds(long[] groupIds, String table) {
488 StringMaker sm = new StringMaker();
489
490 for (int i = 0; i < groupIds.length; i++) {
491 sm.append(table);
492 sm.append(".groupId = ?");
493
494 if ((i + 1) < groupIds.length) {
495 sm.append(" OR ");
496 }
497 }
498
499 return sm.toString();
500 }
501
502 protected void setGroupIds(QueryPos qPos, long[] groupIds) {
503 for (int i = 0; i < groupIds.length; i++) {
504 qPos.add(groupIds[i]);
505 }
506 }
507
508 protected String getJoin(LinkedHashMap params) {
509 if (params == null) {
510 return StringPool.BLANK;
511 }
512
513 StringMaker sm = new StringMaker();
514
515 Iterator itr = params.entrySet().iterator();
516
517 while (itr.hasNext()) {
518 Map.Entry entry = (Map.Entry)itr.next();
519
520 String key = (String)entry.getKey();
521 Object value = entry.getValue();
522
523 if (Validator.isNotNull(value)) {
524 sm.append(getJoin(key));
525 }
526 }
527
528 return sm.toString();
529 }
530
531 protected String getJoin(String key) {
532 String join = StringPool.BLANK;
533
534 if (key.equals("permissionsResourceId")) {
535 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
536 }
537 else if (key.equals("usersRoles")) {
538 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
539 }
540
541 if (Validator.isNotNull(join)) {
542 int pos = join.indexOf("WHERE");
543
544 if (pos != -1) {
545 join = join.substring(0, pos);
546 }
547 }
548
549 return join;
550 }
551
552 protected String getWhere(LinkedHashMap params) {
553 if (params == null) {
554 return StringPool.BLANK;
555 }
556
557 StringMaker sm = new StringMaker();
558
559 Iterator itr = params.entrySet().iterator();
560
561 while (itr.hasNext()) {
562 Map.Entry entry = (Map.Entry)itr.next();
563
564 String key = (String)entry.getKey();
565 Object value = entry.getValue();
566
567 if (Validator.isNotNull(value)) {
568 sm.append(getWhere(key));
569 }
570 }
571
572 return sm.toString();
573 }
574
575 protected String getWhere(String key) {
576 String join = StringPool.BLANK;
577
578 if (key.equals("permissionsResourceId")) {
579 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
580 }
581 else if (key.equals("usersRoles")) {
582 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
583 }
584
585 if (Validator.isNotNull(join)) {
586 int pos = join.indexOf("WHERE");
587
588 if (pos != -1) {
589 join = join.substring(pos + 5, join.length()) + " AND ";
590 }
591 }
592
593 return join;
594 }
595
596 protected void setJoin(QueryPos qPos, LinkedHashMap params) {
597 if (params != null) {
598 Iterator itr = params.entrySet().iterator();
599
600 while (itr.hasNext()) {
601 Map.Entry entry = (Map.Entry)itr.next();
602
603 Object value = entry.getValue();
604
605 if (value instanceof Long) {
606 Long valueLong = (Long)value;
607
608 if (Validator.isNotNull(valueLong)) {
609 qPos.add(valueLong);
610 }
611 }
612 else if (value instanceof String) {
613 String valueString = (String)value;
614
615 if (Validator.isNotNull(valueString)) {
616 qPos.add(valueString);
617 }
618 }
619 }
620 }
621 }
622
623 }