1
19
20 package com.liferay.portal.service.persistence;
21
22 import com.liferay.portal.NoSuchGroupException;
23 import com.liferay.portal.SystemException;
24 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
25 import com.liferay.portal.kernel.dao.orm.QueryPos;
26 import com.liferay.portal.kernel.dao.orm.QueryUtil;
27 import com.liferay.portal.kernel.dao.orm.SQLQuery;
28 import com.liferay.portal.kernel.dao.orm.Session;
29 import com.liferay.portal.kernel.dao.orm.Type;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.OrderByComparator;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.model.Group;
36 import com.liferay.portal.model.impl.GroupImpl;
37 import com.liferay.portal.model.impl.GroupModelImpl;
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_NULL_FRIENDLY_URL =
63 GroupFinder.class.getName() + ".findByNullFriendlyURL";
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 int count = countByC_N_D(
181 session, companyId, name, description, params1);
182
183 if (Validator.isNotNull(userId)) {
184 count += countByC_N_D(
185 session, companyId, name, description, params2);
186
187 count += countByC_N_D(
188 session, companyId, name, description, params3);
189 }
190
191 return count;
192 }
193 catch (Exception e) {
194 throw new SystemException(e);
195 }
196 finally {
197 closeSession(session);
198 }
199 }
200
201 public List<Group> findByNullFriendlyURL() throws SystemException {
202 Session session = null;
203
204 try {
205 session = openSession();
206
207 String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
208
209 SQLQuery q = session.createSQLQuery(sql);
210
211 q.addEntity("Group_", GroupImpl.class);
212
213 return q.list();
214 }
215 catch (Exception e) {
216 throw new SystemException(e);
217 }
218 finally {
219 closeSession(session);
220 }
221 }
222
223 public Group findByC_N(long companyId, String name)
224 throws NoSuchGroupException, SystemException {
225
226 name = StringUtil.lowerCase(name);
227
228 boolean finderClassNameCacheEnabled = GroupModelImpl.CACHE_ENABLED;
229 String finderClassName = Group.class.getName();
230 String finderMethodName = "customFindByC_N";
231 String finderParams[] = new String[] {
232 Long.class.getName(), String.class.getName()
233 };
234 Object finderArgs[] = new Object[] {companyId, name};
235
236 Object result = FinderCacheUtil.getResult(
237 finderClassName, finderMethodName, finderParams, finderArgs, this);
238
239 if (result == null) {
240 Session session = null;
241
242 try {
243 session = openSession();
244
245 String sql = CustomSQLUtil.get(FIND_BY_C_N);
246
247 SQLQuery q = session.createSQLQuery(sql);
248
249 q.addEntity("Group_", GroupImpl.class);
250
251 QueryPos qPos = QueryPos.getInstance(q);
252
253 qPos.add(companyId);
254 qPos.add(name);
255
256 Iterator<Group> itr = q.list().iterator();
257
258 if (itr.hasNext()) {
259 Group group = itr.next();
260
261 FinderCacheUtil.putResult(
262 finderClassNameCacheEnabled, finderClassName,
263 finderMethodName, finderParams, finderArgs, group);
264
265 return group;
266 }
267 }
268 catch (Exception e) {
269 throw new SystemException(e);
270 }
271 finally {
272 closeSession(session);
273 }
274
275 StringBuilder sb = new StringBuilder();
276
277 sb.append("No Group exists with the key {companyId=");
278 sb.append(companyId);
279 sb.append(", name=");
280 sb.append(name);
281 sb.append("}");
282
283 throw new NoSuchGroupException(sb.toString());
284 }
285 else {
286 return (Group)result;
287 }
288 }
289
290 public List<Group> findByC_N_D(
291 long companyId, String name, String description,
292 LinkedHashMap<String, Object> params, int start, int end,
293 OrderByComparator obc)
294 throws SystemException {
295
296 name = StringUtil.lowerCase(name);
297 description = StringUtil.lowerCase(description);
298
299 if (params == null) {
300 params = new LinkedHashMap<String, Object>();
301 }
302
303 Long userId = (Long)params.get("usersGroups");
304
305 LinkedHashMap<String, Object> params1 = params;
306
307 LinkedHashMap<String, Object> params2 =
308 new LinkedHashMap<String, Object>();
309
310 params2.putAll(params1);
311
312 if (userId != null) {
313 params2.remove("usersGroups");
314 params2.put("groupsOrgs", userId);
315 }
316
317 LinkedHashMap<String, Object> params3 =
318 new LinkedHashMap<String, Object>();
319
320 params3.putAll(params1);
321
322 if (userId != null) {
323 params3.remove("usersGroups");
324 params3.put("groupsUserGroups", userId);
325 }
326
327 StringBuilder sb = new StringBuilder();
328
329 sb.append("(");
330
331 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
332
333 String sql = sb.toString();
334
335 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params1));
336 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params1));
337
338 sb = new StringBuilder();
339
340 sb.append(sql);
341
342 sb.append(")");
343
344 if (Validator.isNotNull(userId)) {
345 sb.append(" UNION (");
346
347 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
348
349 sql = sb.toString();
350
351 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params2));
352 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params2));
353
354 sb = new StringBuilder();
355
356 sb.append(sql);
357
358 sb.append(") UNION (");
359
360 sb.append(CustomSQLUtil.get(FIND_BY_C_N_D));
361
362 sql = sb.toString();
363
364 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params3));
365 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params3));
366
367 sb = new StringBuilder();
368
369 sb.append(sql);
370
371 sb.append(")");
372 }
373
374 sql = sb.toString();
375 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
376
377 Session session = null;
378
379 try {
380 session = openSession();
381
382 SQLQuery q = session.createSQLQuery(sql);
383
384 q.addScalar("groupId", Type.STRING);
385
386 QueryPos qPos = QueryPos.getInstance(q);
387
388 setJoin(qPos, params1);
389 qPos.add(companyId);
390 qPos.add(name);
391 qPos.add(name);
392 qPos.add(description);
393 qPos.add(description);
394
395 if (Validator.isNotNull(userId)) {
396 setJoin(qPos, params2);
397 qPos.add(companyId);
398 qPos.add(name);
399 qPos.add(name);
400 qPos.add(description);
401 qPos.add(description);
402
403 setJoin(qPos, params3);
404 qPos.add(companyId);
405 qPos.add(name);
406 qPos.add(name);
407 qPos.add(description);
408 qPos.add(description);
409 }
410
411 List<Group> groups = new ArrayList<Group>();
412
413 Iterator<String> itr = (Iterator<String>)QueryUtil.iterate(
414 q, getDialect(), start, end);
415
416 while (itr.hasNext()) {
417 long groupId = GetterUtil.getLong(itr.next());
418
419 Group group = GroupUtil.findByPrimaryKey(groupId);
420
421 groups.add(group);
422 }
423
424 return groups;
425 }
426 catch (Exception e) {
427 throw new SystemException(e);
428 }
429 finally {
430 closeSession(session);
431 }
432 }
433
434 protected int countByGroupId(
435 Session session, long groupId, LinkedHashMap<String, Object> params) {
436
437 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
438
439 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
440 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
441
442 SQLQuery q = session.createSQLQuery(sql);
443
444 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
445
446 QueryPos qPos = QueryPos.getInstance(q);
447
448 setJoin(qPos, params);
449 qPos.add(groupId);
450
451 Iterator<Long> itr = q.list().iterator();
452
453 if (itr.hasNext()) {
454 Long count = itr.next();
455
456 if (count != null) {
457 return count.intValue();
458 }
459 }
460
461 return 0;
462 }
463
464 protected int countByC_N_D(
465 Session session, long companyId, String name, String description,
466 LinkedHashMap<String, Object> params) {
467
468 String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
469
470 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
471 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
472
473 SQLQuery q = session.createSQLQuery(sql);
474
475 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
476
477 QueryPos qPos = QueryPos.getInstance(q);
478
479 setJoin(qPos, params);
480 qPos.add(companyId);
481 qPos.add(name);
482 qPos.add(name);
483 qPos.add(description);
484 qPos.add(description);
485
486 Iterator<Long> itr = q.list().iterator();
487
488 if (itr.hasNext()) {
489 Long count = itr.next();
490
491 if (count != null) {
492 return count.intValue();
493 }
494 }
495
496 return 0;
497 }
498
499 protected String getJoin(LinkedHashMap<String, Object> params) {
500 if (params == null) {
501 return StringPool.BLANK;
502 }
503
504 StringBuilder sb = new StringBuilder();
505
506 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
507
508 while (itr.hasNext()) {
509 Map.Entry<String, Object> entry = itr.next();
510
511 String key = entry.getKey();
512 Object value = entry.getValue();
513
514 if (Validator.isNotNull(value)) {
515 sb.append(getJoin(key));
516 }
517 }
518
519 return sb.toString();
520 }
521
522 protected String getJoin(String key) {
523 String join = StringPool.BLANK;
524
525 if (key.equals("groupsOrgs")) {
526 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
527 }
528 else if (key.equals("groupsRoles")) {
529 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
530 }
531 else if (key.equals("groupsUserGroups")) {
532 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
533 }
534 else if (key.equals("layoutSet")) {
535 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
536 }
537 else if (key.equals("pageCount")) {
538 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
539 }
540 else if (key.equals("rolePermissions")) {
541 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
542 }
543 else if (key.equals("userGroupRole")) {
544 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
545 }
546 else if (key.equals("usersGroups")) {
547 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
548 }
549
550 if (Validator.isNotNull(join)) {
551 int pos = join.indexOf("WHERE");
552
553 if (pos != -1) {
554 join = join.substring(0, pos);
555 }
556 }
557
558 return join;
559 }
560
561 protected String getWhere(LinkedHashMap<String, Object> params) {
562 if (params == null) {
563 return StringPool.BLANK;
564 }
565
566 StringBuilder sb = new StringBuilder();
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(getWhere(key, value));
578 }
579 }
580
581 return sb.toString();
582 }
583
584 protected String getWhere(String key, Object value) {
585 String join = StringPool.BLANK;
586
587 if (key.equals("active")) {
588 join = CustomSQLUtil.get(JOIN_BY_ACTIVE);
589 }
590 else if (key.equals("creatorUserId")) {
591 join = CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID);
592 }
593 else if (key.equals("groupsOrgs")) {
594 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
595 }
596 else if (key.equals("groupsRoles")) {
597 join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
598 }
599 else if (key.equals("groupsUserGroups")) {
600 join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
601 }
602 else if (key.equals("layoutSet")) {
603 join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
604 }
605 else if (key.equals("pageCount")) {
606 join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
607 }
608 else if (key.equals("rolePermissions")) {
609 join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
610 }
611 else if (key.equals("type")) {
612 join = CustomSQLUtil.get(JOIN_BY_TYPE);
613 }
614 else if (key.equals("types")) {
615 List<Integer> types = (List<Integer>)value;
616
617 StringBuilder sb = new StringBuilder();
618
619 sb.append("WHERE (");
620
621 for (int i = 0; i < types.size(); i++) {
622 sb.append("(Group_.type_ = ?) ");
623
624 if ((i + 1) < types.size()) {
625 sb.append("OR ");
626 }
627 }
628
629 sb.append(")");
630
631 join = sb.toString();
632 }
633 else if (key.equals("userGroupRole")) {
634 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
635 }
636 else if (key.equals("usersGroups")) {
637 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
638 }
639
640 if (Validator.isNotNull(join)) {
641 int pos = join.indexOf("WHERE");
642
643 if (pos != -1) {
644 StringBuilder sb = new StringBuilder();
645
646 sb.append(join.substring(pos + 5, join.length()));
647 sb.append(" AND ");
648
649 join = sb.toString();
650 }
651 else {
652 join = StringPool.BLANK;
653 }
654 }
655
656 return join;
657 }
658
659 protected void setJoin(
660 QueryPos qPos, LinkedHashMap<String, Object> params) {
661
662 if (params != null) {
663 Iterator<Map.Entry<String, Object>> itr =
664 params.entrySet().iterator();
665
666 while (itr.hasNext()) {
667 Map.Entry<String, Object> entry = itr.next();
668
669 String key = entry.getKey();
670
671 if (key.equals("active") || key.equals("layoutSet")) {
672 Boolean value = (Boolean)entry.getValue();
673
674 qPos.add(value);
675 }
676 else if (key.equals("pageCount")) {
677 }
678 else if (key.equals("rolePermissions")) {
679 List<Object> values = (List<Object>)entry.getValue();
680
681 for (int i = 0; i < values.size(); i++) {
682 Object value = values.get(i);
683
684 if (value instanceof Integer) {
685 Integer valueInteger = (Integer)value;
686
687 qPos.add(valueInteger);
688 }
689 else if (value instanceof Long) {
690 Long valueLong = (Long)value;
691
692 qPos.add(valueLong);
693 }
694 else if (value instanceof String) {
695 String valueString = (String)value;
696
697 qPos.add(valueString);
698 }
699 }
700 }
701 else if (key.equals("types")) {
702 List<Integer> values = (List<Integer>)entry.getValue();
703
704 for (int i = 0; i < values.size(); i++) {
705 Integer value = values.get(i);
706
707 qPos.add(value);
708 }
709 }
710 else if (key.equals("userGroupRole")) {
711 List<Long> values = (List<Long>)entry.getValue();
712
713 Long userId = values.get(0);
714 Long roleId = values.get(1);
715
716 qPos.add(userId);
717 qPos.add(roleId);
718 }
719 else {
720 Object value = entry.getValue();
721
722 if (value instanceof Long) {
723 Long valueLong = (Long)value;
724
725 if (Validator.isNotNull(valueLong)) {
726 qPos.add(valueLong);
727 }
728 }
729 else if (value instanceof String) {
730 String valueString = (String)value;
731
732 if (Validator.isNotNull(valueString)) {
733 qPos.add(valueString);
734 }
735 }
736 }
737 }
738 }
739 }
740
741 }