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 @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 List<Long> findByC_P(
603 long companyId, long parentGroupId, long previousGroupId, int size)
604 throws SystemException {
605
606 Session session = null;
607
608 try {
609 session = openSession();
610
611 String sql = CustomSQLUtil.get(FIND_BY_C_P);
612
613 if (previousGroupId <= 0) {
614 sql = StringUtil.replace(
615 sql, "(groupId > ?) AND", StringPool.BLANK);
616 }
617
618 SQLQuery q = session.createSQLQuery(sql);
619
620 q.addScalar("groupId", Type.LONG);
621
622 QueryPos qPos = QueryPos.getInstance(q);
623
624 if (previousGroupId > 0) {
625 qPos.add(previousGroupId);
626 }
627
628 qPos.add(companyId);
629 qPos.add(parentGroupId);
630
631 return (List<Long>)QueryUtil.list(q, getDialect(), 0, size);
632 }
633 catch (Exception e) {
634 throw new SystemException(e);
635 }
636 finally {
637 closeSession(session);
638 }
639 }
640
641 @Override
642 public Group findByC_N(long companyId, String name)
643 throws NoSuchGroupException, SystemException {
644
645 name = StringUtil.lowerCase(name);
646
647 Session session = null;
648
649 try {
650 session = openSession();
651
652 String sql = CustomSQLUtil.get(FIND_BY_C_N);
653
654 SQLQuery q = session.createSQLQuery(sql);
655
656 q.addEntity("Group_", GroupImpl.class);
657
658 QueryPos qPos = QueryPos.getInstance(q);
659
660 qPos.add(companyId);
661 qPos.add(name);
662
663 List<Group> groups = q.list();
664
665 if (!groups.isEmpty()) {
666 return groups.get(0);
667 }
668 }
669 catch (Exception e) {
670 throw new SystemException(e);
671 }
672 finally {
673 closeSession(session);
674 }
675
676 StringBundler sb = new StringBundler(5);
677
678 sb.append("No Group exists with the key {companyId=");
679 sb.append(companyId);
680 sb.append(", name=");
681 sb.append(name);
682 sb.append("}");
683
684 throw new NoSuchGroupException(sb.toString());
685 }
686
687 @Override
688 public List<Group> findByC_C_PG_N_D(
689 long companyId, long[] classNameIds, long parentGroupId,
690 String[] names, String[] descriptions,
691 LinkedHashMap<String, Object> params, boolean andOperator,
692 int start, int end, OrderByComparator obc)
693 throws SystemException {
694
695 String parentGroupIdComparator = StringPool.EQUAL;
696
697 if (parentGroupId == GroupConstants.ANY_PARENT_GROUP_ID) {
698 parentGroupIdComparator = StringPool.NOT_EQUAL;
699 }
700
701 names = CustomSQLUtil.keywords(names);
702 descriptions = CustomSQLUtil.keywords(descriptions);
703
704 if (params == null) {
705 params = _emptyLinkedHashMap;
706 }
707
708 LinkedHashMap<String, Object> params1 = params;
709
710 LinkedHashMap<String, Object> params2 = null;
711
712 LinkedHashMap<String, Object> params3 = null;
713
714 LinkedHashMap<String, Object> params4 = null;
715
716 Long userId = (Long)params.get("usersGroups");
717 boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
718
719 boolean doUnion = Validator.isNotNull(userId) && inherit;
720
721 if (doUnion) {
722 params2 = new LinkedHashMap<String, Object>(params1);
723
724 params2.remove("usersGroups");
725 params2.put("groupOrg", userId);
726
727 params3 = new LinkedHashMap<String, Object>(params1);
728
729 params3.remove("usersGroups");
730 params3.put("groupsOrgs", userId);
731
732 params4 = new LinkedHashMap<String, Object>(params1);
733
734 params4.remove("usersGroups");
735 params4.put("groupsUserGroups", userId);
736 }
737
738 if (obc == null) {
739 obc = new GroupNameComparator(true);
740 }
741
742 String sql = null;
743
744 if (classNameIds == _getGroupOrganizationClassNameIds()) {
745 String sqlKey = _buildSQLKey(
746 params1, params2, params3, params4, obc, doUnion);
747
748 sql = _findByC_C_PG_N_DSQLCache.get(sqlKey);
749 }
750
751 if (sql == null) {
752 String findByC_PG_N_D_SQL = CustomSQLUtil.get(FIND_BY_C_C_PG_N_D);
753
754 if (classNameIds == null) {
755 findByC_PG_N_D_SQL = StringUtil.replace(
756 findByC_PG_N_D_SQL, "AND (Group_.classNameId = ?)",
757 StringPool.BLANK);
758 }
759 else {
760 findByC_PG_N_D_SQL = StringUtil.replace(
761 findByC_PG_N_D_SQL, "Group_.classNameId = ?",
762 "Group_.classNameId = ".concat(
763 StringUtil.merge(
764 classNameIds, " OR Group_.classNameId = ")));
765 }
766
767 findByC_PG_N_D_SQL = replaceOrderBy(findByC_PG_N_D_SQL, obc);
768
769 StringBundler sb = new StringBundler();
770
771 sb.append(StringPool.OPEN_PARENTHESIS);
772 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params1));
773 sb.append(StringPool.CLOSE_PARENTHESIS);
774
775 if (doUnion) {
776 sb.append(" UNION (");
777 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params2));
778 sb.append(") UNION (");
779 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params3));
780 sb.append(") UNION (");
781 sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params4));
782 sb.append(StringPool.CLOSE_PARENTHESIS);
783 }
784
785 if (obc != null) {
786 sb.append(" ORDER BY ");
787 sb.append(obc.toString());
788 }
789
790 sql = sb.toString();
791
792 if (classNameIds == _getGroupOrganizationClassNameIds()) {
793 String sqlKey = _buildSQLKey(
794 params1, params2, params3, params4, obc, doUnion);
795
796 _findByC_C_PG_N_DSQLCache.put(sqlKey, sql);
797 }
798 }
799
800 sql = StringUtil.replace(
801 sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
802 parentGroupIdComparator.equals(StringPool.EQUAL) ?
803 StringPool.EQUAL : StringPool.NOT_EQUAL);
804 sql = CustomSQLUtil.replaceKeywords(
805 sql, "lower(Group_.name)", StringPool.LIKE, false, names);
806 sql = CustomSQLUtil.replaceKeywords(
807 sql, "lower(Group_.description)", StringPool.LIKE, true,
808 descriptions);
809 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
810
811 Session session = null;
812
813 try {
814 session = openSession();
815
816 SQLQuery q = session.createSQLQuery(sql);
817
818 q.addScalar("groupId", Type.LONG);
819
820 QueryPos qPos = QueryPos.getInstance(q);
821
822 setJoin(qPos, params1);
823
824 qPos.add(companyId);
825 qPos.add(parentGroupId);
826 qPos.add(names, 2);
827 qPos.add(descriptions, 2);
828
829 if (doUnion) {
830 setJoin(qPos, params2);
831
832 qPos.add(companyId);
833 qPos.add(parentGroupId);
834 qPos.add(names, 2);
835 qPos.add(descriptions, 2);
836
837 setJoin(qPos, params3);
838
839 qPos.add(companyId);
840 qPos.add(parentGroupId);
841 qPos.add(names, 2);
842 qPos.add(descriptions, 2);
843
844 setJoin(qPos, params4);
845
846 qPos.add(companyId);
847 qPos.add(parentGroupId);
848 qPos.add(names, 2);
849 qPos.add(descriptions, 2);
850 }
851
852 List<Long> groupIds = (List<Long>)QueryUtil.list(
853 q, getDialect(), start, end);
854
855 List<Group> groups = new ArrayList<Group>(groupIds.size());
856
857 for (Long groupId : groupIds) {
858 Group group = GroupUtil.findByPrimaryKey(groupId);
859
860 groups.add(group);
861 }
862
863 return groups;
864 }
865 catch (Exception e) {
866 throw new SystemException(e);
867 }
868 finally {
869 closeSession(session);
870 }
871 }
872
873 protected int countByGroupId(
874 Session session, long groupId, LinkedHashMap<String, Object> params)
875 throws Exception {
876
877 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
878
879 sql = replaceJoinAndWhere(sql, params);
880
881 SQLQuery q = session.createSQLQuery(sql);
882
883 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
884
885 QueryPos qPos = QueryPos.getInstance(q);
886
887 setJoin(qPos, params);
888
889 qPos.add(groupId);
890
891 Iterator<Long> itr = q.iterate();
892
893 if (itr.hasNext()) {
894 Long count = itr.next();
895
896 if (count != null) {
897 return count.intValue();
898 }
899 }
900
901 return 0;
902 }
903
904 protected List<Long> countByC_C_PG_N_D(
905 Session session, long companyId, long[] classNameIds,
906 long parentGroupId, String parentGroupIdComparator, String[] names,
907 String[] descriptions, LinkedHashMap<String, Object> params,
908 boolean andOperator)
909 throws Exception {
910
911 String sql = CustomSQLUtil.get(COUNT_BY_C_C_PG_N_D);
912
913 if (classNameIds == null) {
914 sql = StringUtil.replace(
915 sql, "AND (Group_.classNameId = ?)", StringPool.BLANK);
916 }
917 else {
918 sql = StringUtil.replace(
919 sql, "Group_.classNameId = ?",
920 "Group_.classNameId = ".concat(
921 StringUtil.merge(
922 classNameIds, " OR Group_.classNameId = ")));
923 }
924
925 sql = StringUtil.replace(
926 sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
927 parentGroupIdComparator.equals(StringPool.EQUAL) ?
928 StringPool.EQUAL : StringPool.NOT_EQUAL);
929 sql = CustomSQLUtil.replaceKeywords(
930 sql, "lower(Group_.name)", StringPool.LIKE, false, names);
931 sql = CustomSQLUtil.replaceKeywords(
932 sql, "lower(Group_.description)", StringPool.LIKE, true,
933 descriptions);
934
935 sql = replaceJoinAndWhere(sql, params);
936 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
937
938 SQLQuery q = session.createSQLQuery(sql);
939
940 q.addScalar("groupId", Type.LONG);
941
942 QueryPos qPos = QueryPos.getInstance(q);
943
944 setJoin(qPos, params);
945
946 qPos.add(companyId);
947 qPos.add(parentGroupId);
948 qPos.add(names, 2);
949 qPos.add(descriptions, 2);
950
951 return q.list(true);
952 }
953
954 protected String getJoin(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 Object value = entry.getValue();
964
965 if (Validator.isNull(value)) {
966 continue;
967 }
968
969 if (key.equals("rolePermissions")) {
970 List<Object> values = (List<Object>)value;
971
972 String name = (String)values.get(0);
973
974 if (ResourceBlockLocalServiceUtil.isSupported(name)) {
975 key = "rolePermissions_6_block";
976 }
977 else {
978 key = "rolePermissions_6";
979 }
980 }
981
982 Map<String, String> joinMap = _getJoinMap();
983
984 String joinValue = joinMap.get(key);
985
986 if (Validator.isNotNull(joinValue)) {
987 sb.append(joinValue);
988 }
989 }
990
991 return sb.toString();
992 }
993
994 protected String getWhere(LinkedHashMap<String, Object> params) {
995 if ((params == null) || params.isEmpty()) {
996 return StringPool.BLANK;
997 }
998
999 StringBundler sb = new StringBundler(params.size());
1000
1001 for (Map.Entry<String, Object> entry : params.entrySet()) {
1002 String key = entry.getKey();
1003
1004 if (key.equals("excludedGroupIds")) {
1005 List<Long> excludedGroupIds = (List<Long>)entry.getValue();
1006
1007 if (!excludedGroupIds.isEmpty()) {
1008 sb.append(StringPool.OPEN_PARENTHESIS);
1009
1010 for (int i = 0; i < excludedGroupIds.size(); i++) {
1011 sb.append("(Group_.groupId != ?)");
1012
1013 if ((i + 1) < excludedGroupIds.size()) {
1014 sb.append(" AND ");
1015 }
1016 }
1017
1018 sb.append(") AND ");
1019 }
1020 }
1021 else if (key.equals("groupsTree")) {
1022 List<Group> groupsTree = (List<Group>)entry.getValue();
1023
1024 if (!groupsTree.isEmpty()) {
1025 sb.append(StringPool.OPEN_PARENTHESIS);
1026
1027 for (int i = 0; i < groupsTree.size(); i++) {
1028 sb.append("(Group_.treePath LIKE ?) ");
1029
1030 if ((i + 1) < groupsTree.size()) {
1031 sb.append("OR ");
1032 }
1033 }
1034
1035 sb.append(") AND ");
1036 }
1037 }
1038 else if (key.equals("types")) {
1039 List<Integer> types = (List<Integer>)entry.getValue();
1040
1041 if (!types.isEmpty()) {
1042 sb.append(StringPool.OPEN_PARENTHESIS);
1043
1044 for (int i = 0; i < types.size(); i++) {
1045 sb.append("(Group_.type_ = ?) ");
1046
1047 if ((i + 1) < types.size()) {
1048 sb.append("OR ");
1049 }
1050 }
1051
1052 sb.append(") AND ");
1053 }
1054 }
1055 else {
1056 if (key.equals("rolePermissions")) {
1057 List<Object> values = (List<Object>)entry.getValue();
1058
1059 String name = (String)values.get(0);
1060
1061 if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1062 key = "rolePermissions_6_block";
1063 }
1064 else {
1065 key = "rolePermissions_6";
1066 }
1067 }
1068
1069 Map<String, String> whereMap = _getWhereMap();
1070
1071 String whereValue = whereMap.get(key);
1072
1073 if (Validator.isNotNull(whereValue)) {
1074 sb.append(whereValue);
1075 }
1076 }
1077 }
1078
1079 return sb.toString();
1080 }
1081
1082 protected String replaceJoinAndWhere(
1083 String sql, LinkedHashMap<String, Object> params) {
1084
1085 if (params.isEmpty()) {
1086 return StringUtil.replace(
1087 sql,
1088 new String[] {
1089 "[$JOIN$]", "[$WHERE$]"
1090 },
1091 new String[] {
1092 StringPool.BLANK, StringPool.BLANK
1093 });
1094 }
1095
1096 String cacheKey = _getCacheKey(sql, params);
1097
1098 String resultSQL = _replaceJoinAndWhereSQLCache.get(cacheKey);
1099
1100 if (resultSQL == null) {
1101 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
1102
1103 resultSQL = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
1104
1105 _replaceJoinAndWhereSQLCache.put(cacheKey, resultSQL);
1106 }
1107
1108 return resultSQL;
1109 }
1110
1111 protected String replaceOrderBy(String sql, OrderByComparator obc) {
1112 if (obc instanceof GroupNameComparator) {
1113 sql = StringUtil.replace(
1114 sql, "Group_.name AS groupName",
1115 "REPLACE(Group_.name, '" +
1116 GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX +
1117 "', '') AS groupName");
1118 }
1119
1120 return sql;
1121 }
1122
1123 protected void setJoin(QueryPos qPos, LinkedHashMap<String, Object> params)
1124 throws Exception {
1125
1126 if (params == null) {
1127 return;
1128 }
1129
1130 for (Map.Entry<String, Object> entry : params.entrySet()) {
1131 String key = entry.getKey();
1132
1133 if (key.equals("active") || key.equals("layoutSet") ||
1134 key.equals("manualMembership") || key.equals("site")) {
1135
1136 Boolean value = (Boolean)entry.getValue();
1137
1138 qPos.add(value);
1139 }
1140 else if (key.equals("excludedGroupIds")) {
1141 List<Long> excludedGroupIds = (List<Long>)entry.getValue();
1142
1143 if (!excludedGroupIds.isEmpty()) {
1144 for (long excludedGroupId : excludedGroupIds) {
1145 qPos.add(excludedGroupId);
1146 }
1147 }
1148 }
1149 else if (key.equals("groupsTree")) {
1150 List<Group> groupsTree = (List<Group>)entry.getValue();
1151
1152 if (!groupsTree.isEmpty()) {
1153 for (Group group : groupsTree) {
1154 StringBundler sb = new StringBundler(5);
1155
1156 sb.append(StringPool.PERCENT);
1157 sb.append(StringPool.SLASH);
1158 sb.append(group.getGroupId());
1159 sb.append(StringPool.SLASH);
1160 sb.append(StringPool.PERCENT);
1161
1162 qPos.add(sb.toString());
1163 }
1164 }
1165 }
1166 else if (key.equals("pageCount")) {
1167 }
1168 else if (key.equals("rolePermissions")) {
1169 List<Object> values = (List<Object>)entry.getValue();
1170
1171 String name = (String)values.get(0);
1172 Integer scope = (Integer)values.get(1);
1173 String actionId = (String)values.get(2);
1174 Long roleId = (Long)values.get(3);
1175
1176 ResourceAction resourceAction =
1177 ResourceActionLocalServiceUtil.getResourceAction(
1178 name, actionId);
1179
1180 if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1181
1182
1183
1184 qPos.add(name);
1185 qPos.add(roleId);
1186 qPos.add(resourceAction.getBitwiseValue());
1187 }
1188 else {
1189 qPos.add(name);
1190 qPos.add(scope);
1191 qPos.add(roleId);
1192 qPos.add(resourceAction.getBitwiseValue());
1193 }
1194 }
1195 else if (key.equals("types")) {
1196 List<Integer> values = (List<Integer>)entry.getValue();
1197
1198 for (int i = 0; i < values.size(); i++) {
1199 Integer value = values.get(i);
1200
1201 qPos.add(value);
1202 }
1203 }
1204 else if (key.equals("userGroupRole")) {
1205 List<Long> values = (List<Long>)entry.getValue();
1206
1207 Long userId = values.get(0);
1208 Long roleId = values.get(1);
1209
1210 qPos.add(userId);
1211 qPos.add(roleId);
1212 }
1213 else {
1214 Object value = entry.getValue();
1215
1216 if (value instanceof Integer) {
1217 Integer valueInteger = (Integer)value;
1218
1219 if (Validator.isNotNull(valueInteger)) {
1220 qPos.add(valueInteger);
1221 }
1222 }
1223 else if (value instanceof Long) {
1224 Long valueLong = (Long)value;
1225
1226 if (Validator.isNotNull(valueLong)) {
1227 qPos.add(valueLong);
1228 }
1229 }
1230 else if (value instanceof String) {
1231 String valueString = (String)value;
1232
1233 if (Validator.isNotNull(valueString)) {
1234 qPos.add(valueString);
1235 }
1236 }
1237 }
1238 }
1239 }
1240
1241 private String _buildSQLKey(
1242 LinkedHashMap<String, Object> param1,
1243 LinkedHashMap<String, Object> param2,
1244 LinkedHashMap<String, Object> param3,
1245 LinkedHashMap<String, Object> param4, OrderByComparator obc,
1246 boolean doUnion) {
1247
1248 StringBundler sb = null;
1249
1250 if (doUnion) {
1251 sb = new StringBundler(
1252 param1.size() + param2.size() + param3.size() + param4.size() +
1253 1);
1254
1255 for (String key : param1.keySet()) {
1256 sb.append(key);
1257 }
1258
1259 for (String key : param2.keySet()) {
1260 sb.append(key);
1261 }
1262
1263 for (String key : param3.keySet()) {
1264 sb.append(key);
1265 }
1266
1267 for (String key : param4.keySet()) {
1268 sb.append(key);
1269 }
1270 }
1271 else {
1272 sb = new StringBundler(param1.size() + 1);
1273
1274 for (String key : param1.keySet()) {
1275 sb.append(key);
1276 }
1277 }
1278
1279 sb.append(obc.getOrderBy());
1280
1281 return sb.toString();
1282 }
1283
1284 private String _getCacheKey(
1285 String sql, LinkedHashMap<String, Object> params) {
1286
1287 StringBundler sb = new StringBundler();
1288
1289 sb.append(sql);
1290
1291 for (Map.Entry<String, Object> entry : params.entrySet()) {
1292 String key = entry.getKey();
1293
1294 if (key.equals("rolePermissions")) {
1295 List<Object> values = (List<Object>)entry.getValue();
1296
1297 String name = (String)values.get(0);
1298
1299 if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1300 key = "rolePermissions_6_block";
1301 }
1302 else {
1303 key = "rolePermissions_6";
1304 }
1305 }
1306 else {
1307 Object value = entry.getValue();
1308
1309 if (value instanceof List<?>) {
1310 List<Object> values = (List<Object>)value;
1311
1312 if (!values.isEmpty()) {
1313 for (int i = 0; i < values.size(); i++) {
1314 sb.append(key);
1315 sb.append(StringPool.DASH);
1316 sb.append(i);
1317 }
1318 }
1319 }
1320 }
1321
1322 sb.append(key);
1323 }
1324
1325 return sb.toString();
1326 }
1327
1328 private String _getCondition(String join) {
1329 if (Validator.isNotNull(join)) {
1330 int pos = join.indexOf("WHERE");
1331
1332 if (pos != -1) {
1333 join = join.substring(pos + 5, join.length()).concat(" AND ");
1334 }
1335 else {
1336 join = StringPool.BLANK;
1337 }
1338 }
1339
1340 return join;
1341 }
1342
1343 private long[] _getGroupOrganizationClassNameIds() {
1344 if (_groupOrganizationClassNameIds == null) {
1345 _groupOrganizationClassNameIds = new long[] {
1346 ClassNameLocalServiceUtil.getClassNameId(Group.class),
1347 ClassNameLocalServiceUtil.getClassNameId(Organization.class)
1348 };
1349 }
1350
1351 return _groupOrganizationClassNameIds;
1352 }
1353
1354 private Map<String, String> _getJoinMap() {
1355 if (_joinMap != null) {
1356 return _joinMap;
1357 }
1358
1359 Map<String, String> joinMap = new HashMap<String, String>();
1360
1361 joinMap.put("active", _removeWhere(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1362 joinMap.put(
1363 "groupOrg", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1364 joinMap.put(
1365 "groupsOrgs", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1366 joinMap.put(
1367 "groupsRoles",
1368 _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1369 joinMap.put(
1370 "groupsUserGroups",
1371 _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1372 joinMap.put(
1373 "layoutSet", _removeWhere(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1374 joinMap.put(
1375 "pageCount", _removeWhere(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1376 joinMap.put(
1377 "membershipRestriction",
1378 _removeWhere(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1379 joinMap.put(
1380 "rolePermissions_6",
1381 _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1382 joinMap.put(
1383 "rolePermissions_6_block",
1384 _removeWhere(
1385 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1386 joinMap.put("site", _removeWhere(CustomSQLUtil.get(JOIN_BY_SITE)));
1387 joinMap.put("type", _removeWhere(CustomSQLUtil.get(JOIN_BY_TYPE)));
1388 joinMap.put(
1389 "userGroupRole",
1390 _removeWhere(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1391 joinMap.put(
1392 "usersGroups",
1393 _removeWhere(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1394
1395 _joinMap = joinMap;
1396
1397 return _joinMap;
1398 }
1399
1400 private Map<String, String> _getWhereMap() {
1401 if (_whereMap != null) {
1402 return _whereMap;
1403 }
1404
1405 Map<String, String> whereMap = new HashMap<String, String>();
1406
1407 whereMap.put(
1408 "active", _getCondition(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1409 whereMap.put(
1410 "creatorUserId",
1411 _getCondition(CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID)));
1412 whereMap.put(
1413 "groupOrg", _getCondition(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1414 whereMap.put(
1415 "groupsOrgs",
1416 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1417 whereMap.put(
1418 "groupsRoles",
1419 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1420 whereMap.put(
1421 "groupsUserGroups",
1422 _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1423 whereMap.put(
1424 "layoutSet", _getCondition(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1425 whereMap.put(
1426 "manualMembership",
1427 _getCondition(CustomSQLUtil.get(JOIN_BY_MANUAL_MEMBERSHIP)));
1428 whereMap.put(
1429 "membershipRestriction",
1430 _getCondition(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1431 whereMap.put(
1432 "pageCount", _getCondition(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1433 whereMap.put(
1434 "rolePermissions_6",
1435 _getCondition(
1436 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1437 whereMap.put(
1438 "rolePermissions_6_block",
1439 _getCondition(
1440 CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1441 whereMap.put("site", _getCondition(CustomSQLUtil.get(JOIN_BY_SITE)));
1442 whereMap.put("type", _getCondition(CustomSQLUtil.get(JOIN_BY_TYPE)));
1443 whereMap.put(
1444 "userGroupRole",
1445 _getCondition(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1446 whereMap.put(
1447 "usersGroups",
1448 _getCondition(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1449
1450 _whereMap = whereMap;
1451
1452 return _whereMap;
1453 }
1454
1455 private String _removeWhere(String join) {
1456 if (Validator.isNotNull(join)) {
1457 int pos = join.indexOf("WHERE");
1458
1459 if (pos != -1) {
1460 join = join.substring(0, pos);
1461 }
1462 }
1463
1464 return join;
1465 }
1466
1467 private LinkedHashMap<String, Object> _emptyLinkedHashMap =
1468 new LinkedHashMap<String, Object>(0);
1469 private Map<String, String> _findByC_C_PG_N_DSQLCache =
1470 new ConcurrentHashMap<String, String>();
1471 private Map<String, String> _findByCompanyIdSQLCache =
1472 new ConcurrentHashMap<String, String>();
1473 private volatile long[] _groupOrganizationClassNameIds;
1474 private volatile Map<String, String> _joinMap;
1475 private Map<String, String> _replaceJoinAndWhereSQLCache =
1476 new ConcurrentHashMap<String, String>();
1477 private volatile Map<String, String> _whereMap;
1478
1479 }