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