1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.ArrayUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.model.Group;
35  import com.liferay.portal.model.LayoutSet;
36  import com.liferay.portal.model.Permission;
37  import com.liferay.portal.model.Resource;
38  import com.liferay.portal.model.ResourceCode;
39  import com.liferay.portal.model.UserGroupRole;
40  import com.liferay.portal.model.impl.GroupImpl;
41  import com.liferay.portal.model.impl.GroupModelImpl;
42  import com.liferay.portal.model.impl.LayoutSetModelImpl;
43  import com.liferay.portal.model.impl.PermissionModelImpl;
44  import com.liferay.portal.model.impl.ResourceCodeModelImpl;
45  import com.liferay.portal.model.impl.ResourceModelImpl;
46  import com.liferay.portal.model.impl.RoleModelImpl;
47  import com.liferay.portal.model.impl.UserGroupRoleModelImpl;
48  import com.liferay.portal.model.impl.UserModelImpl;
49  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
50  import com.liferay.portal.spring.hibernate.FinderCache;
51  import com.liferay.portal.spring.hibernate.HibernateUtil;
52  import com.liferay.util.dao.hibernate.QueryPos;
53  import com.liferay.util.dao.hibernate.QueryUtil;
54  
55  import java.util.ArrayList;
56  import java.util.Iterator;
57  import java.util.LinkedHashMap;
58  import java.util.List;
59  import java.util.Map;
60  
61  import org.hibernate.Hibernate;
62  import org.hibernate.SQLQuery;
63  import org.hibernate.Session;
64  
65  /**
66   * <a href="GroupFinderImpl.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Brian Wing Shun Chan
69   *
70   */
71  public class GroupFinderImpl implements GroupFinder {
72  
73      public static String COUNT_BY_GROUP_ID =
74          GroupFinder.class.getName() + ".countByGroupId";
75  
76      public static String COUNT_BY_C_N_D =
77          GroupFinder.class.getName() + ".countByC_N_D";
78  
79      public static String FIND_BY_NULL_FRIENDLY_URL =
80          GroupFinder.class.getName() + ".findByNullFriendlyURL";
81  
82      public static String FIND_BY_C_N =
83          GroupFinder.class.getName() + ".findByC_N";
84  
85      public static String FIND_BY_C_N_D =
86          GroupFinder.class.getName() + ".findByC_N_D";
87  
88      public static String JOIN_BY_ACTIVE =
89          GroupFinder.class.getName() + ".joinByActive";
90  
91      public static String JOIN_BY_CREATOR_USER_ID =
92          GroupFinder.class.getName() + ".joinByCreatorUserId";
93  
94      public static String JOIN_BY_GROUPS_ORGS =
95          GroupFinder.class.getName() + ".joinByGroupsOrgs";
96  
97      public static String JOIN_BY_GROUPS_ROLES =
98          GroupFinder.class.getName() + ".joinByGroupsRoles";
99  
100     public static String JOIN_BY_GROUPS_USER_GROUPS =
101         GroupFinder.class.getName() + ".joinByGroupsUserGroups";
102 
103     public static String JOIN_BY_LAYOUT_SET =
104         GroupFinder.class.getName() + ".joinByLayoutSet";
105 
106     public static String JOIN_BY_PAGE_COUNT =
107         GroupFinder.class.getName() + ".joinByPageCount";
108 
109     public static String JOIN_BY_ROLE_PERMISSIONS =
110         GroupFinder.class.getName() + ".joinByRolePermissions";
111 
112     public static String JOIN_BY_TYPE =
113         GroupFinder.class.getName() + ".joinByType";
114 
115     public static String JOIN_BY_USER_GROUP_ROLE =
116         GroupFinder.class.getName() + ".joinByUserGroupRole";
117 
118     public static String JOIN_BY_USERS_GROUPS =
119         GroupFinder.class.getName() + ".joinByUsersGroups";
120 
121     public int countByG_U(long groupId, long userId) throws SystemException {
122         String finderSQL = Group.class.getName();
123         boolean[] finderClassNamesCacheEnabled = new boolean[] {
124             GroupModelImpl.CACHE_ENABLED,
125             GroupModelImpl.CACHE_ENABLED_GROUPS_ORGS,
126             GroupModelImpl.CACHE_ENABLED_GROUPS_USERGROUPS,
127             UserModelImpl.CACHE_ENABLED_USERS_GROUPS,
128             UserModelImpl.CACHE_ENABLED_USERS_ORGS,
129             UserModelImpl.CACHE_ENABLED_USERS_USERGROUPS
130         };
131         String[] finderClassNames = new String[] {
132             Group.class.getName(), "Groups_Orgs", "Groups_UserGroups",
133             "Users_Groups", "Users_Orgs", "Users_UserGroups"
134         };
135         String finderMethodName = "customCountByG_U";
136         String finderParams[] = new String[] {
137             Long.class.getName(), Long.class.getName()
138         };
139         Object finderArgs[] = new Object[] {groupId, userId};
140 
141         Object result = null;
142 
143         if (!ArrayUtil.contains(finderClassNamesCacheEnabled, false)) {
144             result = FinderCache.getResult(
145                 finderSQL, finderClassNames, finderMethodName, finderParams,
146                 finderArgs);
147         }
148 
149         if (result == null) {
150             LinkedHashMap<String, Object> params1 =
151                 new LinkedHashMap<String, Object>();
152 
153             params1.put("usersGroups", userId);
154 
155             LinkedHashMap<String, Object> params2 =
156                 new LinkedHashMap<String, Object>();
157 
158             params2.put("groupsOrgs", userId);
159 
160             LinkedHashMap<String, Object> params3 =
161                 new LinkedHashMap<String, Object>();
162 
163             params3.put("groupsUserGroups", userId);
164 
165             Session session = null;
166 
167             try {
168                 session = HibernateUtil.openSession();
169 
170                 int count = countByGroupId(session, groupId, params1);
171                 count += countByGroupId(session, groupId, params2);
172                 count += countByGroupId(session, groupId, params3);
173 
174                 FinderCache.putResult(
175                     finderSQL, finderClassNamesCacheEnabled, finderClassNames,
176                     finderMethodName, finderParams, finderArgs,
177                     new Long(count));
178 
179                 return count;
180             }
181             catch (Exception e) {
182                 throw new SystemException(e);
183             }
184             finally {
185                 HibernateUtil.closeSession(session);
186             }
187         }
188         else {
189             return ((Long)result).intValue();
190         }
191     }
192 
193     public int countByC_N_D(
194             long companyId, String name, String description,
195             LinkedHashMap<String, Object> params)
196         throws SystemException {
197 
198         name = StringUtil.lowerCase(name);
199         description = StringUtil.lowerCase(description);
200 
201         if (params == null) {
202             params = new LinkedHashMap<String, Object>();
203         }
204 
205         Long userId = (Long)params.get("usersGroups");
206 
207         LinkedHashMap<String, Object> params1 = params;
208 
209         LinkedHashMap<String, Object> params2 =
210             new LinkedHashMap<String, Object>();
211 
212         params2.putAll(params1);
213 
214         if (userId != null) {
215             params2.remove("usersGroups");
216             params2.put("groupsOrgs", userId);
217         }
218 
219         LinkedHashMap<String, Object> params3 =
220             new LinkedHashMap<String, Object>();
221 
222         params3.putAll(params1);
223 
224         if (userId != null) {
225             params3.remove("usersGroups");
226             params3.put("groupsUserGroups", userId);
227         }
228 
229         Session session = null;
230 
231         try {
232             session = HibernateUtil.openSession();
233 
234             int count = countByC_N_D(
235                 session, companyId, name, description, params1);
236 
237             if (Validator.isNotNull(userId)) {
238                 count += countByC_N_D(
239                     session, companyId, name, description, params2);
240 
241                 count += countByC_N_D(
242                     session, companyId, name, description, params3);
243             }
244 
245             return count;
246         }
247         catch (Exception e) {
248             throw new SystemException(e);
249         }
250         finally {
251             HibernateUtil.closeSession(session);
252         }
253     }
254 
255     public List<Group> findByNullFriendlyURL() throws SystemException {
256         Session session = null;
257 
258         try {
259             session = HibernateUtil.openSession();
260 
261             String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
262 
263             SQLQuery q = session.createSQLQuery(sql);
264 
265             q.addEntity("Group_", GroupImpl.class);
266 
267             return q.list();
268         }
269         catch (Exception e) {
270             throw new SystemException(e);
271         }
272         finally {
273             HibernateUtil.closeSession(session);
274         }
275     }
276 
277     public Group findByC_N(long companyId, String name)
278         throws NoSuchGroupException, SystemException {
279 
280         name = StringUtil.lowerCase(name);
281 
282         boolean finderClassNameCacheEnabled = GroupModelImpl.CACHE_ENABLED;
283         String finderClassName = Group.class.getName();
284         String finderMethodName = "customFindByC_N";
285         String finderParams[] = new String[] {
286             Long.class.getName(), String.class.getName()
287         };
288         Object finderArgs[] = new Object[] {companyId, name};
289 
290         Object result = FinderCache.getResult(
291             finderClassName, finderMethodName, finderParams, finderArgs);
292 
293         if (result == null) {
294             Session session = null;
295 
296             try {
297                 session = HibernateUtil.openSession();
298 
299                 String sql = CustomSQLUtil.get(FIND_BY_C_N);
300 
301                 SQLQuery q = session.createSQLQuery(sql);
302 
303                 q.addEntity("Group_", GroupImpl.class);
304 
305                 QueryPos qPos = QueryPos.getInstance(q);
306 
307                 qPos.add(companyId);
308                 qPos.add(name);
309 
310                 Iterator<Group> itr = q.list().iterator();
311 
312                 if (itr.hasNext()) {
313                     Group group = itr.next();
314 
315                     FinderCache.putResult(
316                         finderClassNameCacheEnabled, finderClassName,
317                         finderMethodName, finderParams, finderArgs, group);
318 
319                     return group;
320                 }
321             }
322             catch (Exception e) {
323                 throw new SystemException(e);
324             }
325             finally {
326                 HibernateUtil.closeSession(session);
327             }
328 
329             StringMaker sm = new StringMaker();
330 
331             sm.append("No Group exists with the key {companyId=");
332             sm.append(companyId);
333             sm.append(", name=");
334             sm.append(name);
335             sm.append("}");
336 
337             throw new NoSuchGroupException(sm.toString());
338         }
339         else {
340             return (Group)result;
341         }
342     }
343 
344     public List<Group> findByC_N_D(
345             long companyId, String name, String description,
346             LinkedHashMap<String, Object> params, int begin, int end,
347             OrderByComparator obc)
348         throws SystemException {
349 
350         name = StringUtil.lowerCase(name);
351         description = StringUtil.lowerCase(description);
352 
353         if (params == null) {
354             params = new LinkedHashMap<String, Object>();
355         }
356 
357         Long userId = (Long)params.get("usersGroups");
358 
359         LinkedHashMap<String, Object> params1 = params;
360 
361         LinkedHashMap<String, Object> params2 =
362             new LinkedHashMap<String, Object>();
363 
364         params2.putAll(params1);
365 
366         if (userId != null) {
367             params2.remove("usersGroups");
368             params2.put("groupsOrgs", userId);
369         }
370 
371         LinkedHashMap<String, Object> params3 =
372             new LinkedHashMap<String, Object>();
373 
374         params3.putAll(params1);
375 
376         if (userId != null) {
377             params3.remove("usersGroups");
378             params3.put("groupsUserGroups", userId);
379         }
380 
381         StringMaker sm = new StringMaker();
382 
383         sm.append("(");
384 
385         sm.append(CustomSQLUtil.get(FIND_BY_C_N_D));
386 
387         String sql = sm.toString();
388 
389         sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params1));
390         sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params1));
391 
392         sm = new StringMaker();
393 
394         sm.append(sql);
395 
396         sm.append(")");
397 
398         if (Validator.isNotNull(userId)) {
399             sm.append(" UNION (");
400 
401             sm.append(CustomSQLUtil.get(FIND_BY_C_N_D));
402 
403             sql = sm.toString();
404 
405             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params2));
406             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params2));
407 
408             sm = new StringMaker();
409 
410             sm.append(sql);
411 
412             sm.append(") UNION (");
413 
414             sm.append(CustomSQLUtil.get(FIND_BY_C_N_D));
415 
416             sql = sm.toString();
417 
418             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params3));
419             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params3));
420 
421             sm = new StringMaker();
422 
423             sm.append(sql);
424 
425             sm.append(")");
426         }
427 
428         sql = sm.toString();
429         sql = CustomSQLUtil.replaceOrderBy(sql, obc);
430 
431         String finderSQL = sql;
432         boolean[] finderClassNamesCacheEnabled = new boolean[] {
433             GroupModelImpl.CACHE_ENABLED, LayoutSetModelImpl.CACHE_ENABLED,
434             PermissionModelImpl.CACHE_ENABLED, ResourceModelImpl.CACHE_ENABLED,
435             ResourceCodeModelImpl.CACHE_ENABLED,
436             UserGroupRoleModelImpl.CACHE_ENABLED,
437             GroupModelImpl.CACHE_ENABLED_GROUPS_ORGS,
438             GroupModelImpl.CACHE_ENABLED_GROUPS_ROLES,
439             GroupModelImpl.CACHE_ENABLED_GROUPS_USERGROUPS,
440             RoleModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS,
441             UserModelImpl.CACHE_ENABLED_USERS_GROUPS,
442             UserModelImpl.CACHE_ENABLED_USERS_ORGS,
443             UserModelImpl.CACHE_ENABLED_USERS_USERGROUPS
444         };
445         String[] finderClassNames = new String[] {
446             Group.class.getName(), LayoutSet.class.getName(),
447             Permission.class.getName(), Resource.class.getName(),
448             ResourceCode.class.getName(), UserGroupRole.class.getName(),
449             "Groups_Orgs", "Groups_Roles", "Groups_UserGroups",
450             "Roles_Permissions", "Users_Groups", "Users_Orgs",
451             "Users_UserGroups"
452         };
453         String finderMethodName = "customFindByC_N_D";
454         String finderParams[] = new String[] {
455             Long.class.getName(), String.class.getName(),
456             String.class.getName(), LinkedHashMap.class.getName(),
457             String.class.getName(), String.class.getName()
458         };
459         Object finderArgs[] = new Object[] {
460             companyId, name, description, params.toString(),
461             String.valueOf(begin), String.valueOf(end)
462         };
463 
464         Object result = null;
465 
466         if (!ArrayUtil.contains(finderClassNamesCacheEnabled, false)) {
467             result = FinderCache.getResult(
468                 finderSQL, finderClassNames, finderMethodName, finderParams,
469                 finderArgs);
470         }
471 
472         if (result == null) {
473             Session session = null;
474 
475             try {
476                 session = HibernateUtil.openSession();
477 
478                 SQLQuery q = session.createSQLQuery(sql);
479 
480                 q.addScalar("groupId", Hibernate.STRING);
481 
482                 QueryPos qPos = QueryPos.getInstance(q);
483 
484                 setJoin(qPos, params1);
485                 qPos.add(companyId);
486                 qPos.add(name);
487                 qPos.add(name);
488                 qPos.add(description);
489                 qPos.add(description);
490 
491                 if (Validator.isNotNull(userId)) {
492                     setJoin(qPos, params2);
493                     qPos.add(companyId);
494                     qPos.add(name);
495                     qPos.add(name);
496                     qPos.add(description);
497                     qPos.add(description);
498 
499                     setJoin(qPos, params3);
500                     qPos.add(companyId);
501                     qPos.add(name);
502                     qPos.add(name);
503                     qPos.add(description);
504                     qPos.add(description);
505                 }
506 
507                 List<Group> groups = new ArrayList<Group>();
508 
509                 Iterator<String> itr = (Iterator<String>)QueryUtil.iterate(
510                     q, HibernateUtil.getDialect(), begin, end);
511 
512                 while (itr.hasNext()) {
513                     long groupId = GetterUtil.getLong(itr.next());
514 
515                     Group group = GroupUtil.findByPrimaryKey(groupId);
516 
517                     groups.add(group);
518                 }
519 
520                 FinderCache.putResult(
521                     finderSQL, finderClassNamesCacheEnabled, finderClassNames,
522                     finderMethodName, finderParams, finderArgs, groups);
523 
524                 return groups;
525             }
526             catch (Exception e) {
527                 throw new SystemException(e);
528             }
529             finally {
530                 HibernateUtil.closeSession(session);
531             }
532         }
533         else {
534             return (List<Group>)result;
535         }
536     }
537 
538     protected int countByGroupId(
539             Session session, long groupId, LinkedHashMap<String, Object> params)
540         throws SystemException {
541 
542         String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
543 
544         sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
545         sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
546 
547         SQLQuery q = session.createSQLQuery(sql);
548 
549         q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
550 
551         QueryPos qPos = QueryPos.getInstance(q);
552 
553         setJoin(qPos, params);
554         qPos.add(groupId);
555 
556         Iterator<Long> itr = q.list().iterator();
557 
558         if (itr.hasNext()) {
559             Long count = itr.next();
560 
561             if (count != null) {
562                 return count.intValue();
563             }
564         }
565 
566         return 0;
567     }
568 
569     protected int countByC_N_D(
570             Session session, long companyId, String name, String description,
571             LinkedHashMap<String, Object> params)
572         throws SystemException {
573 
574         String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
575 
576         sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
577         sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
578 
579         SQLQuery q = session.createSQLQuery(sql);
580 
581         q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
582 
583         QueryPos qPos = QueryPos.getInstance(q);
584 
585         setJoin(qPos, params);
586         qPos.add(companyId);
587         qPos.add(name);
588         qPos.add(name);
589         qPos.add(description);
590         qPos.add(description);
591 
592         Iterator<Long> itr = q.list().iterator();
593 
594         if (itr.hasNext()) {
595             Long count = itr.next();
596 
597             if (count != null) {
598                 return count.intValue();
599             }
600         }
601 
602         return 0;
603     }
604 
605     protected String getJoin(LinkedHashMap<String, Object> params) {
606         if (params == null) {
607             return StringPool.BLANK;
608         }
609 
610         StringMaker sm = new StringMaker();
611 
612         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
613 
614         while (itr.hasNext()) {
615             Map.Entry<String, Object> entry = itr.next();
616 
617             String key = entry.getKey();
618             Object value = entry.getValue();
619 
620             if (Validator.isNotNull(value)) {
621                 sm.append(getJoin(key));
622             }
623         }
624 
625         return sm.toString();
626     }
627 
628     protected String getJoin(String key) {
629         String join = StringPool.BLANK;
630 
631         if (key.equals("groupsOrgs")) {
632             join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
633         }
634         else if (key.equals("groupsRoles")) {
635             join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
636         }
637         else if (key.equals("groupsUserGroups")) {
638             join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
639         }
640         else if (key.equals("layoutSet")) {
641             join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
642         }
643         else if (key.equals("pageCount")) {
644             join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
645         }
646         else if (key.equals("rolePermissions")) {
647             join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
648         }
649         else if (key.equals("userGroupRole")) {
650             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
651         }
652         else if (key.equals("usersGroups")) {
653             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
654         }
655 
656         if (Validator.isNotNull(join)) {
657             int pos = join.indexOf("WHERE");
658 
659             if (pos != -1) {
660                 join = join.substring(0, pos);
661             }
662         }
663 
664         return join;
665     }
666 
667     protected String getWhere(LinkedHashMap<String, Object> params) {
668         if (params == null) {
669             return StringPool.BLANK;
670         }
671 
672         StringMaker sm = new StringMaker();
673 
674         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
675 
676         while (itr.hasNext()) {
677             Map.Entry<String, Object> entry = itr.next();
678 
679             String key = entry.getKey();
680             Object value = entry.getValue();
681 
682             if (Validator.isNotNull(value)) {
683                 sm.append(getWhere(key, value));
684             }
685         }
686 
687         return sm.toString();
688     }
689 
690     protected String getWhere(String key, Object value) {
691         String join = StringPool.BLANK;
692 
693         if (key.equals("active")) {
694             join = CustomSQLUtil.get(JOIN_BY_ACTIVE);
695         }
696         else if (key.equals("creatorUserId")) {
697             join = CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID);
698         }
699         else if (key.equals("groupsOrgs")) {
700             join = CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS);
701         }
702         else if (key.equals("groupsRoles")) {
703             join = CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES);
704         }
705         else if (key.equals("groupsUserGroups")) {
706             join = CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS);
707         }
708         else if (key.equals("layoutSet")) {
709             join = CustomSQLUtil.get(JOIN_BY_LAYOUT_SET);
710         }
711         else if (key.equals("pageCount")) {
712             join = CustomSQLUtil.get(JOIN_BY_PAGE_COUNT);
713         }
714         else if (key.equals("rolePermissions")) {
715             join = CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS);
716         }
717         else if (key.equals("type")) {
718             join = CustomSQLUtil.get(JOIN_BY_TYPE);
719         }
720         else if (key.equals("types")) {
721             List<Integer> types = (List<Integer>)value;
722 
723             StringMaker sm = new StringMaker();
724 
725             sm.append("WHERE (");
726 
727             for (int i = 0; i < types.size(); i++) {
728                 sm.append("(Group_.type_ = ?) ");
729 
730                 if ((i + 1) < types.size()) {
731                     sm.append("OR ");
732                 }
733             }
734 
735             sm.append(")");
736 
737             join = sm.toString();
738         }
739         else if (key.equals("userGroupRole")) {
740             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
741         }
742         else if (key.equals("usersGroups")) {
743             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
744         }
745 
746         if (Validator.isNotNull(join)) {
747             int pos = join.indexOf("WHERE");
748 
749             if (pos != -1) {
750                 StringMaker sm = new StringMaker();
751 
752                 sm.append(join.substring(pos + 5, join.length()));
753                 sm.append(" AND ");
754 
755                 join = sm.toString();
756             }
757         }
758 
759         return join;
760     }
761 
762     protected void setJoin(
763         QueryPos qPos, LinkedHashMap<String, Object> params) {
764 
765         if (params != null) {
766             Iterator<Map.Entry<String, Object>> itr =
767                 params.entrySet().iterator();
768 
769             while (itr.hasNext()) {
770                 Map.Entry<String, Object> entry = itr.next();
771 
772                 String key = entry.getKey();
773 
774                 if (key.equals("active") || key.equals("layoutSet")) {
775                     Boolean value = (Boolean)entry.getValue();
776 
777                     qPos.add(value);
778                 }
779                 else if (key.equals("pageCount")) {
780                 }
781                 else if (key.equals("rolePermissions")) {
782                     List<Object> values = (List<Object>)entry.getValue();
783 
784                     for (int i = 0; i < values.size(); i++) {
785                         Object value = values.get(i);
786 
787                         if (value instanceof Integer) {
788                             Integer valueInteger = (Integer)value;
789 
790                             qPos.add(valueInteger);
791                         }
792                         else if (value instanceof Long) {
793                             Long valueLong = (Long)value;
794 
795                             qPos.add(valueLong);
796                         }
797                         else if (value instanceof String) {
798                             String valueString = (String)value;
799 
800                             qPos.add(valueString);
801                         }
802                     }
803                 }
804                 else if (key.equals("types")) {
805                     List<Integer> values = (List<Integer>)entry.getValue();
806 
807                     for (int i = 0; i < values.size(); i++) {
808                         Integer value = values.get(i);
809 
810                         qPos.add(value);
811                     }
812                 }
813                 else if (key.equals("userGroupRole")) {
814                     List<Long> values = (List<Long>)entry.getValue();
815 
816                     Long userId = values.get(0);
817                     Long roleId = values.get(1);
818 
819                     qPos.add(userId);
820                     qPos.add(roleId);
821                 }
822                 else {
823                     Object value = entry.getValue();
824 
825                     if (value instanceof Long) {
826                         Long valueLong = (Long)value;
827 
828                         if (Validator.isNotNull(valueLong)) {
829                             qPos.add(valueLong);
830                         }
831                     }
832                     else if (value instanceof String) {
833                         String valueString = (String)value;
834 
835                         if (Validator.isNotNull(valueString)) {
836                             qPos.add(valueString);
837                         }
838                     }
839                 }
840             }
841         }
842     }
843 
844 }