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