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