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