001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import com.liferay.portal.exception.NoSuchRoleException;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.ResourceAction;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.impl.RoleImpl;
033    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
034    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
035    import com.liferay.portal.service.persistence.RoleFinder;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
038    import java.util.ArrayList;
039    import java.util.HashMap;
040    import java.util.Iterator;
041    import java.util.LinkedHashMap;
042    import java.util.List;
043    import java.util.Map;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Marcellus Tavares
048     * @author Connor McKay
049     */
050    public class RoleFinderImpl extends RoleFinderBaseImpl implements RoleFinder {
051    
052            public static final String COUNT_BY_ORGANIZATION =
053                    RoleFinder.class.getName() + ".countByOrganization";
054    
055            public static final String COUNT_BY_ORGANIZATION_SITE =
056                    RoleFinder.class.getName() + ".countByOrganizationSite";
057    
058            public static final String COUNT_BY_SITE =
059                    RoleFinder.class.getName() + ".countBySite";
060    
061            public static final String COUNT_BY_USER =
062                    RoleFinder.class.getName() + ".countByUser";
063    
064            public static final String COUNT_BY_USER_GROUP =
065                    RoleFinder.class.getName() + ".countByUserGroup";
066    
067            public static final String COUNT_BY_USER_GROUP_GROUP_ROLE =
068                    RoleFinder.class.getName() + ".countByUserGroupGroupRole";
069    
070            public static final String COUNT_BY_USER_GROUP_SITE =
071                    RoleFinder.class.getName() + ".countByUserGroupSite";
072    
073            public static final String COUNT_BY_U_G_R =
074                    RoleFinder.class.getName() + ".countByU_G_R";
075    
076            public static final String COUNT_BY_C_N_D_T =
077                    RoleFinder.class.getName() + ".countByC_N_D_T";
078    
079            public static final String FIND_BY_SYSTEM =
080                    RoleFinder.class.getName() + ".findBySystem";
081    
082            public static final String FIND_BY_USER_GROUP_GROUP_ROLE =
083                    RoleFinder.class.getName() + ".findByUserGroupGroupRole";
084    
085            public static final String FIND_BY_USER_GROUP_ROLE =
086                    RoleFinder.class.getName() + ".findByUserGroupRole";
087    
088            public static final String FIND_BY_C_N =
089                    RoleFinder.class.getName() + ".findByC_N";
090    
091            public static final String FIND_BY_U_G =
092                    RoleFinder.class.getName() + ".findByU_G";
093    
094            public static final String FIND_BY_R_N_A =
095                    RoleFinder.class.getName() + ".findByR_N_A";
096    
097            public static final String FIND_BY_C_N_D_T =
098                    RoleFinder.class.getName() + ".findByC_N_D_T";
099    
100            public static final String FIND_BY_C_N_S_P =
101                    RoleFinder.class.getName() + ".findByC_N_S_P";
102    
103            public static final String FIND_BY_C_N_S_P_A =
104                    RoleFinder.class.getName() + ".findByC_N_S_P_A";
105    
106            public static final String JOIN_BY_USERS_ROLES =
107                    RoleFinder.class.getName() + ".joinByUsersRoles";
108    
109            @Override
110            public int countByKeywords(
111                    long companyId, String keywords, Integer[] types) {
112    
113                    return countByKeywords(
114                            companyId, keywords, types, new LinkedHashMap<String, Object>());
115            }
116    
117            @Override
118            public int countByKeywords(
119                    long companyId, String keywords, Integer[] types,
120                    LinkedHashMap<String, Object> params) {
121    
122                    String[] names = null;
123                    String[] descriptions = null;
124                    boolean andOperator = false;
125    
126                    if (Validator.isNotNull(keywords)) {
127                            names = CustomSQLUtil.keywords(keywords);
128                            descriptions = CustomSQLUtil.keywords(keywords);
129                    }
130                    else {
131                            andOperator = true;
132                    }
133    
134                    return countByC_N_D_T(
135                            companyId, names, descriptions, types, params, andOperator);
136            }
137    
138            @Override
139            public int countByUserGroupGroupRole(long userId, long groupId) {
140                    Session session = null;
141    
142                    try {
143                            session = openSession();
144    
145                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_GROUP_ROLE);
146    
147                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
148    
149                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
150    
151                            QueryPos qPos = QueryPos.getInstance(q);
152    
153                            qPos.add(groupId);
154                            qPos.add(userId);
155    
156                            Iterator<Long> itr = q.iterate();
157    
158                            if (itr.hasNext()) {
159                                    Long count = itr.next();
160    
161                                    if (count != null) {
162                                            return count.intValue();
163                                    }
164                            }
165    
166                            return 0;
167                    }
168                    catch (Exception e) {
169                            throw new SystemException(e);
170                    }
171                    finally {
172                            closeSession(session);
173                    }
174            }
175    
176            @Override
177            public int countByR_U(long roleId, long userId) {
178                    Session session = null;
179    
180                    try {
181                            session = openSession();
182    
183                            SQLQuery q = session.createSynchronizedSQLQuery(
184                                    getCountByR_U_SQL());
185    
186                            QueryPos qPos = QueryPos.getInstance(q);
187    
188                            for (int i = 0; i < 6; i++) {
189                                    qPos.add(roleId);
190                                    qPos.add(userId);
191                            }
192    
193                            return q.list().size();
194                    }
195                    catch (Exception e) {
196                            throw new SystemException(e);
197                    }
198                    finally {
199                            closeSession(session);
200                    }
201            }
202    
203            @Override
204            public int countByU_G_R(long userId, long groupId, long roleId) {
205                    Session session = null;
206    
207                    try {
208                            session = openSession();
209    
210                            String sql = CustomSQLUtil.get(COUNT_BY_U_G_R);
211    
212                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
213    
214                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
215    
216                            QueryPos qPos = QueryPos.getInstance(q);
217    
218                            qPos.add(roleId);
219                            qPos.add(groupId);
220                            qPos.add(userId);
221    
222                            Iterator<Long> itr = q.iterate();
223    
224                            if (itr.hasNext()) {
225                                    Long count = itr.next();
226    
227                                    if (count != null) {
228                                            return count.intValue();
229                                    }
230                            }
231    
232                            return 0;
233                    }
234                    catch (Exception e) {
235                            throw new SystemException(e);
236                    }
237                    finally {
238                            closeSession(session);
239                    }
240            }
241    
242            @Override
243            public int countByC_N_D_T(
244                    long companyId, String name, String description, Integer[] types,
245                    LinkedHashMap<String, Object> params, boolean andOperator) {
246    
247                    String[] names = CustomSQLUtil.keywords(name);
248                    String[] descriptions = CustomSQLUtil.keywords(description);
249    
250                    return countByC_N_D_T(
251                            companyId, names, descriptions, types, params, andOperator);
252            }
253    
254            @Override
255            public int countByC_N_D_T(
256                    long companyId, String[] names, String[] descriptions, Integer[] types,
257                    LinkedHashMap<String, Object> params, boolean andOperator) {
258    
259                    return doCountByC_N_D_T(
260                            companyId, names, descriptions, types, params, andOperator, false);
261            }
262    
263            @Override
264            public int filterCountByKeywords(
265                    long companyId, String keywords, Integer[] types,
266                    LinkedHashMap<String, Object> params) {
267    
268                    String[] names = null;
269                    String[] descriptions = null;
270                    boolean andOperator = false;
271    
272                    if (Validator.isNotNull(keywords)) {
273                            names = CustomSQLUtil.keywords(keywords);
274                            descriptions = CustomSQLUtil.keywords(keywords);
275                    }
276                    else {
277                            andOperator = true;
278                    }
279    
280                    return filterCountByC_N_D_T(
281                            companyId, names, descriptions, types, params, andOperator);
282            }
283    
284            @Override
285            public int filterCountByC_N_D_T(
286                    long companyId, String name, String description, Integer[] types,
287                    LinkedHashMap<String, Object> params, boolean andOperator) {
288    
289                    String[] names = CustomSQLUtil.keywords(name);
290                    String[] descriptions = CustomSQLUtil.keywords(description);
291    
292                    return filterCountByC_N_D_T(
293                            companyId, names, descriptions, types, params, andOperator);
294            }
295    
296            @Override
297            public int filterCountByC_N_D_T(
298                    long companyId, String[] names, String[] descriptions, Integer[] types,
299                    LinkedHashMap<String, Object> params, boolean andOperator) {
300    
301                    return doCountByC_N_D_T(
302                            companyId, names, descriptions, types, params, andOperator, true);
303            }
304    
305            @Override
306            public List<Role> filterFindByKeywords(
307                    long companyId, String keywords, Integer[] types,
308                    LinkedHashMap<String, Object> params, int start, int end,
309                    OrderByComparator<Role> obc) {
310    
311                    String[] names = null;
312                    String[] descriptions = null;
313                    boolean andOperator = false;
314    
315                    if (Validator.isNotNull(keywords)) {
316                            names = CustomSQLUtil.keywords(keywords);
317                            descriptions = CustomSQLUtil.keywords(keywords);
318                    }
319                    else {
320                            andOperator = true;
321                    }
322    
323                    return filterFindByC_N_D_T(
324                            companyId, names, descriptions, types, params, andOperator, start,
325                            end, obc);
326            }
327    
328            @Override
329            public List<Role> filterFindByC_N_D_T(
330                    long companyId, String name, String description, Integer[] types,
331                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
332                    int end, OrderByComparator<Role> obc) {
333    
334                    String[] names = CustomSQLUtil.keywords(name);
335                    String[] descriptions = CustomSQLUtil.keywords(description);
336    
337                    return filterFindByC_N_D_T(
338                            companyId, names, descriptions, types, params, andOperator, start,
339                            end, obc);
340            }
341    
342            @Override
343            public List<Role> filterFindByC_N_D_T(
344                    long companyId, String[] names, String[] descriptions, Integer[] types,
345                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
346                    int end, OrderByComparator<Role> obc) {
347    
348                    return doFindByC_N_D_T(
349                            companyId, names, descriptions, types, params, andOperator, start,
350                            end, obc, true);
351            }
352    
353            @Override
354            public List<Role> findByKeywords(
355                    long companyId, String keywords, Integer[] types, int start, int end,
356                    OrderByComparator<Role> obc) {
357    
358                    return findByKeywords(
359                            companyId, keywords, types, new LinkedHashMap<String, Object>(),
360                            start, end, obc);
361            }
362    
363            @Override
364            public List<Role> findByKeywords(
365                    long companyId, String keywords, Integer[] types,
366                    LinkedHashMap<String, Object> params, int start, int end,
367                    OrderByComparator<Role> obc) {
368    
369                    String[] names = null;
370                    String[] descriptions = null;
371                    boolean andOperator = false;
372    
373                    if (Validator.isNotNull(keywords)) {
374                            names = CustomSQLUtil.keywords(keywords);
375                            descriptions = CustomSQLUtil.keywords(keywords);
376                    }
377                    else {
378                            andOperator = true;
379                    }
380    
381                    return findByC_N_D_T(
382                            companyId, names, descriptions, types, params, andOperator, start,
383                            end, obc);
384            }
385    
386            @Override
387            public List<Role> findBySystem(long companyId) {
388                    Session session = null;
389    
390                    try {
391                            session = openSession();
392    
393                            String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
394    
395                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
396    
397                            q.addEntity("Role_", RoleImpl.class);
398    
399                            QueryPos qPos = QueryPos.getInstance(q);
400    
401                            qPos.add(companyId);
402    
403                            return q.list(true);
404                    }
405                    catch (Exception e) {
406                            throw new SystemException(e);
407                    }
408                    finally {
409                            closeSession(session);
410                    }
411            }
412    
413            @Override
414            public List<Role> findByUserGroupGroupRole(long userId, long groupId) {
415                    return findByUserGroupGroupRole(
416                            userId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
417            }
418    
419            @Override
420            public List<Role> findByUserGroupGroupRole(
421                    long userId, long groupId, int start, int end) {
422    
423                    Session session = null;
424    
425                    try {
426                            session = openSession();
427    
428                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_GROUP_ROLE);
429    
430                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
431    
432                            q.addEntity("Role_", RoleImpl.class);
433    
434                            QueryPos qPos = QueryPos.getInstance(q);
435    
436                            qPos.add(userId);
437                            qPos.add(groupId);
438    
439                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
440                    }
441                    catch (Exception e) {
442                            throw new SystemException(e);
443                    }
444                    finally {
445                            closeSession(session);
446                    }
447            }
448    
449            @Override
450            public List<Role> findByUserGroupRole(long userId, long groupId) {
451                    Session session = null;
452    
453                    try {
454                            session = openSession();
455    
456                            String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
457    
458                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
459    
460                            q.addEntity("Role_", RoleImpl.class);
461    
462                            QueryPos qPos = QueryPos.getInstance(q);
463    
464                            qPos.add(userId);
465                            qPos.add(groupId);
466    
467                            return q.list(true);
468                    }
469                    catch (Exception e) {
470                            throw new SystemException(e);
471                    }
472                    finally {
473                            closeSession(session);
474                    }
475            }
476    
477            @Override
478            public Role findByC_N(long companyId, String name)
479                    throws NoSuchRoleException {
480    
481                    name = StringUtil.lowerCase(name);
482    
483                    Session session = null;
484    
485                    try {
486                            session = openSession();
487    
488                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
489    
490                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
491    
492                            q.addEntity("Role_", RoleImpl.class);
493    
494                            QueryPos qPos = QueryPos.getInstance(q);
495    
496                            qPos.add(companyId);
497                            qPos.add(name);
498    
499                            List<Role> roles = q.list();
500    
501                            if (!roles.isEmpty()) {
502                                    return roles.get(0);
503                            }
504                    }
505                    catch (Exception e) {
506                            throw new SystemException(e);
507                    }
508                    finally {
509                            closeSession(session);
510                    }
511    
512                    StringBundler sb = new StringBundler(5);
513    
514                    sb.append("No Role exists with the key {companyId=");
515                    sb.append(companyId);
516                    sb.append(", name=");
517                    sb.append(name);
518                    sb.append("}");
519    
520                    throw new NoSuchRoleException(sb.toString());
521            }
522    
523            @Override
524            public List<Role> findByU_G(long userId, List<Group> groups) {
525                    long[] groupIds = new long[groups.size()];
526    
527                    for (int i = 0; i < groups.size(); i++) {
528                            Group group = groups.get(i);
529    
530                            groupIds[i] = group.getGroupId();
531                    }
532    
533                    return findByU_G(userId, groupIds);
534            }
535    
536            @Override
537            public List<Role> findByU_G(long userId, long groupId) {
538                    return findByU_G(userId, new long[] {groupId});
539            }
540    
541            @Override
542            public List<Role> findByU_G(long userId, long[] groupIds) {
543                    Session session = null;
544    
545                    try {
546                            session = openSession();
547    
548                            String sql = CustomSQLUtil.get(FIND_BY_U_G);
549    
550                            sql = StringUtil.replace(
551                                    sql, "[$GROUP_ID$]", getGroupIds(groupIds, "Groups_Roles"));
552    
553                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
554    
555                            q.addEntity("Role_", RoleImpl.class);
556    
557                            QueryPos qPos = QueryPos.getInstance(q);
558    
559                            qPos.add(userId);
560                            qPos.add(groupIds);
561    
562                            return q.list(true);
563                    }
564                    catch (Exception e) {
565                            throw new SystemException(e);
566                    }
567                    finally {
568                            closeSession(session);
569                    }
570            }
571    
572            @Override
573            public List<Role> findByR_N_A(
574                    long resourceBlockId, String className, String actionId) {
575    
576                    Session session = null;
577    
578                    try {
579                            session = openSession();
580    
581                            String sql = CustomSQLUtil.get(FIND_BY_R_N_A);
582    
583                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
584    
585                            q.addEntity("Role_", RoleImpl.class);
586    
587                            QueryPos qPos = QueryPos.getInstance(q);
588    
589                            qPos.add(resourceBlockId);
590                            qPos.add(className);
591    
592                            ResourceAction resourceAction =
593                                    ResourceActionLocalServiceUtil.getResourceAction(
594                                            className, actionId);
595    
596                            qPos.add(resourceAction.getBitwiseValue());
597    
598                            return q.list(true);
599                    }
600                    catch (Exception e) {
601                            throw new SystemException(e);
602                    }
603                    finally {
604                            closeSession(session);
605                    }
606            }
607    
608            @Override
609            public List<Role> findByC_N_D_T(
610                    long companyId, String name, String description, Integer[] types,
611                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
612                    int end, OrderByComparator<Role> obc) {
613    
614                    String[] names = CustomSQLUtil.keywords(name);
615                    String[] descriptions = CustomSQLUtil.keywords(description);
616    
617                    return findByC_N_D_T(
618                            companyId, names, descriptions, types, params, andOperator, start,
619                            end, obc);
620            }
621    
622            @Override
623            public List<Role> findByC_N_D_T(
624                    long companyId, String[] names, String[] descriptions, Integer[] types,
625                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
626                    int end, OrderByComparator<Role> obc) {
627    
628                    return doFindByC_N_D_T(
629                            companyId, names, descriptions, types, params, andOperator, start,
630                            end, obc, false);
631            }
632    
633            @Override
634            public Map<String, List<String>> findByC_N_S_P(
635                    long companyId, String name, int scope, String primKey) {
636    
637                    Session session = null;
638    
639                    try {
640                            session = openSession();
641    
642                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
643    
644                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
645    
646                            q.addScalar("roleName", Type.STRING);
647                            q.addScalar("actionId", Type.STRING);
648    
649                            QueryPos qPos = QueryPos.getInstance(q);
650    
651                            qPos.add(companyId);
652                            qPos.add(name);
653                            qPos.add(scope);
654                            qPos.add(primKey);
655    
656                            Map<String, List<String>> roleMap = new HashMap<>();
657    
658                            Iterator<Object[]> itr = q.iterate();
659    
660                            while (itr.hasNext()) {
661                                    Object[] array = itr.next();
662    
663                                    String roleName = (String)array[0];
664                                    String actionId = (String)array[1];
665    
666                                    List<String> roleList = roleMap.get(roleName);
667    
668                                    if (roleList == null) {
669                                            roleList = new ArrayList<>();
670                                    }
671    
672                                    roleList.add(actionId);
673    
674                                    roleMap.put(roleName, roleList);
675                            }
676    
677                            return roleMap;
678                    }
679                    catch (Exception e) {
680                            throw new SystemException(e);
681                    }
682                    finally {
683                            closeSession(session);
684                    }
685            }
686    
687            @Override
688            public List<Role> findByC_N_S_P_A(
689                    long companyId, String name, int scope, String primKey,
690                    String actionId) {
691    
692                    Session session = null;
693    
694                    try {
695                            session = openSession();
696    
697                            String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P_A);
698    
699                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
700    
701                            q.addEntity("Role_", RoleImpl.class);
702    
703                            QueryPos qPos = QueryPos.getInstance(q);
704    
705                            qPos.add(companyId);
706                            qPos.add(name);
707                            qPos.add(scope);
708                            qPos.add(primKey);
709    
710                            ResourceAction resourceAction =
711                                    ResourceActionLocalServiceUtil.getResourceAction(
712                                            name, actionId);
713    
714                            qPos.add(resourceAction.getBitwiseValue());
715    
716                            return q.list(true);
717                    }
718                    catch (Exception e) {
719                            throw new SystemException(e);
720                    }
721                    finally {
722                            closeSession(session);
723                    }
724            }
725    
726            protected int doCountByC_N_D_T(
727                    long companyId, String[] names, String[] descriptions, Integer[] types,
728                    LinkedHashMap<String, Object> params, boolean andOperator,
729                    boolean inlineSQLHelper) {
730    
731                    names = CustomSQLUtil.keywords(names, true);
732                    descriptions = CustomSQLUtil.keywords(descriptions, true);
733    
734                    if (types == null) {
735                            types = new Integer[0];
736                    }
737    
738                    Session session = null;
739    
740                    try {
741                            session = openSession();
742    
743                            String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
744    
745                            sql = CustomSQLUtil.replaceKeywords(
746                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
747                            sql = CustomSQLUtil.replaceKeywords(
748                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
749                                    descriptions);
750                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types.length));
751                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
752                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
753                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
754    
755                            if (inlineSQLHelper) {
756                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
757                                            sql, Role.class.getName(), "Role_.roleId", null, null,
758                                            new long[] {0}, null);
759                            }
760    
761                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
762    
763                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
764    
765                            QueryPos qPos = QueryPos.getInstance(q);
766    
767                            setJoin(qPos, params);
768    
769                            qPos.add(companyId);
770                            qPos.add(names, 2);
771                            qPos.add(descriptions, 2);
772                            qPos.add(types);
773    
774                            Iterator<Long> itr = q.iterate();
775    
776                            if (itr.hasNext()) {
777                                    Long count = itr.next();
778    
779                                    if (count != null) {
780                                            return count.intValue();
781                                    }
782                            }
783    
784                            return 0;
785                    }
786                    catch (Exception e) {
787                            throw new SystemException(e);
788                    }
789                    finally {
790                            closeSession(session);
791                    }
792            }
793    
794            protected List<Role> doFindByC_N_D_T(
795                    long companyId, String[] names, String[] descriptions, Integer[] types,
796                    LinkedHashMap<String, Object> params, boolean andOperator, int start,
797                    int end, OrderByComparator<Role> obc, boolean inlineSQLHelper) {
798    
799                    names = CustomSQLUtil.keywords(names, true);
800                    descriptions = CustomSQLUtil.keywords(descriptions, true);
801    
802                    if (types == null) {
803                            types = new Integer[0];
804                    }
805    
806                    Session session = null;
807    
808                    try {
809                            session = openSession();
810    
811                            String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
812    
813                            sql = CustomSQLUtil.replaceKeywords(
814                                    sql, "lower(Role_.name)", StringPool.LIKE, false, names);
815                            sql = CustomSQLUtil.replaceKeywords(
816                                    sql, "lower(Role_.description)", StringPool.LIKE, true,
817                                    descriptions);
818                            sql = StringUtil.replace(sql, "[$TYPE$]", getTypes(types.length));
819                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
820                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
821                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
822                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
823    
824                            if (inlineSQLHelper) {
825                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
826                                            sql, Role.class.getName(), "Role_.roleId", null, null,
827                                            new long[] {0}, null);
828                            }
829    
830                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
831    
832                            q.addEntity("Role_", RoleImpl.class);
833    
834                            QueryPos qPos = QueryPos.getInstance(q);
835    
836                            setJoin(qPos, params);
837    
838                            qPos.add(companyId);
839                            qPos.add(names, 2);
840                            qPos.add(descriptions, 2);
841                            qPos.add(types);
842    
843                            return (List<Role>)QueryUtil.list(q, getDialect(), start, end);
844                    }
845                    catch (Exception e) {
846                            throw new SystemException(e);
847                    }
848                    finally {
849                            closeSession(session);
850                    }
851            }
852    
853            protected String getCountByR_U_SQL() {
854                    if (_countByR_U == null) {
855                            StringBundler sb = new StringBundler(13);
856    
857                            sb.append(StringPool.OPEN_PARENTHESIS);
858                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
859                            sb.append(") UNION (");
860                            sb.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION_SITE));
861                            sb.append(") UNION (");
862                            sb.append(CustomSQLUtil.get(COUNT_BY_SITE));
863                            sb.append(") UNION (");
864                            sb.append(CustomSQLUtil.get(COUNT_BY_USER));
865                            sb.append(") UNION (");
866                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
867                            sb.append(") UNION (");
868                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_SITE));
869                            sb.append(StringPool.CLOSE_PARENTHESIS);
870    
871                            _countByR_U = sb.toString();
872                    }
873    
874                    return _countByR_U;
875            }
876    
877            protected String getGroupIds(long[] groupIds, String table) {
878                    if (groupIds.length == 0) {
879                            return StringPool.BLANK;
880                    }
881    
882                    StringBundler sb = new StringBundler(groupIds.length * 3 - 1);
883    
884                    for (int i = 0; i < groupIds.length; i++) {
885                            sb.append(table);
886                            sb.append(".groupId = ?");
887    
888                            if ((i + 1) < groupIds.length) {
889                                    sb.append(" OR ");
890                            }
891                    }
892    
893                    return sb.toString();
894            }
895    
896            protected String getJoin(LinkedHashMap<String, Object> params) {
897                    if ((params == null) || params.isEmpty()) {
898                            return StringPool.BLANK;
899                    }
900    
901                    StringBundler sb = new StringBundler(params.size());
902    
903                    for (Map.Entry<String, Object> entry : params.entrySet()) {
904                            String key = entry.getKey();
905                            Object value = entry.getValue();
906    
907                            if (Validator.isNotNull(value)) {
908                                    sb.append(getJoin(key));
909                            }
910                    }
911    
912                    return sb.toString();
913            }
914    
915            protected String getJoin(String key) {
916                    String join = StringPool.BLANK;
917    
918                    if (key.equals("usersRoles")) {
919                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
920                    }
921    
922                    if (Validator.isNotNull(join)) {
923                            int pos = join.indexOf("WHERE");
924    
925                            if (pos != -1) {
926                                    join = join.substring(0, pos);
927                            }
928                    }
929    
930                    return join;
931            }
932    
933            protected String getTypes(int size) {
934                    if (size == 0) {
935                            return StringPool.BLANK;
936                    }
937    
938                    StringBundler sb = new StringBundler(size + 1);
939    
940                    sb.append(" AND (");
941    
942                    for (int i = 0; i < size - 1; i++) {
943                            sb.append("Role_.type_ = ? OR ");
944                    }
945    
946                    sb.append("Role_.type_ = ?)");
947    
948                    return sb.toString();
949            }
950    
951            protected String getWhere(LinkedHashMap<String, Object> params) {
952                    if ((params == null) || params.isEmpty()) {
953                            return StringPool.BLANK;
954                    }
955    
956                    StringBundler sb = new StringBundler(params.size());
957    
958                    for (Map.Entry<String, Object> entry : params.entrySet()) {
959                            String key = entry.getKey();
960                            Object value = entry.getValue();
961    
962                            if (Validator.isNotNull(value)) {
963                                    sb.append(getWhere(key));
964                            }
965                    }
966    
967                    return sb.toString();
968            }
969    
970            protected String getWhere(String key) {
971                    String join = StringPool.BLANK;
972    
973                    if (key.equals("usersRoles")) {
974                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
975                    }
976    
977                    if (Validator.isNotNull(join)) {
978                            int pos = join.indexOf("WHERE");
979    
980                            if (pos != -1) {
981                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
982                            }
983                            else {
984                                    join = StringPool.BLANK;
985                            }
986                    }
987    
988                    return join;
989            }
990    
991            protected void setJoin(
992                    QueryPos qPos, LinkedHashMap<String, Object> params) {
993    
994                    if (params == null) {
995                            return;
996                    }
997    
998                    for (Map.Entry<String, Object> entry : params.entrySet()) {
999                            Object value = entry.getValue();
1000    
1001                            if (value instanceof Long) {
1002                                    Long valueLong = (Long)value;
1003    
1004                                    if (Validator.isNotNull(valueLong)) {
1005                                            qPos.add(valueLong);
1006                                    }
1007                            }
1008                            else if (value instanceof String) {
1009                                    String valueString = (String)value;
1010    
1011                                    if (Validator.isNotNull(valueString)) {
1012                                            qPos.add(valueString);
1013                                    }
1014                            }
1015                    }
1016            }
1017    
1018            private String _countByR_U;
1019    
1020    }