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