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