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