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.OrderByComparator;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.model.impl.GroupImpl;
031 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.util.dao.orm.CustomSQLUtil;
034
035 import java.util.ArrayList;
036 import java.util.HashSet;
037 import java.util.Iterator;
038 import java.util.LinkedHashMap;
039 import java.util.List;
040 import java.util.Map;
041 import java.util.Set;
042
043
046 public class GroupFinderImpl
047 extends BasePersistenceImpl<Group> implements GroupFinder {
048
049 public static String COUNT_BY_GROUP_ID =
050 GroupFinder.class.getName() + ".countByGroupId";
051
052 public static String COUNT_BY_C_N_D =
053 GroupFinder.class.getName() + ".countByC_N_D";
054
055 public static String FIND_BY_LIVE_GROUPS =
056 GroupFinder.class.getName() + ".findByLiveGroups";
057
058 public static String FIND_BY_NO_LAYOUTS =
059 GroupFinder.class.getName() + ".findByNoLayouts";
060
061 public static String FIND_BY_NULL_FRIENDLY_URL =
062 GroupFinder.class.getName() + ".findByNullFriendlyURL";
063
064 public static String FIND_BY_SYSTEM =
065 GroupFinder.class.getName() + ".findBySystem";
066
067 public static String FIND_BY_C_N =
068 GroupFinder.class.getName() + ".findByC_N";
069
070 public static String FIND_BY_C_N_D =
071 GroupFinder.class.getName() + ".findByC_N_D";
072
073 public static String JOIN_BY_ACTIVE =
074 GroupFinder.class.getName() + ".joinByActive";
075
076 public static String JOIN_BY_CREATOR_USER_ID =
077 GroupFinder.class.getName() + ".joinByCreatorUserId";
078
079 public static String JOIN_BY_GROUPS_ORGS =
080 GroupFinder.class.getName() + ".joinByGroupsOrgs";
081
082 public static String JOIN_BY_GROUPS_ROLES =
083 GroupFinder.class.getName() + ".joinByGroupsRoles";
084
085 public static String JOIN_BY_GROUPS_USER_GROUPS =
086 GroupFinder.class.getName() + ".joinByGroupsUserGroups";
087
088 public static String JOIN_BY_LAYOUT_SET =
089 GroupFinder.class.getName() + ".joinByLayoutSet";
090
091 public static String JOIN_BY_PAGE_COUNT =
092 GroupFinder.class.getName() + ".joinByPageCount";
093
094 public static String JOIN_BY_ROLE_PERMISSIONS =
095 GroupFinder.class.getName() + ".joinByRolePermissions";
096
097 public static String JOIN_BY_ROLE_RESOURCE_PERMISSIONS =
098 GroupFinder.class.getName() + ".joinByRoleResourcePermissions";
099
100 public static String JOIN_BY_TYPE =
101 GroupFinder.class.getName() + ".joinByType";
102
103 public static String JOIN_BY_USER_GROUP_ROLE =
104 GroupFinder.class.getName() + ".joinByUserGroupRole";
105
106 public static String JOIN_BY_USERS_GROUPS =
107 GroupFinder.class.getName() + ".joinByUsersGroups";
108
109 public int countByG_U(long groupId, long userId) throws SystemException {
110 LinkedHashMap<String, Object> params1 =
111 new LinkedHashMap<String, Object>();
112
113 params1.put("usersGroups", userId);
114
115 LinkedHashMap<String, Object> params2 =
116 new LinkedHashMap<String, Object>();
117
118 params2.put("groupsOrgs", userId);
119
120 LinkedHashMap<String, Object> params3 =
121 new LinkedHashMap<String, Object>();
122
123 params3.put("groupsUserGroups", userId);
124
125 Session session = null;
126
127 try {
128 session = openSession();
129
130 int count = countByGroupId(session, groupId, params1);
131 count += countByGroupId(session, groupId, params2);
132 count += countByGroupId(session, groupId, params3);
133
134 return count;
135 }
136 catch (Exception e) {
137 throw new SystemException(e);
138 }
139 finally {
140 closeSession(session);
141 }
142 }
143
144 public int countByC_N_D(
145 long companyId, String name, String realName, String description,
146 LinkedHashMap<String, Object> params)
147 throws SystemException {
148
149 name = StringUtil.lowerCase(name);
150 description = StringUtil.lowerCase(description);
151
152 if (params == null) {
153 params = new LinkedHashMap<String, Object>();
154 }
155
156 Long userId = (Long)params.get("usersGroups");
157
158 LinkedHashMap<String, Object> params1 = params;
159
160 LinkedHashMap<String, Object> params2 =
161 new LinkedHashMap<String, Object>();
162
163 params2.putAll(params1);
164
165 if (userId != null) {
166 params2.remove("usersGroups");
167 params2.put("groupsOrgs", userId);
168 }
169
170 LinkedHashMap<String, Object> params3 =
171 new LinkedHashMap<String, Object>();
172
173 params3.putAll(params1);
174
175 if (userId != null) {
176 params3.remove("usersGroups");
177 params3.put("groupsUserGroups", userId);
178 }
179
180 Session session = null;
181
182 try {
183 session = openSession();
184
185 Set<Long> groupIds = new HashSet<Long>();
186
187 groupIds.addAll(
188 countByC_N_D(
189 session, companyId, name, realName, description, params1));
190
191 if (Validator.isNotNull(userId)) {
192 groupIds.addAll(
193 countByC_N_D(
194 session, companyId, name, realName, description,
195 params2));
196
197 groupIds.addAll(
198 countByC_N_D(
199 session, companyId, name, realName, description,
200 params3));
201 }
202
203 return groupIds.size();
204 }
205 catch (Exception e) {
206 throw new SystemException(e);
207 }
208 finally {
209 closeSession(session);
210 }
211 }
212
213 public List<Group> findByLiveGroups() throws SystemException {
214 Session session = null;
215
216 try {
217 session = openSession();
218
219 String sql = CustomSQLUtil.get(FIND_BY_LIVE_GROUPS);
220
221 SQLQuery q = session.createSQLQuery(sql);
222
223 q.addEntity("Group_", GroupImpl.class);
224
225 return q.list();
226 }
227 catch (Exception e) {
228 throw new SystemException(e);
229 }
230 finally {
231 closeSession(session);
232 }
233 }
234
235 public List<Group> findByNoLayouts(
236 long classNameId, boolean privateLayout, int start, int end)
237 throws SystemException {
238
239 Session session = null;
240
241 try {
242 session = openSession();
243
244 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
245
246 SQLQuery q = session.createSQLQuery(sql);
247
248 q.addEntity("Group_", GroupImpl.class);
249
250 QueryPos qPos = QueryPos.getInstance(q);
251
252 qPos.add(classNameId);
253 qPos.add(privateLayout);
254
255 return q.list();
256 }
257 catch (Exception e) {
258 throw new SystemException(e);
259 }
260 finally {
261 closeSession(session);
262 }
263 }
264
265 public List<Group> findByNullFriendlyURL() throws SystemException {
266 Session session = null;
267
268 try {
269 session = openSession();
270
271 String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
272
273 SQLQuery q = session.createSQLQuery(sql);
274
275 q.addEntity("Group_", GroupImpl.class);
276
277 return q.list();
278 }
279 catch (Exception e) {
280 throw new SystemException(e);
281 }
282 finally {
283 closeSession(session);
284 }
285 }
286
287 public List<Group> findBySystem(long companyId) throws SystemException {
288 Session session = null;
289
290 try {
291 session = openSession();
292
293 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
294
295 SQLQuery q = session.createSQLQuery(sql);
296
297 q.addEntity("Group_", GroupImpl.class);
298
299 QueryPos qPos = QueryPos.getInstance(q);
300
301 qPos.add(companyId);
302
303 return q.list();
304 }
305 catch (Exception e) {
306 throw new SystemException(e);
307 }
308 finally {
309 closeSession(session);
310 }
311 }
312
313 public Group findByC_N(long companyId, String name)
314 throws NoSuchGroupException, SystemException {
315
316 name = StringUtil.lowerCase(name);
317
318 Session session = null;
319
320 try {
321 session = openSession();
322
323 String sql = CustomSQLUtil.get(FIND_BY_C_N);
324
325 SQLQuery q = session.createSQLQuery(sql);
326
327 q.addEntity("Group_", GroupImpl.class);
328
329 QueryPos qPos = QueryPos.getInstance(q);
330
331 qPos.add(companyId);
332 qPos.add(name);
333
334 List<Group> list = q.list();
335
336 if (!list.isEmpty()) {
337 return list.get(0);
338 }
339 }
340 catch (Exception e) {
341 throw new SystemException(e);
342 }
343 finally {
344 closeSession(session);
345 }
346
347 StringBundler sb = new StringBundler(5);
348
349 sb.append("No Group exists with the key {companyId=");
350 sb.append(companyId);
351 sb.append(", name=");
352 sb.append(name);
353 sb.append("}");
354
355 throw new NoSuchGroupException(sb.toString());
356 }
357
358 public List<Group> findByC_N_D(
359 long companyId, String name, String realName, String description,
360 LinkedHashMap<String, Object> params, int start, int end,
361 OrderByComparator obc)
362 throws SystemException {
363
364 name = StringUtil.lowerCase(name);
365 description = StringUtil.lowerCase(description);
366
367 if (params == null) {
368 params = new LinkedHashMap<String, Object>();
369 }
370
371 Long userId = (Long)params.get("usersGroups");
372
373 LinkedHashMap<String, Object> params1 = params;
374
375 LinkedHashMap<String, Object> params2 =
376 new LinkedHashMap<String, Object>();
377
378 params2.putAll(params1);
379
380 if (userId != null) {
381 params2.remove("usersGroups");
382 params2.put("groupsOrgs", userId);
383 }
384
385 LinkedHashMap<String, Object> params3 =
386 new LinkedHashMap<String, Object>();
387
388 params3.putAll(params1);
389
390 if (userId != null) {
391 params3.remove("usersGroups");
392 params3.put("groupsUserGroups", userId);
393 }
394
395 StringBundler sb = new StringBundler();
396
397 sb.append("(");
398
399 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
400
401 String sql = sb.toString();
402
403 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params1));
404 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params1));
405
406 sb.setIndex(0);
407
408 sb.append(sql);
409
410 sb.append(")");
411
412 if (Validator.isNotNull(userId)) {
413 sb.append(" UNION (");
414
415 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
416
417 sql = sb.toString();
418
419 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params2));
420 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params2));
421
422 sb.setIndex(0);
423
424 sb.append(sql);
425
426 sb.append(") UNION (");
427
428 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
429
430 sql = sb.toString();
431
432 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params3));
433 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params3));
434
435 sb.setIndex(0);
436
437 sb.append(sql);
438
439 sb.append(")");
440 }
441
442 sql = sb.toString();
443 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
444
445 Session session = null;
446
447 try {
448 session = openSession();
449
450 SQLQuery q = session.createSQLQuery(sql);
451
452 q.addScalar("groupId", Type.LONG);
453
454 QueryPos qPos = QueryPos.getInstance(q);
455
456 setJoin(qPos, params1);
457 qPos.add(companyId);
458 qPos.add(name);
459 qPos.add(realName);
460 qPos.add(name);
461 qPos.add(description);
462 qPos.add(description);
463
464 if (Validator.isNotNull(userId)) {
465 setJoin(qPos, params2);
466 qPos.add(companyId);
467 qPos.add(name);
468 qPos.add(realName);
469 qPos.add(name);
470 qPos.add(description);
471 qPos.add(description);
472
473 setJoin(qPos, params3);
474 qPos.add(companyId);
475 qPos.add(name);
476 qPos.add(realName);
477 qPos.add(name);
478 qPos.add(description);
479 qPos.add(description);
480 }
481
482 List<Group> groups = new ArrayList<Group>();
483
484 Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
485 q, getDialect(), start, end);
486
487 while (itr.hasNext()) {
488 long groupId = itr.next();
489
490 Group group = GroupUtil.findByPrimaryKey(groupId);
491
492 groups.add(group);
493 }
494
495 return groups;
496 }
497 catch (Exception e) {
498 throw new SystemException(e);
499 }
500 finally {
501 closeSession(session);
502 }
503 }
504
505 protected int countByGroupId(
506 Session session, long groupId, LinkedHashMap<String, Object> params) {
507
508 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
509
510 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
511 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
512
513 SQLQuery q = session.createSQLQuery(sql);
514
515 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
516
517 QueryPos qPos = QueryPos.getInstance(q);
518
519 setJoin(qPos, params);
520 qPos.add(groupId);
521
522 Iterator<Long> itr = q.list().iterator();
523
524 if (itr.hasNext()) {
525 Long count = itr.next();
526
527 if (count != null) {
528 return count.intValue();
529 }
530 }
531
532 return 0;
533 }
534
535 protected List<Long> countByC_N_D(
536 Session session, long companyId, String name, String realName,
537 String description, LinkedHashMap<String, Object> params) {
538
539 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
540
541 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
542 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
543
544 SQLQuery q = session.createSQLQuery(sql);
545
546 q.addScalar("groupId", Type.LONG);
547
548 QueryPos qPos = QueryPos.getInstance(q);
549
550 setJoin(qPos, params);
551 qPos.add(companyId);
552 qPos.add(name);
553 qPos.add(realName);
554 qPos.add(name);
555 qPos.add(description);
556 qPos.add(description);
557
558 return q.list();
559 }
560
561 protected String getJoin(LinkedHashMap<String, Object> params) {
562 if ((params == null) || params.isEmpty()) {
563 return StringPool.BLANK;
564 }
565
566 StringBundler sb = new StringBundler(params.size());
567
568 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
569
570 while (itr.hasNext()) {
571 Map.Entry<String, Object> entry = itr.next();
572
573 String key = entry.getKey();
574 Object value = entry.getValue();
575
576 if (Validator.isNotNull(value)) {
577 sb.append(getJoin(key));
578 }
579 }
580
581 return sb.toString();
582 }
583
584 protected String getJoin(String key) {
585 String join = StringPool.BLANK;
586
587 if (key.equals("groupsOrgs")) {
588 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
589 }
590 else if (key.equals("groupsRoles")) {
591 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
592 }
593 else if (key.equals("groupsUserGroups")) {
594 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
595 }
596 else if (key.equals("layoutSet")) {
597 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
598 }
599 else if (key.equals("pageCount")) {
600 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
601 }
602 else if (key.equals("rolePermissions")) {
603 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
604 join = CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS);
605 }
606 else {
607 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
608 }
609 }
610 else if (key.equals("userGroupRole")) {
611 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
612 }
613 else if (key.equals("usersGroups")) {
614 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
615 }
616
617 if (Validator.isNotNull(join)) {
618 int pos = join.indexOf("WHERE");
619
620 if (pos != -1) {
621 join = join.substring(0, pos);
622 }
623 }
624
625 return join;
626 }
627
628 protected String getWhere(LinkedHashMap<String, Object> params) {
629 if ((params == null) || params.isEmpty()) {
630 return StringPool.BLANK;
631 }
632
633 StringBundler sb = new StringBundler(params.size());
634
635 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
636
637 while (itr.hasNext()) {
638 Map.Entry<String, Object> entry = itr.next();
639
640 String key = entry.getKey();
641 Object value = entry.getValue();
642
643 if (Validator.isNotNull(value)) {
644 sb.append(getWhere(key, value));
645 }
646 }
647
648 return sb.toString();
649 }
650
651 protected String getWhere(String key, Object value) {
652 String join = StringPool.BLANK;
653
654 if (key.equals("active")) {
655 join = CustomSQLUtil.get(JOIN_BY_ACTIVE);
656 }
657 else if (key.equals("creatorUserId")) {
658 join = CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID);
659 }
660 else if (key.equals("groupsOrgs")) {
661 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
662 }
663 else if (key.equals("groupsRoles")) {
664 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
665 }
666 else if (key.equals("groupsUserGroups")) {
667 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
668 }
669 else if (key.equals("layoutSet")) {
670 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
671 }
672 else if (key.equals("pageCount")) {
673 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
674 }
675 else if (key.equals("rolePermissions")) {
676 if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
677 join = CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS);
678 }
679 else {
680 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
681 }
682 }
683 else if (key.equals("type")) {
684 join = CustomSQLUtil.get(JOIN_BY_TYPE);
685 }
686 else if (key.equals("types")) {
687 List<Integer> types = (List<Integer>)value;
688
689 if (!types.isEmpty()) {
690 StringBundler sb = new StringBundler(types.size() * 2 + 1);
691
692 sb.append("WHERE (");
693
694 for (int i = 0; i < types.size(); i++) {
695 sb.append("(Group_.type_ = ?) ");
696
697 if ((i + 1) < types.size()) {
698 sb.append("OR ");
699 }
700 }
701
702 sb.append(")");
703
704 join = sb.toString();
705 }
706 }
707 else if (key.equals("userGroupRole")) {
708 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
709 }
710 else if (key.equals("usersGroups")) {
711 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
712 }
713
714 if (Validator.isNotNull(join)) {
715 int pos = join.indexOf("WHERE");
716
717 if (pos != -1) {
718 join = join.substring(pos + 5, join.length()).concat(" AND ");
719 }
720 else {
721 join = StringPool.BLANK;
722 }
723 }
724
725 return join;
726 }
727
728 protected void setJoin(
729 QueryPos qPos, LinkedHashMap<String, Object> params) {
730
731 if (params != null) {
732 Iterator<Map.Entry<String, Object>> itr =
733 params.entrySet().iterator();
734
735 while (itr.hasNext()) {
736 Map.Entry<String, Object> entry = itr.next();
737
738 String key = entry.getKey();
739
740 if (key.equals("active") || key.equals("layoutSet")) {
741 Boolean value = (Boolean)entry.getValue();
742
743 qPos.add(value);
744 }
745 else if (key.equals("pageCount")) {
746 }
747 else if (key.equals("rolePermissions")) {
748 List<Object> values = (List<Object>)entry.getValue();
749
750 for (int i = 0; i < values.size(); i++) {
751 Object value = values.get(i);
752
753 if (value instanceof Integer) {
754 Integer valueInteger = (Integer)value;
755
756 qPos.add(valueInteger);
757 }
758 else if (value instanceof Long) {
759 Long valueLong = (Long)value;
760
761 qPos.add(valueLong);
762 }
763 else if (value instanceof String) {
764 String valueString = (String)value;
765
766 qPos.add(valueString);
767 }
768 }
769 }
770 else if (key.equals("types")) {
771 List<Integer> values = (List<Integer>)entry.getValue();
772
773 for (int i = 0; i < values.size(); i++) {
774 Integer value = values.get(i);
775
776 qPos.add(value);
777 }
778 }
779 else if (key.equals("userGroupRole")) {
780 List<Long> values = (List<Long>)entry.getValue();
781
782 Long userId = values.get(0);
783 Long roleId = values.get(1);
784
785 qPos.add(userId);
786 qPos.add(roleId);
787 }
788 else {
789 Object value = entry.getValue();
790
791 if (value instanceof Integer) {
792 Integer valueInteger = (Integer)value;
793
794 if (Validator.isNotNull(valueInteger)) {
795 qPos.add(valueInteger);
796 }
797 }
798 else if (value instanceof Long) {
799 Long valueLong = (Long)value;
800
801 if (Validator.isNotNull(valueLong)) {
802 qPos.add(valueLong);
803 }
804 }
805 else if (value instanceof String) {
806 String valueString = (String)value;
807
808 if (Validator.isNotNull(valueString)) {
809 qPos.add(valueString);
810 }
811 }
812 }
813 }
814 }
815 }
816
817 }