001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import com.liferay.portal.kernel.dao.orm.QueryPos;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.dao.orm.SQLQuery;
020 import com.liferay.portal.kernel.dao.orm.Session;
021 import com.liferay.portal.kernel.dao.orm.Type;
022 import com.liferay.portal.kernel.exception.NoSuchGroupException;
023 import com.liferay.portal.kernel.exception.SystemException;
024 import com.liferay.portal.kernel.model.Group;
025 import com.liferay.portal.kernel.model.GroupConstants;
026 import com.liferay.portal.kernel.model.Organization;
027 import com.liferay.portal.kernel.model.ResourceAction;
028 import com.liferay.portal.kernel.security.permission.RolePermissions;
029 import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil;
030 import com.liferay.portal.kernel.service.ResourceActionLocalServiceUtil;
031 import com.liferay.portal.kernel.service.ResourceBlockLocalServiceUtil;
032 import com.liferay.portal.kernel.service.persistence.GroupFinder;
033 import com.liferay.portal.kernel.service.persistence.GroupUtil;
034 import com.liferay.portal.kernel.util.GetterUtil;
035 import com.liferay.portal.kernel.util.OrderByComparator;
036 import com.liferay.portal.kernel.util.StringBundler;
037 import com.liferay.portal.kernel.util.StringPool;
038 import com.liferay.portal.kernel.util.StringUtil;
039 import com.liferay.portal.kernel.util.Validator;
040 import com.liferay.portal.kernel.util.comparator.GroupNameComparator;
041 import com.liferay.portal.model.impl.GroupImpl;
042 import com.liferay.portal.service.impl.GroupLocalServiceImpl;
043 import com.liferay.util.dao.orm.CustomSQLUtil;
044
045 import java.util.ArrayList;
046 import java.util.HashMap;
047 import java.util.HashSet;
048 import java.util.Iterator;
049 import java.util.LinkedHashMap;
050 import java.util.List;
051 import java.util.Map;
052 import java.util.Set;
053 import java.util.concurrent.ConcurrentHashMap;
054
055
059 public class GroupFinderImpl
060 extends GroupFinderBaseImpl implements GroupFinder {
061
062 public static final String COUNT_BY_LAYOUTS =
063 GroupFinder.class.getName() + ".countByLayouts";
064
065 public static final String COUNT_BY_GROUP_ID =
066 GroupFinder.class.getName() + ".countByGroupId";
067
068 public static final String COUNT_BY_C_C_PG_N_D =
069 GroupFinder.class.getName() + ".countByC_C_PG_N_D";
070
071 public static final String FIND_BY_LAYOUTS =
072 GroupFinder.class.getName() + ".findByLayouts";
073
074 public static final String FIND_BY_LIVE_GROUPS =
075 GroupFinder.class.getName() + ".findByLiveGroups";
076
077 public static final String FIND_BY_NO_LAYOUTS =
078 GroupFinder.class.getName() + ".findByNoLayouts";
079
080 public static final String FIND_BY_NULL_FRIENDLY_URL =
081 GroupFinder.class.getName() + ".findByNullFriendlyURL";
082
083 public static final String FIND_BY_SYSTEM =
084 GroupFinder.class.getName() + ".findBySystem";
085
086 public static final String FIND_BY_C_C =
087 GroupFinder.class.getName() + ".findByC_C";
088
089 public static final String FIND_BY_C_P =
090 GroupFinder.class.getName() + ".findByC_P";
091
092 public static final String FIND_BY_C_GK =
093 GroupFinder.class.getName() + ".findByC_GK";
094
095 public static final String FIND_BY_L_TS_S_RSGC =
096 GroupFinder.class.getName() + ".findByL_TS_S_RSGC";
097
098 public static final String FIND_BY_C_C_PG_N_D =
099 GroupFinder.class.getName() + ".findByC_C_PG_N_D";
100
101 public static final String JOIN_BY_ACTIVE =
102 GroupFinder.class.getName() + ".joinByActive";
103
104 public static final String JOIN_BY_CREATOR_USER_ID =
105 GroupFinder.class.getName() + ".joinByCreatorUserId";
106
107 public static final String JOIN_BY_GROUP_ORG =
108 GroupFinder.class.getName() + ".joinByGroupOrg";
109
110 public static final String JOIN_BY_GROUPS_ORGS =
111 GroupFinder.class.getName() + ".joinByGroupsOrgs";
112
113 public static final String JOIN_BY_GROUPS_ROLES =
114 GroupFinder.class.getName() + ".joinByGroupsRoles";
115
116 public static final String JOIN_BY_GROUPS_USER_GROUPS =
117 GroupFinder.class.getName() + ".joinByGroupsUserGroups";
118
119 public static final String JOIN_BY_LAYOUT_SET =
120 GroupFinder.class.getName() + ".joinByLayoutSet";
121
122 public static final String JOIN_BY_MANUAL_MEMBERSHIP =
123 GroupFinder.class.getName() + ".joinByManualMembership";
124
125 public static final String JOIN_BY_MEMBERSHIP_RESTRICTION =
126 GroupFinder.class.getName() + ".joinByMembershipRestriction";
127
128 public static final String JOIN_BY_PAGE_COUNT =
129 GroupFinder.class.getName() + ".joinByPageCount";
130
131 public static final String JOIN_BY_ROLE_RESOURCE_PERMISSIONS =
132 GroupFinder.class.getName() + ".joinByRoleResourcePermissions";
133
134 public static final String JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS =
135 GroupFinder.class.getName() + ".joinByRoleResourceTypePermissions";
136
137 public static final String JOIN_BY_SITE =
138 GroupFinder.class.getName() + ".joinBySite";
139
140 public static final String JOIN_BY_TYPE =
141 GroupFinder.class.getName() + ".joinByType";
142
143 public static final String JOIN_BY_USER_GROUP_ROLE =
144 GroupFinder.class.getName() + ".joinByUserGroupRole";
145
146 public static final String JOIN_BY_USERS_GROUPS =
147 GroupFinder.class.getName() + ".joinByUsersGroups";
148
149 @Override
150 public int countByLayouts(
151 long companyId, long parentGroupId, boolean site) {
152
153 Session session = null;
154
155 try {
156 session = openSession();
157
158 String sql = CustomSQLUtil.get(COUNT_BY_LAYOUTS);
159
160 SQLQuery q = session.createSynchronizedSQLQuery(sql);
161
162 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
163
164 QueryPos qPos = QueryPos.getInstance(q);
165
166 qPos.add(companyId);
167 qPos.add(parentGroupId);
168 qPos.add(site);
169
170 Iterator<Long> itr = q.iterate();
171
172 if (itr.hasNext()) {
173 Long count = itr.next();
174
175 if (count != null) {
176 return count.intValue();
177 }
178 }
179
180 return 0;
181 }
182 catch (Exception e) {
183 throw new SystemException(e);
184 }
185 finally {
186 closeSession(session);
187 }
188 }
189
190 @Override
191 public int countByG_U(long groupId, long userId, boolean inherit) {
192 LinkedHashMap<String, Object> params1 = new LinkedHashMap<>();
193
194 params1.put("usersGroups", userId);
195
196 LinkedHashMap<String, Object> params2 = new LinkedHashMap<>();
197
198 params2.put("groupOrg", userId);
199
200 LinkedHashMap<String, Object> params3 = new LinkedHashMap<>();
201
202 params3.put("groupsOrgs", userId);
203
204 LinkedHashMap<String, Object> params4 = new LinkedHashMap<>();
205
206 params4.put("groupsUserGroups", userId);
207
208 Session session = null;
209
210 try {
211 session = openSession();
212
213 int count = countByGroupId(session, groupId, params1);
214
215 if (inherit) {
216 count += countByGroupId(session, groupId, params2);
217 count += countByGroupId(session, groupId, params3);
218 count += countByGroupId(session, groupId, params4);
219 }
220
221 return count;
222 }
223 catch (Exception e) {
224 throw new SystemException(e);
225 }
226 finally {
227 closeSession(session);
228 }
229 }
230
231 @Override
232 public int countByC_C_PG_N_D(
233 long companyId, long[] classNameIds, long parentGroupId, String[] names,
234 String[] descriptions, LinkedHashMap<String, Object> params,
235 boolean andOperator) {
236
237 String parentGroupIdComparator = StringPool.EQUAL;
238
239 if (parentGroupId == GroupConstants.ANY_PARENT_GROUP_ID) {
240 parentGroupIdComparator = StringPool.NOT_EQUAL;
241 }
242
243 names = CustomSQLUtil.keywords(names);
244 descriptions = CustomSQLUtil.keywords(descriptions);
245
246 if (params == null) {
247 params = _emptyLinkedHashMap;
248 }
249
250 LinkedHashMap<String, Object> params1 = params;
251
252 LinkedHashMap<String, Object> params2 = null;
253
254 LinkedHashMap<String, Object> params3 = null;
255
256 LinkedHashMap<String, Object> params4 = null;
257
258 Long userId = (Long)params.get("usersGroups");
259
260 boolean doUnion = Validator.isNotNull(userId);
261
262 if (doUnion) {
263 params2 = new LinkedHashMap<>(params1);
264
265 params2.remove("usersGroups");
266 params2.put("groupOrg", userId);
267
268 params3 = new LinkedHashMap<>(params1);
269
270 params3.remove("usersGroups");
271 params3.put("groupsOrgs", userId);
272
273 params4 = new LinkedHashMap<>(params1);
274
275 params4.remove("usersGroups");
276 params4.put("groupsUserGroups", userId);
277 }
278
279 Session session = null;
280
281 try {
282 session = openSession();
283
284 Set<Long> groupIds = new HashSet<>();
285
286 groupIds.addAll(
287 countByC_C_PG_N_D(
288 session, companyId, classNameIds, parentGroupId,
289 parentGroupIdComparator, names, descriptions, params1,
290 andOperator));
291
292 if (doUnion) {
293 groupIds.addAll(
294 countByC_C_PG_N_D(
295 session, companyId, classNameIds, parentGroupId,
296 parentGroupIdComparator, names, descriptions, params2,
297 andOperator));
298
299 groupIds.addAll(
300 countByC_C_PG_N_D(
301 session, companyId, classNameIds, parentGroupId,
302 parentGroupIdComparator, names, descriptions, params3,
303 andOperator));
304
305 groupIds.addAll(
306 countByC_C_PG_N_D(
307 session, companyId, classNameIds, parentGroupId,
308 parentGroupIdComparator, names, descriptions, params4,
309 andOperator));
310 }
311
312 return groupIds.size();
313 }
314 catch (Exception e) {
315 throw new SystemException(e);
316 }
317 finally {
318 closeSession(session);
319 }
320 }
321
322 @Deprecated
323 @Override
324 public List<Group> findByLayouts(
325 long companyId, long parentGroupId, boolean site, int start, int end) {
326
327 return findByLayouts(companyId, parentGroupId, site, start, end, null);
328 }
329
330 @Override
331 public List<Group> findByLayouts(
332 long companyId, long parentGroupId, boolean site, int start, int end,
333 OrderByComparator<Group> obc) {
334
335 Session session = null;
336
337 try {
338 session = openSession();
339
340 String sql = CustomSQLUtil.get(FIND_BY_LAYOUTS);
341
342 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
343
344 SQLQuery q = session.createSynchronizedSQLQuery(sql);
345
346 q.addEntity("Group_", GroupImpl.class);
347
348 QueryPos qPos = QueryPos.getInstance(q);
349
350 qPos.add(companyId);
351 qPos.add(parentGroupId);
352 qPos.add(site);
353
354 return q.list(true);
355 }
356 catch (Exception e) {
357 throw new SystemException(e);
358 }
359 finally {
360 closeSession(session);
361 }
362 }
363
364 @Override
365 public List<Group> findByLiveGroups() {
366 Session session = null;
367
368 try {
369 session = openSession();
370
371 String sql = CustomSQLUtil.get(FIND_BY_LIVE_GROUPS);
372
373 SQLQuery q = session.createSynchronizedSQLQuery(sql);
374
375 q.addEntity("Group_", GroupImpl.class);
376
377 return q.list(true);
378 }
379 catch (Exception e) {
380 throw new SystemException(e);
381 }
382 finally {
383 closeSession(session);
384 }
385 }
386
387 @Override
388 public List<Group> findByNoLayouts(
389 long classNameId, boolean privateLayout, int start, int end) {
390
391 Session session = null;
392
393 try {
394 session = openSession();
395
396 String sql = CustomSQLUtil.get(FIND_BY_NO_LAYOUTS);
397
398 SQLQuery q = session.createSynchronizedSQLQuery(sql);
399
400 q.addEntity("Group_", GroupImpl.class);
401
402 QueryPos qPos = QueryPos.getInstance(q);
403
404 qPos.add(classNameId);
405 qPos.add(privateLayout);
406
407 return q.list(true);
408 }
409 catch (Exception e) {
410 throw new SystemException(e);
411 }
412 finally {
413 closeSession(session);
414 }
415 }
416
417 @Override
418 public List<Group> findByNullFriendlyURL() {
419 Session session = null;
420
421 try {
422 session = openSession();
423
424 String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
425
426 SQLQuery q = session.createSynchronizedSQLQuery(sql);
427
428 q.addEntity("Group_", GroupImpl.class);
429
430 return q.list(true);
431 }
432 catch (Exception e) {
433 throw new SystemException(e);
434 }
435 finally {
436 closeSession(session);
437 }
438 }
439
440 @Override
441 public List<Group> findBySystem(long companyId) {
442 Session session = null;
443
444 try {
445 session = openSession();
446
447 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
448
449 SQLQuery q = session.createSynchronizedSQLQuery(sql);
450
451 q.addEntity("Group_", GroupImpl.class);
452
453 QueryPos qPos = QueryPos.getInstance(q);
454
455 qPos.add(companyId);
456
457 return q.list(true);
458 }
459 catch (Exception e) {
460 throw new SystemException(e);
461 }
462 finally {
463 closeSession(session);
464 }
465 }
466
467 @Override
468 public List<Group> findByCompanyId(
469 long companyId, LinkedHashMap<String, Object> params, int start,
470 int end, OrderByComparator<Group> obc) {
471
472 if (params == null) {
473 params = _emptyLinkedHashMap;
474 }
475
476 LinkedHashMap<String, Object> params1 = params;
477
478 LinkedHashMap<String, Object> params2 = null;
479
480 LinkedHashMap<String, Object> params3 = null;
481
482 LinkedHashMap<String, Object> params4 = null;
483
484 Long userId = (Long)params.get("usersGroups");
485 boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
486
487 boolean doUnion = Validator.isNotNull(userId) && inherit;
488
489 if (doUnion) {
490 params2 = new LinkedHashMap<>(params1);
491
492 params2.remove("usersGroups");
493 params2.put("groupOrg", userId);
494
495 params3 = new LinkedHashMap<>(params1);
496
497 params3.remove("usersGroups");
498 params3.put("groupsOrgs", userId);
499
500 params4 = new LinkedHashMap<>(params1);
501
502 params4.remove("usersGroups");
503 params4.put("groupsUserGroups", userId);
504 }
505
506 String sql = null;
507
508 String sqlKey = _buildSQLKey(
509 params1, params2, params3, params4, obc, doUnion);
510
511 sql = _findByCompanyIdSQLCache.get(sqlKey);
512
513 if (sql == null) {
514 String findByC_C_SQL = CustomSQLUtil.get(FIND_BY_C_C);
515
516 if (params.get("active") == Boolean.TRUE) {
517 findByC_C_SQL = StringUtil.replace(
518 findByC_C_SQL, "(Group_.liveGroupId = 0) AND",
519 StringPool.BLANK);
520 }
521
522 findByC_C_SQL = StringUtil.replace(
523 findByC_C_SQL, "Group_.classNameId = ?",
524 "Group_.classNameId = ".concat(
525 StringUtil.merge(
526 _getGroupOrganizationClassNameIds(),
527 " OR Group_.classNameId = ")));
528 findByC_C_SQL = replaceOrderBy(findByC_C_SQL, obc);
529
530 StringBundler sb = new StringBundler(12);
531
532 sb.append(StringPool.OPEN_PARENTHESIS);
533 sb.append(replaceJoinAndWhere(findByC_C_SQL, params1));
534 sb.append(StringPool.CLOSE_PARENTHESIS);
535
536 if (doUnion) {
537 sb.append(" UNION (");
538 sb.append(replaceJoinAndWhere(findByC_C_SQL, params2));
539 sb.append(") UNION (");
540 sb.append(replaceJoinAndWhere(findByC_C_SQL, params3));
541 sb.append(") UNION (");
542 sb.append(replaceJoinAndWhere(findByC_C_SQL, params4));
543 sb.append(StringPool.CLOSE_PARENTHESIS);
544 }
545
546 if (obc != null) {
547 sb.append(" ORDER BY ");
548 sb.append(obc.toString());
549 }
550
551 sql = sb.toString();
552
553 _findByCompanyIdSQLCache.put(sqlKey, sql);
554 }
555
556 Session session = null;
557
558 try {
559 session = openSession();
560
561 SQLQuery q = session.createSynchronizedSQLQuery(sql);
562
563 q.addScalar("groupId", Type.LONG);
564
565 QueryPos qPos = QueryPos.getInstance(q);
566
567 setJoin(qPos, params1);
568
569 qPos.add(companyId);
570
571 if (doUnion) {
572 setJoin(qPos, params2);
573
574 qPos.add(companyId);
575
576 setJoin(qPos, params3);
577
578 qPos.add(companyId);
579
580 setJoin(qPos, params4);
581
582 qPos.add(companyId);
583 }
584
585 List<Long> groupIds = (List<Long>)QueryUtil.list(
586 q, getDialect(), start, end);
587
588 List<Group> groups = new ArrayList<>(groupIds.size());
589
590 for (Long groupId : groupIds) {
591 Group group = GroupUtil.findByPrimaryKey(groupId);
592
593 groups.add(group);
594 }
595
596 return groups;
597 }
598 catch (Exception e) {
599 throw new SystemException(e);
600 }
601 finally {
602 closeSession(session);
603 }
604 }
605
606 @Override
607 public List<Long> findByC_P(
608 long companyId, long parentGroupId, long previousGroupId, int size) {
609
610 Session session = null;
611
612 try {
613 session = openSession();
614
615 String sql = CustomSQLUtil.get(FIND_BY_C_P);
616
617 if (previousGroupId <= 0) {
618 sql = StringUtil.replace(
619 sql, "(groupId > ?) AND", StringPool.BLANK);
620 }
621
622 SQLQuery q = session.createSynchronizedSQLQuery(sql);
623
624 q.addScalar("groupId", Type.LONG);
625
626 QueryPos qPos = QueryPos.getInstance(q);
627
628 if (previousGroupId > 0) {
629 qPos.add(previousGroupId);
630 }
631
632 qPos.add(companyId);
633 qPos.add(parentGroupId);
634
635 return (List<Long>)QueryUtil.list(q, getDialect(), 0, size);
636 }
637 catch (Exception e) {
638 throw new SystemException(e);
639 }
640 finally {
641 closeSession(session);
642 }
643 }
644
645 @Override
646 public Group findByC_GK(long companyId, String groupKey)
647 throws NoSuchGroupException {
648
649 groupKey = StringUtil.lowerCase(groupKey);
650
651 Session session = null;
652
653 try {
654 session = openSession();
655
656 String sql = CustomSQLUtil.get(FIND_BY_C_GK);
657
658 SQLQuery q = session.createSynchronizedSQLQuery(sql);
659
660 q.addEntity("Group_", GroupImpl.class);
661
662 QueryPos qPos = QueryPos.getInstance(q);
663
664 qPos.add(companyId);
665 qPos.add(groupKey);
666
667 List<Group> groups = q.list();
668
669 if (!groups.isEmpty()) {
670 return groups.get(0);
671 }
672 }
673 catch (Exception e) {
674 throw new SystemException(e);
675 }
676 finally {
677 closeSession(session);
678 }
679
680 StringBundler sb = new StringBundler(5);
681
682 sb.append("No Group exists with the key {companyId=");
683 sb.append(companyId);
684 sb.append(", groupKey=");
685 sb.append(groupKey);
686 sb.append("}");
687
688 throw new NoSuchGroupException(sb.toString());
689 }
690
691 @Override
692 public List<Group> findByL_TS_S_RSGC(
693 long liveGroupId, String typeSettings, boolean site,
694 int remoteStagingGroupCount) {
695
696 String sql = CustomSQLUtil.get(FIND_BY_L_TS_S_RSGC);
697
698 Session session = null;
699
700 try {
701 session = openSession();
702
703 SQLQuery q = session.createSynchronizedSQLQuery(sql);
704
705 q.addEntity("Group_", GroupImpl.class);
706
707 QueryPos qPos = QueryPos.getInstance(q);
708
709 qPos.add(liveGroupId);
710 qPos.add(StringUtil.quote(typeSettings, StringPool.PERCENT));
711 qPos.add(site);
712 qPos.add(remoteStagingGroupCount);
713
714 List<Group> groups = (List<Group>)QueryUtil.list(
715 q, getDialect(), QueryUtil.ALL_POS, QueryUtil.ALL_POS);
716
717 return groups;
718 }
719 catch (Exception e) {
720 throw new SystemException(e);
721 }
722 finally {
723 closeSession(session);
724 }
725 }
726
727 @Override
728 public List<Group> findByC_C_PG_N_D(
729 long companyId, long[] classNameIds, long parentGroupId, String[] names,
730 String[] descriptions, LinkedHashMap<String, Object> params,
731 boolean andOperator, int start, int end, OrderByComparator<Group> obc) {
732
733 String parentGroupIdComparator = StringPool.EQUAL;
734
735 if (parentGroupId == GroupConstants.ANY_PARENT_GROUP_ID) {
736 parentGroupIdComparator = StringPool.NOT_EQUAL;
737 }
738
739 names = CustomSQLUtil.keywords(names);
740 descriptions = CustomSQLUtil.keywords(descriptions);
741
742 if (params == null) {
743 params = _emptyLinkedHashMap;
744 }
745
746 LinkedHashMap<String, Object> params1 = params;
747
748 LinkedHashMap<String, Object> params2 = null;
749
750 LinkedHashMap<String, Object> params3 = null;
751
752 LinkedHashMap<String, Object> params4 = null;
753
754 Long userId = (Long)params.get("usersGroups");
755 boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
756
757 boolean doUnion = Validator.isNotNull(userId) && inherit;
758
759 if (doUnion) {
760 params2 = new LinkedHashMap<>(params1);
761
762 params2.remove("usersGroups");
763 params2.put("groupOrg", userId);
764
765 params3 = new LinkedHashMap<>(params1);
766
767 params3.remove("usersGroups");
768 params3.put("groupsOrgs", userId);
769
770 params4 = new LinkedHashMap<>(params1);
771
772 params4.remove("usersGroups");
773 params4.put("groupsUserGroups", userId);
774 }
775
776 if (obc == null) {
777 obc = new GroupNameComparator(true);
778 }
779
780 String sql = null;
781
782 if (classNameIds == _getGroupOrganizationClassNameIds()) {
783 String sqlKey = _buildSQLKey(
784 params1, params2, params3, params4, obc, doUnion);
785
786 sql = _findByC_C_PG_N_DSQLCache.get(sqlKey);
787 }
788
789 if (sql == null) {
790 String findByC_PG_N_D_SQL = CustomSQLUtil.get(FIND_BY_C_C_PG_N_D);
791
792 if (classNameIds == null) {
793 findByC_PG_N_D_SQL = StringUtil.replace(
794 findByC_PG_N_D_SQL, "AND (Group_.classNameId = ?)",
795 StringPool.BLANK);
796 }
797 else {
798 findByC_PG_N_D_SQL = StringUtil.replace(
799 findByC_PG_N_D_SQL, "Group_.classNameId = ?",
800 "Group_.classNameId = ".concat(
801 StringUtil.merge(
802 classNameIds, " OR Group_.classNameId = ")));
803 }
804
805 findByC_PG_N_D_SQL = replaceOrderBy(findByC_PG_N_D_SQL, obc);
806
807 StringBundler sb = new StringBundler(12);
808
809 sb.append(StringPool.OPEN_PARENTHESIS);
810 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params1));
811 sb.append(StringPool.CLOSE_PARENTHESIS);
812
813 if (doUnion) {
814 sb.append(" UNION (");
815 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params2));
816 sb.append(") UNION (");
817 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params3));
818 sb.append(") UNION (");
819 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params4));
820 sb.append(StringPool.CLOSE_PARENTHESIS);
821 }
822
823 if (obc != null) {
824 sb.append(" ORDER BY ");
825 sb.append(obc.toString());
826 }
827
828 sql = sb.toString();
829
830 if (classNameIds == _getGroupOrganizationClassNameIds()) {
831 String sqlKey = _buildSQLKey(
832 params1, params2, params3, params4, obc, doUnion);
833
834 _findByC_C_PG_N_DSQLCache.put(sqlKey, sql);
835 }
836 }
837
838 sql = StringUtil.replace(
839 sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
840 parentGroupIdComparator.equals(StringPool.EQUAL) ?
841 StringPool.EQUAL : StringPool.NOT_EQUAL);
842 sql = CustomSQLUtil.replaceKeywords(
843 sql, "lower(Group_.name)", StringPool.LIKE, false, names);
844 sql = CustomSQLUtil.replaceKeywords(
845 sql, "lower(Group_.description)", StringPool.LIKE, true,
846 descriptions);
847 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
848
849 Session session = null;
850
851 try {
852 session = openSession();
853
854 SQLQuery q = session.createSynchronizedSQLQuery(sql);
855
856 q.addScalar("groupId", Type.LONG);
857
858 QueryPos qPos = QueryPos.getInstance(q);
859
860 setJoin(qPos, params1);
861
862 qPos.add(companyId);
863 qPos.add(parentGroupId);
864 qPos.add(names, 2);
865 qPos.add(descriptions, 2);
866
867 if (doUnion) {
868 setJoin(qPos, params2);
869
870 qPos.add(companyId);
871 qPos.add(parentGroupId);
872 qPos.add(names, 2);
873 qPos.add(descriptions, 2);
874
875 setJoin(qPos, params3);
876
877 qPos.add(companyId);
878 qPos.add(parentGroupId);
879 qPos.add(names, 2);
880 qPos.add(descriptions, 2);
881
882 setJoin(qPos, params4);
883
884 qPos.add(companyId);
885 qPos.add(parentGroupId);
886 qPos.add(names, 2);
887 qPos.add(descriptions, 2);
888 }
889
890 List<Long> groupIds = (List<Long>)QueryUtil.list(
891 q, getDialect(), start, end);
892
893 List<Group> groups = new ArrayList<>(groupIds.size());
894
895 for (Long groupId : groupIds) {
896 Group group = GroupUtil.findByPrimaryKey(groupId);
897
898 groups.add(group);
899 }
900
901 return groups;
902 }
903 catch (Exception e) {
904 throw new SystemException(e);
905 }
906 finally {
907 closeSession(session);
908 }
909 }
910
911 protected int countByGroupId(
912 Session session, long groupId, LinkedHashMap<String, Object> params)
913 throws Exception {
914
915 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
916
917 sql = replaceJoinAndWhere(sql, params);
918
919 SQLQuery q = session.createSynchronizedSQLQuery(sql);
920
921 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
922
923 QueryPos qPos = QueryPos.getInstance(q);
924
925 setJoin(qPos, params);
926
927 qPos.add(groupId);
928
929 Iterator<Long> itr = q.iterate();
930
931 if (itr.hasNext()) {
932 Long count = itr.next();
933
934 if (count != null) {
935 return count.intValue();
936 }
937 }
938
939 return 0;
940 }
941
942 protected List<Long> countByC_C_PG_N_D(
943 Session session, long companyId, long[] classNameIds,
944 long parentGroupId, String parentGroupIdComparator, String[] names,
945 String[] descriptions, LinkedHashMap<String, Object> params,
946 boolean andOperator)
947 throws Exception {
948
949 String sql = CustomSQLUtil.get(COUNT_BY_C_C_PG_N_D);
950
951 if (classNameIds == null) {
952 sql = StringUtil.replace(
953 sql, "AND (Group_.classNameId = ?)", StringPool.BLANK);
954 }
955 else {
956 sql = StringUtil.replace(
957 sql, "Group_.classNameId = ?",
958 "Group_.classNameId = ".concat(
959 StringUtil.merge(
960 classNameIds, " OR Group_.classNameId = ")));
961 }
962
963 sql = StringUtil.replace(
964 sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
965 parentGroupIdComparator.equals(StringPool.EQUAL) ?
966 StringPool.EQUAL : StringPool.NOT_EQUAL);
967 sql = CustomSQLUtil.replaceKeywords(
968 sql, "lower(Group_.name)", StringPool.LIKE, false, names);
969 sql = CustomSQLUtil.replaceKeywords(
970 sql, "lower(Group_.description)", StringPool.LIKE, true,
971 descriptions);
972
973 sql = replaceJoinAndWhere(sql, params);
974 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
975
976 SQLQuery q = session.createSynchronizedSQLQuery(sql);
977
978 q.addScalar("groupId", Type.LONG);
979
980 QueryPos qPos = QueryPos.getInstance(q);
981
982 setJoin(qPos, params);
983
984 qPos.add(companyId);
985 qPos.add(parentGroupId);
986 qPos.add(names, 2);
987 qPos.add(descriptions, 2);
988
989 return q.list(true);
990 }
991
992 protected String getJoin(LinkedHashMap<String, Object> params) {
993 if ((params == null) || params.isEmpty()) {
994 return StringPool.BLANK;
995 }
996
997 StringBundler sb = new StringBundler(params.size());
998
999 for (Map.Entry<String, Object> entry : params.entrySet()) {
1000 String key = entry.getKey();
1001 Object value = entry.getValue();
1002
1003 if (Validator.isNull(value)) {
1004 continue;
1005 }
1006
1007 if (key.equals("rolePermissions")) {
1008 RolePermissions rolePermissions = (RolePermissions)value;
1009
1010 if (ResourceBlockLocalServiceUtil.isSupported(
1011 rolePermissions.getName())) {
1012
1013 key = "rolePermissions_6_block";
1014 }
1015 else {
1016 key = "rolePermissions_6";
1017 }
1018 }
1019
1020 Map<String, String> joinMap = _getJoinMap();
1021
1022 String joinValue = joinMap.get(key);
1023
1024 if (Validator.isNotNull(joinValue)) {
1025 sb.append(joinValue);
1026 }
1027 }
1028
1029 return sb.toString();
1030 }
1031
1032 protected String getWhere(LinkedHashMap<String, Object> params) {
1033 if ((params == null) || params.isEmpty()) {
1034 return StringPool.BLANK;
1035 }
1036
1037 StringBundler sb = new StringBundler(params.size());
1038
1039 for (Map.Entry<String, Object> entry : params.entrySet()) {
1040 String key = entry.getKey();
1041
1042 if (key.equals("excludedGroupIds")) {
1043 List<Long> excludedGroupIds = (List<Long>)entry.getValue();
1044
1045 if (!excludedGroupIds.isEmpty()) {
1046 sb.append(StringPool.OPEN_PARENTHESIS);
1047
1048 for (int i = 0; i < excludedGroupIds.size(); i++) {
1049 sb.append("(Group_.groupId != ?)");
1050
1051 if ((i + 1) < excludedGroupIds.size()) {
1052 sb.append(" AND ");
1053 }
1054 }
1055
1056 sb.append(") AND ");
1057 }
1058 }
1059 else if (key.equals("groupsTree")) {
1060 List<Group> groupsTree = (List<Group>)entry.getValue();
1061
1062 if (!groupsTree.isEmpty()) {
1063 sb.append(StringPool.OPEN_PARENTHESIS);
1064
1065 for (int i = 0; i < groupsTree.size(); i++) {
1066 sb.append("(Group_.treePath LIKE ?) ");
1067
1068 if ((i + 1) < groupsTree.size()) {
1069 sb.append("OR ");
1070 }
1071 }
1072
1073 sb.append(") AND ");
1074 }
1075 }
1076 else if (key.equals("types")) {
1077 List<Integer> types = (List<Integer>)entry.getValue();
1078
1079 if (!types.isEmpty()) {
1080 sb.append(StringPool.OPEN_PARENTHESIS);
1081
1082 for (int i = 0; i < types.size(); i++) {
1083 sb.append("(Group_.type_ = ?) ");
1084
1085 if ((i + 1) < types.size()) {
1086 sb.append("OR ");
1087 }
1088 }
1089
1090 sb.append(") AND ");
1091 }
1092 }
1093 else {
1094 if (key.equals("rolePermissions")) {
1095 RolePermissions rolePermissions =
1096 (RolePermissions)entry.getValue();
1097
1098 if (ResourceBlockLocalServiceUtil.isSupported(
1099 rolePermissions.getName())) {
1100
1101 key = "rolePermissions_6_block";
1102 }
1103 else {
1104 key = "rolePermissions_6";
1105 }
1106 }
1107
1108 Map<String, String> whereMap = _getWhereMap();
1109
1110 String whereValue = whereMap.get(key);
1111
1112 if (Validator.isNotNull(whereValue)) {
1113 sb.append(whereValue);
1114 }
1115 }
1116 }
1117
1118 return sb.toString();
1119 }
1120
1121 protected String replaceJoinAndWhere(
1122 String sql, LinkedHashMap<String, Object> params) {
1123
1124 if (params.isEmpty()) {
1125 return StringUtil.removeSubstrings(sql, "[$JOIN$]", "[$WHERE$]");
1126 }
1127
1128 String cacheKey = _getCacheKey(sql, params);
1129
1130 String resultSQL = _replaceJoinAndWhereSQLCache.get(cacheKey);
1131
1132 if (resultSQL == null) {
1133 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
1134
1135 resultSQL = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
1136
1137 _replaceJoinAndWhereSQLCache.put(cacheKey, resultSQL);
1138 }
1139
1140 return resultSQL;
1141 }
1142
1143 protected String replaceOrderBy(String sql, OrderByComparator<Group> obc) {
1144 if (obc instanceof GroupNameComparator) {
1145 sql = StringUtil.replace(
1146 sql, "Group_.name AS groupName",
1147 "REPLACE(Group_.name, '" +
1148 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX +
1149 "', '') AS groupName");
1150 }
1151
1152 return sql;
1153 }
1154
1155 protected void setJoin(QueryPos qPos, LinkedHashMap<String, Object> params)
1156 throws Exception {
1157
1158 if (params == null) {
1159 return;
1160 }
1161
1162 for (Map.Entry<String, Object> entry : params.entrySet()) {
1163 String key = entry.getKey();
1164
1165 if (key.equals("active") || key.equals("layoutSet") ||
1166 key.equals("manualMembership") || key.equals("site")) {
1167
1168 Boolean value = (Boolean)entry.getValue();
1169
1170 qPos.add(value);
1171 }
1172 else if (key.equals("excludedGroupIds")) {
1173 List<Long> excludedGroupIds = (List<Long>)entry.getValue();
1174
1175 if (!excludedGroupIds.isEmpty()) {
1176 for (long excludedGroupId : excludedGroupIds) {
1177 qPos.add(excludedGroupId);
1178 }
1179 }
1180 }
1181 else if (key.equals("groupsTree")) {
1182 List<Group> groupsTree = (List<Group>)entry.getValue();
1183
1184 if (!groupsTree.isEmpty()) {
1185 for (Group group : groupsTree) {
1186 StringBundler sb = new StringBundler(5);
1187
1188 sb.append(StringPool.PERCENT);
1189 sb.append(StringPool.SLASH);
1190 sb.append(group.getGroupId());
1191 sb.append(StringPool.SLASH);
1192 sb.append(StringPool.PERCENT);
1193
1194 qPos.add(sb.toString());
1195 }
1196 }
1197 }
1198 else if (key.equals("pageCount")) {
1199 }
1200 else if (key.equals("rolePermissions")) {
1201 RolePermissions rolePermissions =
1202 (RolePermissions)entry.getValue();
1203
1204 ResourceAction resourceAction =
1205 ResourceActionLocalServiceUtil.getResourceAction(
1206 rolePermissions.getName(),
1207 rolePermissions.getActionId());
1208
1209 if (ResourceBlockLocalServiceUtil.isSupported(
1210 rolePermissions.getName())) {
1211
1212
1213
1214 qPos.add(rolePermissions.getName());
1215 qPos.add(rolePermissions.getRoleId());
1216 qPos.add(resourceAction.getBitwiseValue());
1217 }
1218 else {
1219 qPos.add(rolePermissions.getName());
1220 qPos.add(rolePermissions.getScope());
1221 qPos.add(rolePermissions.getRoleId());
1222 qPos.add(resourceAction.getBitwiseValue());
1223 }
1224 }
1225 else if (key.equals("types")) {
1226 List<Integer> values = (List<Integer>)entry.getValue();
1227
1228 for (int i = 0; i < values.size(); i++) {
1229 Integer value = values.get(i);
1230
1231 qPos.add(value);
1232 }
1233 }
1234 else if (key.equals("userGroupRole")) {
1235 List<Long> values = (List<Long>)entry.getValue();
1236
1237 Long userId = values.get(0);
1238 Long roleId = values.get(1);
1239
1240 qPos.add(userId);
1241 qPos.add(roleId);
1242 }
1243 else {
1244 Object value = entry.getValue();
1245
1246 if (value instanceof Integer) {
1247 Integer valueInteger = (Integer)value;
1248
1249 if (Validator.isNotNull(valueInteger)) {
1250 qPos.add(valueInteger);
1251 }
1252 }
1253 else if (value instanceof Long) {
1254 Long valueLong = (Long)value;
1255
1256 if (Validator.isNotNull(valueLong)) {
1257 qPos.add(valueLong);
1258 }
1259 }
1260 else if (value instanceof String) {
1261 String valueString = (String)value;
1262
1263 if (Validator.isNotNull(valueString)) {
1264 qPos.add(valueString);
1265 }
1266 }
1267 }
1268 }
1269 }
1270
1271 private String _buildSQLKey(
1272 LinkedHashMap<String, Object> param1,
1273 LinkedHashMap<String, Object> param2,
1274 LinkedHashMap<String, Object> param3,
1275 LinkedHashMap<String, Object> param4, OrderByComparator<Group> obc,
1276 boolean doUnion) {
1277
1278 StringBundler sb = null;
1279
1280 if (doUnion) {
1281 sb = new StringBundler(
1282 param1.size() + param2.size() + param3.size() + param4.size() +
1283 1);
1284
1285 for (String key : param1.keySet()) {
1286 sb.append(key);
1287 }
1288
1289 for (String key : param2.keySet()) {
1290 sb.append(key);
1291 }
1292
1293 for (String key : param3.keySet()) {
1294 sb.append(key);
1295 }
1296
1297 for (String key : param4.keySet()) {
1298 sb.append(key);
1299 }
1300 }
1301 else {
1302 sb = new StringBundler(param1.size() + 1);
1303
1304 for (String key : param1.keySet()) {
1305 sb.append(key);
1306 }
1307 }
1308
1309 sb.append(obc.getOrderBy());
1310
1311 return sb.toString();
1312 }
1313
1314 private String _getCacheKey(
1315 String sql, LinkedHashMap<String, Object> params) {
1316
1317 StringBundler sb = new StringBundler();
1318
1319 sb.append(sql);
1320
1321 for (Map.Entry<String, Object> entry : params.entrySet()) {
1322 String key = entry.getKey();
1323
1324 if (key.equals("rolePermissions")) {
1325 RolePermissions rolePermissions =
1326 (RolePermissions)entry.getValue();
1327
1328 if (ResourceBlockLocalServiceUtil.isSupported(
1329 rolePermissions.getName())) {
1330
1331 key = "rolePermissions_6_block";
1332 }
1333 else {
1334 key = "rolePermissions_6";
1335 }
1336 }
1337 else {
1338 Object value = entry.getValue();
1339
1340 if (value instanceof List<?>) {
1341 List<Object> values = (List<Object>)value;
1342
1343 if (!values.isEmpty()) {
1344 for (int i = 0; i < values.size(); i++) {
1345 sb.append(key);
1346 sb.append(StringPool.DASH);
1347 sb.append(i);
1348 }
1349 }
1350 }
1351 }
1352
1353 sb.append(key);
1354 }
1355
1356 return sb.toString();
1357 }
1358
1359 private String _getCondition(String join) {
1360 if (Validator.isNotNull(join)) {
1361 int pos = join.indexOf("WHERE");
1362
1363 if (pos != -1) {
1364 join = join.substring(pos + 5, join.length()).concat(" AND ");
1365 }
1366 else {
1367 join = StringPool.BLANK;
1368 }
1369 }
1370
1371 return join;
1372 }
1373
1374 private long[] _getGroupOrganizationClassNameIds() {
1375 if (_groupOrganizationClassNameIds == null) {
1376 _groupOrganizationClassNameIds = new long[] {
1377 ClassNameLocalServiceUtil.getClassNameId(Group.class),
1378 ClassNameLocalServiceUtil.getClassNameId(Organization.class)
1379 };
1380 }
1381
1382 return _groupOrganizationClassNameIds;
1383 }
1384
1385 private Map<String, String> _getJoinMap() {
1386 if (_joinMap != null) {
1387 return _joinMap;
1388 }
1389
1390 Map<String, String> joinMap = new HashMap<>();
1391
1392 joinMap.put("active", _removeWhere(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1393 joinMap.put(
1394 "groupOrg", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1395 joinMap.put(
1396 "groupsOrgs", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1397 joinMap.put(
1398 "groupsRoles",
1399 _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1400 joinMap.put(
1401 "groupsUserGroups",
1402 _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1403 joinMap.put(
1404 "layoutSet", _removeWhere(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1405 joinMap.put(
1406 "pageCount", _removeWhere(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1407 joinMap.put(
1408 "membershipRestriction",
1409 _removeWhere(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1410 joinMap.put(
1411 "rolePermissions_6",
1412 _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1413 joinMap.put(
1414 "rolePermissions_6_block",
1415 _removeWhere(
1416 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1417 joinMap.put("site", _removeWhere(CustomSQLUtil.get(JOIN_BY_SITE)));
1418 joinMap.put("type", _removeWhere(CustomSQLUtil.get(JOIN_BY_TYPE)));
1419 joinMap.put(
1420 "userGroupRole",
1421 _removeWhere(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1422 joinMap.put(
1423 "usersGroups",
1424 _removeWhere(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1425
1426 _joinMap = joinMap;
1427
1428 return _joinMap;
1429 }
1430
1431 private Map<String, String> _getWhereMap() {
1432 if (_whereMap != null) {
1433 return _whereMap;
1434 }
1435
1436 Map<String, String> whereMap = new HashMap<>();
1437
1438 whereMap.put(
1439 "active", _getCondition(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1440 whereMap.put(
1441 "creatorUserId",
1442 _getCondition(CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID)));
1443 whereMap.put(
1444 "groupOrg", _getCondition(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1445 whereMap.put(
1446 "groupsOrgs",
1447 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1448 whereMap.put(
1449 "groupsRoles",
1450 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1451 whereMap.put(
1452 "groupsUserGroups",
1453 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1454 whereMap.put(
1455 "layoutSet", _getCondition(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1456 whereMap.put(
1457 "manualMembership",
1458 _getCondition(CustomSQLUtil.get(JOIN_BY_MANUAL_MEMBERSHIP)));
1459 whereMap.put(
1460 "membershipRestriction",
1461 _getCondition(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1462 whereMap.put(
1463 "pageCount", _getCondition(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1464 whereMap.put(
1465 "rolePermissions_6",
1466 _getCondition(
1467 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1468 whereMap.put(
1469 "rolePermissions_6_block",
1470 _getCondition(
1471 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1472 whereMap.put("site", _getCondition(CustomSQLUtil.get(JOIN_BY_SITE)));
1473 whereMap.put("type", _getCondition(CustomSQLUtil.get(JOIN_BY_TYPE)));
1474 whereMap.put(
1475 "userGroupRole",
1476 _getCondition(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1477 whereMap.put(
1478 "usersGroups",
1479 _getCondition(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1480
1481 _whereMap = whereMap;
1482
1483 return _whereMap;
1484 }
1485
1486 private String _removeWhere(String join) {
1487 if (Validator.isNotNull(join)) {
1488 int pos = join.indexOf("WHERE");
1489
1490 if (pos != -1) {
1491 join = join.substring(0, pos);
1492 }
1493 }
1494
1495 return join;
1496 }
1497
1498 private final LinkedHashMap<String, Object> _emptyLinkedHashMap =
1499 new LinkedHashMap<>(0);
1500 private final Map<String, String> _findByC_C_PG_N_DSQLCache =
1501 new ConcurrentHashMap<>();
1502 private final Map<String, String> _findByCompanyIdSQLCache =
1503 new ConcurrentHashMap<>();
1504 private volatile long[] _groupOrganizationClassNameIds;
1505 private volatile Map<String, String> _joinMap;
1506 private final Map<String, String> _replaceJoinAndWhereSQLCache =
1507 new ConcurrentHashMap<>();
1508 private volatile Map<String, String> _whereMap;
1509
1510 }