001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.ResourceAction;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.impl.RoleImpl;
033    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
034    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.ArrayList;
038    import java.util.HashMap;
039    import java.util.Iterator;
040    import java.util.LinkedHashMap;
041    import java.util.List;
042    import java.util.Map;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Marcellus Tavares
047     * @author Connor McKay
048     */
049    public class RoleFinderImpl
050            extends BasePersistenceImpl<Role> implements RoleFinder {
051    
052            public static final String COUNT_BY_ORGANIZATION =
053                    RoleFinder.class.getName() + ".countByOrganization";
054    
055            public static final String COUNT_BY_ORGANIZATION_SITE =
056                    RoleFinder.class.getName() + ".countByOrganizationSite";
057    
058            public static final String COUNT_BY_SITE =
059                    RoleFinder.class.getName() + ".countBySite";
060    
061            public static final String COUNT_BY_USER =
062                    RoleFinder.class.getName() + ".countByUser";
063    
064            public static final String COUNT_BY_USER_GROUP =
065                    RoleFinder.class.getName() + ".countByUserGroup";
066    
067            public static final String COUNT_BY_USER_GROUP_SITE =
068                    RoleFinder.class.getName() + ".countByUserGroupSite";
069    
070            public static final String COUNT_BY_U_G_R =
071                    RoleFinder.class.getName() + ".countByU_G_R";
072    
073            public static final String COUNT_BY_C_N_D_T =
074                    RoleFinder.class.getName() + ".countByC_N_D_T";
075    
076            public static final String FIND_BY_SYSTEM =
077                    RoleFinder.class.getName() + ".findBySystem";
078    
079            public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
080                    RoleFinder.class.getName() + ".findByUserGroupGroupRole";
081    
082            public static final String FIND_BY_USER_GROUP_ROLE =
083                    RoleFinder.class.getName() + ".findByUserGroupRole";
084    
085            public static final String FIND_BY_C_N =
086                    RoleFinder.class.getName() + ".findByC_N";
087    
088            public static final String FIND_BY_U_G =
089                    RoleFinder.class.getName() + ".findByU_G";
090    
091            public static final String FIND_BY_R_N_A =
092                    RoleFinder.class.getName() + ".findByR_N_A";
093    
094            public static final String FIND_BY_C_N_D_T =
095                    RoleFinder.class.getName() + ".findByC_N_D_T";
096    
097            public static final String FIND_BY_C_N_S_P =
098                    RoleFinder.class.getName() + ".findByC_N_S_P";
099    
100            public static final String FIND_BY_C_N_S_P_A =
101                    RoleFinder.class.getName() + ".findByC_N_S_P_A";
102    
103            public static final String JOIN_BY_USERS_ROLES =
104                    RoleFinder.class.getName() + ".joinByUsersRoles";
105    
106            @Override
107            public int countByR_U(long roleId, long userId) throws SystemException {
108                    Session session = null;
109    
110                    try {
111                            session = openSession();
112    
113                            SQLQuery q = session.createSQLQuery(getCountByR_U_SQL());
114    
115                            QueryPos qPos = QueryPos.getInstance(q);
116    
117                            for (int i = 0; i < 6; i++) {
118                                    qPos.add(roleId);
119                                    qPos.add(userId);
120                            }
121    
122                            return q.list().size();
123                    }
124                    catch (Exception e) {
125                            throw new SystemException(e);
126                    }
127                    finally {
128                            closeSession(session);
129                    }
130            }
131    
132            @Override
133            public int countByU_G_R(long userId, long groupId, long roleId)
134                    throws SystemException {
135    
136                    Session session = null;
137    
138                    try {
139                            session = openSession();
140    
141                            String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
142    
143                            SQLQuery q = session.createSQLQuery(sql);
144    
145                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
146    
147                            QueryPos qPos = QueryPos.getInstance(q);
148    
149                            qPos.add(roleId);
150                            qPos.add(groupId);
151                            qPos.add(userId);
152    
153                            Iterator<Long> itr = q.iterate();
154    
155                            if (itr.hasNext()) {
156                                    Long count = itr.next();
157    
158                                    if (count != null) {
159                                            return count.intValue();
160                                    }
161                            }
162    
163                            return 0;
164                    }
165                    catch (Exception e) {
166                            throw new SystemException(e);
167                    }
168                    finally {
169                            closeSession(session);
170                    }
171            }
172    
173            @Override
174            public int countByC_N_D_T(
175                            long companyId, String name, String description, Integer[] types,
176                            LinkedHashMap<String, Object> params, boolean andOperator)
177                    throws SystemException {
178    
179                    String[] names = CustomSQLUtil.keywords(name);
180                    String[] descriptions = CustomSQLUtil.keywords(description);
181    
182                    return countByC_N_D_T(
183                            companyId, names, descriptions, types, params, andOperator);
184            }
185    
186            @Override
187            public int countByC_N_D_T(
188                            long companyId, String[] names, String[] descriptions,
189                            Integer[] types, LinkedHashMap<String, Object> params,
190                            boolean andOperator)
191                    throws SystemException {
192    
193                    names = CustomSQLUtil.keywords(names, true);
194                    descriptions = CustomSQLUtil.keywords(descriptions, true);
195    
196                    if (types == null) {
197                            types = new Integer[0];
198                    }
199    
200                    Session session = null;
201    
202                    try {
203                            session = openSession();
204    
205                            String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
206    
207                            sql = CustomSQLUtil.replaceKeywords(
208                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
209                            sql = CustomSQLUtil.replaceKeywords(
210                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
211                                    descriptions);
212                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
213                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
214                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
215                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
216    
217                            SQLQuery q = session.createSQLQuery(sql);
218    
219                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
220    
221                            QueryPos qPos = QueryPos.getInstance(q);
222    
223                            setJoin(qPos, params);
224    
225                            qPos.add(companyId);
226                            qPos.add(names, 2);
227                            qPos.add(descriptions, 2);
228                            qPos.add(types);
229    
230                            Iterator<Long> itr = q.iterate();
231    
232                            if (itr.hasNext()) {
233                                    Long count = itr.next();
234    
235                                    if (count != null) {
236                                            return count.intValue();
237                                    }
238                            }
239    
240                            return 0;
241                    }
242                    catch (Exception e) {
243                            throw new SystemException(e);
244                    }
245                    finally {
246                            closeSession(session);
247                    }
248            }
249    
250            @Override
251            public int countByKeywords(long companyId, String keywords, Integer[] types)
252                    throws SystemException {
253    
254                    return countByKeywords(
255                            companyId, keywords, types, new LinkedHashMap<String, Object>());
256            }
257    
258            @Override
259            public int countByKeywords(
260                            long companyId, String keywords, Integer[] types,
261                            LinkedHashMap<String, Object> params)
262                    throws SystemException {
263    
264                    String[] names = null;
265                    String[] descriptions = null;
266                    boolean andOperator = false;
267    
268                    if (Validator.isNotNull(keywords)) {
269                            names = CustomSQLUtil.keywords(keywords);
270                            descriptions = CustomSQLUtil.keywords(keywords);
271                    }
272                    else {
273                            andOperator = true;
274                    }
275    
276                    return countByC_N_D_T(
277                            companyId, names, descriptions, types, params, andOperator);
278            }
279    
280            @Override
281            public List<Role> findBySystem(long companyId) throws SystemException {
282                    Session session = null;
283    
284                    try {
285                            session = openSession();
286    
287                            String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
288    
289                            SQLQuery q = session.createSQLQuery(sql);
290    
291                            q.addEntity("Role_", RoleImpl.class);
292    
293                            QueryPos qPos = QueryPos.getInstance(q);
294    
295                            qPos.add(companyId);
296    
297                            return q.list(true);
298                    }
299                    catch (Exception e) {
300                            throw new SystemException(e);
301                    }
302                    finally {
303                            closeSession(session);
304                    }
305            }
306    
307            @Override
308            public List<Role> findByUserGroupGroupRole(long userId, long groupId)
309                    throws SystemException {
310    
311                    Session session = null;
312    
313                    try {
314                            session = openSession();
315    
316                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
317    
318                            SQLQuery q = session.createSQLQuery(sql);
319    
320                            q.addEntity("Role_", RoleImpl.class);
321    
322                            QueryPos qPos = QueryPos.getInstance(q);
323    
324                            qPos.add(userId);
325                            qPos.add(groupId);
326    
327                            return q.list(true);
328                    }
329                    catch (Exception e) {
330                            throw new SystemException(e);
331                    }
332                    finally {
333                            closeSession(session);
334                    }
335            }
336    
337            @Override
338            public List<Role> findByUserGroupRole(long userId, long groupId)
339                    throws SystemException {
340    
341                    Session session = null;
342    
343                    try {
344                            session = openSession();
345    
346                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
347    
348                            SQLQuery q = session.createSQLQuery(sql);
349    
350                            q.addEntity("Role_", RoleImpl.class);
351    
352                            QueryPos qPos = QueryPos.getInstance(q);
353    
354                            qPos.add(userId);
355                            qPos.add(groupId);
356    
357                            return q.list(true);
358                    }
359                    catch (Exception e) {
360                            throw new SystemException(e);
361                    }
362                    finally {
363                            closeSession(session);
364                    }
365            }
366    
367            @Override
368            public Role findByC_N(long companyId, String name)
369                    throws NoSuchRoleException, SystemException {
370    
371                    name = StringUtil.lowerCase(name);
372    
373                    Session session = null;
374    
375                    try {
376                            session = openSession();
377    
378                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
379    
380                            SQLQuery q = session.createSQLQuery(sql);
381    
382                            q.addEntity("Role_", RoleImpl.class);
383    
384                            QueryPos qPos = QueryPos.getInstance(q);
385    
386                            qPos.add(companyId);
387                            qPos.add(name);
388    
389                            List<Role> roles = q.list();
390    
391                            if (!roles.isEmpty()) {
392                                    return roles.get(0);
393                            }
394                    }
395                    catch (Exception e) {
396                            throw new SystemException(e);
397                    }
398                    finally {
399                            closeSession(session);
400                    }
401    
402                    StringBundler sb = new StringBundler(5);
403    
404                    sb.append("No Role exists with the key {companyId=");
405                    sb.append(companyId);
406                    sb.append(", name=");
407                    sb.append(name);
408                    sb.append("}");
409    
410                    throw new NoSuchRoleException(sb.toString());
411            }
412    
413            @Override
414            public List<Role> findByU_G(long userId, List<Group> groups)
415                    throws SystemException {
416    
417                    long[] groupIds = new long[groups.size()];
418    
419                    for (int i = 0; i < groups.size(); i++) {
420                            Group group = groups.get(i);
421    
422                            groupIds[i] = group.getGroupId();
423                    }
424    
425                    return findByU_G(userId, groupIds);
426            }
427    
428            @Override
429            public List<Role> findByU_G(long userId, long groupId)
430                    throws SystemException {
431    
432                    return findByU_G(userId, new long[] {groupId});
433            }
434    
435            @Override
436            public List<Role> findByU_G(long userId, long[] groupIds)
437                    throws SystemException {
438    
439                    Session session = null;
440    
441                    try {
442                            session = openSession();
443    
444                            String sql = CustomSQLUtil.get(FIND_BY_U_G);
445    
446                            sql = StringUtil.replace(
447                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
448    
449                            SQLQuery q = session.createSQLQuery(sql);
450    
451                            q.addEntity("Role_", RoleImpl.class);
452    
453                            QueryPos qPos = QueryPos.getInstance(q);
454    
455                            qPos.add(userId);
456                            qPos.add(groupIds);
457    
458                            return q.list(true);
459                    }
460                    catch (Exception e) {
461                            throw new SystemException(e);
462                    }
463                    finally {
464                            closeSession(session);
465                    }
466            }
467    
468            @Override
469            public List<Role> findByR_N_A(
470                            long resourceBlockId, String className, String actionId)
471                    throws SystemException {
472    
473                    Session session = null;
474    
475                    try {
476                            session = openSession();
477    
478                            String sql = CustomSQLUtil.get(FIND_BY_R_N_A);
479    
480                            SQLQuery q = session.createSQLQuery(sql);
481    
482                            q.addEntity("Role_", RoleImpl.class);
483    
484                            QueryPos qPos = QueryPos.getInstance(q);
485    
486                            qPos.add(resourceBlockId);
487                            qPos.add(className);
488    
489                            ResourceAction resourceAction =
490                                    ResourceActionLocalServiceUtil.getResourceAction(
491                                            className, actionId);
492    
493                            qPos.add(resourceAction.getBitwiseValue());
494    
495                            return q.list(true);
496                    }
497                    catch (Exception e) {
498                            throw new SystemException(e);
499                    }
500                    finally {
501                            closeSession(session);
502                    }
503            }
504    
505            @Override
506            public List<Role> findByC_N_D_T(
507                            long companyId, String name, String description, Integer[] types,
508                            LinkedHashMap<String, Object> params, boolean andOperator,
509                            int start, int end, OrderByComparator obc)
510                    throws SystemException {
511    
512                    String[] names = CustomSQLUtil.keywords(name);
513                    String[] descriptions = CustomSQLUtil.keywords(description);
514    
515                    return findByC_N_D_T(
516                            companyId, names, descriptions, types, params, andOperator, start,
517                            end, obc);
518            }
519    
520            @Override
521            public List<Role> findByC_N_D_T(
522                            long companyId, String[] names, String[] descriptions,
523                            Integer[] types, LinkedHashMap<String, Object> params,
524                            boolean andOperator, int start, int end, OrderByComparator obc)
525                    throws SystemException {
526    
527                    names = CustomSQLUtil.keywords(names, true);
528                    descriptions = CustomSQLUtil.keywords(descriptions, true);
529    
530                    if (types == null) {
531                            types = new Integer[0];
532                    }
533    
534                    Session session = null;
535    
536                    try {
537                            session = openSession();
538    
539                            String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
540    
541                            sql = CustomSQLUtil.replaceKeywords(
542                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
543                            sql = CustomSQLUtil.replaceKeywords(
544                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
545                                    descriptions);
546                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
547                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
548                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
549                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
550                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
551    
552                            SQLQuery q = session.createSQLQuery(sql);
553    
554                            q.addEntity("Role_", RoleImpl.class);
555    
556                            QueryPos qPos = QueryPos.getInstance(q);
557    
558                            setJoin(qPos, params);
559    
560                            qPos.add(companyId);
561                            qPos.add(names, 2);
562                            qPos.add(descriptions, 2);
563                            qPos.add(types);
564    
565                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
566                    }
567                    catch (Exception e) {
568                            throw new SystemException(e);
569                    }
570                    finally {
571                            closeSession(session);
572                    }
573            }
574    
575            @Override
576            public Map<String, List<String>> findByC_N_S_P(
577                            long companyId, String name, int scope, String primKey)
578                    throws SystemException {
579    
580                    Session session = null;
581    
582                    try {
583                            session = openSession();
584    
585                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
586    
587                            SQLQuery q = session.createSQLQuery(sql);
588    
589                            q.addScalar("roleName", Type.STRING);
590                            q.addScalar("actionId", Type.STRING);
591    
592                            QueryPos qPos = QueryPos.getInstance(q);
593    
594                            qPos.add(companyId);
595                            qPos.add(name);
596                            qPos.add(scope);
597                            qPos.add(primKey);
598    
599                            Map<String, List<String>> roleMap =
600                                    new HashMap<String, List<String>>();
601    
602                            Iterator<Object[]> itr = q.iterate();
603    
604                            while (itr.hasNext()) {
605                                    Object[] array = itr.next();
606    
607                                    String roleName = (String)array[0];
608                                    String actionId = (String)array[1];
609    
610                                    List<String> roleList = roleMap.get(roleName);
611    
612                                    if (roleList == null) {
613                                            roleList = new ArrayList<String>();
614                                    }
615    
616                                    roleList.add(actionId);
617    
618                                    roleMap.put(roleName, roleList);
619                            }
620    
621                            return roleMap;
622                    }
623                    catch (Exception e) {
624                            throw new SystemException(e);
625                    }
626                    finally {
627                            closeSession(session);
628                    }
629            }
630    
631            @Override
632            public List<Role> findByC_N_S_P_A(
633                            long companyId, String name, int scope, String primKey,
634                            String actionId)
635                    throws SystemException {
636    
637                    Session session = null;
638    
639                    try {
640                            session = openSession();
641    
642                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
643    
644                            SQLQuery q = session.createSQLQuery(sql);
645    
646                            q.addEntity("Role_", RoleImpl.class);
647    
648                            QueryPos qPos = QueryPos.getInstance(q);
649    
650                            qPos.add(companyId);
651                            qPos.add(name);
652                            qPos.add(scope);
653                            qPos.add(primKey);
654    
655                            ResourceAction resourceAction =
656                                    ResourceActionLocalServiceUtil.getResourceAction(
657                                            name, actionId);
658    
659                            qPos.add(resourceAction.getBitwiseValue());
660    
661                            return q.list(true);
662                    }
663                    catch (Exception e) {
664                            throw new SystemException(e);
665                    }
666                    finally {
667                            closeSession(session);
668                    }
669            }
670    
671            @Override
672            public List<Role> findByKeywords(
673                            long companyId, String keywords, Integer[] types, int start,
674                            int end, OrderByComparator obc)
675                    throws SystemException {
676    
677                    return findByKeywords(
678                            companyId, keywords, types, new LinkedHashMap<String, Object>(),
679                            start, end, obc);
680            }
681    
682            @Override
683            public List<Role> findByKeywords(
684                            long companyId, String keywords, Integer[] types,
685                            LinkedHashMap<String, Object> params, int start, int end,
686                            OrderByComparator obc)
687                    throws SystemException {
688    
689                    String[] names = null;
690                    String[] descriptions = null;
691                    boolean andOperator = false;
692    
693                    if (Validator.isNotNull(keywords)) {
694                            names = CustomSQLUtil.keywords(keywords);
695                            descriptions = CustomSQLUtil.keywords(keywords);
696                    }
697                    else {
698                            andOperator = true;
699                    }
700    
701                    return findByC_N_D_T(
702                            companyId, names, descriptions, types, params, andOperator, start,
703                            end, obc);
704            }
705    
706            protected String getCountByR_U_SQL() {
707                    if (_countByR_U == null) {
708                            StringBundler sb = new StringBundler(13);
709    
710                            sb.append(StringPool.OPEN_PARENTHESIS);
711                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
712                            sb.append(") UNION (");
713                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
714                            sb.append(") UNION (");
715                            sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
716                            sb.append(") UNION (");
717                            sb.append(CustomSQLUtil.get(COUNT_BY_USER));
718                            sb.append(") UNION (");
719                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
720                            sb.append(") UNION (");
721                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
722                            sb.append(StringPool.CLOSE_PARENTHESIS);
723    
724                            _countByR_U = sb.toString();
725                    }
726    
727                    return _countByR_U;
728            }
729    
730            protected String getGroupIds(long[] groupIds, String table) {
731                    if (groupIds.length == 0) {
732                            return StringPool.BLANK;
733                    }
734    
735                    StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
736    
737                    for (int i = 0; i < groupIds.length; i++) {
738                            sb.append(table);
739                            sb.append(".groupId = ?");
740    
741                            if ((i + 1) < groupIds.length) {
742                                    sb.append(" OR ");
743                            }
744                    }
745    
746                    return sb.toString();
747            }
748    
749            protected String getJoin(LinkedHashMap<String, Object> params) {
750                    if ((params == null) || params.isEmpty()) {
751                            return StringPool.BLANK;
752                    }
753    
754                    StringBundler sb = new StringBundler(params.size());
755    
756                    for (Map.Entry<String, Object> entry : params.entrySet()) {
757                            String key = entry.getKey();
758                            Object value = entry.getValue();
759    
760                            if (Validator.isNotNull(value)) {
761                                    sb.append(getJoin(key));
762                            }
763                    }
764    
765                    return sb.toString();
766            }
767    
768            protected String getJoin(String key) {
769                    String join = StringPool.BLANK;
770    
771                    if (key.equals("usersRoles")) {
772                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
773                    }
774    
775                    if (Validator.isNotNull(join)) {
776                            int pos = join.indexOf("WHERE");
777    
778                            if (pos != -1) {
779                                    join = join.substring(0, pos);
780                            }
781                    }
782    
783                    return join;
784            }
785    
786            protected String getTypes(Integer[] types) {
787                    if (types.length == 0) {
788                            return StringPool.BLANK;
789                    }
790    
791                    StringBundler sb = new StringBundler(types.length * 2);
792    
793                    sb.append(" AND (");
794    
795                    for (int i = 0; i < types.length; i++) {
796                            sb.append("Role_.type_ = ?");
797    
798                            if ((i + 1) < types.length) {
799                                    sb.append(" OR ");
800                            }
801                    }
802    
803                    sb.append(StringPool.CLOSE_PARENTHESIS);
804    
805                    return sb.toString();
806            }
807    
808            protected String getWhere(LinkedHashMap<String, Object> params) {
809                    if ((params == null) || params.isEmpty()) {
810                            return StringPool.BLANK;
811                    }
812    
813                    StringBundler sb = new StringBundler(params.size());
814    
815                    for (Map.Entry<String, Object> entry : params.entrySet()) {
816                            String key = entry.getKey();
817                            Object value = entry.getValue();
818    
819                            if (Validator.isNotNull(value)) {
820                                    sb.append(getWhere(key));
821                            }
822                    }
823    
824                    return sb.toString();
825            }
826    
827            protected String getWhere(String key) {
828                    String join = StringPool.BLANK;
829    
830                    if (key.equals("usersRoles")) {
831                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
832                    }
833    
834                    if (Validator.isNotNull(join)) {
835                            int pos = join.indexOf("WHERE");
836    
837                            if (pos != -1) {
838                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
839                            }
840                            else {
841                                    join = StringPool.BLANK;
842                            }
843                    }
844    
845                    return join;
846            }
847    
848            protected void setJoin(
849                    QueryPos qPos, LinkedHashMap<String, Object> params) {
850    
851                    if (params == null) {
852                            return;
853                    }
854    
855                    for (Map.Entry<String, Object> entry : params.entrySet()) {
856                            Object value = entry.getValue();
857    
858                            if (value instanceof Long) {
859                                    Long valueLong = (Long)value;
860    
861                                    if (Validator.isNotNull(valueLong)) {
862                                            qPos.add(valueLong);
863                                    }
864                            }
865                            else if (value instanceof String) {
866                                    String valueString = (String)value;
867    
868                                    if (Validator.isNotNull(valueString)) {
869                                            qPos.add(valueString);
870                                    }
871                            }
872                    }
873            }
874    
875            private String _countByR_U;
876    
877    }