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