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