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