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