001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.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.ArrayUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.OrderByComparator;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.StringUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.Group;
032    import com.liferay.portal.model.GroupConstants;
033    import com.liferay.portal.model.Organization;
034    import com.liferay.portal.model.ResourceAction;
035    import com.liferay.portal.model.impl.GroupImpl;
036    import com.liferay.portal.service.ClassNameLocalServiceUtil;
037    import com.liferay.portal.service.ResourceActionLocalServiceUtil;
038    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
039    import com.liferay.portal.service.impl.GroupLocalServiceImpl;
040    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
041    import com.liferay.portal.util.comparator.GroupNameComparator;
042    import com.liferay.util.dao.orm.CustomSQLUtil;
043    
044    import java.util.ArrayList;
045    import java.util.Collection;
046    import java.util.HashMap;
047    import java.util.HashSet;
048    import java.util.Iterator;
049    import java.util.LinkedHashMap;
050    import java.util.List;
051    import java.util.Map;
052    import java.util.Set;
053    import java.util.concurrent.ConcurrentHashMap;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     * @author Shuyang Zhou
058     */
059    public class GroupFinderImpl
060            extends BasePersistenceImpl<Group> implements GroupFinder {
061    
062            public static final String COUNT_BY_LAYOUTS =
063                    GroupFinder.class.getName() + ".countByLayouts";
064    
065            public static final String COUNT_BY_GROUP_ID =
066                    GroupFinder.class.getName() + ".countByGroupId";
067    
068            public static final String COUNT_BY_C_PG_N_D =
069                    GroupFinder.class.getName() + ".countByC_PG_N_D";
070    
071            public static final String FIND_BY_COMPANY_ID =
072                    GroupFinder.class.getName() + ".findByCompanyId";
073    
074            public static final String FIND_BY_LAYOUTS =
075                    GroupFinder.class.getName() + ".findByLayouts";
076    
077            public static final String FIND_BY_LIVE_GROUPS =
078                    GroupFinder.class.getName() + ".findByLiveGroups";
079    
080            public static final String FIND_BY_NO_LAYOUTS =
081                    GroupFinder.class.getName() + ".findByNoLayouts";
082    
083            public static final String FIND_BY_NULL_FRIENDLY_URL =
084                    GroupFinder.class.getName() + ".findByNullFriendlyURL";
085    
086            public static final String FIND_BY_SYSTEM =
087                    GroupFinder.class.getName() + ".findBySystem";
088    
089            public static final String FIND_BY_C_P =
090                    GroupFinder.class.getName() + ".findByC_P";
091    
092            public static final String FIND_BY_C_N =
093                    GroupFinder.class.getName() + ".findByC_N";
094    
095            public static final String FIND_BY_C_PG_N_D =
096                    GroupFinder.class.getName() + ".findByC_PG_N_D";
097    
098            public static final String JOIN_BY_ACTIVE =
099                    GroupFinder.class.getName() + ".joinByActive";
100    
101            public static final String JOIN_BY_CREATOR_USER_ID =
102                    GroupFinder.class.getName() + ".joinByCreatorUserId";
103    
104            public static final String JOIN_BY_GROUP_ORG =
105                    GroupFinder.class.getName() + ".joinByGroupOrg";
106    
107            public static final String JOIN_BY_GROUPS_ORGS =
108                    GroupFinder.class.getName() + ".joinByGroupsOrgs";
109    
110            public static final String JOIN_BY_GROUPS_ROLES =
111                    GroupFinder.class.getName() + ".joinByGroupsRoles";
112    
113            public static final String JOIN_BY_GROUPS_USER_GROUPS =
114                    GroupFinder.class.getName() + ".joinByGroupsUserGroups";
115    
116            public static final String JOIN_BY_LAYOUT_SET =
117                    GroupFinder.class.getName() + ".joinByLayoutSet";
118    
119            public static final String JOIN_BY_MANUAL_MEMBERSHIP =
120                    GroupFinder.class.getName() + ".joinByManualMembership";
121    
122            public static final String JOIN_BY_MEMBERSHIP_RESTRICTION =
123                    GroupFinder.class.getName() + ".joinByMembershipRestriction";
124    
125            public static final String JOIN_BY_PAGE_COUNT =
126                    GroupFinder.class.getName() + ".joinByPageCount";
127    
128            public static final String JOIN_BY_ROLE_RESOURCE_PERMISSIONS =
129                    GroupFinder.class.getName() + ".joinByRoleResourcePermissions";
130    
131            public static final String JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS =
132                    GroupFinder.class.getName() + ".joinByRoleResourceTypePermissions";
133    
134            public static final String JOIN_BY_SITE =
135                    GroupFinder.class.getName() + ".joinBySite";
136    
137            public static final String JOIN_BY_TYPE =
138                    GroupFinder.class.getName() + ".joinByType";
139    
140            public static final String JOIN_BY_USER_GROUP_ROLE =
141                    GroupFinder.class.getName() + ".joinByUserGroupRole";
142    
143            public static final String JOIN_BY_USERS_GROUPS =
144                    GroupFinder.class.getName() + ".joinByUsersGroups";
145    
146            @Override
147            public int countByLayouts(long companyId, long parentGroupId, boolean site)
148                    throws SystemException {
149    
150                    Session session = null;
151    
152                    try {
153                            session = openSession();
154    
155                            String sql = CustomSQLUtil.get(COUNT_BY_LAYOUTS);
156    
157                            SQLQuery q = session.createSQLQuery(sql);
158    
159                            q.addEntity("Group_", GroupImpl.class);
160    
161                            QueryPos qPos = QueryPos.getInstance(q);
162    
163                            qPos.add(companyId);
164                            qPos.add(parentGroupId);
165                            qPos.add(site);
166    
167                            Iterator<Long> itr = q.iterate();
168    
169                            if (itr.hasNext()) {
170                                    Long count = itr.next();
171    
172                                    if (count != null) {
173                                            return count.intValue();
174                                    }
175                            }
176    
177                            return 0;
178                    }
179                    catch (Exception e) {
180                            throw new SystemException(e);
181                    }
182                    finally {
183                            closeSession(session);
184                    }
185            }
186    
187            @Override
188            public int countByG_U(long groupId, long userId, boolean inherit)
189                    throws SystemException {
190    
191                    LinkedHashMap<String, Object> params1 =
192                            new LinkedHashMap<String, Object>();
193    
194                    params1.put("usersGroups", userId);
195    
196                    LinkedHashMap<String, Object> params2 =
197                            new LinkedHashMap<String, Object>();
198    
199                    params2.put("groupOrg", userId);
200    
201                    LinkedHashMap<String, Object> params3 =
202                            new LinkedHashMap<String, Object>();
203    
204                    params3.put("groupsOrgs", userId);
205    
206                    LinkedHashMap<String, Object> params4 =
207                            new LinkedHashMap<String, Object>();
208    
209                    params4.put("groupsUserGroups", userId);
210    
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            int count = countByGroupId(session, groupId, params1);
217    
218                            if (inherit) {
219                                    count += countByGroupId(session, groupId, params2);
220                                    count += countByGroupId(session, groupId, params3);
221                                    count += countByGroupId(session, groupId, params4);
222                            }
223    
224                            return count;
225                    }
226                    catch (Exception e) {
227                            throw new SystemException(e);
228                    }
229                    finally {
230                            closeSession(session);
231                    }
232            }
233    
234            @Override
235            public int countByC_C_PG_N_D(
236                            long companyId, long[] classNameIds, long parentGroupId,
237                            String[] names, String[] descriptions,
238                            LinkedHashMap<String, Object> params, boolean andOperator)
239                    throws SystemException {
240    
241                    String parentGroupIdComparator = StringPool.EQUAL;
242    
243                    if (parentGroupId == GroupConstants.ANY_PARENT_GROUP_ID) {
244                            parentGroupIdComparator = StringPool.NOT_EQUAL;
245                    }
246    
247                    names = CustomSQLUtil.keywords(names);
248                    descriptions = CustomSQLUtil.keywords(descriptions);
249    
250                    if (params == null) {
251                            params = _emptyLinkedHashMap;
252                    }
253    
254                    LinkedHashMap<String, Object> params1 = params;
255    
256                    LinkedHashMap<String, Object> params2 = null;
257    
258                    LinkedHashMap<String, Object> params3 = null;
259    
260                    LinkedHashMap<String, Object> params4 = null;
261    
262                    Long userId = (Long)params.get("usersGroups");
263    
264                    boolean doUnion = Validator.isNotNull(userId);
265    
266                    if (doUnion) {
267                            params2 = new LinkedHashMap<String, Object>(params1);
268                            params3 = new LinkedHashMap<String, Object>(params1);
269                            params4 = new LinkedHashMap<String, Object>(params1);
270    
271                            _populateUnionParams(
272                                    userId, classNameIds, params1, params2, params3, params4);
273                    }
274                    else if (classNameIds != null) {
275                            params1.put("classNameIds", classNameIds);
276                    }
277    
278                    Session session = null;
279    
280                    try {
281                            session = openSession();
282    
283                            Set<Long> groupIds = new HashSet<Long>();
284    
285                            groupIds.addAll(
286                                    countByC_PG_N_D(
287                                            session, companyId, parentGroupId, parentGroupIdComparator,
288                                            names, descriptions, params1, andOperator));
289    
290                            if (doUnion) {
291                                    if (params2.containsKey("classNameIds")) {
292                                            groupIds.addAll(
293                                                    countByC_PG_N_D(
294                                                            session, companyId, parentGroupId,
295                                                            parentGroupIdComparator, names, descriptions,
296                                                            params2, andOperator));
297                                    }
298    
299                                    if (params3.containsKey("classNameIds")) {
300                                            groupIds.addAll(
301                                                    countByC_PG_N_D(
302                                                            session, companyId, parentGroupId,
303                                                            parentGroupIdComparator, names, descriptions,
304                                                            params3, andOperator));
305                                    }
306    
307                                    if (params4.containsKey("classNameIds")) {
308                                            groupIds.addAll(
309                                                    countByC_PG_N_D(
310                                                            session, companyId, parentGroupId,
311                                                            parentGroupIdComparator, names, descriptions,
312                                                            params4, andOperator));
313                                    }
314                            }
315    
316                            return groupIds.size();
317                    }
318                    catch (Exception e) {
319                            throw new SystemException(e);
320                    }
321                    finally {
322                            closeSession(session);
323                    }
324            }
325    
326            @Deprecated
327            @Override
328            public List<Group> findByLayouts(
329                            long companyId, long parentGroupId, boolean site, int start,
330                            int end)
331                    throws SystemException {
332    
333                    return findByLayouts(companyId, parentGroupId, site, start, end, null);
334            }
335    
336            @Override
337            public List<Group> findByLayouts(
338                            long companyId, long parentGroupId, boolean site, int start,
339                            int end, OrderByComparator obc)
340                    throws SystemException {
341    
342                    Session session = null;
343    
344                    try {
345                            session = openSession();
346    
347                            String sql = CustomSQLUtil.get(FIND_BY_LAYOUTS);
348    
349                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
350    
351                            SQLQuery q = session.createSQLQuery(sql);
352    
353                            q.addEntity("Group_", GroupImpl.class);
354    
355                            QueryPos qPos = QueryPos.getInstance(q);
356    
357                            qPos.add(companyId);
358                            qPos.add(parentGroupId);
359                            qPos.add(site);
360    
361                            return q.list(true);
362                    }
363                    catch (Exception e) {
364                            throw new SystemException(e);
365                    }
366                    finally {
367                            closeSession(session);
368                    }
369            }
370    
371            @Override
372            public List<Group> findByLiveGroups() throws SystemException {
373                    Session session = null;
374    
375                    try {
376                            session = openSession();
377    
378                            String sql = CustomSQLUtil.get(FIND_BY_LIVE_GROUPS);
379    
380                            SQLQuery q = session.createSQLQuery(sql);
381    
382                            q.addEntity("Group_", GroupImpl.class);
383    
384                            return q.list(true);
385                    }
386                    catch (Exception e) {
387                            throw new SystemException(e);
388                    }
389                    finally {
390                            closeSession(session);
391                    }
392            }
393    
394            @Override
395            public List<Group> findByNoLayouts(
396                            long classNameId, boolean privateLayout, int start, int end)
397                    throws SystemException {
398    
399                    Session session = null;
400    
401                    try {
402                            session = openSession();
403    
404                            String sql = CustomSQLUtil.get(FIND_BY_NO_LAYOUTS);
405    
406                            SQLQuery q = session.createSQLQuery(sql);
407    
408                            q.addEntity("Group_", GroupImpl.class);
409    
410                            QueryPos qPos = QueryPos.getInstance(q);
411    
412                            qPos.add(classNameId);
413                            qPos.add(privateLayout);
414    
415                            return q.list(true);
416                    }
417                    catch (Exception e) {
418                            throw new SystemException(e);
419                    }
420                    finally {
421                            closeSession(session);
422                    }
423            }
424    
425            @Override
426            public List<Group> findByNullFriendlyURL() throws SystemException {
427                    Session session = null;
428    
429                    try {
430                            session = openSession();
431    
432                            String sql = CustomSQLUtil.get(FIND_BY_NULL_FRIENDLY_URL);
433    
434                            SQLQuery q = session.createSQLQuery(sql);
435    
436                            q.addEntity("Group_", GroupImpl.class);
437    
438                            return q.list(true);
439                    }
440                    catch (Exception e) {
441                            throw new SystemException(e);
442                    }
443                    finally {
444                            closeSession(session);
445                    }
446            }
447    
448            @Override
449            public List<Group> findBySystem(long companyId) throws SystemException {
450                    Session session = null;
451    
452                    try {
453                            session = openSession();
454    
455                            String sql = CustomSQLUtil.get(FIND_BY_SYSTEM);
456    
457                            SQLQuery q = session.createSQLQuery(sql);
458    
459                            q.addEntity("Group_", GroupImpl.class);
460    
461                            QueryPos qPos = QueryPos.getInstance(q);
462    
463                            qPos.add(companyId);
464    
465                            return q.list(true);
466                    }
467                    catch (Exception e) {
468                            throw new SystemException(e);
469                    }
470                    finally {
471                            closeSession(session);
472                    }
473            }
474    
475            @Override
476            public List<Group> findByCompanyId(
477                            long companyId, LinkedHashMap<String, Object> params, int start,
478                            int end, OrderByComparator obc)
479                    throws SystemException {
480    
481                    if (params == null) {
482                            params = _emptyLinkedHashMap;
483                    }
484    
485                    LinkedHashMap<String, Object> params1 = params;
486    
487                    LinkedHashMap<String, Object> params2 = null;
488    
489                    LinkedHashMap<String, Object> params3 = null;
490    
491                    Long userId = (Long)params.get("usersGroups");
492                    boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
493    
494                    boolean doUnion = Validator.isNotNull(userId) && inherit;
495    
496                    long[] groupOrganizationClassNameIds =
497                            _getGroupOrganizationClassNameIds();
498    
499                    if (doUnion) {
500                            params2 = new LinkedHashMap<String, Object>(params1);
501    
502                            params2.remove("usersGroups");
503                            params2.put("groupOrg", userId);
504    
505                            params3 = new LinkedHashMap<String, Object>(params1);
506    
507                            params3.remove("usersGroups");
508                            params3.put("groupsOrgs", userId);
509                            params2.put("classNameIds", groupOrganizationClassNameIds[1]);
510                            params3.put("classNameIds", groupOrganizationClassNameIds[0]);
511                    }
512    
513                    params1.put("classNameIds", _getGroupOrganizationClassNameIds());
514    
515                    String sqlKey = _buildSQLCacheKey(obc, params1, params2, params3);
516    
517                    String sql = _findByCompanyIdSQLCache.get(sqlKey);
518    
519                    if (sql == null) {
520                            String findByCompanyIdSQL = CustomSQLUtil.get(FIND_BY_COMPANY_ID);
521    
522                            if (params.get("active") == Boolean.TRUE) {
523                                    findByCompanyIdSQL = StringUtil.replace(
524                                            findByCompanyIdSQL, "(Group_.liveGroupId = 0) AND",
525                                            StringPool.BLANK);
526                            }
527    
528                            findByCompanyIdSQL = replaceOrderBy(findByCompanyIdSQL, obc);
529    
530                            StringBundler sb = new StringBundler(9);
531    
532                            sb.append(StringPool.OPEN_PARENTHESIS);
533                            sb.append(replaceJoinAndWhere(findByCompanyIdSQL, params1));
534    
535                            if (doUnion) {
536                                    sb.append(") UNION (");
537                                    sb.append(replaceJoinAndWhere(findByCompanyIdSQL, params2));
538                                    sb.append(") UNION (");
539                                    sb.append(replaceJoinAndWhere(findByCompanyIdSQL, params3));
540                            }
541    
542                            sb.append(StringPool.CLOSE_PARENTHESIS);
543    
544                            if (obc != null) {
545                                    sb.append(" ORDER BY ");
546                                    sb.append(obc.toString());
547                            }
548    
549                            sql = sb.toString();
550    
551                            _findByCompanyIdSQLCache.put(sqlKey, sql);
552                    }
553    
554                    Session session = null;
555    
556                    try {
557                            session = openSession();
558    
559                            SQLQuery q = session.createSQLQuery(sql);
560    
561                            q.addScalar("groupId", Type.LONG);
562    
563                            QueryPos qPos = QueryPos.getInstance(q);
564    
565                            setJoin(qPos, params1);
566    
567                            qPos.add(companyId);
568    
569                            if (doUnion) {
570                                    setJoin(qPos, params2);
571    
572                                    qPos.add(companyId);
573    
574                                    setJoin(qPos, params3);
575    
576                                    qPos.add(companyId);
577                            }
578    
579                            List<Long> groupIds = (List<Long>)QueryUtil.list(
580                                    q, getDialect(), start, end);
581    
582                            List<Group> groups = new ArrayList<Group>(groupIds.size());
583    
584                            for (Long groupId : groupIds) {
585                                    Group group = GroupUtil.findByPrimaryKey(groupId);
586    
587                                    groups.add(group);
588                            }
589    
590                            return groups;
591                    }
592                    catch (Exception e) {
593                            throw new SystemException(e);
594                    }
595                    finally {
596                            closeSession(session);
597                    }
598            }
599    
600            @Override
601            public List<Long> findByC_P(
602                            long companyId, long parentGroupId, long previousGroupId, int size)
603                    throws SystemException {
604    
605                    Session session = null;
606    
607                    try {
608                            session = openSession();
609    
610                            String sql = CustomSQLUtil.get(FIND_BY_C_P);
611    
612                            if (previousGroupId <= 0) {
613                                    sql = StringUtil.replace(
614                                            sql, "(groupId > ?) AND", StringPool.BLANK);
615                            }
616    
617                            SQLQuery q = session.createSQLQuery(sql);
618    
619                            q.addScalar("groupId", Type.LONG);
620    
621                            QueryPos qPos = QueryPos.getInstance(q);
622    
623                            if (previousGroupId > 0) {
624                                    qPos.add(previousGroupId);
625                            }
626    
627                            qPos.add(companyId);
628                            qPos.add(parentGroupId);
629    
630                            return (List<Long>)QueryUtil.list(q, getDialect(), 0, size);
631                    }
632                    catch (Exception e) {
633                            throw new SystemException(e);
634                    }
635                    finally {
636                            closeSession(session);
637                    }
638            }
639    
640            @Override
641            public Group findByC_N(long companyId, String name)
642                    throws NoSuchGroupException, SystemException {
643    
644                    name = StringUtil.lowerCase(name);
645    
646                    Session session = null;
647    
648                    try {
649                            session = openSession();
650    
651                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
652    
653                            SQLQuery q = session.createSQLQuery(sql);
654    
655                            q.addEntity("Group_", GroupImpl.class);
656    
657                            QueryPos qPos = QueryPos.getInstance(q);
658    
659                            qPos.add(companyId);
660                            qPos.add(name);
661    
662                            List<Group> groups = q.list();
663    
664                            if (!groups.isEmpty()) {
665                                    return groups.get(0);
666                            }
667                    }
668                    catch (Exception e) {
669                            throw new SystemException(e);
670                    }
671                    finally {
672                            closeSession(session);
673                    }
674    
675                    StringBundler sb = new StringBundler(5);
676    
677                    sb.append("No Group exists with the key {companyId=");
678                    sb.append(companyId);
679                    sb.append(", name=");
680                    sb.append(name);
681                    sb.append("}");
682    
683                    throw new NoSuchGroupException(sb.toString());
684            }
685    
686            @Override
687            public List<Group> findByC_C_PG_N_D(
688                            long companyId, long[] classNameIds, long parentGroupId,
689                            String[] names, String[] descriptions,
690                            LinkedHashMap<String, Object> params, boolean andOperator,
691                            int start, int end, OrderByComparator obc)
692                    throws SystemException {
693    
694                    String parentGroupIdComparator = StringPool.EQUAL;
695    
696                    if (parentGroupId == GroupConstants.ANY_PARENT_GROUP_ID) {
697                            parentGroupIdComparator = StringPool.NOT_EQUAL;
698                    }
699    
700                    names = CustomSQLUtil.keywords(names);
701                    descriptions = CustomSQLUtil.keywords(descriptions);
702    
703                    if (params == null) {
704                            params = _emptyLinkedHashMap;
705                    }
706    
707                    LinkedHashMap<String, Object> params1 = params;
708    
709                    LinkedHashMap<String, Object> params2 = null;
710    
711                    LinkedHashMap<String, Object> params3 = null;
712    
713                    LinkedHashMap<String, Object> params4 = null;
714    
715                    Long userId = (Long)params.get("usersGroups");
716                    boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
717    
718                    boolean doUnion = Validator.isNotNull(userId) && inherit;
719    
720                    if (doUnion) {
721                            params2 = new LinkedHashMap<String, Object>(params1);
722                            params3 = new LinkedHashMap<String, Object>(params1);
723                            params4 = new LinkedHashMap<String, Object>(params1);
724    
725                            _populateUnionParams(
726                                    userId, classNameIds, params1, params2, params3, params4);
727                    }
728                    else if (classNameIds != null) {
729                            params1.put("classNameIds", classNameIds);
730                    }
731    
732                    if (obc == null) {
733                            obc = new GroupNameComparator(true);
734                    }
735    
736                    String sql = null;
737                    String sqlKey = null;
738    
739                    if (_isCacheableSQL(classNameIds)) {
740                            sqlKey = _buildSQLCacheKey(obc, params1, params2, params3, params4);
741    
742                            sql = _findByC_C_PG_N_DSQLCache.get(sqlKey);
743                    }
744    
745                    if (sql == null) {
746                            String findByC_PG_N_D_SQL = CustomSQLUtil.get(FIND_BY_C_PG_N_D);
747    
748                            findByC_PG_N_D_SQL = replaceOrderBy(findByC_PG_N_D_SQL, obc);
749    
750                            StringBundler sb = new StringBundler(10);
751    
752                            sb.append(StringPool.OPEN_PARENTHESIS);
753                            sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params1));
754    
755                            if (doUnion) {
756                                    if (params2.containsKey("classNameIds")) {
757                                            sb.append(") UNION (");
758                                            sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params2));
759                                    }
760    
761                                    if (params3.containsKey("classNameIds")) {
762                                            sb.append(") UNION (");
763                                            sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params3));
764                                    }
765    
766                                    if (params4.containsKey("classNameIds")) {
767                                            sb.append(") UNION (");
768                                            sb.append(replaceJoinAndWhere(findByC_PG_N_D_SQL, params4));
769                                    }
770                            }
771    
772                            sb.append(") ORDER BY ");
773                            sb.append(obc.toString());
774    
775                            sql = sb.toString();
776    
777                            if (sqlKey != null) {
778                                    _findByC_C_PG_N_DSQLCache.put(sqlKey, sql);
779                            }
780                    }
781    
782                    sql = StringUtil.replace(
783                            sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
784                            parentGroupIdComparator.equals(StringPool.EQUAL) ?
785                                    StringPool.EQUAL : StringPool.NOT_EQUAL);
786                    sql = CustomSQLUtil.replaceKeywords(
787                            sql, "lower(Group_.name)", StringPool.LIKE, false, names);
788                    sql = CustomSQLUtil.replaceKeywords(
789                            sql, "lower(Group_.description)", StringPool.LIKE, true,
790                            descriptions);
791                    sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
792    
793                    Session session = null;
794    
795                    try {
796                            session = openSession();
797    
798                            SQLQuery q = session.createSQLQuery(sql);
799    
800                            q.addScalar("groupId", Type.LONG);
801    
802                            QueryPos qPos = QueryPos.getInstance(q);
803    
804                            setJoin(qPos, params1);
805    
806                            qPos.add(companyId);
807                            qPos.add(parentGroupId);
808                            qPos.add(names, 2);
809                            qPos.add(descriptions, 2);
810    
811                            if (doUnion) {
812                                    setJoin(qPos, params2);
813    
814                                    qPos.add(companyId);
815                                    qPos.add(parentGroupId);
816                                    qPos.add(names, 2);
817                                    qPos.add(descriptions, 2);
818    
819                                    setJoin(qPos, params3);
820    
821                                    qPos.add(companyId);
822                                    qPos.add(parentGroupId);
823                                    qPos.add(names, 2);
824                                    qPos.add(descriptions, 2);
825    
826                                    setJoin(qPos, params4);
827    
828                                    qPos.add(companyId);
829                                    qPos.add(parentGroupId);
830                                    qPos.add(names, 2);
831                                    qPos.add(descriptions, 2);
832                            }
833    
834                            List<Long> groupIds = (List<Long>)QueryUtil.list(
835                                    q, getDialect(), start, end);
836    
837                            List<Group> groups = new ArrayList<Group>(groupIds.size());
838    
839                            for (Long groupId : groupIds) {
840                                    Group group = GroupUtil.findByPrimaryKey(groupId);
841    
842                                    groups.add(group);
843                            }
844    
845                            return groups;
846                    }
847                    catch (Exception e) {
848                            throw new SystemException(e);
849                    }
850                    finally {
851                            closeSession(session);
852                    }
853            }
854    
855            protected int countByGroupId(
856                            Session session, long groupId, Map<String, Object> params)
857                    throws Exception {
858    
859                    String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
860    
861                    sql = replaceJoinAndWhere(sql, params);
862    
863                    SQLQuery q = session.createSQLQuery(sql);
864    
865                    q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
866    
867                    QueryPos qPos = QueryPos.getInstance(q);
868    
869                    setJoin(qPos, params);
870    
871                    qPos.add(groupId);
872    
873                    Iterator<Long> itr = q.iterate();
874    
875                    if (itr.hasNext()) {
876                            Long count = itr.next();
877    
878                            if (count != null) {
879                                    return count.intValue();
880                            }
881                    }
882    
883                    return 0;
884            }
885    
886            protected List<Long> countByC_PG_N_D(
887                            Session session, long companyId, long parentGroupId,
888                            String parentGroupIdComparator, String[] names,
889                            String[] descriptions, Map<String, Object> params,
890                            boolean andOperator)
891                    throws Exception {
892    
893                    String sql = CustomSQLUtil.get(COUNT_BY_C_PG_N_D);
894    
895                    sql = StringUtil.replace(
896                            sql, "[$PARENT_GROUP_ID_COMPARATOR$]",
897                            parentGroupIdComparator.equals(StringPool.EQUAL) ?
898                                    StringPool.EQUAL : StringPool.NOT_EQUAL);
899                    sql = CustomSQLUtil.replaceKeywords(
900                            sql, "lower(Group_.name)", StringPool.LIKE, false, names);
901                    sql = CustomSQLUtil.replaceKeywords(
902                            sql, "lower(Group_.description)", StringPool.LIKE, true,
903                            descriptions);
904    
905                    sql = replaceJoinAndWhere(sql, params);
906                    sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
907    
908                    SQLQuery q = session.createSQLQuery(sql);
909    
910                    q.addScalar("groupId", Type.LONG);
911    
912                    QueryPos qPos = QueryPos.getInstance(q);
913    
914                    setJoin(qPos, params);
915    
916                    qPos.add(companyId);
917                    qPos.add(parentGroupId);
918                    qPos.add(names, 2);
919                    qPos.add(descriptions, 2);
920    
921                    return q.list(true);
922            }
923    
924            protected String getJoin(Map<String, Object> params) {
925                    if ((params == null) || params.isEmpty()) {
926                            return StringPool.BLANK;
927                    }
928    
929                    StringBundler sb = new StringBundler(params.size());
930    
931                    for (Map.Entry<String, Object> entry : params.entrySet()) {
932                            String key = entry.getKey();
933                            Object value = entry.getValue();
934    
935                            if (Validator.isNull(value)) {
936                                    continue;
937                            }
938    
939                            if (key.equals("rolePermissions")) {
940                                    List<Object> values = (List<Object>)value;
941    
942                                    String name = (String)values.get(0);
943    
944                                    if (ResourceBlockLocalServiceUtil.isSupported(name)) {
945                                            key = "rolePermissions_6_block";
946                                    }
947                                    else {
948                                            key = "rolePermissions_6";
949                                    }
950                            }
951    
952                            Map<String, String> joinMap = _getJoinMap();
953    
954                            String joinValue = joinMap.get(key);
955    
956                            if (Validator.isNotNull(joinValue)) {
957                                    sb.append(joinValue);
958                            }
959                    }
960    
961                    return sb.toString();
962            }
963    
964            protected String getWhere(Map<String, Object> params) {
965                    if ((params == null) || params.isEmpty()) {
966                            return StringPool.BLANK;
967                    }
968    
969                    StringBundler sb = new StringBundler(params.size());
970    
971                    for (Map.Entry<String, Object> entry : params.entrySet()) {
972                            String key = entry.getKey();
973    
974                            if (key.equals("classNameIds")) {
975                                    if (entry.getValue() instanceof Long) {
976                                            sb.append("(Group_.classNameId = ?) AND ");
977                                    }
978                                    else {
979                                            sb.append(StringPool.OPEN_PARENTHESIS);
980    
981                                            long[] classNameIds = (long[])entry.getValue();
982    
983                                            for (int i = 0; i < classNameIds.length; i++) {
984                                                    sb.append("(Group_.classNameId = ?) OR ");
985                                            }
986    
987                                            sb.setStringAt(
988                                                    "(Group_.classNameId = ?)) AND ", sb.index() - 1);
989                                    }
990                            }
991                            else if (key.equals("excludedGroupIds")) {
992                                    List<Long> excludedGroupIds = (List<Long>)entry.getValue();
993    
994                                    if (!excludedGroupIds.isEmpty()) {
995                                            sb.append(StringPool.OPEN_PARENTHESIS);
996    
997                                            for (int i = 0; i < excludedGroupIds.size(); i++) {
998                                                    sb.append("(Group_.groupId != ?) AND ");
999                                            }
1000    
1001                                            sb.setStringAt(
1002                                                    "(Group_.groupId != ?)) AND ", sb.index() - 1);
1003                                    }
1004                            }
1005                            else if (key.equals("groupsTree")) {
1006                                    List<Group> groupsTree = (List<Group>)entry.getValue();
1007    
1008                                    if (!groupsTree.isEmpty()) {
1009                                            sb.append(StringPool.OPEN_PARENTHESIS);
1010    
1011                                            for (int i = 0; i < groupsTree.size(); i++) {
1012                                                    sb.append("(Group_.treePath LIKE ?) OR ");
1013                                            }
1014    
1015                                            sb.setStringAt(
1016                                                    "(Group_.treePath LIKE ?)) AND ", sb.index() - 1);
1017                                    }
1018                            }
1019                            else if (key.equals("types")) {
1020                                    List<Integer> types = (List<Integer>)entry.getValue();
1021    
1022                                    if (!types.isEmpty()) {
1023                                            sb.append(StringPool.OPEN_PARENTHESIS);
1024    
1025                                            for (int i = 0; i < types.size(); i++) {
1026                                                    sb.append("(Group_.type_ = ?) OR ");
1027                                            }
1028    
1029                                            sb.setStringAt("(Group_.type_ = ?)) AND ", sb.index() - 1);
1030                                    }
1031                            }
1032                            else {
1033                                    if (key.equals("rolePermissions")) {
1034                                            List<Object> values = (List<Object>)entry.getValue();
1035    
1036                                            String name = (String)values.get(0);
1037    
1038                                            if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1039                                                    key = "rolePermissions_6_block";
1040                                            }
1041                                            else {
1042                                                    key = "rolePermissions_6";
1043                                            }
1044                                    }
1045    
1046                                    Map<String, String> whereMap = _getWhereMap();
1047    
1048                                    String whereValue = whereMap.get(key);
1049    
1050                                    if (Validator.isNotNull(whereValue)) {
1051                                            sb.append(whereValue);
1052                                    }
1053                            }
1054                    }
1055    
1056                    return sb.toString();
1057            }
1058    
1059            protected String replaceJoinAndWhere(
1060                    String sql, Map<String, Object> params) {
1061    
1062                    if (params.isEmpty()) {
1063                            return StringUtil.replace(
1064                                    sql,
1065                                    new String[] {
1066                                            "[$JOIN$]", "[$WHERE$]"
1067                                    },
1068                                    new String[] {
1069                                            StringPool.BLANK, StringPool.BLANK
1070                                    });
1071                    }
1072    
1073                    String cacheKey = _buildSQLCacheKey(sql, params);
1074    
1075                    String resultSQL = _replaceJoinAndWhereSQLCache.get(cacheKey);
1076    
1077                    if (resultSQL == null) {
1078                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
1079    
1080                            resultSQL = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
1081    
1082                            _replaceJoinAndWhereSQLCache.put(cacheKey, resultSQL);
1083                    }
1084    
1085                    return resultSQL;
1086            }
1087    
1088            protected String replaceOrderBy(String sql, OrderByComparator obc) {
1089                    if (obc instanceof GroupNameComparator) {
1090                            sql = StringUtil.replace(
1091                                    sql, "Group_.name AS groupName",
1092                                    "REPLACE(Group_.name, '" +
1093                                            GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX +
1094                                                    "', '') AS groupName");
1095                    }
1096    
1097                    return sql;
1098            }
1099    
1100            protected void setJoin(QueryPos qPos, Map<String, Object> params)
1101                    throws Exception {
1102    
1103                    if (params == null) {
1104                            return;
1105                    }
1106    
1107                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1108                            String key = entry.getKey();
1109    
1110                            if (key.equals("active") || key.equals("layoutSet") ||
1111                                    key.equals("manualMembership") || key.equals("site")) {
1112    
1113                                    Boolean value = (Boolean)entry.getValue();
1114    
1115                                    qPos.add(value);
1116                            }
1117                            else if (key.equals("classNameIds")) {
1118                                    if (entry.getValue() instanceof Long) {
1119                                            Long entryValue = (Long)entry.getValue();
1120    
1121                                            qPos.add(entryValue);
1122                                    }
1123                                    else {
1124                                            for (long classNameId : (long[])entry.getValue()) {
1125                                                    qPos.add(classNameId);
1126                                            }
1127                                    }
1128                            }
1129                            else if (key.equals("excludedGroupIds")) {
1130                                    List<Long> excludedGroupIds = (List<Long>)entry.getValue();
1131    
1132                                    if (!excludedGroupIds.isEmpty()) {
1133                                            for (long excludedGroupId : excludedGroupIds) {
1134                                                    qPos.add(excludedGroupId);
1135                                            }
1136                                    }
1137                            }
1138                            else if (key.equals("groupsTree")) {
1139                                    List<Group> groupsTree = (List<Group>)entry.getValue();
1140    
1141                                    if (!groupsTree.isEmpty()) {
1142                                            for (Group group : groupsTree) {
1143                                                    StringBundler sb = new StringBundler(5);
1144    
1145                                                    sb.append(StringPool.PERCENT);
1146                                                    sb.append(StringPool.SLASH);
1147                                                    sb.append(group.getGroupId());
1148                                                    sb.append(StringPool.SLASH);
1149                                                    sb.append(StringPool.PERCENT);
1150    
1151                                                    qPos.add(sb.toString());
1152                                            }
1153                                    }
1154                            }
1155                            else if (key.equals("pageCount")) {
1156                            }
1157                            else if (key.equals("rolePermissions")) {
1158                                    List<Object> values = (List<Object>)entry.getValue();
1159    
1160                                    String name = (String)values.get(0);
1161                                    Integer scope = (Integer)values.get(1);
1162                                    String actionId = (String)values.get(2);
1163                                    Long roleId = (Long)values.get(3);
1164    
1165                                    ResourceAction resourceAction =
1166                                            ResourceActionLocalServiceUtil.getResourceAction(
1167                                                    name, actionId);
1168    
1169                                    if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1170    
1171                                            // Scope is assumed to always be group
1172    
1173                                            qPos.add(name);
1174                                            qPos.add(roleId);
1175                                            qPos.add(resourceAction.getBitwiseValue());
1176                                    }
1177                                    else {
1178                                            qPos.add(name);
1179                                            qPos.add(scope);
1180                                            qPos.add(roleId);
1181                                            qPos.add(resourceAction.getBitwiseValue());
1182                                    }
1183                            }
1184                            else if (key.equals("types")) {
1185                                    List<Integer> values = (List<Integer>)entry.getValue();
1186    
1187                                    for (int i = 0; i < values.size(); i++) {
1188                                            Integer value = values.get(i);
1189    
1190                                            qPos.add(value);
1191                                    }
1192                            }
1193                            else if (key.equals("userGroupRole")) {
1194                                    List<Long> values = (List<Long>)entry.getValue();
1195    
1196                                    Long userId = values.get(0);
1197                                    Long roleId = values.get(1);
1198    
1199                                    qPos.add(userId);
1200                                    qPos.add(roleId);
1201                            }
1202                            else {
1203                                    Object value = entry.getValue();
1204    
1205                                    if (value instanceof Integer) {
1206                                            Integer valueInteger = (Integer)value;
1207    
1208                                            if (Validator.isNotNull(valueInteger)) {
1209                                                    qPos.add(valueInteger);
1210                                            }
1211                                    }
1212                                    else if (value instanceof Long) {
1213                                            Long valueLong = (Long)value;
1214    
1215                                            if (Validator.isNotNull(valueLong)) {
1216                                                    qPos.add(valueLong);
1217                                            }
1218                                    }
1219                                    else if (value instanceof String) {
1220                                            String valueString = (String)value;
1221    
1222                                            if (Validator.isNotNull(valueString)) {
1223                                                    qPos.add(valueString);
1224                                            }
1225                                    }
1226                            }
1227                    }
1228            }
1229    
1230            private final String _buildSQLCacheKey(
1231                    OrderByComparator obc, Map<String, Object>... params) {
1232    
1233                    if (obc == null) {
1234                            return _buildSQLCacheKey(StringPool.BLANK, params);
1235                    }
1236    
1237                    return _buildSQLCacheKey(obc.getOrderBy(), params);
1238            }
1239    
1240            private final String _buildSQLCacheKey(
1241                    String sql, Map<String, Object>... params) {
1242    
1243                    int size = 1;
1244    
1245                    for (Map<String, Object> param : params) {
1246                            if (param != null) {
1247                                    size += param.size() * 5;
1248                            }
1249                    }
1250    
1251                    StringBundler sb = new StringBundler(size);
1252    
1253                    sb.append(sql);
1254    
1255                    for (Map<String, Object> param : params) {
1256                            if (param == null) {
1257                                    continue;
1258                            }
1259    
1260                            for (Map.Entry<String, Object> entry : param.entrySet()) {
1261                                    sb.append(StringPool.COMMA);
1262    
1263                                    String key = entry.getKey();
1264    
1265                                    if (key.equals("rolePermissions")) {
1266                                            List<Object> values = (List<Object>)entry.getValue();
1267    
1268                                            String name = (String)values.get(0);
1269    
1270                                            if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1271                                                    key = "rolePermissions_6_block";
1272                                            }
1273                                            else {
1274                                                    key = "rolePermissions_6";
1275                                            }
1276                                    }
1277    
1278                                    sb.append(key);
1279                                    sb.append(StringPool.DASH);
1280    
1281                                    Object value = entry.getValue();
1282    
1283                                    if (value instanceof long[]) {
1284                                            long[] values = (long[])value;
1285    
1286                                            sb.append(values.length);
1287                                    }
1288                                    else if (value instanceof Collection<?>) {
1289                                            Collection<?> values = (Collection<?>)value;
1290    
1291                                            sb.append(values.size());
1292                                    }
1293    
1294                                    sb.append(StringPool.COMMA);
1295                            }
1296                    }
1297    
1298                    return sb.toString();
1299            }
1300    
1301            private String _getCondition(String join) {
1302                    if (Validator.isNotNull(join)) {
1303                            int pos = join.indexOf("WHERE");
1304    
1305                            if (pos != -1) {
1306                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
1307                            }
1308                            else {
1309                                    join = StringPool.BLANK;
1310                            }
1311                    }
1312    
1313                    return join;
1314            }
1315    
1316            private long[] _getGroupOrganizationClassNameIds() {
1317                    if (_groupOrganizationClassNameIds == null) {
1318                            _groupOrganizationClassNameIds = new long[] {
1319                                    ClassNameLocalServiceUtil.getClassNameId(Group.class),
1320                                    ClassNameLocalServiceUtil.getClassNameId(Organization.class)
1321                            };
1322                    }
1323    
1324                    return _groupOrganizationClassNameIds;
1325            }
1326    
1327            private Map<String, String> _getJoinMap() {
1328                    if (_joinMap != null) {
1329                            return _joinMap;
1330                    }
1331    
1332                    Map<String, String> joinMap = new HashMap<String, String>();
1333    
1334                    joinMap.put("active", _removeWhere(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1335                    joinMap.put(
1336                            "groupOrg", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1337                    joinMap.put(
1338                            "groupsOrgs", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1339                    joinMap.put(
1340                            "groupsRoles",
1341                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1342                    joinMap.put(
1343                            "groupsUserGroups",
1344                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1345                    joinMap.put(
1346                            "layoutSet", _removeWhere(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1347                    joinMap.put(
1348                            "pageCount", _removeWhere(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1349                    joinMap.put(
1350                            "membershipRestriction",
1351                            _removeWhere(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1352                    joinMap.put(
1353                            "rolePermissions_6",
1354                            _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1355                    joinMap.put(
1356                            "rolePermissions_6_block",
1357                            _removeWhere(
1358                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1359                    joinMap.put("site", _removeWhere(CustomSQLUtil.get(JOIN_BY_SITE)));
1360                    joinMap.put("type", _removeWhere(CustomSQLUtil.get(JOIN_BY_TYPE)));
1361                    joinMap.put(
1362                            "userGroupRole",
1363                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1364                    joinMap.put(
1365                            "usersGroups",
1366                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1367    
1368                    _joinMap = joinMap;
1369    
1370                    return _joinMap;
1371            }
1372    
1373            private Map<String, String> _getWhereMap() {
1374                    if (_whereMap != null) {
1375                            return _whereMap;
1376                    }
1377    
1378                    Map<String, String> whereMap = new HashMap<String, String>();
1379    
1380                    whereMap.put(
1381                            "active", _getCondition(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1382                    whereMap.put(
1383                            "creatorUserId",
1384                            _getCondition(CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID)));
1385                    whereMap.put(
1386                            "groupOrg", _getCondition(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1387                    whereMap.put(
1388                            "groupsOrgs",
1389                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1390                    whereMap.put(
1391                            "groupsRoles",
1392                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1393                    whereMap.put(
1394                            "groupsUserGroups",
1395                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1396                    whereMap.put(
1397                            "layoutSet", _getCondition(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1398                    whereMap.put(
1399                            "manualMembership",
1400                            _getCondition(CustomSQLUtil.get(JOIN_BY_MANUAL_MEMBERSHIP)));
1401                    whereMap.put(
1402                            "membershipRestriction",
1403                            _getCondition(CustomSQLUtil.get(JOIN_BY_MEMBERSHIP_RESTRICTION)));
1404                    whereMap.put(
1405                            "pageCount", _getCondition(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1406                    whereMap.put(
1407                            "rolePermissions_6",
1408                            _getCondition(
1409                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1410                    whereMap.put(
1411                            "rolePermissions_6_block",
1412                            _getCondition(
1413                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1414                    whereMap.put("site", _getCondition(CustomSQLUtil.get(JOIN_BY_SITE)));
1415                    whereMap.put("type", _getCondition(CustomSQLUtil.get(JOIN_BY_TYPE)));
1416                    whereMap.put(
1417                            "userGroupRole",
1418                            _getCondition(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1419                    whereMap.put(
1420                            "usersGroups",
1421                            _getCondition(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1422    
1423                    _whereMap = whereMap;
1424    
1425                    return _whereMap;
1426            }
1427    
1428            private boolean _isCacheableSQL(long[] classNameIds) {
1429                    if (classNameIds == null) {
1430                            return true;
1431                    }
1432    
1433                    if (classNameIds.length > 2) {
1434                            return false;
1435                    }
1436    
1437                    long[] groupOrganizationClassNameIds =
1438                            _getGroupOrganizationClassNameIds();
1439    
1440                    long groupClassNameId = groupOrganizationClassNameIds[0];
1441                    long organizationClassNameId = groupOrganizationClassNameIds[1];
1442    
1443                    for (long classNameId : classNameIds) {
1444                            if ((classNameId != groupClassNameId) &&
1445                                    (classNameId != organizationClassNameId)) {
1446    
1447                                    return false;
1448                            }
1449                    }
1450    
1451                    return true;
1452            }
1453    
1454            private void _populateUnionParams(
1455                    long userId, long[] classNameIds, Map<String, Object> params1,
1456                    Map<String, Object> params2, Map<String, Object> params3,
1457                    Map<String, Object> params4) {
1458    
1459                    params2.remove("usersGroups");
1460                    params2.put("groupOrg", userId);
1461    
1462                    params3.remove("usersGroups");
1463                    params3.put("groupsOrgs", userId);
1464    
1465                    params4.remove("usersGroups");
1466                    params4.put("groupsUserGroups", userId);
1467    
1468                    long[] groupOrganizationClassNameIds =
1469                            _getGroupOrganizationClassNameIds();
1470    
1471                    long groupClassNameId = groupOrganizationClassNameIds[0];
1472                    long organizationClassNameId = groupOrganizationClassNameIds[1];
1473    
1474                    if (classNameIds == null) {
1475                            params1.put("classNameIds", groupOrganizationClassNameIds);
1476                            params2.put("classNameIds", organizationClassNameId);
1477                            params3.put("classNameIds", groupClassNameId);
1478                            params4.put("classNameIds", groupOrganizationClassNameIds);
1479                    }
1480                    else {
1481                            params1.put("classNameIds", classNameIds);
1482    
1483                            if (ArrayUtil.contains(classNameIds, organizationClassNameId)) {
1484                                    params2.put("classNameIds", organizationClassNameId);
1485    
1486                                    if (ArrayUtil.contains(classNameIds, groupClassNameId)) {
1487                                            params3.put("classNameIds", groupClassNameId);
1488                                            params4.put("classNameIds", groupOrganizationClassNameIds);
1489                                    }
1490                                    else {
1491                                            params4.put("classNameIds", organizationClassNameId);
1492                                    }
1493                            }
1494                            else if (ArrayUtil.contains(classNameIds, groupClassNameId)) {
1495                                    params3.put("classNameIds", groupClassNameId);
1496                                    params4.put("classNameIds", groupClassNameId);
1497                            }
1498                    }
1499            }
1500    
1501            private String _removeWhere(String join) {
1502                    if (Validator.isNotNull(join)) {
1503                            int pos = join.indexOf("WHERE");
1504    
1505                            if (pos != -1) {
1506                                    join = join.substring(0, pos);
1507                            }
1508                    }
1509    
1510                    return join;
1511            }
1512    
1513            private LinkedHashMap<String, Object> _emptyLinkedHashMap =
1514                    new LinkedHashMap<String, Object>(0);
1515            private Map<String, String> _findByC_C_PG_N_DSQLCache =
1516                    new ConcurrentHashMap<String, String>();
1517            private Map<String, String> _findByCompanyIdSQLCache =
1518                    new ConcurrentHashMap<String, String>();
1519            private volatile long[] _groupOrganizationClassNameIds;
1520            private volatile Map<String, String> _joinMap;
1521            private Map<String, String> _replaceJoinAndWhereSQLCache =
1522                    new ConcurrentHashMap<String, String>();
1523            private volatile Long _userGroupClassNameId;
1524            private volatile Map<String, String> _whereMap;
1525    
1526    }