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