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