001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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_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) 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 int countByUserGroupGroupRole(long userId, long groupId)
285                    throws SystemException {
286    
287                    Session session = null;
288    
289                    try {
290                            session = openSession();
291    
292                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_GROUP_ROLE);
293    
294                            SQLQuery q = session.createSQLQuery(sql);
295    
296                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
297    
298                            QueryPos qPos = QueryPos.getInstance(q);
299    
300                            qPos.add(groupId);
301                            qPos.add(userId);
302    
303                            Iterator<Long> itr = q.iterate();
304    
305                            if (itr.hasNext()) {
306                                    Long count = itr.next();
307    
308                                    if (count != null) {
309                                            return count.intValue();
310                                    }
311                            }
312    
313                            return 0;
314                    }
315                    catch (Exception e) {
316                            throw new SystemException(e);
317                    }
318                    finally {
319                            closeSession(session);
320                    }
321            }
322    
323            @Override
324            public List<Role> findBySystem(long companyId) throws SystemException {
325                    Session session = null;
326    
327                    try {
328                            session = openSession();
329    
330                            String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
331    
332                            SQLQuery q = session.createSQLQuery(sql);
333    
334                            q.addEntity("Role_", RoleImpl.class);
335    
336                            QueryPos qPos = QueryPos.getInstance(q);
337    
338                            qPos.add(companyId);
339    
340                            return q.list(true);
341                    }
342                    catch (Exception e) {
343                            throw new SystemException(e);
344                    }
345                    finally {
346                            closeSession(session);
347                    }
348            }
349    
350            @Override
351            public List<Role> findByUserGroupGroupRole(long userId, long groupId)
352                    throws SystemException {
353    
354                    return findByUserGroupGroupRole(
355                            userId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
356            }
357    
358            @Override
359            public List<Role> findByUserGroupGroupRole(
360                            long userId, long groupId, int start, int end)
361                    throws SystemException {
362    
363                    Session session = null;
364    
365                    try {
366                            session = openSession();
367    
368                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
369    
370                            SQLQuery q = session.createSQLQuery(sql);
371    
372                            q.addEntity("Role_", RoleImpl.class);
373    
374                            QueryPos qPos = QueryPos.getInstance(q);
375    
376                            qPos.add(userId);
377                            qPos.add(groupId);
378    
379                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
380                    }
381                    catch (Exception e) {
382                            throw new SystemException(e);
383                    }
384                    finally {
385                            closeSession(session);
386                    }
387            }
388    
389            @Override
390            public List<Role> findByUserGroupRole(long userId, long groupId)
391                    throws SystemException {
392    
393                    Session session = null;
394    
395                    try {
396                            session = openSession();
397    
398                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
399    
400                            SQLQuery q = session.createSQLQuery(sql);
401    
402                            q.addEntity("Role_", RoleImpl.class);
403    
404                            QueryPos qPos = QueryPos.getInstance(q);
405    
406                            qPos.add(userId);
407                            qPos.add(groupId);
408    
409                            return q.list(true);
410                    }
411                    catch (Exception e) {
412                            throw new SystemException(e);
413                    }
414                    finally {
415                            closeSession(session);
416                    }
417            }
418    
419            @Override
420            public Role findByC_N(long companyId, String name)
421                    throws NoSuchRoleException, SystemException {
422    
423                    name = StringUtil.lowerCase(name);
424    
425                    Session session = null;
426    
427                    try {
428                            session = openSession();
429    
430                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
431    
432                            SQLQuery q = session.createSQLQuery(sql);
433    
434                            q.addEntity("Role_", RoleImpl.class);
435    
436                            QueryPos qPos = QueryPos.getInstance(q);
437    
438                            qPos.add(companyId);
439                            qPos.add(name);
440    
441                            List<Role> roles = q.list();
442    
443                            if (!roles.isEmpty()) {
444                                    return roles.get(0);
445                            }
446                    }
447                    catch (Exception e) {
448                            throw new SystemException(e);
449                    }
450                    finally {
451                            closeSession(session);
452                    }
453    
454                    StringBundler sb = new StringBundler(5);
455    
456                    sb.append("No Role exists with the key {companyId=");
457                    sb.append(companyId);
458                    sb.append(", name=");
459                    sb.append(name);
460                    sb.append("}");
461    
462                    throw new NoSuchRoleException(sb.toString());
463            }
464    
465            @Override
466            public List<Role> findByU_G(long userId, List<Group> groups)
467                    throws SystemException {
468    
469                    long[] groupIds = new long[groups.size()];
470    
471                    for (int i = 0; i < groups.size(); i++) {
472                            Group group = groups.get(i);
473    
474                            groupIds[i] = group.getGroupId();
475                    }
476    
477                    return findByU_G(userId, groupIds);
478            }
479    
480            @Override
481            public List<Role> findByU_G(long userId, long groupId)
482                    throws SystemException {
483    
484                    return findByU_G(userId, new long[] {groupId});
485            }
486    
487            @Override
488            public List<Role> findByU_G(long userId, long[] groupIds)
489                    throws SystemException {
490    
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            String sql = CustomSQLUtil.get(FIND_BY_U_G);
497    
498                            sql = StringUtil.replace(
499                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
500    
501                            SQLQuery q = session.createSQLQuery(sql);
502    
503                            q.addEntity("Role_", RoleImpl.class);
504    
505                            QueryPos qPos = QueryPos.getInstance(q);
506    
507                            qPos.add(userId);
508                            qPos.add(groupIds);
509    
510                            return q.list(true);
511                    }
512                    catch (Exception e) {
513                            throw new SystemException(e);
514                    }
515                    finally {
516                            closeSession(session);
517                    }
518            }
519    
520            @Override
521            public List<Role> findByR_N_A(
522                            long resourceBlockId, String className, String actionId)
523                    throws SystemException {
524    
525                    Session session = null;
526    
527                    try {
528                            session = openSession();
529    
530                            String sql = CustomSQLUtil.get(FIND_BY_R_N_A);
531    
532                            SQLQuery q = session.createSQLQuery(sql);
533    
534                            q.addEntity("Role_", RoleImpl.class);
535    
536                            QueryPos qPos = QueryPos.getInstance(q);
537    
538                            qPos.add(resourceBlockId);
539                            qPos.add(className);
540    
541                            ResourceAction resourceAction =
542                                    ResourceActionLocalServiceUtil.getResourceAction(
543                                            className, actionId);
544    
545                            qPos.add(resourceAction.getBitwiseValue());
546    
547                            return q.list(true);
548                    }
549                    catch (Exception e) {
550                            throw new SystemException(e);
551                    }
552                    finally {
553                            closeSession(session);
554                    }
555            }
556    
557            @Override
558            public List<Role> findByC_N_D_T(
559                            long companyId, String name, String description, Integer[] types,
560                            LinkedHashMap<String, Object> params, boolean andOperator,
561                            int start, int end, OrderByComparator obc)
562                    throws SystemException {
563    
564                    String[] names = CustomSQLUtil.keywords(name);
565                    String[] descriptions = CustomSQLUtil.keywords(description);
566    
567                    return findByC_N_D_T(
568                            companyId, names, descriptions, types, params, andOperator, start,
569                            end, obc);
570            }
571    
572            @Override
573            public List<Role> findByC_N_D_T(
574                            long companyId, String[] names, String[] descriptions,
575                            Integer[] types, LinkedHashMap<String, Object> params,
576                            boolean andOperator, int start, int end, OrderByComparator obc)
577                    throws SystemException {
578    
579                    names = CustomSQLUtil.keywords(names, true);
580                    descriptions = CustomSQLUtil.keywords(descriptions, true);
581    
582                    if (types == null) {
583                            types = new Integer[0];
584                    }
585    
586                    Session session = null;
587    
588                    try {
589                            session = openSession();
590    
591                            String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
592    
593                            sql = CustomSQLUtil.replaceKeywords(
594                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
595                            sql = CustomSQLUtil.replaceKeywords(
596                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
597                                    descriptions);
598                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
599                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
600                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
601                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
602                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
603    
604                            SQLQuery q = session.createSQLQuery(sql);
605    
606                            q.addEntity("Role_", RoleImpl.class);
607    
608                            QueryPos qPos = QueryPos.getInstance(q);
609    
610                            setJoin(qPos, params);
611    
612                            qPos.add(companyId);
613                            qPos.add(names, 2);
614                            qPos.add(descriptions, 2);
615                            qPos.add(types);
616    
617                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
618                    }
619                    catch (Exception e) {
620                            throw new SystemException(e);
621                    }
622                    finally {
623                            closeSession(session);
624                    }
625            }
626    
627            @Override
628            public Map<String, List<String>> findByC_N_S_P(
629                            long companyId, String name, int scope, String primKey)
630                    throws SystemException {
631    
632                    Session session = null;
633    
634                    try {
635                            session = openSession();
636    
637                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
638    
639                            SQLQuery q = session.createSQLQuery(sql);
640    
641                            q.addScalar("roleName", Type.STRING);
642                            q.addScalar("actionId", Type.STRING);
643    
644                            QueryPos qPos = QueryPos.getInstance(q);
645    
646                            qPos.add(companyId);
647                            qPos.add(name);
648                            qPos.add(scope);
649                            qPos.add(primKey);
650    
651                            Map<String, List<String>> roleMap =
652                                    new HashMap<String, List<String>>();
653    
654                            Iterator<Object[]> itr = q.iterate();
655    
656                            while (itr.hasNext()) {
657                                    Object[] array = itr.next();
658    
659                                    String roleName = (String)array[0];
660                                    String actionId = (String)array[1];
661    
662                                    List<String> roleList = roleMap.get(roleName);
663    
664                                    if (roleList == null) {
665                                            roleList = new ArrayList<String>();
666                                    }
667    
668                                    roleList.add(actionId);
669    
670                                    roleMap.put(roleName, roleList);
671                            }
672    
673                            return roleMap;
674                    }
675                    catch (Exception e) {
676                            throw new SystemException(e);
677                    }
678                    finally {
679                            closeSession(session);
680                    }
681            }
682    
683            @Override
684            public List<Role> findByC_N_S_P_A(
685                            long companyId, String name, int scope, String primKey,
686                            String actionId)
687                    throws SystemException {
688    
689                    Session session = null;
690    
691                    try {
692                            session = openSession();
693    
694                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
695    
696                            SQLQuery q = session.createSQLQuery(sql);
697    
698                            q.addEntity("Role_", RoleImpl.class);
699    
700                            QueryPos qPos = QueryPos.getInstance(q);
701    
702                            qPos.add(companyId);
703                            qPos.add(name);
704                            qPos.add(scope);
705                            qPos.add(primKey);
706    
707                            ResourceAction resourceAction =
708                                    ResourceActionLocalServiceUtil.getResourceAction(
709                                            name, actionId);
710    
711                            qPos.add(resourceAction.getBitwiseValue());
712    
713                            return q.list(true);
714                    }
715                    catch (Exception e) {
716                            throw new SystemException(e);
717                    }
718                    finally {
719                            closeSession(session);
720                    }
721            }
722    
723            @Override
724            public List<Role> findByKeywords(
725                            long companyId, String keywords, Integer[] types, int start,
726                            int end, OrderByComparator obc)
727                    throws SystemException {
728    
729                    return findByKeywords(
730                            companyId, keywords, types, new LinkedHashMap<String, Object>(),
731                            start, end, obc);
732            }
733    
734            @Override
735            public List<Role> findByKeywords(
736                            long companyId, String keywords, Integer[] types,
737                            LinkedHashMap<String, Object> params, int start, int end,
738                            OrderByComparator obc)
739                    throws SystemException {
740    
741                    String[] names = null;
742                    String[] descriptions = null;
743                    boolean andOperator = false;
744    
745                    if (Validator.isNotNull(keywords)) {
746                            names = CustomSQLUtil.keywords(keywords);
747                            descriptions = CustomSQLUtil.keywords(keywords);
748                    }
749                    else {
750                            andOperator = true;
751                    }
752    
753                    return findByC_N_D_T(
754                            companyId, names, descriptions, types, params, andOperator, start,
755                            end, obc);
756            }
757    
758            protected String getCountByR_U_SQL() {
759                    if (_countByR_U == null) {
760                            StringBundler sb = new StringBundler(13);
761    
762                            sb.append(StringPool.OPEN_PARENTHESIS);
763                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
764                            sb.append(") UNION (");
765                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
766                            sb.append(") UNION (");
767                            sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
768                            sb.append(") UNION (");
769                            sb.append(CustomSQLUtil.get(COUNT_BY_USER));
770                            sb.append(") UNION (");
771                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
772                            sb.append(") UNION (");
773                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
774                            sb.append(StringPool.CLOSE_PARENTHESIS);
775    
776                            _countByR_U = sb.toString();
777                    }
778    
779                    return _countByR_U;
780            }
781    
782            protected String getGroupIds(long[] groupIds, String table) {
783                    if (groupIds.length == 0) {
784                            return StringPool.BLANK;
785                    }
786    
787                    StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
788    
789                    for (int i = 0; i < groupIds.length; i++) {
790                            sb.append(table);
791                            sb.append(".groupId = ?");
792    
793                            if ((i + 1) < groupIds.length) {
794                                    sb.append(" OR ");
795                            }
796                    }
797    
798                    return sb.toString();
799            }
800    
801            protected String getJoin(LinkedHashMap<String, Object> params) {
802                    if ((params == null) || params.isEmpty()) {
803                            return StringPool.BLANK;
804                    }
805    
806                    StringBundler sb = new StringBundler(params.size());
807    
808                    for (Map.Entry<String, Object> entry : params.entrySet()) {
809                            String key = entry.getKey();
810                            Object value = entry.getValue();
811    
812                            if (Validator.isNotNull(value)) {
813                                    sb.append(getJoin(key));
814                            }
815                    }
816    
817                    return sb.toString();
818            }
819    
820            protected String getJoin(String key) {
821                    String join = StringPool.BLANK;
822    
823                    if (key.equals("usersRoles")) {
824                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
825                    }
826    
827                    if (Validator.isNotNull(join)) {
828                            int pos = join.indexOf("WHERE");
829    
830                            if (pos != -1) {
831                                    join = join.substring(0, pos);
832                            }
833                    }
834    
835                    return join;
836            }
837    
838            protected String getTypes(Integer[] types) {
839                    if (types.length == 0) {
840                            return StringPool.BLANK;
841                    }
842    
843                    StringBundler sb = new StringBundler(types.length * 2);
844    
845                    sb.append(" AND (");
846    
847                    for (int i = 0; i < types.length; i++) {
848                            sb.append("Role_.type_ = ?");
849    
850                            if ((i + 1) < types.length) {
851                                    sb.append(" OR ");
852                            }
853                    }
854    
855                    sb.append(StringPool.CLOSE_PARENTHESIS);
856    
857                    return sb.toString();
858            }
859    
860            protected String getWhere(LinkedHashMap<String, Object> params) {
861                    if ((params == null) || params.isEmpty()) {
862                            return StringPool.BLANK;
863                    }
864    
865                    StringBundler sb = new StringBundler(params.size());
866    
867                    for (Map.Entry<String, Object> entry : params.entrySet()) {
868                            String key = entry.getKey();
869                            Object value = entry.getValue();
870    
871                            if (Validator.isNotNull(value)) {
872                                    sb.append(getWhere(key));
873                            }
874                    }
875    
876                    return sb.toString();
877            }
878    
879            protected String getWhere(String key) {
880                    String join = StringPool.BLANK;
881    
882                    if (key.equals("usersRoles")) {
883                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
884                    }
885    
886                    if (Validator.isNotNull(join)) {
887                            int pos = join.indexOf("WHERE");
888    
889                            if (pos != -1) {
890                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
891                            }
892                            else {
893                                    join = StringPool.BLANK;
894                            }
895                    }
896    
897                    return join;
898            }
899    
900            protected void setJoin(
901                    QueryPos qPos, LinkedHashMap<String, Object> params) {
902    
903                    if (params == null) {
904                            return;
905                    }
906    
907                    for (Map.Entry<String, Object> entry : params.entrySet()) {
908                            Object value = entry.getValue();
909    
910                            if (value instanceof Long) {
911                                    Long valueLong = (Long)value;
912    
913                                    if (Validator.isNotNull(valueLong)) {
914                                            qPos.add(valueLong);
915                                    }
916                            }
917                            else if (value instanceof String) {
918                                    String valueString = (String)value;
919    
920                                    if (Validator.isNotNull(valueString)) {
921                                            qPos.add(valueString);
922                                    }
923                            }
924                    }
925            }
926    
927            private String _countByR_U;
928    
929    }