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