1
14
15 package com.liferay.portal.service.persistence;
16
17 import com.liferay.portal.NoSuchGroupException;
18 import com.liferay.portal.kernel.dao.orm.QueryPos;
19 import com.liferay.portal.kernel.dao.orm.QueryUtil;
20 import com.liferay.portal.kernel.dao.orm.SQLQuery;
21 import com.liferay.portal.kernel.dao.orm.Session;
22 import com.liferay.portal.kernel.dao.orm.Type;
23 import com.liferay.portal.kernel.exception.SystemException;
24 import com.liferay.portal.kernel.util.OrderByComparator;
25 import com.liferay.portal.kernel.util.StringBundler;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Group;
30 import com.liferay.portal.model.impl.GroupImpl;
31 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32 import com.liferay.util.dao.orm.CustomSQLUtil;
33
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42
47 public class GroupFinderImpl
48 extends BasePersistenceImpl<Group> implements GroupFinder {
49
50 public static String COUNT_BY_GROUP_ID =
51 GroupFinder.class.getName() + ".countByGroupId";
52
53 public static String COUNT_BY_C_N_D =
54 GroupFinder.class.getName() + ".countByC_N_D";
55
56 public static String FIND_BY_NO_LAYOUTS =
57 GroupFinder.class.getName() + ".findByNoLayouts";
58
59 public static String FIND_BY_NULL_FRIENDLY_URL =
60 GroupFinder.class.getName() + ".findByNullFriendlyURL";
61
62 public static String FIND_BY_SYSTEM =
63 GroupFinder.class.getName() + ".findBySystem";
64
65 public static String FIND_BY_C_N =
66 GroupFinder.class.getName() + ".findByC_N";
67
68 public static String FIND_BY_C_N_D =
69 GroupFinder.class.getName() + ".findByC_N_D";
70
71 public static String JOIN_BY_ACTIVE =
72 GroupFinder.class.getName() + ".joinByActive";
73
74 public static String JOIN_BY_CREATOR_USER_ID =
75 GroupFinder.class.getName() + ".joinByCreatorUserId";
76
77 public static String JOIN_BY_GROUPS_ORGS =
78 GroupFinder.class.getName() + ".joinByGroupsOrgs";
79
80 public static String JOIN_BY_GROUPS_ROLES =
81 GroupFinder.class.getName() + ".joinByGroupsRoles";
82
83 public static String JOIN_BY_GROUPS_USER_GROUPS =
84 GroupFinder.class.getName() + ".joinByGroupsUserGroups";
85
86 public static String JOIN_BY_LAYOUT_SET =
87 GroupFinder.class.getName() + ".joinByLayoutSet";
88
89 public static String JOIN_BY_PAGE_COUNT =
90 GroupFinder.class.getName() + ".joinByPageCount";
91
92 public static String JOIN_BY_ROLE_PERMISSIONS =
93 GroupFinder.class.getName() + ".joinByRolePermissions";
94
95 public static String JOIN_BY_TYPE =
96 GroupFinder.class.getName() + ".joinByType";
97
98 public static String JOIN_BY_USER_GROUP_ROLE =
99 GroupFinder.class.getName() + ".joinByUserGroupRole";
100
101 public static String JOIN_BY_USERS_GROUPS =
102 GroupFinder.class.getName() + ".joinByUsersGroups";
103
104 public int countByG_U(long groupId, long userId) throws SystemException {
105 LinkedHashMap<String, Object> params1 =
106 new LinkedHashMap<String, Object>();
107
108 params1.put("usersGroups", userId);
109
110 LinkedHashMap<String, Object> params2 =
111 new LinkedHashMap<String, Object>();
112
113 params2.put("groupsOrgs", userId);
114
115 LinkedHashMap<String, Object> params3 =
116 new LinkedHashMap<String, Object>();
117
118 params3.put("groupsUserGroups", userId);
119
120 Session session = null;
121
122 try {
123 session = openSession();
124
125 int count = countByGroupId(session, groupId, params1);
126 count += countByGroupId(session, groupId, params2);
127 count += countByGroupId(session, groupId, params3);
128
129 return count;
130 }
131 catch (Exception e) {
132 throw new SystemException(e);
133 }
134 finally {
135 closeSession(session);
136 }
137 }
138
139 public int countByC_N_D(
140 long companyId, String name, String description,
141 LinkedHashMap<String, Object> params)
142 throws SystemException {
143
144 name = StringUtil.lowerCase(name);
145 description = StringUtil.lowerCase(description);
146
147 if (params == null) {
148 params = new LinkedHashMap<String, Object>();
149 }
150
151 Long userId = (Long)params.get("usersGroups");
152
153 LinkedHashMap<String, Object> params1 = params;
154
155 LinkedHashMap<String, Object> params2 =
156 new LinkedHashMap<String, Object>();
157
158 params2.putAll(params1);
159
160 if (userId != null) {
161 params2.remove("usersGroups");
162 params2.put("groupsOrgs", userId);
163 }
164
165 LinkedHashMap<String, Object> params3 =
166 new LinkedHashMap<String, Object>();
167
168 params3.putAll(params1);
169
170 if (userId != null) {
171 params3.remove("usersGroups");
172 params3.put("groupsUserGroups", userId);
173 }
174
175 Session session = null;
176
177 try {
178 session = openSession();
179
180 Set<Long> groupIds = new HashSet<Long>();
181
182 groupIds.addAll(
183 countByC_N_D(session, companyId, name, description, params1));
184
185 if (Validator.isNotNull(userId)) {
186 groupIds.addAll(
187 countByC_N_D(
188 session, companyId, name, description, params2));
189
190 groupIds.addAll(
191 countByC_N_D(
192 session, companyId, name, description, params3));
193 }
194
195 return groupIds.size();
196 }
197 catch (Exception e) {
198 throw new SystemException(e);
199 }
200 finally {
201 closeSession(session);
202 }
203 }
204
205 public List<Group> findByNoLayouts(
206 long classNameId, boolean privateLayout, int start, int end)
207 throws SystemException {
208
209 Session session = null;
210
211 try {
212 session = openSession();
213
214 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
215
216 SQLQuery q = session.createSQLQuery(sql);
217
218 q.addEntity("Group_", GroupImpl.class);
219
220 QueryPos qPos = QueryPos.getInstance(q);
221
222 qPos.add(classNameId);
223 qPos.add(privateLayout);
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> findByNullFriendlyURL() throws SystemException {
236 Session session = null;
237
238 try {
239 session = openSession();
240
241 String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
242
243 SQLQuery q = session.createSQLQuery(sql);
244
245 q.addEntity("Group_", GroupImpl.class);
246
247 return q.list();
248 }
249 catch (Exception e) {
250 throw new SystemException(e);
251 }
252 finally {
253 closeSession(session);
254 }
255 }
256
257 public List<Group> findBySystem(long companyId) throws SystemException {
258 Session session = null;
259
260 try {
261 session = openSession();
262
263 String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
264
265 SQLQuery q = session.createSQLQuery(sql);
266
267 q.addEntity("Group_", GroupImpl.class);
268
269 QueryPos qPos = QueryPos.getInstance(q);
270
271 qPos.add(companyId);
272
273 return q.list();
274 }
275 catch (Exception e) {
276 throw new SystemException(e);
277 }
278 finally {
279 closeSession(session);
280 }
281 }
282
283 public Group findByC_N(long companyId, String name)
284 throws NoSuchGroupException, SystemException {
285
286 name = StringUtil.lowerCase(name);
287
288 Session session = null;
289
290 try {
291 session = openSession();
292
293 String sql = CustomSQLUtil.get(FIND_BY_C_N);
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 qPos.add(name);
303
304 List<Group> list = q.list();
305
306 if (!list.isEmpty()) {
307 return list.get(0);
308 }
309 }
310 catch (Exception e) {
311 throw new SystemException(e);
312 }
313 finally {
314 closeSession(session);
315 }
316
317 StringBundler sb = new StringBundler(5);
318
319 sb.append("No Group exists with the key {companyId=");
320 sb.append(companyId);
321 sb.append(", name=");
322 sb.append(name);
323 sb.append("}");
324
325 throw new NoSuchGroupException(sb.toString());
326 }
327
328 public List<Group> findByC_N_D(
329 long companyId, String name, String description,
330 LinkedHashMap<String, Object> params, int start, int end,
331 OrderByComparator obc)
332 throws SystemException {
333
334 name = StringUtil.lowerCase(name);
335 description = StringUtil.lowerCase(description);
336
337 if (params == null) {
338 params = new LinkedHashMap<String, Object>();
339 }
340
341 Long userId = (Long)params.get("usersGroups");
342
343 LinkedHashMap<String, Object> params1 = params;
344
345 LinkedHashMap<String, Object> params2 =
346 new LinkedHashMap<String, Object>();
347
348 params2.putAll(params1);
349
350 if (userId != null) {
351 params2.remove("usersGroups");
352 params2.put("groupsOrgs", userId);
353 }
354
355 LinkedHashMap<String, Object> params3 =
356 new LinkedHashMap<String, Object>();
357
358 params3.putAll(params1);
359
360 if (userId != null) {
361 params3.remove("usersGroups");
362 params3.put("groupsUserGroups", userId);
363 }
364
365 StringBundler sb = new StringBundler();
366
367 sb.append("(");
368
369 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
370
371 String sql = sb.toString();
372
373 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params1));
374 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params1));
375
376 sb.setIndex(0);
377
378 sb.append(sql);
379
380 sb.append(")");
381
382 if (Validator.isNotNull(userId)) {
383 sb.append(" UNION (");
384
385 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
386
387 sql = sb.toString();
388
389 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params2));
390 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params2));
391
392 sb.setIndex(0);
393
394 sb.append(sql);
395
396 sb.append(") UNION (");
397
398 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
399
400 sql = sb.toString();
401
402 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params3));
403 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params3));
404
405 sb.setIndex(0);
406
407 sb.append(sql);
408
409 sb.append(")");
410 }
411
412 sql = sb.toString();
413 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
414
415 Session session = null;
416
417 try {
418 session = openSession();
419
420 SQLQuery q = session.createSQLQuery(sql);
421
422 q.addScalar("groupId", Type.LONG);
423
424 QueryPos qPos = QueryPos.getInstance(q);
425
426 setJoin(qPos, params1);
427 qPos.add(companyId);
428 qPos.add(name);
429 qPos.add(name);
430 qPos.add(description);
431 qPos.add(description);
432
433 if (Validator.isNotNull(userId)) {
434 setJoin(qPos, params2);
435 qPos.add(companyId);
436 qPos.add(name);
437 qPos.add(name);
438 qPos.add(description);
439 qPos.add(description);
440
441 setJoin(qPos, params3);
442 qPos.add(companyId);
443 qPos.add(name);
444 qPos.add(name);
445 qPos.add(description);
446 qPos.add(description);
447 }
448
449 List<Group> groups = new ArrayList<Group>();
450
451 Iterator<Long> itr = (Iterator<Long>)QueryUtil.iterate(
452 q, getDialect(), start, end);
453
454 while (itr.hasNext()) {
455 long groupId = itr.next();
456
457 Group group = GroupUtil.findByPrimaryKey(groupId);
458
459 groups.add(group);
460 }
461
462 return groups;
463 }
464 catch (Exception e) {
465 throw new SystemException(e);
466 }
467 finally {
468 closeSession(session);
469 }
470 }
471
472 protected int countByGroupId(
473 Session session, long groupId, LinkedHashMap<String, Object> params) {
474
475 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
476
477 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
478 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
479
480 SQLQuery q = session.createSQLQuery(sql);
481
482 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
483
484 QueryPos qPos = QueryPos.getInstance(q);
485
486 setJoin(qPos, params);
487 qPos.add(groupId);
488
489 Iterator<Long> itr = q.list().iterator();
490
491 if (itr.hasNext()) {
492 Long count = itr.next();
493
494 if (count != null) {
495 return count.intValue();
496 }
497 }
498
499 return 0;
500 }
501
502 protected List<Long> countByC_N_D(
503 Session session, long companyId, String name, String description,
504 LinkedHashMap<String, Object> params) {
505
506 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
507
508 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
509 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
510
511 SQLQuery q = session.createSQLQuery(sql);
512
513 q.addScalar("groupId", Type.LONG);
514
515 QueryPos qPos = QueryPos.getInstance(q);
516
517 setJoin(qPos, params);
518 qPos.add(companyId);
519 qPos.add(name);
520 qPos.add(name);
521 qPos.add(description);
522 qPos.add(description);
523
524 return q.list();
525 }
526
527 protected String getJoin(LinkedHashMap<String, Object> params) {
528 if ((params == null) || params.isEmpty()) {
529 return StringPool.BLANK;
530 }
531
532 StringBundler sb = new StringBundler(params.size());
533
534 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
535
536 while (itr.hasNext()) {
537 Map.Entry<String, Object> entry = itr.next();
538
539 String key = entry.getKey();
540 Object value = entry.getValue();
541
542 if (Validator.isNotNull(value)) {
543 sb.append(getJoin(key));
544 }
545 }
546
547 return sb.toString();
548 }
549
550 protected String getJoin(String key) {
551 String join = StringPool.BLANK;
552
553 if (key.equals("groupsOrgs")) {
554 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
555 }
556 else if (key.equals("groupsRoles")) {
557 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
558 }
559 else if (key.equals("groupsUserGroups")) {
560 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
561 }
562 else if (key.equals("layoutSet")) {
563 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
564 }
565 else if (key.equals("pageCount")) {
566 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
567 }
568 else if (key.equals("rolePermissions")) {
569 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
570 }
571 else if (key.equals("userGroupRole")) {
572 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
573 }
574 else if (key.equals("usersGroups")) {
575 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
576 }
577
578 if (Validator.isNotNull(join)) {
579 int pos = join.indexOf("WHERE");
580
581 if (pos != -1) {
582 join = join.substring(0, pos);
583 }
584 }
585
586 return join;
587 }
588
589 protected String getWhere(LinkedHashMap<String, Object> params) {
590 if ((params == null) || params.isEmpty()) {
591 return StringPool.BLANK;
592 }
593
594 StringBundler sb = new StringBundler(params.size());
595
596 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
597
598 while (itr.hasNext()) {
599 Map.Entry<String, Object> entry = itr.next();
600
601 String key = entry.getKey();
602 Object value = entry.getValue();
603
604 if (Validator.isNotNull(value)) {
605 sb.append(getWhere(key, value));
606 }
607 }
608
609 return sb.toString();
610 }
611
612 protected String getWhere(String key, Object value) {
613 String join = StringPool.BLANK;
614
615 if (key.equals("active")) {
616 join = CustomSQLUtil.get(JOIN_BY_ACTIVE);
617 }
618 else if (key.equals("creatorUserId")) {
619 join = CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID);
620 }
621 else if (key.equals("groupsOrgs")) {
622 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
623 }
624 else if (key.equals("groupsRoles")) {
625 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
626 }
627 else if (key.equals("groupsUserGroups")) {
628 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
629 }
630 else if (key.equals("layoutSet")) {
631 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
632 }
633 else if (key.equals("pageCount")) {
634 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
635 }
636 else if (key.equals("rolePermissions")) {
637 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
638 }
639 else if (key.equals("type")) {
640 join = CustomSQLUtil.get(JOIN_BY_TYPE);
641 }
642 else if (key.equals("types")) {
643 List<Integer> types = (List<Integer>)value;
644
645 if (!types.isEmpty()) {
646 StringBundler sb = new StringBundler(types.size() * 2 + 1);
647
648 sb.append("WHERE (");
649
650 for (int i = 0; i < types.size(); i++) {
651 sb.append("(Group_.type_ = ?) ");
652
653 if ((i + 1) < types.size()) {
654 sb.append("OR ");
655 }
656 }
657
658 sb.append(")");
659
660 join = sb.toString();
661 }
662 }
663 else if (key.equals("userGroupRole")) {
664 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
665 }
666 else if (key.equals("usersGroups")) {
667 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
668 }
669
670 if (Validator.isNotNull(join)) {
671 int pos = join.indexOf("WHERE");
672
673 if (pos != -1) {
674 join = join.substring(pos + 5, join.length()).concat(" AND ");
675 }
676 else {
677 join = StringPool.BLANK;
678 }
679 }
680
681 return join;
682 }
683
684 protected void setJoin(
685 QueryPos qPos, LinkedHashMap<String, Object> params) {
686
687 if (params != null) {
688 Iterator<Map.Entry<String, Object>> itr =
689 params.entrySet().iterator();
690
691 while (itr.hasNext()) {
692 Map.Entry<String, Object> entry = itr.next();
693
694 String key = entry.getKey();
695
696 if (key.equals("active") || key.equals("layoutSet")) {
697 Boolean value = (Boolean)entry.getValue();
698
699 qPos.add(value);
700 }
701 else if (key.equals("pageCount")) {
702 }
703 else if (key.equals("rolePermissions")) {
704 List<Object> values = (List<Object>)entry.getValue();
705
706 for (int i = 0; i < values.size(); i++) {
707 Object value = values.get(i);
708
709 if (value instanceof Integer) {
710 Integer valueInteger = (Integer)value;
711
712 qPos.add(valueInteger);
713 }
714 else if (value instanceof Long) {
715 Long valueLong = (Long)value;
716
717 qPos.add(valueLong);
718 }
719 else if (value instanceof String) {
720 String valueString = (String)value;
721
722 qPos.add(valueString);
723 }
724 }
725 }
726 else if (key.equals("types")) {
727 List<Integer> values = (List<Integer>)entry.getValue();
728
729 for (int i = 0; i < values.size(); i++) {
730 Integer value = values.get(i);
731
732 qPos.add(value);
733 }
734 }
735 else if (key.equals("userGroupRole")) {
736 List<Long> values = (List<Long>)entry.getValue();
737
738 Long userId = values.get(0);
739 Long roleId = values.get(1);
740
741 qPos.add(userId);
742 qPos.add(roleId);
743 }
744 else {
745 Object value = entry.getValue();
746
747 if (value instanceof Long) {
748 Long valueLong = (Long)value;
749
750 if (Validator.isNotNull(valueLong)) {
751 qPos.add(valueLong);
752 }
753 }
754 else if (value instanceof String) {
755 String valueString = (String)value;
756
757 if (Validator.isNotNull(valueString)) {
758 qPos.add(valueString);
759 }
760 }
761 }
762 }
763 }
764 }
765
766 }