001    /**
002     * Copyright (c) 2000-2011 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.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 String COUNT_BY_GROUP_ID =
057                    GroupFinder.class.getName() + ".countByGroupId";
058    
059            public static String COUNT_BY_C_N_D =
060                    GroupFinder.class.getName() + ".countByC_N_D";
061    
062            public static String FIND_BY_LIVE_GROUPS =
063                    GroupFinder.class.getName() + ".findByLiveGroups";
064    
065            public static String FIND_BY_NO_LAYOUTS =
066                    GroupFinder.class.getName() + ".findByNoLayouts";
067    
068            public static String FIND_BY_NULL_FRIENDLY_URL =
069                    GroupFinder.class.getName() + ".findByNullFriendlyURL";
070    
071            public static String FIND_BY_SYSTEM =
072                    GroupFinder.class.getName() + ".findBySystem";
073    
074            public static String FIND_BY_C_C =
075                    GroupFinder.class.getName() + ".findByC_C";
076    
077            public static String FIND_BY_C_N =
078                    GroupFinder.class.getName() + ".findByC_N";
079    
080            public static String FIND_BY_C_N_D =
081                    GroupFinder.class.getName() + ".findByC_N_D";
082    
083            public static String JOIN_BY_ACTIVE =
084                    GroupFinder.class.getName() + ".joinByActive";
085    
086            public static String JOIN_BY_CREATOR_USER_ID =
087                    GroupFinder.class.getName() + ".joinByCreatorUserId";
088    
089            public static String JOIN_BY_GROUP_ORG =
090                    GroupFinder.class.getName() + ".joinByGroupOrg";
091    
092            public static String JOIN_BY_GROUPS_ORGS =
093                    GroupFinder.class.getName() + ".joinByGroupsOrgs";
094    
095            public static String JOIN_BY_GROUPS_ROLES =
096                    GroupFinder.class.getName() + ".joinByGroupsRoles";
097    
098            public static String JOIN_BY_GROUPS_USER_GROUPS =
099                    GroupFinder.class.getName() + ".joinByGroupsUserGroups";
100    
101            public static String JOIN_BY_LAYOUT_SET =
102                    GroupFinder.class.getName() + ".joinByLayoutSet";
103    
104            public static String JOIN_BY_PAGE_COUNT =
105                    GroupFinder.class.getName() + ".joinByPageCount";
106    
107            public static String JOIN_BY_ROLE_PERMISSIONS =
108                    GroupFinder.class.getName() + ".joinByRolePermissions";
109    
110            public static String JOIN_BY_ROLE_RESOURCE_PERMISSIONS =
111                    GroupFinder.class.getName() + ".joinByRoleResourcePermissions";
112    
113            public static String JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS =
114                    GroupFinder.class.getName() + ".joinByRoleResourceTypePermissions";
115    
116            public static String JOIN_BY_SITE =
117                    GroupFinder.class.getName() + ".joinBySite";
118    
119            public static String JOIN_BY_TYPE =
120                    GroupFinder.class.getName() + ".joinByType";
121    
122            public static String JOIN_BY_USER_GROUP_ROLE =
123                    GroupFinder.class.getName() + ".joinByUserGroupRole";
124    
125            public static 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,
561                            int start, 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$]",
902                                            "[$WHERE$]"
903                                    },
904                                    new String[] {
905                                            StringPool.BLANK,
906                                            StringPool.BLANK
907                                    });
908                    }
909    
910                    String cacheKey = _getCacheKey(sql, params);
911    
912                    String resultSQL = _replaceJoinAndWhereSQLCache.get(cacheKey);
913    
914                    if (resultSQL == null) {
915                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
916    
917                            resultSQL = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
918    
919                            _replaceJoinAndWhereSQLCache.put(cacheKey, resultSQL);
920                    }
921    
922                    return resultSQL;
923            }
924    
925            protected void setJoin(
926                    QueryPos qPos, LinkedHashMap<String, Object> params) {
927    
928                    if (params == null) {
929                            return;
930                    }
931    
932                    for (Map.Entry<String, Object> entry : params.entrySet()) {
933                            String key = entry.getKey();
934    
935                            if (key.equals("active") || key.equals("layoutSet") ||
936                                    key.equals("site")) {
937    
938                                    Boolean value = (Boolean)entry.getValue();
939    
940                                    qPos.add(value);
941                            }
942                            else if (key.equals("pageCount")) {
943                            }
944                            else if (key.equals("rolePermissions")) {
945                                    List<Object> values = (List<Object>)entry.getValue();
946    
947                                    String name = (String)values.get(0);
948                                    Integer scope = (Integer)values.get(1);
949                                    String actionId = (String)values.get(2);
950                                    Long roleId = (Long)values.get(3);
951    
952                                    if ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) &&
953                                            ResourceBlockLocalServiceUtil.isSupported(name)) {
954    
955                                            // Scope is assumed to always be group
956    
957                                            qPos.add(name);
958                                            qPos.add(roleId);
959                                            qPos.add(actionId);
960                                    }
961                                    else {
962                                            qPos.add(name);
963                                            qPos.add(scope);
964                                            qPos.add(actionId);
965                                            qPos.add(roleId);
966                                    }
967                            }
968                            else if (key.equals("types")) {
969                                    List<Integer> values = (List<Integer>)entry.getValue();
970    
971                                    for (int i = 0; i < values.size(); i++) {
972                                            Integer value = values.get(i);
973    
974                                            qPos.add(value);
975                                    }
976                            }
977                            else if (key.equals("userGroupRole")) {
978                                    List<Long> values = (List<Long>)entry.getValue();
979    
980                                    Long userId = values.get(0);
981                                    Long roleId = values.get(1);
982    
983                                    qPos.add(userId);
984                                    qPos.add(roleId);
985                            }
986                            else {
987                                    Object value = entry.getValue();
988    
989                                    if (value instanceof Integer) {
990                                            Integer valueInteger = (Integer)value;
991    
992                                            if (Validator.isNotNull(valueInteger)) {
993                                                    qPos.add(valueInteger);
994                                            }
995                                    }
996                                    else if (value instanceof Long) {
997                                            Long valueLong = (Long)value;
998    
999                                            if (Validator.isNotNull(valueLong)) {
1000                                                    qPos.add(valueLong);
1001                                            }
1002                                    }
1003                                    else if (value instanceof String) {
1004                                            String valueString = (String)value;
1005    
1006                                            if (Validator.isNotNull(valueString)) {
1007                                                    qPos.add(valueString);
1008                                            }
1009                                    }
1010                            }
1011                    }
1012            }
1013    
1014            private String _buildSQLKey(
1015                    LinkedHashMap<String, Object> param1,
1016                    LinkedHashMap<String, Object> param2,
1017                    LinkedHashMap<String, Object> param3,
1018                    LinkedHashMap<String, Object> param4, OrderByComparator obc,
1019                    boolean doUnion) {
1020    
1021                    StringBundler sb = null;
1022    
1023                    if (doUnion) {
1024                            sb = new StringBundler(
1025                                    param1.size() + param2.size() + param3.size() + param4.size() +
1026                                            1);
1027    
1028                            for (String key : param1.keySet()) {
1029                                    sb.append(key);
1030                            }
1031    
1032                            for (String key : param2.keySet()) {
1033                                    sb.append(key);
1034                            }
1035    
1036                            for (String key : param3.keySet()) {
1037                                    sb.append(key);
1038                            }
1039    
1040                            for (String key : param4.keySet()) {
1041                                    sb.append(key);
1042                            }
1043                    }
1044                    else {
1045                            sb = new StringBundler(param1.size() + 1);
1046    
1047                            for (String key : param1.keySet()) {
1048                                    sb.append(key);
1049                            }
1050                    }
1051    
1052                    sb.append(obc.getOrderBy());
1053    
1054                    return sb.toString();
1055            }
1056    
1057            private String _getCacheKey(
1058                    String sql, LinkedHashMap<String, Object> params) {
1059    
1060                    StringBundler sb = new StringBundler(params.size() + 1);
1061    
1062                    sb.append(sql);
1063    
1064                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1065                            String key = entry.getKey();
1066    
1067                            if (key.equals("rolePermissions") &&
1068                                    (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6)) {
1069    
1070                                    List<Object> values = (List<Object>)entry.getValue();
1071    
1072                                    String name = (String)values.get(0);
1073    
1074                                    if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1075                                            key = "rolePermissions_6_block";
1076                                    }
1077                                    else {
1078                                            key = "rolePermissions_6";
1079                                    }
1080                            }
1081    
1082                            sb.append(key);
1083                    }
1084    
1085                    return sb.toString();
1086            }
1087    
1088            private String _getCondition(String join) {
1089                    if (Validator.isNotNull(join)) {
1090                            int pos = join.indexOf("WHERE");
1091    
1092                            if (pos != -1) {
1093                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
1094                            }
1095                            else {
1096                                    join = StringPool.BLANK;
1097                            }
1098                    }
1099    
1100                    return join;
1101            }
1102    
1103            private String _removeWhere(String join) {
1104                    if (Validator.isNotNull(join)) {
1105                            int pos = join.indexOf("WHERE");
1106    
1107                            if (pos != -1) {
1108                                    join = join.substring(0, pos);
1109                            }
1110                    }
1111    
1112                    return join;
1113            }
1114    
1115            private long[] _getGroupOrganizationClassNameIds() {
1116                    if (_groupOrganizationClassNameIds == null) {
1117                            _groupOrganizationClassNameIds = new long[] {
1118                                    ClassNameLocalServiceUtil.getClassNameId(Group.class),
1119                                    ClassNameLocalServiceUtil.getClassNameId(Organization.class)
1120                            };
1121                    }
1122    
1123                    return _groupOrganizationClassNameIds;
1124            }
1125    
1126            private Map<String, String> _getJoinMap() {
1127                    if (_joinMap != null) {
1128                            return _joinMap;
1129                    }
1130    
1131                    Map<String, String> joinMap = new HashMap<String, String>();
1132    
1133                    joinMap.put("active", _removeWhere(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1134                    joinMap.put(
1135                            "groupOrg", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1136                    joinMap.put(
1137                            "groupsOrgs", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1138                    joinMap.put(
1139                            "groupsRoles",
1140                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1141                    joinMap.put(
1142                            "groupsUserGroups",
1143                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1144                    joinMap.put(
1145                            "layoutSet", _removeWhere(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1146                    joinMap.put(
1147                            "pageCount", _removeWhere(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1148                    joinMap.put(
1149                            "rolePermissions",
1150                            _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS)));
1151                    joinMap.put(
1152                            "rolePermissions_6",
1153                            _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1154                    joinMap.put(
1155                            "rolePermissions_6_block",
1156                            _removeWhere(
1157                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1158                    joinMap.put("site", _removeWhere(CustomSQLUtil.get(JOIN_BY_SITE)));
1159                    joinMap.put("type", _removeWhere(CustomSQLUtil.get(JOIN_BY_TYPE)));
1160                    joinMap.put("userGroupRole",
1161                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1162                    joinMap.put("usersGroups",
1163                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1164    
1165                    _joinMap = joinMap;
1166    
1167                    return _joinMap;
1168            }
1169    
1170            private Map<String, String> _getWhereMap() {
1171                    if (_whereMap != null) {
1172                            return _whereMap;
1173                    }
1174    
1175                    Map<String, String> whereMap = new HashMap<String, String>();
1176    
1177                    whereMap.put(
1178                            "active", _getCondition(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1179                    whereMap.put(
1180                            "creatorUserId",
1181                            _getCondition(CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID)));
1182                    whereMap.put(
1183                            "groupOrg", _getCondition(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1184                    whereMap.put(
1185                            "groupsOrgs",
1186                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1187                    whereMap.put(
1188                            "groupsRoles",
1189                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1190                    whereMap.put(
1191                            "groupsUserGroups",
1192                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1193                    whereMap.put(
1194                            "layoutSet", _getCondition(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1195                    whereMap.put(
1196                            "pageCount", _getCondition(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1197                    whereMap.put(
1198                            "rolePermissions",
1199                            _getCondition(CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS)));
1200                    whereMap.put(
1201                            "rolePermissions_6",
1202                            _getCondition(
1203                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1204                    whereMap.put(
1205                            "rolePermissions_6_block",
1206                            _getCondition(
1207                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1208                    whereMap.put(
1209                            "site", _getCondition(CustomSQLUtil.get(JOIN_BY_SITE)));
1210                    whereMap.put(
1211                            "type", _getCondition(CustomSQLUtil.get(JOIN_BY_TYPE)));
1212                    whereMap.put(
1213                            "userGroupRole",
1214                            _getCondition(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1215                    whereMap.put(
1216                            "usersGroups",
1217                            _getCondition(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1218    
1219                    _whereMap = whereMap;
1220    
1221                    return _whereMap;
1222            }
1223    
1224            private LinkedHashMap<String, Object> _emptyLinkedHashMap =
1225                    new LinkedHashMap<String, Object>(0);
1226            private Map<String, String> _findByCompanyIdSQLCache =
1227                    new ConcurrentHashMap<String, String>();
1228            private Map<String, String> _findByC_C_N_DSQLCache =
1229                    new ConcurrentHashMap<String, String>();
1230            private volatile long[] _groupOrganizationClassNameIds;
1231            private volatile Map<String, String> _joinMap;
1232            private Map<String, String> _replaceJoinAndWhereSQLCache =
1233                    new ConcurrentHashMap<String, String>();
1234            private volatile Map<String, String> _whereMap;
1235    
1236    }