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