001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchRoleException;
018 import com.liferay.portal.kernel.dao.orm.QueryPos;
019 import com.liferay.portal.kernel.dao.orm.QueryUtil;
020 import com.liferay.portal.kernel.dao.orm.SQLQuery;
021 import com.liferay.portal.kernel.dao.orm.Session;
022 import com.liferay.portal.kernel.dao.orm.Type;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.util.OrderByComparator;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.model.Role;
031 import com.liferay.portal.model.impl.RoleImpl;
032 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
033 import com.liferay.util.dao.orm.CustomSQLUtil;
034
035 import java.util.ArrayList;
036 import java.util.HashMap;
037 import java.util.Iterator;
038 import java.util.LinkedHashMap;
039 import java.util.List;
040 import java.util.Map;
041
042
047 public class RoleFinderImpl
048 extends BasePersistenceImpl<Role> implements RoleFinder {
049
050 public static final String COUNT_BY_ORGANIZATION =
051 RoleFinder.class.getName() + ".countByOrganization";
052
053 public static final String COUNT_BY_ORGANIZATION_SITE =
054 RoleFinder.class.getName() + ".countByOrganizationSite";
055
056 public static final String COUNT_BY_SITE =
057 RoleFinder.class.getName() + ".countBySite";
058
059 public static final String COUNT_BY_USER =
060 RoleFinder.class.getName() + ".countByUser";
061
062 public static final String COUNT_BY_USER_GROUP =
063 RoleFinder.class.getName() + ".countByUserGroup";
064
065 public static final String COUNT_BY_USER_GROUP_SITE =
066 RoleFinder.class.getName() + ".countByUserGroupSite";
067
068 public static final String COUNT_BY_U_G_R =
069 RoleFinder.class.getName() + ".countByU_G_R";
070
071 public static final String COUNT_BY_C_N_D_T =
072 RoleFinder.class.getName() + ".countByC_N_D_T";
073
074 public static final String FIND_BY_SYSTEM =
075 RoleFinder.class.getName() + ".findBySystem";
076
077 public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
078 RoleFinder.class.getName() + ".findByUserGroupGroupRole";
079
080 public static final String FIND_BY_USER_GROUP_ROLE =
081 RoleFinder.class.getName() + ".findByUserGroupRole";
082
083 public static final String FIND_BY_C_N =
084 RoleFinder.class.getName() + ".findByC_N";
085
086 public static final String FIND_BY_U_G =
087 RoleFinder.class.getName() + ".findByU_G";
088
089 public static final String FIND_BY_C_N_D_T =
090 RoleFinder.class.getName() + ".findByC_N_D_T";
091
092 public static final String FIND_BY_C_N_S_P =
093 RoleFinder.class.getName() + ".findByC_N_S_P";
094
095 public static final String FIND_BY_C_N_S_P_A =
096 RoleFinder.class.getName() + ".findByC_N_S_P_A";
097
098 public static final String JOIN_BY_ROLES_PERMISSIONS =
099 RoleFinder.class.getName() + ".joinByRolesPermissions";
100
101 public static final String JOIN_BY_USERS_ROLES =
102 RoleFinder.class.getName() + ".joinByUsersRoles";
103
104 public int countByR_U(long roleId, long userId) throws SystemException {
105 Session session = null;
106
107 try {
108 session = openSession();
109
110 SQLQuery q = session.createSQLQuery(getCountByR_U_SQL());
111
112 QueryPos qPos = QueryPos.getInstance(q);
113
114 for (int i = 0; i < 6; i++) {
115 qPos.add(roleId);
116 qPos.add(userId);
117 }
118
119 return q.list().size();
120 }
121 catch (Exception e) {
122 throw new SystemException(e);
123 }
124 finally {
125 closeSession(session);
126 }
127 }
128
129 public int countByU_G_R(long userId, long groupId, long roleId)
130 throws SystemException {
131
132 Session session = null;
133
134 try {
135 session = openSession();
136
137 String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
138
139 SQLQuery q = session.createSQLQuery(sql);
140
141 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
142
143 QueryPos qPos = QueryPos.getInstance(q);
144
145 qPos.add(roleId);
146 qPos.add(groupId);
147 qPos.add(userId);
148
149 Iterator<Long> itr = q.iterate();
150
151 if (itr.hasNext()) {
152 Long count = itr.next();
153
154 if (count != null) {
155 return count.intValue();
156 }
157 }
158
159 return 0;
160 }
161 catch (Exception e) {
162 throw new SystemException(e);
163 }
164 finally {
165 closeSession(session);
166 }
167 }
168
169 public int countByC_N_D_T(
170 long companyId, String name, String description, Integer[] types,
171 LinkedHashMap<String, Object> params, boolean andOperator)
172 throws SystemException {
173
174 String[] names = CustomSQLUtil.keywords(name);
175 String[] descriptions = CustomSQLUtil.keywords(description);
176
177 return countByC_N_D_T(
178 companyId, names, descriptions, types, params, andOperator);
179 }
180
181 public int countByC_N_D_T(
182 long companyId, String[] names, String[] descriptions,
183 Integer[] types, LinkedHashMap<String, Object> params,
184 boolean andOperator)
185 throws SystemException {
186
187 names = CustomSQLUtil.keywords(names, true);
188 descriptions = CustomSQLUtil.keywords(descriptions, true);
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 = CustomSQLUtil.replaceKeywords(
202 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
203 sql = CustomSQLUtil.replaceKeywords(
204 sql, "lower(Role_.description)", StringPool.LIKE, true,
205 descriptions);
206 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
207 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
208 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
209 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
210
211 SQLQuery q = session.createSQLQuery(sql);
212
213 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
214
215 QueryPos qPos = QueryPos.getInstance(q);
216
217 setJoin(qPos, params);
218
219 qPos.add(companyId);
220 qPos.add(names, 2);
221 qPos.add(descriptions, 2);
222 qPos.add(types);
223
224 Iterator<Long> itr = q.iterate();
225
226 if (itr.hasNext()) {
227 Long count = itr.next();
228
229 if (count != null) {
230 return count.intValue();
231 }
232 }
233
234 return 0;
235 }
236 catch (Exception e) {
237 throw new SystemException(e);
238 }
239 finally {
240 closeSession(session);
241 }
242 }
243
244 public int countByKeywords(long companyId, String keywords, Integer[] types)
245 throws SystemException {
246
247 return countByKeywords(
248 companyId, keywords, types, new LinkedHashMap<String, Object>());
249 }
250
251 public int countByKeywords(
252 long companyId, String keywords, Integer[] types,
253 LinkedHashMap<String, Object> params)
254 throws SystemException {
255
256 String[] names = null;
257 String[] descriptions = null;
258 boolean andOperator = false;
259
260 if (Validator.isNotNull(keywords)) {
261 names = CustomSQLUtil.keywords(keywords);
262 descriptions = CustomSQLUtil.keywords(keywords);
263 }
264 else {
265 andOperator = true;
266 }
267
268 return countByC_N_D_T(
269 companyId, names, descriptions, types, params, andOperator);
270 }
271
272 public List<Role> findBySystem(long companyId) throws SystemException {
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
279
280 SQLQuery q = session.createSQLQuery(sql);
281
282 q.addEntity("Role_", RoleImpl.class);
283
284 QueryPos qPos = QueryPos.getInstance(q);
285
286 qPos.add(companyId);
287
288 return q.list(true);
289 }
290 catch (Exception e) {
291 throw new SystemException(e);
292 }
293 finally {
294 closeSession(session);
295 }
296 }
297
298 public List<Role> findByUserGroupGroupRole(long userId, long groupId)
299 throws SystemException {
300
301 Session session = null;
302
303 try {
304 session = openSession();
305
306 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
307
308 SQLQuery q = session.createSQLQuery(sql);
309
310 q.addEntity("Role_", RoleImpl.class);
311
312 QueryPos qPos = QueryPos.getInstance(q);
313
314 qPos.add(userId);
315 qPos.add(groupId);
316
317 return q.list(true);
318 }
319 catch (Exception e) {
320 throw new SystemException(e);
321 }
322 finally {
323 closeSession(session);
324 }
325 }
326
327 public List<Role> findByUserGroupRole(long userId, long groupId)
328 throws SystemException {
329
330 Session session = null;
331
332 try {
333 session = openSession();
334
335 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
336
337 SQLQuery q = session.createSQLQuery(sql);
338
339 q.addEntity("Role_", RoleImpl.class);
340
341 QueryPos qPos = QueryPos.getInstance(q);
342
343 qPos.add(userId);
344 qPos.add(groupId);
345
346 return q.list(true);
347 }
348 catch (Exception e) {
349 throw new SystemException(e);
350 }
351 finally {
352 closeSession(session);
353 }
354 }
355
356 public Role findByC_N(long companyId, String name)
357 throws NoSuchRoleException, SystemException {
358
359 name = StringUtil.lowerCase(name);
360
361 Session session = null;
362
363 try {
364 session = openSession();
365
366 String sql = CustomSQLUtil.get(FIND_BY_C_N);
367
368 SQLQuery q = session.createSQLQuery(sql);
369
370 q.addEntity("Role_", RoleImpl.class);
371
372 QueryPos qPos = QueryPos.getInstance(q);
373
374 qPos.add(companyId);
375 qPos.add(name);
376
377 List<Role> roles = q.list();
378
379 if (!roles.isEmpty()) {
380 return roles.get(0);
381 }
382 }
383 catch (Exception e) {
384 throw new SystemException(e);
385 }
386 finally {
387 closeSession(session);
388 }
389
390 StringBundler sb = new StringBundler(5);
391
392 sb.append("No Role exists with the key {companyId=");
393 sb.append(companyId);
394 sb.append(", name=");
395 sb.append(name);
396 sb.append("}");
397
398 throw new NoSuchRoleException(sb.toString());
399 }
400
401 public List<Role> findByU_G(long userId, List<Group> groups)
402 throws SystemException {
403
404 long[] groupIds = new long[groups.size()];
405
406 for (int i = 0; i < groups.size(); i++) {
407 Group group = groups.get(i);
408
409 groupIds[i] = group.getGroupId();
410 }
411
412 return findByU_G(userId, groupIds);
413 }
414
415 public List<Role> findByU_G(long userId, long groupId)
416 throws SystemException {
417
418 return findByU_G(userId, new long[] {groupId});
419 }
420
421 public List<Role> findByU_G(long userId, long[] groupIds)
422 throws SystemException {
423
424 Session session = null;
425
426 try {
427 session = openSession();
428
429 String sql = CustomSQLUtil.get(FIND_BY_U_G);
430
431 sql = StringUtil.replace(
432 sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
433
434 SQLQuery q = session.createSQLQuery(sql);
435
436 q.addEntity("Role_", RoleImpl.class);
437
438 QueryPos qPos = QueryPos.getInstance(q);
439
440 qPos.add(userId);
441 qPos.add(groupIds);
442
443 return q.list(true);
444 }
445 catch (Exception e) {
446 throw new SystemException(e);
447 }
448 finally {
449 closeSession(session);
450 }
451 }
452
453 public List<Role> findByC_N_D_T(
454 long companyId, String name, String description, Integer[] types,
455 LinkedHashMap<String, Object> params, boolean andOperator,
456 int start, int end, OrderByComparator obc)
457 throws SystemException {
458
459 String[] names = CustomSQLUtil.keywords(name);
460 String[] descriptions = CustomSQLUtil.keywords(description);
461
462 return findByC_N_D_T(
463 companyId, names, descriptions, types, params, andOperator, start,
464 end, obc);
465 }
466
467 public List<Role> findByC_N_D_T(
468 long companyId, String[] names, String[] descriptions,
469 Integer[] types, LinkedHashMap<String, Object> params,
470 boolean andOperator, int start, int end, OrderByComparator obc)
471 throws SystemException {
472
473 names = CustomSQLUtil.keywords(names, true);
474 descriptions = CustomSQLUtil.keywords(descriptions, true);
475
476 if (types == null) {
477 types = new Integer[0];
478 }
479
480 Session session = null;
481
482 try {
483 session = openSession();
484
485 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
486
487 sql = CustomSQLUtil.replaceKeywords(
488 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
489 sql = CustomSQLUtil.replaceKeywords(
490 sql, "lower(Role_.description)", StringPool.LIKE, true,
491 descriptions);
492 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
493 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
494 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
495 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
496 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
497
498 SQLQuery q = session.createSQLQuery(sql);
499
500 q.addEntity("Role_", RoleImpl.class);
501
502 QueryPos qPos = QueryPos.getInstance(q);
503
504 setJoin(qPos, params);
505
506 qPos.add(companyId);
507 qPos.add(names, 2);
508 qPos.add(descriptions, 2);
509 qPos.add(types);
510
511 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
512 }
513 catch (Exception e) {
514 throw new SystemException(e);
515 }
516 finally {
517 closeSession(session);
518 }
519 }
520
521 public Map<String, List<String>> findByC_N_S_P(
522 long companyId, String name, int scope, String primKey)
523 throws SystemException {
524
525 Session session = null;
526
527 try {
528 session = openSession();
529
530 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
531
532 SQLQuery q = session.createSQLQuery(sql);
533
534 q.addScalar("roleName", Type.STRING);
535 q.addScalar("actionId", Type.STRING);
536
537 QueryPos qPos = QueryPos.getInstance(q);
538
539 qPos.add(companyId);
540 qPos.add(name);
541 qPos.add(scope);
542 qPos.add(primKey);
543
544 Map<String, List<String>> roleMap =
545 new HashMap<String, List<String>>();
546
547 Iterator<Object[]> itr = q.iterate();
548
549 while (itr.hasNext()) {
550 Object[] array = itr.next();
551
552 String roleName = (String)array[0];
553 String actionId = (String)array[1];
554
555 List<String> roleList = roleMap.get(roleName);
556
557 if (roleList == null) {
558 roleList = new ArrayList<String>();
559 }
560
561 roleList.add(actionId);
562
563 roleMap.put(roleName, roleList);
564 }
565
566 return roleMap;
567 }
568 catch (Exception e) {
569 throw new SystemException(e);
570 }
571 finally {
572 closeSession(session);
573 }
574 }
575
576 public List<Role> findByC_N_S_P_A(
577 long companyId, String name, int scope, String primKey,
578 String actionId)
579 throws SystemException {
580
581 Session session = null;
582
583 try {
584 session = openSession();
585
586 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
587
588 SQLQuery q = session.createSQLQuery(sql);
589
590 q.addEntity("Role_", RoleImpl.class);
591
592 QueryPos qPos = QueryPos.getInstance(q);
593
594 qPos.add(companyId);
595 qPos.add(name);
596 qPos.add(scope);
597 qPos.add(primKey);
598 qPos.add(actionId);
599
600 return q.list(true);
601 }
602 catch (Exception e) {
603 throw new SystemException(e);
604 }
605 finally {
606 closeSession(session);
607 }
608 }
609
610 public List<Role> findByKeywords(
611 long companyId, String keywords, Integer[] types, int start,
612 int end, OrderByComparator obc)
613 throws SystemException {
614
615 return findByKeywords(
616 companyId, keywords, types, new LinkedHashMap<String, Object>(),
617 start, end, obc);
618 }
619
620 public List<Role> findByKeywords(
621 long companyId, String keywords, Integer[] types,
622 LinkedHashMap<String, Object> params, int start, int end,
623 OrderByComparator obc)
624 throws SystemException {
625
626 String[] names = null;
627 String[] descriptions = null;
628 boolean andOperator = false;
629
630 if (Validator.isNotNull(keywords)) {
631 names = CustomSQLUtil.keywords(keywords);
632 descriptions = CustomSQLUtil.keywords(keywords);
633 }
634 else {
635 andOperator = true;
636 }
637
638 return findByC_N_D_T(
639 companyId, names, descriptions, types, params, andOperator, start,
640 end, obc);
641 }
642
643 protected String getCountByR_U_SQL() {
644 if (_countByR_U == null) {
645 StringBundler sb = new StringBundler(13);
646
647 sb.append("(");
648 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
649 sb.append(") UNION (");
650 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
651 sb.append(") UNION (");
652 sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
653 sb.append(") UNION (");
654 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
655 sb.append(") UNION (");
656 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
657 sb.append(") UNION (");
658 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
659 sb.append(")");
660
661 _countByR_U = sb.toString();
662 }
663
664 return _countByR_U;
665 }
666
667 protected String getGroupIds(long[] groupIds, String table) {
668 if (groupIds.length == 0) {
669 return StringPool.BLANK;
670 }
671
672 StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
673
674 for (int i = 0; i < groupIds.length; i++) {
675 sb.append(table);
676 sb.append(".groupId = ?");
677
678 if ((i + 1) < groupIds.length) {
679 sb.append(" OR ");
680 }
681 }
682
683 return sb.toString();
684 }
685
686 protected String getJoin(LinkedHashMap<String, Object> params) {
687 if ((params == null) || params.isEmpty()) {
688 return StringPool.BLANK;
689 }
690
691 StringBundler sb = new StringBundler(params.size());
692
693 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
694
695 while (itr.hasNext()) {
696 Map.Entry<String, Object> entry = itr.next();
697
698 String key = entry.getKey();
699 Object value = entry.getValue();
700
701 if (Validator.isNotNull(value)) {
702 sb.append(getJoin(key));
703 }
704 }
705
706 return sb.toString();
707 }
708
709 protected String getJoin(String key) {
710 String join = StringPool.BLANK;
711
712 if (key.equals("permissionsResourceId")) {
713 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
714 }
715 else if (key.equals("usersRoles")) {
716 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
717 }
718
719 if (Validator.isNotNull(join)) {
720 int pos = join.indexOf("WHERE");
721
722 if (pos != -1) {
723 join = join.substring(0, pos);
724 }
725 }
726
727 return join;
728 }
729
730 protected String getTypes(Integer[] types) {
731 if (types.length == 0) {
732 return StringPool.BLANK;
733 }
734
735 StringBundler sb = new StringBundler(types.length * 2);
736
737 sb.append(" AND (");
738
739 for (int i = 0; i < types.length; i++) {
740 sb.append("Role_.type_ = ?");
741
742 if ((i + 1) < types.length) {
743 sb.append(" OR ");
744 }
745 }
746
747 sb.append(")");
748
749 return sb.toString();
750 }
751
752 protected String getWhere(LinkedHashMap<String, Object> params) {
753 if ((params == null) || params.isEmpty()) {
754 return StringPool.BLANK;
755 }
756
757 StringBundler sb = new StringBundler(params.size());
758
759 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
760
761 while (itr.hasNext()) {
762 Map.Entry<String, Object> entry = itr.next();
763
764 String key = entry.getKey();
765 Object value = entry.getValue();
766
767 if (Validator.isNotNull(value)) {
768 sb.append(getWhere(key));
769 }
770 }
771
772 return sb.toString();
773 }
774
775 protected String getWhere(String key) {
776 String join = StringPool.BLANK;
777
778 if (key.equals("permissionsResourceId")) {
779 join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
780 }
781 else if (key.equals("usersRoles")) {
782 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
783 }
784
785 if (Validator.isNotNull(join)) {
786 int pos = join.indexOf("WHERE");
787
788 if (pos != -1) {
789 join = join.substring(pos + 5, join.length()).concat(" AND ");
790 }
791 else {
792 join = StringPool.BLANK;
793 }
794 }
795
796 return join;
797 }
798
799 protected void setJoin(
800 QueryPos qPos, LinkedHashMap<String, Object> params) {
801
802 if (params == null) {
803 return;
804 }
805
806 for (Map.Entry<String, Object> entry : params.entrySet()) {
807 Object value = entry.getValue();
808
809 if (value instanceof Long) {
810 Long valueLong = (Long)value;
811
812 if (Validator.isNotNull(valueLong)) {
813 qPos.add(valueLong);
814 }
815 }
816 else if (value instanceof String) {
817 String valueString = (String)value;
818
819 if (Validator.isNotNull(valueString)) {
820 qPos.add(valueString);
821 }
822 }
823 }
824 }
825
826 private String _countByR_U;
827
828 }