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