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