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