001
014
015 package com.liferay.portal.service.persistence.impl;
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.ResourceAction;
031 import com.liferay.portal.model.Role;
032 import com.liferay.portal.model.impl.RoleImpl;
033 import com.liferay.portal.security.permission.InlineSQLHelperUtil;
034 import com.liferay.portal.service.ResourceActionLocalServiceUtil;
035 import com.liferay.portal.service.persistence.RoleFinder;
036 import com.liferay.util.dao.orm.CustomSQLUtil;
037
038 import java.util.ArrayList;
039 import java.util.HashMap;
040 import java.util.Iterator;
041 import java.util.LinkedHashMap;
042 import java.util.List;
043 import java.util.Map;
044
045
050 public class RoleFinderImpl
051 extends BasePersistenceImpl<Role> implements RoleFinder {
052
053 public static final String COUNT_BY_ORGANIZATION =
054 RoleFinder.class.getName() + ".countByOrganization";
055
056 public static final String COUNT_BY_ORGANIZATION_SITE =
057 RoleFinder.class.getName() + ".countByOrganizationSite";
058
059 public static final String COUNT_BY_SITE =
060 RoleFinder.class.getName() + ".countBySite";
061
062 public static final String COUNT_BY_USER =
063 RoleFinder.class.getName() + ".countByUser";
064
065 public static final String COUNT_BY_USER_GROUP =
066 RoleFinder.class.getName() + ".countByUserGroup";
067
068 public static final String COUNT_BY_USER_GROUP_GROUP_ROLE =
069 RoleFinder.class.getName() + ".countByUserGroupGroupRole";
070
071 public static final String COUNT_BY_USER_GROUP_SITE =
072 RoleFinder.class.getName() + ".countByUserGroupSite";
073
074 public static final String COUNT_BY_U_G_R =
075 RoleFinder.class.getName() + ".countByU_G_R";
076
077 public static final String COUNT_BY_C_N_D_T =
078 RoleFinder.class.getName() + ".countByC_N_D_T";
079
080 public static final String FIND_BY_SYSTEM =
081 RoleFinder.class.getName() + ".findBySystem";
082
083 public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
084 RoleFinder.class.getName() + ".findByUserGroupGroupRole";
085
086 public static final String FIND_BY_USER_GROUP_ROLE =
087 RoleFinder.class.getName() + ".findByUserGroupRole";
088
089 public static final String FIND_BY_C_N =
090 RoleFinder.class.getName() + ".findByC_N";
091
092 public static final String FIND_BY_U_G =
093 RoleFinder.class.getName() + ".findByU_G";
094
095 public static final String FIND_BY_R_N_A =
096 RoleFinder.class.getName() + ".findByR_N_A";
097
098 public static final String FIND_BY_C_N_D_T =
099 RoleFinder.class.getName() + ".findByC_N_D_T";
100
101 public static final String FIND_BY_C_N_S_P =
102 RoleFinder.class.getName() + ".findByC_N_S_P";
103
104 public static final String FIND_BY_C_N_S_P_A =
105 RoleFinder.class.getName() + ".findByC_N_S_P_A";
106
107 public static final String JOIN_BY_USERS_ROLES =
108 RoleFinder.class.getName() + ".joinByUsersRoles";
109
110 @Override
111 public int countByKeywords(
112 long companyId, String keywords, Integer[] types) {
113
114 return countByKeywords(
115 companyId, keywords, types, new LinkedHashMap<String, Object>());
116 }
117
118 @Override
119 public int countByKeywords(
120 long companyId, String keywords, Integer[] types,
121 LinkedHashMap<String, Object> params) {
122
123 String[] names = null;
124 String[] descriptions = null;
125 boolean andOperator = false;
126
127 if (Validator.isNotNull(keywords)) {
128 names = CustomSQLUtil.keywords(keywords);
129 descriptions = CustomSQLUtil.keywords(keywords);
130 }
131 else {
132 andOperator = true;
133 }
134
135 return countByC_N_D_T(
136 companyId, names, descriptions, types, params, andOperator);
137 }
138
139 @Override
140 public int countByUserGroupGroupRole(long userId, long groupId) {
141 Session session = null;
142
143 try {
144 session = openSession();
145
146 String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_GROUP_ROLE);
147
148 SQLQuery q = session.createSynchronizedSQLQuery(sql);
149
150 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
151
152 QueryPos qPos = QueryPos.getInstance(q);
153
154 qPos.add(groupId);
155 qPos.add(userId);
156
157 Iterator<Long> itr = q.iterate();
158
159 if (itr.hasNext()) {
160 Long count = itr.next();
161
162 if (count != null) {
163 return count.intValue();
164 }
165 }
166
167 return 0;
168 }
169 catch (Exception e) {
170 throw new SystemException(e);
171 }
172 finally {
173 closeSession(session);
174 }
175 }
176
177 @Override
178 public int countByR_U(long roleId, long userId) {
179 Session session = null;
180
181 try {
182 session = openSession();
183
184 SQLQuery q = session.createSynchronizedSQLQuery(
185 getCountByR_U_SQL());
186
187 QueryPos qPos = QueryPos.getInstance(q);
188
189 for (int i = 0; i < 6; i++) {
190 qPos.add(roleId);
191 qPos.add(userId);
192 }
193
194 return q.list().size();
195 }
196 catch (Exception e) {
197 throw new SystemException(e);
198 }
199 finally {
200 closeSession(session);
201 }
202 }
203
204 @Override
205 public int countByU_G_R(long userId, long groupId, long roleId) {
206 Session session = null;
207
208 try {
209 session = openSession();
210
211 String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
212
213 SQLQuery q = session.createSynchronizedSQLQuery(sql);
214
215 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
216
217 QueryPos qPos = QueryPos.getInstance(q);
218
219 qPos.add(roleId);
220 qPos.add(groupId);
221 qPos.add(userId);
222
223 Iterator<Long> itr = q.iterate();
224
225 if (itr.hasNext()) {
226 Long count = itr.next();
227
228 if (count != null) {
229 return count.intValue();
230 }
231 }
232
233 return 0;
234 }
235 catch (Exception e) {
236 throw new SystemException(e);
237 }
238 finally {
239 closeSession(session);
240 }
241 }
242
243 @Override
244 public int countByC_N_D_T(
245 long companyId, String name, String description, Integer[] types,
246 LinkedHashMap<String, Object> params, boolean andOperator) {
247
248 String[] names = CustomSQLUtil.keywords(name);
249 String[] descriptions = CustomSQLUtil.keywords(description);
250
251 return countByC_N_D_T(
252 companyId, names, descriptions, types, params, andOperator);
253 }
254
255 @Override
256 public int countByC_N_D_T(
257 long companyId, String[] names, String[] descriptions, Integer[] types,
258 LinkedHashMap<String, Object> params, boolean andOperator) {
259
260 return doCountByC_N_D_T(
261 companyId, names, descriptions, types, params, andOperator, false);
262 }
263
264 @Override
265 public int filterCountByKeywords(
266 long companyId, String keywords, Integer[] types,
267 LinkedHashMap<String, Object> params) {
268
269 String[] names = null;
270 String[] descriptions = null;
271 boolean andOperator = false;
272
273 if (Validator.isNotNull(keywords)) {
274 names = CustomSQLUtil.keywords(keywords);
275 descriptions = CustomSQLUtil.keywords(keywords);
276 }
277 else {
278 andOperator = true;
279 }
280
281 return filterCountByC_N_D_T(
282 companyId, names, descriptions, types, params, andOperator);
283 }
284
285 @Override
286 public int filterCountByC_N_D_T(
287 long companyId, String name, String description, Integer[] types,
288 LinkedHashMap<String, Object> params, boolean andOperator) {
289
290 String[] names = CustomSQLUtil.keywords(name);
291 String[] descriptions = CustomSQLUtil.keywords(description);
292
293 return filterCountByC_N_D_T(
294 companyId, names, descriptions, types, params, andOperator);
295 }
296
297 @Override
298 public int filterCountByC_N_D_T(
299 long companyId, String[] names, String[] descriptions, Integer[] types,
300 LinkedHashMap<String, Object> params, boolean andOperator) {
301
302 return doCountByC_N_D_T(
303 companyId, names, descriptions, types, params, andOperator, true);
304 }
305
306 @Override
307 public List<Role> filterFindByKeywords(
308 long companyId, String keywords, Integer[] types,
309 LinkedHashMap<String, Object> params, int start, int end,
310 OrderByComparator<Role> obc) {
311
312 String[] names = null;
313 String[] descriptions = null;
314 boolean andOperator = false;
315
316 if (Validator.isNotNull(keywords)) {
317 names = CustomSQLUtil.keywords(keywords);
318 descriptions = CustomSQLUtil.keywords(keywords);
319 }
320 else {
321 andOperator = true;
322 }
323
324 return filterFindByC_N_D_T(
325 companyId, names, descriptions, types, params, andOperator, start,
326 end, obc);
327 }
328
329 @Override
330 public List<Role> filterFindByC_N_D_T(
331 long companyId, String name, String description, Integer[] types,
332 LinkedHashMap<String, Object> params, boolean andOperator, int start,
333 int end, OrderByComparator<Role> obc) {
334
335 String[] names = CustomSQLUtil.keywords(name);
336 String[] descriptions = CustomSQLUtil.keywords(description);
337
338 return filterFindByC_N_D_T(
339 companyId, names, descriptions, types, params, andOperator, start,
340 end, obc);
341 }
342
343 @Override
344 public List<Role> filterFindByC_N_D_T(
345 long companyId, String[] names, String[] descriptions, Integer[] types,
346 LinkedHashMap<String, Object> params, boolean andOperator, int start,
347 int end, OrderByComparator<Role> obc) {
348
349 return doFindByC_N_D_T(
350 companyId, names, descriptions, types, params, andOperator, start,
351 end, obc, true);
352 }
353
354 @Override
355 public List<Role> findByKeywords(
356 long companyId, String keywords, Integer[] types, int start, int end,
357 OrderByComparator<Role> obc) {
358
359 return findByKeywords(
360 companyId, keywords, types, new LinkedHashMap<String, Object>(),
361 start, end, obc);
362 }
363
364 @Override
365 public List<Role> findByKeywords(
366 long companyId, String keywords, Integer[] types,
367 LinkedHashMap<String, Object> params, int start, int end,
368 OrderByComparator<Role> obc) {
369
370 String[] names = null;
371 String[] descriptions = null;
372 boolean andOperator = false;
373
374 if (Validator.isNotNull(keywords)) {
375 names = CustomSQLUtil.keywords(keywords);
376 descriptions = CustomSQLUtil.keywords(keywords);
377 }
378 else {
379 andOperator = true;
380 }
381
382 return findByC_N_D_T(
383 companyId, names, descriptions, types, params, andOperator, start,
384 end, obc);
385 }
386
387 @Override
388 public List<Role> findBySystem(long companyId) {
389 Session session = null;
390
391 try {
392 session = openSession();
393
394 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
395
396 SQLQuery q = session.createSynchronizedSQLQuery(sql);
397
398 q.addEntity("Role_", RoleImpl.class);
399
400 QueryPos qPos = QueryPos.getInstance(q);
401
402 qPos.add(companyId);
403
404 return q.list(true);
405 }
406 catch (Exception e) {
407 throw new SystemException(e);
408 }
409 finally {
410 closeSession(session);
411 }
412 }
413
414 @Override
415 public List<Role> findByUserGroupGroupRole(long userId, long groupId) {
416 return findByUserGroupGroupRole(
417 userId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
418 }
419
420 @Override
421 public List<Role> findByUserGroupGroupRole(
422 long userId, long groupId, int start, int end) {
423
424 Session session = null;
425
426 try {
427 session = openSession();
428
429 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
430
431 SQLQuery q = session.createSynchronizedSQLQuery(sql);
432
433 q.addEntity("Role_", RoleImpl.class);
434
435 QueryPos qPos = QueryPos.getInstance(q);
436
437 qPos.add(userId);
438 qPos.add(groupId);
439
440 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
441 }
442 catch (Exception e) {
443 throw new SystemException(e);
444 }
445 finally {
446 closeSession(session);
447 }
448 }
449
450 @Override
451 public List<Role> findByUserGroupRole(long userId, long groupId) {
452 Session session = null;
453
454 try {
455 session = openSession();
456
457 String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
458
459 SQLQuery q = session.createSynchronizedSQLQuery(sql);
460
461 q.addEntity("Role_", RoleImpl.class);
462
463 QueryPos qPos = QueryPos.getInstance(q);
464
465 qPos.add(userId);
466 qPos.add(groupId);
467
468 return q.list(true);
469 }
470 catch (Exception e) {
471 throw new SystemException(e);
472 }
473 finally {
474 closeSession(session);
475 }
476 }
477
478 @Override
479 public Role findByC_N(long companyId, String name)
480 throws NoSuchRoleException {
481
482 name = StringUtil.lowerCase(name);
483
484 Session session = null;
485
486 try {
487 session = openSession();
488
489 String sql = CustomSQLUtil.get(FIND_BY_C_N);
490
491 SQLQuery q = session.createSynchronizedSQLQuery(sql);
492
493 q.addEntity("Role_", RoleImpl.class);
494
495 QueryPos qPos = QueryPos.getInstance(q);
496
497 qPos.add(companyId);
498 qPos.add(name);
499
500 List<Role> roles = q.list();
501
502 if (!roles.isEmpty()) {
503 return roles.get(0);
504 }
505 }
506 catch (Exception e) {
507 throw new SystemException(e);
508 }
509 finally {
510 closeSession(session);
511 }
512
513 StringBundler sb = new StringBundler(5);
514
515 sb.append("No Role exists with the key {companyId=");
516 sb.append(companyId);
517 sb.append(", name=");
518 sb.append(name);
519 sb.append("}");
520
521 throw new NoSuchRoleException(sb.toString());
522 }
523
524 @Override
525 public List<Role> findByU_G(long userId, List<Group> groups) {
526 long[] groupIds = new long[groups.size()];
527
528 for (int i = 0; i < groups.size(); i++) {
529 Group group = groups.get(i);
530
531 groupIds[i] = group.getGroupId();
532 }
533
534 return findByU_G(userId, groupIds);
535 }
536
537 @Override
538 public List<Role> findByU_G(long userId, long groupId) {
539 return findByU_G(userId, new long[] {groupId});
540 }
541
542 @Override
543 public List<Role> findByU_G(long userId, long[] groupIds) {
544 Session session = null;
545
546 try {
547 session = openSession();
548
549 String sql = CustomSQLUtil.get(FIND_BY_U_G);
550
551 sql = StringUtil.replace(
552 sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
553
554 SQLQuery q = session.createSynchronizedSQLQuery(sql);
555
556 q.addEntity("Role_", RoleImpl.class);
557
558 QueryPos qPos = QueryPos.getInstance(q);
559
560 qPos.add(userId);
561 qPos.add(groupIds);
562
563 return q.list(true);
564 }
565 catch (Exception e) {
566 throw new SystemException(e);
567 }
568 finally {
569 closeSession(session);
570 }
571 }
572
573 @Override
574 public List<Role> findByR_N_A(
575 long resourceBlockId, String className, String actionId) {
576
577 Session session = null;
578
579 try {
580 session = openSession();
581
582 String sql = CustomSQLUtil.get(FIND_BY_R_N_A);
583
584 SQLQuery q = session.createSynchronizedSQLQuery(sql);
585
586 q.addEntity("Role_", RoleImpl.class);
587
588 QueryPos qPos = QueryPos.getInstance(q);
589
590 qPos.add(resourceBlockId);
591 qPos.add(className);
592
593 ResourceAction resourceAction =
594 ResourceActionLocalServiceUtil.getResourceAction(
595 className, actionId);
596
597 qPos.add(resourceAction.getBitwiseValue());
598
599 return q.list(true);
600 }
601 catch (Exception e) {
602 throw new SystemException(e);
603 }
604 finally {
605 closeSession(session);
606 }
607 }
608
609 @Override
610 public List<Role> findByC_N_D_T(
611 long companyId, String name, String description, Integer[] types,
612 LinkedHashMap<String, Object> params, boolean andOperator, int start,
613 int end, OrderByComparator<Role> obc) {
614
615 String[] names = CustomSQLUtil.keywords(name);
616 String[] descriptions = CustomSQLUtil.keywords(description);
617
618 return findByC_N_D_T(
619 companyId, names, descriptions, types, params, andOperator, start,
620 end, obc);
621 }
622
623 @Override
624 public List<Role> findByC_N_D_T(
625 long companyId, String[] names, String[] descriptions, Integer[] types,
626 LinkedHashMap<String, Object> params, boolean andOperator, int start,
627 int end, OrderByComparator<Role> obc) {
628
629 return doFindByC_N_D_T(
630 companyId, names, descriptions, types, params, andOperator, start,
631 end, obc, false);
632 }
633
634 @Override
635 public Map<String, List<String>> findByC_N_S_P(
636 long companyId, String name, int scope, String primKey) {
637
638 Session session = null;
639
640 try {
641 session = openSession();
642
643 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
644
645 SQLQuery q = session.createSynchronizedSQLQuery(sql);
646
647 q.addScalar("roleName", Type.STRING);
648 q.addScalar("actionId", Type.STRING);
649
650 QueryPos qPos = QueryPos.getInstance(q);
651
652 qPos.add(companyId);
653 qPos.add(name);
654 qPos.add(scope);
655 qPos.add(primKey);
656
657 Map<String, List<String>> roleMap = new HashMap<>();
658
659 Iterator<Object[]> itr = q.iterate();
660
661 while (itr.hasNext()) {
662 Object[] array = itr.next();
663
664 String roleName = (String)array[0];
665 String actionId = (String)array[1];
666
667 List<String> roleList = roleMap.get(roleName);
668
669 if (roleList == null) {
670 roleList = new ArrayList<>();
671 }
672
673 roleList.add(actionId);
674
675 roleMap.put(roleName, roleList);
676 }
677
678 return roleMap;
679 }
680 catch (Exception e) {
681 throw new SystemException(e);
682 }
683 finally {
684 closeSession(session);
685 }
686 }
687
688 @Override
689 public List<Role> findByC_N_S_P_A(
690 long companyId, String name, int scope, String primKey,
691 String actionId) {
692
693 Session session = null;
694
695 try {
696 session = openSession();
697
698 String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
699
700 SQLQuery q = session.createSynchronizedSQLQuery(sql);
701
702 q.addEntity("Role_", RoleImpl.class);
703
704 QueryPos qPos = QueryPos.getInstance(q);
705
706 qPos.add(companyId);
707 qPos.add(name);
708 qPos.add(scope);
709 qPos.add(primKey);
710
711 ResourceAction resourceAction =
712 ResourceActionLocalServiceUtil.getResourceAction(
713 name, actionId);
714
715 qPos.add(resourceAction.getBitwiseValue());
716
717 return q.list(true);
718 }
719 catch (Exception e) {
720 throw new SystemException(e);
721 }
722 finally {
723 closeSession(session);
724 }
725 }
726
727 protected int doCountByC_N_D_T(
728 long companyId, String[] names, String[] descriptions, Integer[] types,
729 LinkedHashMap<String, Object> params, boolean andOperator,
730 boolean inlineSQLHelper) {
731
732 names = CustomSQLUtil.keywords(names, true);
733 descriptions = CustomSQLUtil.keywords(descriptions, true);
734
735 if (types == null) {
736 types = new Integer[0];
737 }
738
739 Session session = null;
740
741 try {
742 session = openSession();
743
744 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
745
746 sql = CustomSQLUtil.replaceKeywords(
747 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
748 sql = CustomSQLUtil.replaceKeywords(
749 sql, "lower(Role_.description)", StringPool.LIKE, true,
750 descriptions);
751 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
752 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
753 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
754 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
755
756 if (inlineSQLHelper) {
757 sql = InlineSQLHelperUtil.replacePermissionCheck(
758 sql, Role.class.getName(), "Role_.roleId", null, null,
759 new long[] {0}, null);
760 }
761
762 SQLQuery q = session.createSynchronizedSQLQuery(sql);
763
764 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
765
766 QueryPos qPos = QueryPos.getInstance(q);
767
768 setJoin(qPos, params);
769
770 qPos.add(companyId);
771 qPos.add(names, 2);
772 qPos.add(descriptions, 2);
773 qPos.add(types);
774
775 Iterator<Long> itr = q.iterate();
776
777 if (itr.hasNext()) {
778 Long count = itr.next();
779
780 if (count != null) {
781 return count.intValue();
782 }
783 }
784
785 return 0;
786 }
787 catch (Exception e) {
788 throw new SystemException(e);
789 }
790 finally {
791 closeSession(session);
792 }
793 }
794
795 protected List<Role> doFindByC_N_D_T(
796 long companyId, String[] names, String[] descriptions, Integer[] types,
797 LinkedHashMap<String, Object> params, boolean andOperator, int start,
798 int end, OrderByComparator<Role> obc, boolean inlineSQLHelper) {
799
800 names = CustomSQLUtil.keywords(names, true);
801 descriptions = CustomSQLUtil.keywords(descriptions, true);
802
803 if (types == null) {
804 types = new Integer[0];
805 }
806
807 Session session = null;
808
809 try {
810 session = openSession();
811
812 String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
813
814 sql = CustomSQLUtil.replaceKeywords(
815 sql, "lower(Role_.name)", StringPool.LIKE, false, names);
816 sql = CustomSQLUtil.replaceKeywords(
817 sql, "lower(Role_.description)", StringPool.LIKE, true,
818 descriptions);
819 sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
820 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
821 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
822 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
823 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
824
825 if (inlineSQLHelper) {
826 sql = InlineSQLHelperUtil.replacePermissionCheck(
827 sql, Role.class.getName(), "Role_.roleId", null, null,
828 new long[] {0}, null);
829 }
830
831 SQLQuery q = session.createSynchronizedSQLQuery(sql);
832
833 q.addEntity("Role_", RoleImpl.class);
834
835 QueryPos qPos = QueryPos.getInstance(q);
836
837 setJoin(qPos, params);
838
839 qPos.add(companyId);
840 qPos.add(names, 2);
841 qPos.add(descriptions, 2);
842 qPos.add(types);
843
844 return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
845 }
846 catch (Exception e) {
847 throw new SystemException(e);
848 }
849 finally {
850 closeSession(session);
851 }
852 }
853
854 protected String getCountByR_U_SQL() {
855 if (_countByR_U == null) {
856 StringBundler sb = new StringBundler(13);
857
858 sb.append(StringPool.OPEN_PARENTHESIS);
859 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
860 sb.append(") UNION (");
861 sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
862 sb.append(") UNION (");
863 sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
864 sb.append(") UNION (");
865 sb.append(CustomSQLUtil.get(COUNT_BY_USER));
866 sb.append(") UNION (");
867 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
868 sb.append(") UNION (");
869 sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
870 sb.append(StringPool.CLOSE_PARENTHESIS);
871
872 _countByR_U = sb.toString();
873 }
874
875 return _countByR_U;
876 }
877
878 protected String getGroupIds(long[] groupIds, String table) {
879 if (groupIds.length == 0) {
880 return StringPool.BLANK;
881 }
882
883 StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
884
885 for (int i = 0; i < groupIds.length; i++) {
886 sb.append(table);
887 sb.append(".groupId = ?");
888
889 if ((i + 1) < groupIds.length) {
890 sb.append(" OR ");
891 }
892 }
893
894 return sb.toString();
895 }
896
897 protected String getJoin(LinkedHashMap<String, Object> params) {
898 if ((params == null) || params.isEmpty()) {
899 return StringPool.BLANK;
900 }
901
902 StringBundler sb = new StringBundler(params.size());
903
904 for (Map.Entry<String, Object> entry : params.entrySet()) {
905 String key = entry.getKey();
906 Object value = entry.getValue();
907
908 if (Validator.isNotNull(value)) {
909 sb.append(getJoin(key));
910 }
911 }
912
913 return sb.toString();
914 }
915
916 protected String getJoin(String key) {
917 String join = StringPool.BLANK;
918
919 if (key.equals("usersRoles")) {
920 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
921 }
922
923 if (Validator.isNotNull(join)) {
924 int pos = join.indexOf("WHERE");
925
926 if (pos != -1) {
927 join = join.substring(0, pos);
928 }
929 }
930
931 return join;
932 }
933
934 protected String getTypes(Integer[] types) {
935 if (types.length == 0) {
936 return StringPool.BLANK;
937 }
938
939 StringBundler sb = new StringBundler(types.length * 2);
940
941 sb.append(" AND (");
942
943 for (int i = 0; i < types.length; i++) {
944 sb.append("Role_.type_ = ?");
945
946 if ((i + 1) < types.length) {
947 sb.append(" OR ");
948 }
949 }
950
951 sb.append(StringPool.CLOSE_PARENTHESIS);
952
953 return sb.toString();
954 }
955
956 protected String getWhere(LinkedHashMap<String, Object> params) {
957 if ((params == null) || params.isEmpty()) {
958 return StringPool.BLANK;
959 }
960
961 StringBundler sb = new StringBundler(params.size());
962
963 for (Map.Entry<String, Object> entry : params.entrySet()) {
964 String key = entry.getKey();
965 Object value = entry.getValue();
966
967 if (Validator.isNotNull(value)) {
968 sb.append(getWhere(key));
969 }
970 }
971
972 return sb.toString();
973 }
974
975 protected String getWhere(String key) {
976 String join = StringPool.BLANK;
977
978 if (key.equals("usersRoles")) {
979 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
980 }
981
982 if (Validator.isNotNull(join)) {
983 int pos = join.indexOf("WHERE");
984
985 if (pos != -1) {
986 join = join.substring(pos + 5, join.length()).concat(" AND ");
987 }
988 else {
989 join = StringPool.BLANK;
990 }
991 }
992
993 return join;
994 }
995
996 protected void setJoin(
997 QueryPos qPos, LinkedHashMap<String, Object> params) {
998
999 if (params == null) {
1000 return;
1001 }
1002
1003 for (Map.Entry<String, Object> entry : params.entrySet()) {
1004 Object value = entry.getValue();
1005
1006 if (value instanceof Long) {
1007 Long valueLong = (Long)value;
1008
1009 if (Validator.isNotNull(valueLong)) {
1010 qPos.add(valueLong);
1011 }
1012 }
1013 else if (value instanceof String) {
1014 String valueString = (String)value;
1015
1016 if (Validator.isNotNull(valueString)) {
1017 qPos.add(valueString);
1018 }
1019 }
1020 }
1021 }
1022
1023 private String _countByR_U;
1024
1025 }