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