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.Team;
033    import com.liferay.portal.model.impl.RoleImpl;
034    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
035    import com.liferay.portal.service.ClassNameLocalServiceUtil;
036    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
037    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
038    import com.liferay.util.dao.orm.CustomSQLUtil;
039    
040    import java.util.ArrayList;
041    import java.util.Collections;
042    import java.util.HashMap;
043    import java.util.Iterator;
044    import java.util.LinkedHashMap;
045    import java.util.List;
046    import java.util.Map;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     * @author Marcellus Tavares
051     * @author Connor McKay
052     */
053    public class RoleFinderImpl
054            extends BasePersistenceImpl<Role> implements RoleFinder {
055    
056            public static final String COUNT_BY_GROUP_ROLE_AND_TEAM_ROLE =
057                    RoleFinder.class.getName() + ".countByGroupRoleAndTeamRole";
058    
059            public static final String COUNT_BY_ORGANIZATION =
060                    RoleFinder.class.getName() + ".countByOrganization";
061    
062            public static final String COUNT_BY_ORGANIZATION_SITE =
063                    RoleFinder.class.getName() + ".countByOrganizationSite";
064    
065            public static final String COUNT_BY_SITE =
066                    RoleFinder.class.getName() + ".countBySite";
067    
068            public static final String COUNT_BY_USER =
069                    RoleFinder.class.getName() + ".countByUser";
070    
071            public static final String COUNT_BY_USER_GROUP =
072                    RoleFinder.class.getName() + ".countByUserGroup";
073    
074            public static final String COUNT_BY_USER_GROUP_GROUP_ROLE =
075                    RoleFinder.class.getName() + ".countByUserGroupGroupRole";
076    
077            public static final String COUNT_BY_USER_GROUP_SITE =
078                    RoleFinder.class.getName() + ".countByUserGroupSite";
079    
080            public static final String COUNT_BY_U_G_R =
081                    RoleFinder.class.getName() + ".countByU_G_R";
082    
083            public static final String COUNT_BY_C_N_D_T =
084                    RoleFinder.class.getName() + ".countByC_N_D_T";
085    
086            public static final String FIND_BY_GROUP_ROLE_AND_TEAM_ROLE =
087                    RoleFinder.class.getName() + ".findByGroupRoleAndTeamRole";
088    
089            public static final String FIND_BY_SYSTEM =
090                    RoleFinder.class.getName() + ".findBySystem";
091    
092            public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
093                    RoleFinder.class.getName() + ".findByUserGroupGroupRole";
094    
095            public static final String FIND_BY_USER_GROUP_ROLE =
096                    RoleFinder.class.getName() + ".findByUserGroupRole";
097    
098            public static final String FIND_BY_C_N =
099                    RoleFinder.class.getName() + ".findByC_N";
100    
101            public static final String FIND_BY_U_G =
102                    RoleFinder.class.getName() + ".findByU_G";
103    
104            public static final String FIND_BY_R_N_A =
105                    RoleFinder.class.getName() + ".findByR_N_A";
106    
107            public static final String FIND_BY_C_N_D_T =
108                    RoleFinder.class.getName() + ".findByC_N_D_T";
109    
110            public static final String FIND_BY_C_N_S_P =
111                    RoleFinder.class.getName() + ".findByC_N_S_P";
112    
113            public static final String FIND_BY_C_N_S_P_A =
114                    RoleFinder.class.getName() + ".findByC_N_S_P_A";
115    
116            public static final String JOIN_BY_USERS_ROLES =
117                    RoleFinder.class.getName() + ".joinByUsersRoles";
118    
119            @Override
120            public int countByGroupRoleAndTeamRole(
121                            long companyId, String keywords, List<String> excludedNames,
122                            int[] types, long excludedTeamRoleId, long teamGroupId)
123                    throws SystemException {
124    
125                    return doCountByGroupRoleAndTeamRole(
126                            companyId, keywords, excludedNames, types, excludedTeamRoleId,
127                            teamGroupId, false);
128            }
129    
130            @Override
131            public int countByKeywords(long companyId, String keywords, Integer[] types)
132                    throws SystemException {
133    
134                    return countByKeywords(
135                            companyId, keywords, types, new LinkedHashMap<String, Object>());
136            }
137    
138            @Override
139            public int countByKeywords(
140                            long companyId, String keywords, Integer[] types,
141                            LinkedHashMap<String, Object> params)
142                    throws SystemException {
143    
144                    String[] names = null;
145                    String[] descriptions = null;
146                    boolean andOperator = false;
147    
148                    if (Validator.isNotNull(keywords)) {
149                            names = CustomSQLUtil.keywords(keywords);
150                            descriptions = CustomSQLUtil.keywords(keywords);
151                    }
152                    else {
153                            andOperator = true;
154                    }
155    
156                    return countByC_N_D_T(
157                            companyId, names, descriptions, types, params, andOperator);
158            }
159    
160            @Override
161            public int countByR_U(long roleId, long userId) throws SystemException {
162                    Session session = null;
163    
164                    try {
165                            session = openSession();
166    
167                            SQLQuery q = session.createSQLQuery(getCountByR_U_SQL());
168    
169                            QueryPos qPos = QueryPos.getInstance(q);
170    
171                            for (int i = 0; i < 6; i++) {
172                                    qPos.add(roleId);
173                                    qPos.add(userId);
174                            }
175    
176                            return q.list().size();
177                    }
178                    catch (Exception e) {
179                            throw new SystemException(e);
180                    }
181                    finally {
182                            closeSession(session);
183                    }
184            }
185    
186            @Override
187            public int countByU_G_R(long userId, long groupId, long roleId)
188                    throws SystemException {
189    
190                    Session session = null;
191    
192                    try {
193                            session = openSession();
194    
195                            String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
196    
197                            SQLQuery q = session.createSQLQuery(sql);
198    
199                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
200    
201                            QueryPos qPos = QueryPos.getInstance(q);
202    
203                            qPos.add(roleId);
204                            qPos.add(groupId);
205                            qPos.add(userId);
206    
207                            Iterator<Long> itr = q.iterate();
208    
209                            if (itr.hasNext()) {
210                                    Long count = itr.next();
211    
212                                    if (count != null) {
213                                            return count.intValue();
214                                    }
215                            }
216    
217                            return 0;
218                    }
219                    catch (Exception e) {
220                            throw new SystemException(e);
221                    }
222                    finally {
223                            closeSession(session);
224                    }
225            }
226    
227            @Override
228            public int countByC_N_D_T(
229                            long companyId, String name, String description, Integer[] types,
230                            LinkedHashMap<String, Object> params, boolean andOperator)
231                    throws SystemException {
232    
233                    String[] names = CustomSQLUtil.keywords(name);
234                    String[] descriptions = CustomSQLUtil.keywords(description);
235    
236                    return countByC_N_D_T(
237                            companyId, names, descriptions, types, params, andOperator);
238            }
239    
240            @Override
241            public int countByC_N_D_T(
242                            long companyId, String[] names, String[] descriptions,
243                            Integer[] types, LinkedHashMap<String, Object> params,
244                            boolean andOperator)
245                    throws SystemException {
246    
247                    return doCountByC_N_D_T(
248                            companyId, names, descriptions, types, params, andOperator, false);
249            }
250    
251            @Override
252            public int filterCountByC_N_D_T(
253                            long companyId, String[] names, String[] descriptions,
254                            Integer[] types, LinkedHashMap<String, Object> params,
255                            boolean andOperator)
256                    throws SystemException {
257    
258                    return doCountByC_N_D_T(
259                            companyId, names, descriptions, types, params, andOperator, true);
260            }
261    
262            @Override
263            public List<Role> filterFindByC_N_D_T(
264                            long companyId, String[] names, String[] descriptions,
265                            Integer[] types, LinkedHashMap<String, Object> params,
266                            boolean andOperator, int start, int end, OrderByComparator obc)
267                    throws SystemException {
268    
269                    return doFindByC_N_D_T(
270                            companyId, names, descriptions, types, params, andOperator, start,
271                            end, obc, true);
272            }
273    
274            @Override
275            public List<Role> findByUserGroupRole(long userId, long groupId)
276                    throws SystemException {
277    
278                    Session session = null;
279    
280                    try {
281                            session = openSession();
282    
283                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
284    
285                            SQLQuery q = session.createSQLQuery(sql);
286    
287                            q.addEntity("Role_", RoleImpl.class);
288    
289                            QueryPos qPos = QueryPos.getInstance(q);
290    
291                            qPos.add(userId);
292                            qPos.add(groupId);
293    
294                            return q.list(true);
295                    }
296                    catch (Exception e) {
297                            throw new SystemException(e);
298                    }
299                    finally {
300                            closeSession(session);
301                    }
302            }
303    
304            @Override
305            public int countByUserGroupGroupRole(long userId, long groupId)
306                    throws SystemException {
307    
308                    Session session = null;
309    
310                    try {
311                            session = openSession();
312    
313                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_GROUP_ROLE);
314    
315                            SQLQuery q = session.createSQLQuery(sql);
316    
317                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
318    
319                            QueryPos qPos = QueryPos.getInstance(q);
320    
321                            qPos.add(groupId);
322                            qPos.add(userId);
323    
324                            Iterator<Long> itr = q.iterate();
325    
326                            if (itr.hasNext()) {
327                                    Long count = itr.next();
328    
329                                    if (count != null) {
330                                            return count.intValue();
331                                    }
332                            }
333    
334                            return 0;
335                    }
336                    catch (Exception e) {
337                            throw new SystemException(e);
338                    }
339                    finally {
340                            closeSession(session);
341                    }
342            }
343    
344            @Override
345            public int filterCountByGroupRoleAndTeamRole(
346                            long companyId, String keywords, List<String> excludedNames,
347                            int[] types, long excludedTeamRoleId, long teamGroupId)
348                    throws SystemException {
349    
350                    return doCountByGroupRoleAndTeamRole(
351                            companyId, keywords, excludedNames, types, excludedTeamRoleId,
352                            teamGroupId, false);
353            }
354    
355            @Override
356            public List<Role> filterFindByKeywords(
357                            long companyId, String keywords, Integer[] types,
358                            LinkedHashMap<String, Object> params, int start, int end,
359                            OrderByComparator obc)
360                    throws SystemException {
361    
362                    String[] names = null;
363                    String[] descriptions = null;
364                    boolean andOperator = false;
365    
366                    if (Validator.isNotNull(keywords)) {
367                            names = CustomSQLUtil.keywords(keywords);
368                            descriptions = CustomSQLUtil.keywords(keywords);
369                    }
370                    else {
371                            andOperator = true;
372                    }
373    
374                    return filterFindByC_N_D_T(
375                            companyId, names, descriptions, types, params, andOperator, start,
376                            end, obc);
377            }
378    
379            @Override
380            public List<Role> filterFindByC_N_D_T(
381                            long companyId, String name, String description, Integer[] types,
382                            LinkedHashMap<String, Object> params, boolean andOperator,
383                            int start, int end, OrderByComparator obc)
384                    throws SystemException {
385    
386                    String[] names = CustomSQLUtil.keywords(name);
387                    String[] descriptions = CustomSQLUtil.keywords(description);
388    
389                    return filterFindByC_N_D_T(
390                            companyId, names, descriptions, types, params, andOperator, start,
391                            end, obc);
392            }
393    
394            @Override
395            public List<Role> filterFindByGroupRoleAndTeamRole(
396                            long companyId, String keywords, List<String> excludedNames,
397                            int[] types, long excludedTeamRoleId, long teamGroupId, int start,
398                            int end)
399                    throws SystemException {
400    
401                    return doFindByGroupRoleAndTeamRole(
402                            companyId, keywords, excludedNames, types, excludedTeamRoleId,
403                            teamGroupId, start, end, false);
404            }
405    
406            @Override
407            public List<Role> findByGroupRoleAndTeamRole(
408                            long companyId, String keywords, List<String> excludedNames,
409                            int[] types, long excludedTeamRoleId, long teamGroupId, int start,
410                            int end)
411                    throws SystemException {
412    
413                    return doFindByGroupRoleAndTeamRole(
414                            companyId, keywords, excludedNames, types, excludedTeamRoleId,
415                            teamGroupId, start, end, false);
416            }
417    
418            @Override
419            public List<Role> findBySystem(long companyId) throws SystemException {
420                    Session session = null;
421    
422                    try {
423                            session = openSession();
424    
425                            String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
426    
427                            SQLQuery q = session.createSQLQuery(sql);
428    
429                            q.addEntity("Role_", RoleImpl.class);
430    
431                            QueryPos qPos = QueryPos.getInstance(q);
432    
433                            qPos.add(companyId);
434    
435                            return q.list(true);
436                    }
437                    catch (Exception e) {
438                            throw new SystemException(e);
439                    }
440                    finally {
441                            closeSession(session);
442                    }
443            }
444    
445            @Override
446            public List<Role> findByUserGroupGroupRole(long userId, long groupId)
447                    throws SystemException {
448    
449                    return findByUserGroupGroupRole(
450                            userId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
451            }
452    
453            @Override
454            public List<Role> findByUserGroupGroupRole(
455                            long userId, long groupId, int start, int end)
456                    throws SystemException {
457    
458                    Session session = null;
459    
460                    try {
461                            session = openSession();
462    
463                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
464    
465                            SQLQuery q = session.createSQLQuery(sql);
466    
467                            q.addEntity("Role_", RoleImpl.class);
468    
469                            QueryPos qPos = QueryPos.getInstance(q);
470    
471                            qPos.add(userId);
472                            qPos.add(groupId);
473    
474                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
475                    }
476                    catch (Exception e) {
477                            throw new SystemException(e);
478                    }
479                    finally {
480                            closeSession(session);
481                    }
482            }
483    
484            @Override
485            public Role findByC_N(long companyId, String name)
486                    throws NoSuchRoleException, SystemException {
487    
488                    name = StringUtil.lowerCase(name);
489    
490                    Session session = null;
491    
492                    try {
493                            session = openSession();
494    
495                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
496    
497                            SQLQuery q = session.createSQLQuery(sql);
498    
499                            q.addEntity("Role_", RoleImpl.class);
500    
501                            QueryPos qPos = QueryPos.getInstance(q);
502    
503                            qPos.add(companyId);
504                            qPos.add(name);
505    
506                            List<Role> roles = q.list();
507    
508                            if (!roles.isEmpty()) {
509                                    return roles.get(0);
510                            }
511                    }
512                    catch (Exception e) {
513                            throw new SystemException(e);
514                    }
515                    finally {
516                            closeSession(session);
517                    }
518    
519                    StringBundler sb = new StringBundler(5);
520    
521                    sb.append("No Role exists with the key {companyId=");
522                    sb.append(companyId);
523                    sb.append(", name=");
524                    sb.append(name);
525                    sb.append("}");
526    
527                    throw new NoSuchRoleException(sb.toString());
528            }
529    
530            @Override
531            public List<Role> findByU_G(long userId, List<Group> groups)
532                    throws SystemException {
533    
534                    long[] groupIds = new long[groups.size()];
535    
536                    for (int i = 0; i < groups.size(); i++) {
537                            Group group = groups.get(i);
538    
539                            groupIds[i] = group.getGroupId();
540                    }
541    
542                    return findByU_G(userId, groupIds);
543            }
544    
545            @Override
546            public List<Role> findByU_G(long userId, long groupId)
547                    throws SystemException {
548    
549                    return findByU_G(userId, new long[] {groupId});
550            }
551    
552            @Override
553            public List<Role> findByU_G(long userId, long[] groupIds)
554                    throws SystemException {
555    
556                    Session session = null;
557    
558                    try {
559                            session = openSession();
560    
561                            String sql = CustomSQLUtil.get(FIND_BY_U_G);
562    
563                            sql = StringUtil.replace(
564                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
565    
566                            SQLQuery q = session.createSQLQuery(sql);
567    
568                            q.addEntity("Role_", RoleImpl.class);
569    
570                            QueryPos qPos = QueryPos.getInstance(q);
571    
572                            qPos.add(userId);
573                            qPos.add(groupIds);
574    
575                            return q.list(true);
576                    }
577                    catch (Exception e) {
578                            throw new SystemException(e);
579                    }
580                    finally {
581                            closeSession(session);
582                    }
583            }
584    
585            @Override
586            public List<Role> findByR_N_A(
587                            long resourceBlockId, String className, String actionId)
588                    throws SystemException {
589    
590                    Session session = null;
591    
592                    try {
593                            session = openSession();
594    
595                            String sql = CustomSQLUtil.get(FIND_BY_R_N_A);
596    
597                            SQLQuery q = session.createSQLQuery(sql);
598    
599                            q.addEntity("Role_", RoleImpl.class);
600    
601                            QueryPos qPos = QueryPos.getInstance(q);
602    
603                            qPos.add(resourceBlockId);
604                            qPos.add(className);
605    
606                            ResourceAction resourceAction =
607                                    ResourceActionLocalServiceUtil.getResourceAction(
608                                            className, actionId);
609    
610                            qPos.add(resourceAction.getBitwiseValue());
611    
612                            return q.list(true);
613                    }
614                    catch (Exception e) {
615                            throw new SystemException(e);
616                    }
617                    finally {
618                            closeSession(session);
619                    }
620            }
621    
622            @Override
623            public List<Role> findByC_N_D_T(
624                            long companyId, String name, String description, Integer[] types,
625                            LinkedHashMap<String, Object> params, boolean andOperator,
626                            int start, int end, OrderByComparator obc)
627                    throws SystemException {
628    
629                    String[] names = CustomSQLUtil.keywords(name);
630                    String[] descriptions = CustomSQLUtil.keywords(description);
631    
632                    return findByC_N_D_T(
633                            companyId, names, descriptions, types, params, andOperator, start,
634                            end, obc);
635            }
636    
637            @Override
638            public List<Role> findByC_N_D_T(
639                            long companyId, String[] names, String[] descriptions,
640                            Integer[] types, LinkedHashMap<String, Object> params,
641                            boolean andOperator, int start, int end, OrderByComparator obc)
642                    throws SystemException {
643    
644                    return doFindByC_N_D_T(
645                            companyId, names, descriptions, types, params, andOperator, start,
646                            end, obc, false);
647            }
648    
649            @Override
650            public Map<String, List<String>> findByC_N_S_P(
651                            long companyId, String name, int scope, String primKey)
652                    throws SystemException {
653    
654                    Session session = null;
655    
656                    try {
657                            session = openSession();
658    
659                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
660    
661                            SQLQuery q = session.createSQLQuery(sql);
662    
663                            q.addScalar("roleName", Type.STRING);
664                            q.addScalar("actionId", Type.STRING);
665    
666                            QueryPos qPos = QueryPos.getInstance(q);
667    
668                            qPos.add(companyId);
669                            qPos.add(name);
670                            qPos.add(scope);
671                            qPos.add(primKey);
672    
673                            Map<String, List<String>> roleMap =
674                                    new HashMap<String, List<String>>();
675    
676                            Iterator<Object[]> itr = q.iterate();
677    
678                            while (itr.hasNext()) {
679                                    Object[] array = itr.next();
680    
681                                    String roleName = (String)array[0];
682                                    String actionId = (String)array[1];
683    
684                                    List<String> roleList = roleMap.get(roleName);
685    
686                                    if (roleList == null) {
687                                            roleList = new ArrayList<String>();
688                                    }
689    
690                                    roleList.add(actionId);
691    
692                                    roleMap.put(roleName, roleList);
693                            }
694    
695                            return roleMap;
696                    }
697                    catch (Exception e) {
698                            throw new SystemException(e);
699                    }
700                    finally {
701                            closeSession(session);
702                    }
703            }
704    
705            @Override
706            public List<Role> findByC_N_S_P_A(
707                            long companyId, String name, int scope, String primKey,
708                            String actionId)
709                    throws SystemException {
710    
711                    Session session = null;
712    
713                    try {
714                            session = openSession();
715    
716                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
717    
718                            SQLQuery q = session.createSQLQuery(sql);
719    
720                            q.addEntity("Role_", RoleImpl.class);
721    
722                            QueryPos qPos = QueryPos.getInstance(q);
723    
724                            qPos.add(companyId);
725                            qPos.add(name);
726                            qPos.add(scope);
727                            qPos.add(primKey);
728    
729                            ResourceAction resourceAction =
730                                    ResourceActionLocalServiceUtil.getResourceAction(
731                                            name, actionId);
732    
733                            qPos.add(resourceAction.getBitwiseValue());
734    
735                            return q.list(true);
736                    }
737                    catch (Exception e) {
738                            throw new SystemException(e);
739                    }
740                    finally {
741                            closeSession(session);
742                    }
743            }
744    
745            @Override
746            public List<Role> findByKeywords(
747                            long companyId, String keywords, Integer[] types, int start,
748                            int end, OrderByComparator obc)
749                    throws SystemException {
750    
751                    return findByKeywords(
752                            companyId, keywords, types, new LinkedHashMap<String, Object>(),
753                            start, end, obc);
754            }
755    
756            @Override
757            public List<Role> findByKeywords(
758                            long companyId, String keywords, Integer[] types,
759                            LinkedHashMap<String, Object> params, int start, int end,
760                            OrderByComparator obc)
761                    throws SystemException {
762    
763                    String[] names = null;
764                    String[] descriptions = null;
765                    boolean andOperator = false;
766    
767                    if (Validator.isNotNull(keywords)) {
768                            names = CustomSQLUtil.keywords(keywords);
769                            descriptions = CustomSQLUtil.keywords(keywords);
770                    }
771                    else {
772                            andOperator = true;
773                    }
774    
775                    return findByC_N_D_T(
776                            companyId, names, descriptions, types, params, andOperator, start,
777                            end, obc);
778            }
779    
780            @Override
781            public int filterCountByKeywords(
782                            long companyId, String keywords, Integer[] types,
783                            LinkedHashMap<String, Object> params)
784                    throws SystemException {
785    
786                    String[] names = null;
787                    String[] descriptions = null;
788                    boolean andOperator = false;
789    
790                    if (Validator.isNotNull(keywords)) {
791                            names = CustomSQLUtil.keywords(keywords);
792                            descriptions = CustomSQLUtil.keywords(keywords);
793                    }
794                    else {
795                            andOperator = true;
796                    }
797    
798                    return filterCountByC_N_D_T(
799                            companyId, names, descriptions, types, params, andOperator);
800            }
801    
802            @Override
803            public int filterCountByC_N_D_T(
804                            long companyId, String name, String description, Integer[] types,
805                            LinkedHashMap<String, Object> params, boolean andOperator)
806                    throws SystemException {
807    
808                    String[] names = CustomSQLUtil.keywords(name);
809                    String[] descriptions = CustomSQLUtil.keywords(description);
810    
811                    return filterCountByC_N_D_T(
812                            companyId, names, descriptions, types, params, andOperator);
813            }
814    
815            protected int doCountByC_N_D_T(
816                            long companyId, String[] names, String[] descriptions,
817                            Integer[] types, LinkedHashMap<String, Object> params,
818                            boolean andOperator, boolean inlineSQLHelper)
819                    throws SystemException {
820    
821                    names = CustomSQLUtil.keywords(names, true);
822                    descriptions = CustomSQLUtil.keywords(descriptions, true);
823    
824                    if (types == null) {
825                            types = new Integer[0];
826                    }
827    
828                    Session session = null;
829    
830                    try {
831                            session = openSession();
832    
833                            String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
834    
835                            sql = CustomSQLUtil.replaceKeywords(
836                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
837                            sql = CustomSQLUtil.replaceKeywords(
838                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
839                                    descriptions);
840                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
841                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
842                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
843                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
844    
845                            if (inlineSQLHelper) {
846                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
847                                            sql, Role.class.getName(), "Role_.roleId", null, null,
848                                            new long[] {0}, null);
849                            }
850    
851                            SQLQuery q = session.createSQLQuery(sql);
852    
853                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
854    
855                            QueryPos qPos = QueryPos.getInstance(q);
856    
857                            setJoin(qPos, params);
858    
859                            qPos.add(companyId);
860                            qPos.add(names, 2);
861                            qPos.add(descriptions, 2);
862                            qPos.add(types);
863    
864                            Iterator<Long> itr = q.iterate();
865    
866                            if (itr.hasNext()) {
867                                    Long count = itr.next();
868    
869                                    if (count != null) {
870                                            return count.intValue();
871                                    }
872                            }
873    
874                            return 0;
875                    }
876                    catch (Exception e) {
877                            throw new SystemException(e);
878                    }
879                    finally {
880                            closeSession(session);
881                    }
882            }
883    
884            protected int doCountByGroupRoleAndTeamRole(
885                            long companyId, String keywords, List<String> excludedNames,
886                            int[] types, long excludedTeamRoleId, long teamGroupId,
887                            boolean inlineSQLHelper)
888                    throws SystemException {
889    
890                    if ((types == null) || (types.length == 0)) {
891                            return 0;
892                    }
893    
894                    boolean andOperator = false;
895    
896                    if (Validator.isNull(keywords)) {
897                            andOperator = true;
898                    }
899    
900                    String[] keywordsArray = CustomSQLUtil.keywords(keywords, true);
901    
902                    Session session = null;
903    
904                    try {
905                            session = openSession();
906    
907                            String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ROLE_AND_TEAM_ROLE);
908    
909                            sql = CustomSQLUtil.replaceKeywords(
910                                    sql, "lower(Role_.name)", StringPool.LIKE, false,
911                                    keywordsArray);
912                            sql = CustomSQLUtil.replaceKeywords(
913                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
914                                    keywordsArray);
915                            sql = CustomSQLUtil.replaceKeywords(
916                                    sql, "lower(Team.name)", StringPool.LIKE, false, keywordsArray);
917                            sql = CustomSQLUtil.replaceKeywords(
918                                    sql, "lower(Team.description)", StringPool.LIKE, true,
919                                    keywordsArray);
920                            sql = StringUtil.replace(
921                                    sql, "[$EXCLUDED_NAMES$]", getExcludedNames(excludedNames));
922                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(
923                                    new Integer[types.length]));
924                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
925    
926                            if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
927                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
928                                            sql, Role.class.getName(), "Role_.roleId", null, null,
929                                            new long[] {0}, null);
930                            }
931    
932                            SQLQuery q = session.createSQLQuery(sql);
933    
934                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
935    
936                            QueryPos qPos = QueryPos.getInstance(q);
937    
938                            qPos.add(companyId);
939                            qPos.add(keywordsArray, 2);
940                            qPos.add(keywordsArray, 2);
941    
942                            for (String excludedName : excludedNames) {
943                                    qPos.add(excludedName);
944                            }
945    
946                            qPos.add(types);
947                            qPos.add(ClassNameLocalServiceUtil.getClassNameId(Team.class));
948                            qPos.add(excludedTeamRoleId);
949                            qPos.add(teamGroupId);
950                            qPos.add(keywordsArray, 2);
951                            qPos.add(keywordsArray, 2);
952    
953                            Iterator<Long> itr = q.iterate();
954    
955                            if (itr.hasNext()) {
956                                    Long count = itr.next();
957    
958                                    if (count != null) {
959                                            return count.intValue();
960                                    }
961                            }
962    
963                            return 0;
964                    }
965                    catch (Exception e) {
966                            throw new SystemException(e);
967                    }
968                    finally {
969                            closeSession(session);
970                    }
971            }
972    
973            protected List<Role> doFindByC_N_D_T(
974                            long companyId, String[] names, String[] descriptions,
975                            Integer[] types, LinkedHashMap<String, Object> params,
976                            boolean andOperator, int start, int end, OrderByComparator obc,
977                            boolean inlineSQLHelper)
978                    throws SystemException {
979    
980                    names = CustomSQLUtil.keywords(names, true);
981                    descriptions = CustomSQLUtil.keywords(descriptions, true);
982    
983                    if (types == null) {
984                            types = new Integer[0];
985                    }
986    
987                    Session session = null;
988    
989                    try {
990                            session = openSession();
991    
992                            String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
993    
994                            sql = CustomSQLUtil.replaceKeywords(
995                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
996                            sql = CustomSQLUtil.replaceKeywords(
997                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
998                                    descriptions);
999                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types));
1000                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
1001                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
1002                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
1003                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
1004    
1005                            if (inlineSQLHelper) {
1006                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1007                                            sql, Role.class.getName(), "Role_.roleId", null, null,
1008                                            new long[] {0}, null);
1009                            }
1010    
1011                            SQLQuery q = session.createSQLQuery(sql);
1012    
1013                            q.addEntity("Role_", RoleImpl.class);
1014    
1015                            QueryPos qPos = QueryPos.getInstance(q);
1016    
1017                            setJoin(qPos, params);
1018    
1019                            qPos.add(companyId);
1020                            qPos.add(names, 2);
1021                            qPos.add(descriptions, 2);
1022                            qPos.add(types);
1023    
1024                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
1025                    }
1026                    catch (Exception e) {
1027                            throw new SystemException(e);
1028                    }
1029                    finally {
1030                            closeSession(session);
1031                    }
1032            }
1033    
1034            protected List<Role> doFindByGroupRoleAndTeamRole(
1035                            long companyId, String keywords, List<String> excludedNames,
1036                            int[] types, long excludedTeamRoleId, long teamGroupId, int start,
1037                            int end, boolean inlineSQLHelper)
1038                    throws SystemException {
1039    
1040                    if ((types == null) || (types.length == 0)) {
1041                            return Collections.emptyList();
1042                    }
1043    
1044                    boolean andOperator = false;
1045    
1046                    if (Validator.isNull(keywords)) {
1047                            andOperator = true;
1048                    }
1049    
1050                    String[] keywordsArray = CustomSQLUtil.keywords(keywords, true);
1051    
1052                    Session session = null;
1053    
1054                    try {
1055                            session = openSession();
1056    
1057                            String sql = CustomSQLUtil.get(FIND_BY_GROUP_ROLE_AND_TEAM_ROLE);
1058    
1059                            sql = CustomSQLUtil.replaceKeywords(
1060                                    sql, "lower(Role_.name)", StringPool.LIKE, false,
1061                                    keywordsArray);
1062                            sql = CustomSQLUtil.replaceKeywords(
1063                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
1064                                    keywordsArray);
1065                            sql = CustomSQLUtil.replaceKeywords(
1066                                    sql, "lower(Team.name)", StringPool.LIKE, false, keywordsArray);
1067                            sql = CustomSQLUtil.replaceKeywords(
1068                                    sql, "lower(Team.description)", StringPool.LIKE, true,
1069                                    keywordsArray);
1070                            sql = StringUtil.replace(
1071                                    sql, "[$EXCLUDED_NAMES$]", getExcludedNames(excludedNames));
1072                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(
1073                                    new Integer[types.length]));
1074                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
1075    
1076                            if (inlineSQLHelper && InlineSQLHelperUtil.isEnabled()) {
1077                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1078                                            sql, Role.class.getName(), "Role_.roleId", null, null,
1079                                            new long[] {0}, null);
1080                            }
1081    
1082                            SQLQuery q = session.createSQLQuery(sql);
1083    
1084                            q.addEntity("Role_", RoleImpl.class);
1085    
1086                            QueryPos qPos = QueryPos.getInstance(q);
1087    
1088                            qPos.add(companyId);
1089                            qPos.add(keywordsArray, 2);
1090                            qPos.add(keywordsArray, 2);
1091    
1092                            for (String excludedName : excludedNames) {
1093                                    qPos.add(excludedName);
1094                            }
1095    
1096                            qPos.add(types);
1097                            qPos.add(ClassNameLocalServiceUtil.getClassNameId(Team.class));
1098                            qPos.add(excludedTeamRoleId);
1099                            qPos.add(teamGroupId);
1100                            qPos.add(keywordsArray, 2);
1101                            qPos.add(keywordsArray, 2);
1102    
1103                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
1104                    }
1105                    catch (Exception e) {
1106                            throw new SystemException(e);
1107                    }
1108                    finally {
1109                            closeSession(session);
1110                    }
1111            }
1112    
1113            protected String getCountByR_U_SQL() {
1114                    if (_countByR_U == null) {
1115                            StringBundler sb = new StringBundler(13);
1116    
1117                            sb.append(StringPool.OPEN_PARENTHESIS);
1118                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
1119                            sb.append(") UNION (");
1120                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
1121                            sb.append(") UNION (");
1122                            sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
1123                            sb.append(") UNION (");
1124                            sb.append(CustomSQLUtil.get(COUNT_BY_USER));
1125                            sb.append(") UNION (");
1126                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
1127                            sb.append(") UNION (");
1128                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
1129                            sb.append(StringPool.CLOSE_PARENTHESIS);
1130    
1131                            _countByR_U = sb.toString();
1132                    }
1133    
1134                    return _countByR_U;
1135            }
1136    
1137            protected String getExcludedNames(List<String> excludedNames) {
1138                    if ((excludedNames == null) || excludedNames.isEmpty()) {
1139                            return StringPool.BLANK;
1140                    }
1141    
1142                    StringBundler sb = new StringBundler(excludedNames.size() + 1);
1143    
1144                    sb.append(" AND (");
1145    
1146                    for (int i = 0; i < excludedNames.size() - 1; i++) {
1147                            sb.append("Role_.name != ? AND ");
1148                    }
1149    
1150                    sb.append("Role_.name != ?)");
1151    
1152                    return sb.toString();
1153            }
1154    
1155            protected String getGroupIds(long[] groupIds, String table) {
1156                    if (groupIds.length == 0) {
1157                            return StringPool.BLANK;
1158                    }
1159    
1160                    StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
1161    
1162                    for (int i = 0; i < groupIds.length; i++) {
1163                            sb.append(table);
1164                            sb.append(".groupId = ?");
1165    
1166                            if ((i + 1) < groupIds.length) {
1167                                    sb.append(" OR ");
1168                            }
1169                    }
1170    
1171                    return sb.toString();
1172            }
1173    
1174            protected String getJoin(LinkedHashMap<String, Object> params) {
1175                    if ((params == null) || params.isEmpty()) {
1176                            return StringPool.BLANK;
1177                    }
1178    
1179                    StringBundler sb = new StringBundler(params.size());
1180    
1181                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1182                            String key = entry.getKey();
1183                            Object value = entry.getValue();
1184    
1185                            if (Validator.isNotNull(value)) {
1186                                    sb.append(getJoin(key));
1187                            }
1188                    }
1189    
1190                    return sb.toString();
1191            }
1192    
1193            protected String getJoin(String key) {
1194                    String join = StringPool.BLANK;
1195    
1196                    if (key.equals("usersRoles")) {
1197                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
1198                    }
1199    
1200                    if (Validator.isNotNull(join)) {
1201                            int pos = join.indexOf("WHERE");
1202    
1203                            if (pos != -1) {
1204                                    join = join.substring(0, pos);
1205                            }
1206                    }
1207    
1208                    return join;
1209            }
1210    
1211            protected String getTypes(Integer[] types) {
1212                    if (types.length == 0) {
1213                            return StringPool.BLANK;
1214                    }
1215    
1216                    StringBundler sb = new StringBundler(types.length * 2);
1217    
1218                    sb.append(" AND (");
1219    
1220                    for (int i = 0; i < types.length; i++) {
1221                            sb.append("Role_.type_ = ?");
1222    
1223                            if ((i + 1) < types.length) {
1224                                    sb.append(" OR ");
1225                            }
1226                    }
1227    
1228                    sb.append(StringPool.CLOSE_PARENTHESIS);
1229    
1230                    return sb.toString();
1231            }
1232    
1233            protected String getWhere(LinkedHashMap<String, Object> params) {
1234                    if ((params == null) || params.isEmpty()) {
1235                            return StringPool.BLANK;
1236                    }
1237    
1238                    StringBundler sb = new StringBundler(params.size());
1239    
1240                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1241                            String key = entry.getKey();
1242                            Object value = entry.getValue();
1243    
1244                            if (Validator.isNotNull(value)) {
1245                                    sb.append(getWhere(key));
1246                            }
1247                    }
1248    
1249                    return sb.toString();
1250            }
1251    
1252            protected String getWhere(String key) {
1253                    String join = StringPool.BLANK;
1254    
1255                    if (key.equals("usersRoles")) {
1256                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
1257                    }
1258    
1259                    if (Validator.isNotNull(join)) {
1260                            int pos = join.indexOf("WHERE");
1261    
1262                            if (pos != -1) {
1263                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
1264                            }
1265                            else {
1266                                    join = StringPool.BLANK;
1267                            }
1268                    }
1269    
1270                    return join;
1271            }
1272    
1273            protected void setJoin(
1274                    QueryPos qPos, LinkedHashMap<String, Object> params) {
1275    
1276                    if (params == null) {
1277                            return;
1278                    }
1279    
1280                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1281                            Object value = entry.getValue();
1282    
1283                            if (value instanceof Long) {
1284                                    Long valueLong = (Long)value;
1285    
1286                                    if (Validator.isNotNull(valueLong)) {
1287                                            qPos.add(valueLong);
1288                                    }
1289                            }
1290                            else if (value instanceof String) {
1291                                    String valueString = (String)value;
1292    
1293                                    if (Validator.isNotNull(valueString)) {
1294                                            qPos.add(valueString);
1295                                    }
1296                            }
1297                    }
1298            }
1299    
1300            private String _countByR_U;
1301    
1302    }