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