001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.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                    Long userId = (Long)params.get("usersGroups");
431    
432                    boolean doUnion = Validator.isNotNull(userId);
433    
434                    LinkedHashMap<String, Object> params1 = params;
435    
436                    LinkedHashMap<String, Object> params2 = null;
437    
438                    LinkedHashMap<String, Object> params3 = null;
439    
440                    LinkedHashMap<String, Object> params4 = null;
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                    Long userId = (Long)params.get("usersGroups");
749                    boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
750    
751                    boolean doUnion = Validator.isNotNull(userId) && inherit;
752    
753                    LinkedHashMap<String, Object> params1 = params;
754    
755                    LinkedHashMap<String, Object> params2 = null;
756    
757                    LinkedHashMap<String, Object> params3 = null;
758    
759                    LinkedHashMap<String, Object> params4 = null;
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                    Long userId = (Long)params.get("usersGroups");
1036                    boolean inherit = GetterUtil.getBoolean(params.get("inherit"), true);
1037    
1038                    boolean doUnion = Validator.isNotNull(userId) && inherit;
1039    
1040                    LinkedHashMap<String, Object> params1 = params;
1041    
1042                    LinkedHashMap<String, Object> params2 = null;
1043    
1044                    LinkedHashMap<String, Object> params3 = null;
1045    
1046                    LinkedHashMap<String, Object> params4 = null;
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("groupsTree")) {
1339                                    List<Group> groupsTree = (List<Group>)entry.getValue();
1340    
1341                                    if (!groupsTree.isEmpty()) {
1342                                            sb.append(StringPool.OPEN_PARENTHESIS);
1343    
1344                                            for (int i = 0; i < groupsTree.size(); i++) {
1345                                                    sb.append("(Group_.treePath LIKE ?) ");
1346    
1347                                                    if ((i + 1) < groupsTree.size()) {
1348                                                            sb.append("OR ");
1349                                                    }
1350                                            }
1351    
1352                                            sb.append(") AND ");
1353                                    }
1354                            }
1355                            else if (key.equals("types")) {
1356                                    List<Integer> types = (List<Integer>)entry.getValue();
1357    
1358                                    if (!types.isEmpty()) {
1359                                            sb.append(StringPool.OPEN_PARENTHESIS);
1360    
1361                                            for (int i = 0; i < types.size(); i++) {
1362                                                    sb.append("(Group_.type_ = ?) ");
1363    
1364                                                    if ((i + 1) < types.size()) {
1365                                                            sb.append("OR ");
1366                                                    }
1367                                            }
1368    
1369                                            sb.append(") AND ");
1370                                    }
1371                            }
1372                            else {
1373                                    if (key.equals("rolePermissions")) {
1374    
1375                                            List<Object> values = (List<Object>)entry.getValue();
1376    
1377                                            String name = (String)values.get(0);
1378    
1379                                            if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1380                                                    key = "rolePermissions_6_block";
1381                                            }
1382                                            else {
1383                                                    key = "rolePermissions_6";
1384                                            }
1385                                    }
1386    
1387                                    Map<String, String> whereMap = _getWhereMap();
1388    
1389                                    String whereValue = whereMap.get(key);
1390    
1391                                    if (Validator.isNotNull(whereValue)) {
1392                                            sb.append(whereValue);
1393                                    }
1394                            }
1395                    }
1396    
1397                    return sb.toString();
1398            }
1399    
1400            protected String replaceJoinAndWhere(
1401                    String sql, LinkedHashMap<String, Object> params) {
1402    
1403                    if (params.isEmpty()) {
1404                            return StringUtil.replace(
1405                                    sql,
1406                                    new String[] {
1407                                            "[$JOIN$]", "[$WHERE$]"
1408                                    },
1409                                    new String[] {
1410                                            StringPool.BLANK, StringPool.BLANK
1411                                    });
1412                    }
1413    
1414                    String cacheKey = _getCacheKey(sql, params);
1415    
1416                    String resultSQL = _replaceJoinAndWhereSQLCache.get(cacheKey);
1417    
1418                    if (resultSQL == null) {
1419                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
1420    
1421                            resultSQL = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
1422    
1423                            _replaceJoinAndWhereSQLCache.put(cacheKey, resultSQL);
1424                    }
1425    
1426                    return resultSQL;
1427            }
1428    
1429            protected String replaceOrderBy(String sql, OrderByComparator obc) {
1430                    if (obc instanceof GroupNameComparator) {
1431                            sql = StringUtil.replace(
1432                                    sql, "Group_.name AS groupName",
1433                                    "REPLACE(Group_.name, '" +
1434                                            GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX +
1435                                                    "', '') AS groupName");
1436                    }
1437    
1438                    return sql;
1439            }
1440    
1441            protected void setJoin(
1442                    QueryPos qPos, LinkedHashMap<String, Object> params) {
1443    
1444                    if (params == null) {
1445                            return;
1446                    }
1447    
1448                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1449                            String key = entry.getKey();
1450    
1451                            if (key.equals("active") || key.equals("layoutSet") ||
1452                                    key.equals("site")) {
1453    
1454                                    Boolean value = (Boolean)entry.getValue();
1455    
1456                                    qPos.add(value);
1457                            }
1458                            else if (key.equals("groupsTree")) {
1459                                    List<Group> groupsTree = (List<Group>)entry.getValue();
1460    
1461                                    if (!groupsTree.isEmpty()) {
1462                                            for (Group group : groupsTree) {
1463                                                    StringBundler sb = new StringBundler(5);
1464    
1465                                                    sb.append(StringPool.PERCENT);
1466                                                    sb.append(StringPool.SLASH);
1467                                                    sb.append(group.getGroupId());
1468                                                    sb.append(StringPool.SLASH);
1469                                                    sb.append(StringPool.PERCENT);
1470    
1471                                                    qPos.add(sb.toString());
1472                                            }
1473                                    }
1474                            }
1475                            else if (key.equals("pageCount")) {
1476                            }
1477                            else if (key.equals("rolePermissions")) {
1478                                    List<Object> values = (List<Object>)entry.getValue();
1479    
1480                                    String name = (String)values.get(0);
1481                                    Integer scope = (Integer)values.get(1);
1482                                    String actionId = (String)values.get(2);
1483                                    Long roleId = (Long)values.get(3);
1484    
1485                                    if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1486    
1487                                            // Scope is assumed to always be group
1488    
1489                                            qPos.add(name);
1490                                            qPos.add(roleId);
1491                                            qPos.add(actionId);
1492                                    }
1493                                    else {
1494                                            qPos.add(name);
1495                                            qPos.add(scope);
1496                                            qPos.add(actionId);
1497                                            qPos.add(roleId);
1498                                    }
1499                            }
1500                            else if (key.equals("types")) {
1501                                    List<Integer> values = (List<Integer>)entry.getValue();
1502    
1503                                    for (int i = 0; i < values.size(); i++) {
1504                                            Integer value = values.get(i);
1505    
1506                                            qPos.add(value);
1507                                    }
1508                            }
1509                            else if (key.equals("userGroupRole")) {
1510                                    List<Long> values = (List<Long>)entry.getValue();
1511    
1512                                    Long userId = values.get(0);
1513                                    Long roleId = values.get(1);
1514    
1515                                    qPos.add(userId);
1516                                    qPos.add(roleId);
1517                            }
1518                            else {
1519                                    Object value = entry.getValue();
1520    
1521                                    if (value instanceof Integer) {
1522                                            Integer valueInteger = (Integer)value;
1523    
1524                                            if (Validator.isNotNull(valueInteger)) {
1525                                                    qPos.add(valueInteger);
1526                                            }
1527                                    }
1528                                    else if (value instanceof Long) {
1529                                            Long valueLong = (Long)value;
1530    
1531                                            if (Validator.isNotNull(valueLong)) {
1532                                                    qPos.add(valueLong);
1533                                            }
1534                                    }
1535                                    else if (value instanceof String) {
1536                                            String valueString = (String)value;
1537    
1538                                            if (Validator.isNotNull(valueString)) {
1539                                                    qPos.add(valueString);
1540                                            }
1541                                    }
1542                            }
1543                    }
1544            }
1545    
1546            private String _buildSQLKey(
1547                    LinkedHashMap<String, Object> param1,
1548                    LinkedHashMap<String, Object> param2,
1549                    LinkedHashMap<String, Object> param3,
1550                    LinkedHashMap<String, Object> param4, OrderByComparator obc,
1551                    boolean doUnion) {
1552    
1553                    StringBundler sb = null;
1554    
1555                    if (doUnion) {
1556                            sb = new StringBundler(
1557                                    param1.size() + param2.size() + param3.size() + param4.size() +
1558                                            1);
1559    
1560                            for (String key : param1.keySet()) {
1561                                    sb.append(key);
1562                            }
1563    
1564                            for (String key : param2.keySet()) {
1565                                    sb.append(key);
1566                            }
1567    
1568                            for (String key : param3.keySet()) {
1569                                    sb.append(key);
1570                            }
1571    
1572                            for (String key : param4.keySet()) {
1573                                    sb.append(key);
1574                            }
1575                    }
1576                    else {
1577                            sb = new StringBundler(param1.size() + 1);
1578    
1579                            for (String key : param1.keySet()) {
1580                                    sb.append(key);
1581                            }
1582                    }
1583    
1584                    sb.append(obc.getOrderBy());
1585    
1586                    return sb.toString();
1587            }
1588    
1589            private String _getCacheKey(
1590                    String sql, LinkedHashMap<String, Object> params) {
1591    
1592                    StringBundler sb = new StringBundler(params.size() + 1);
1593    
1594                    sb.append(sql);
1595    
1596                    for (Map.Entry<String, Object> entry : params.entrySet()) {
1597                            String key = entry.getKey();
1598    
1599                            if (key.equals("rolePermissions")) {
1600    
1601                                    List<Object> values = (List<Object>)entry.getValue();
1602    
1603                                    String name = (String)values.get(0);
1604    
1605                                    if (ResourceBlockLocalServiceUtil.isSupported(name)) {
1606                                            key = "rolePermissions_6_block";
1607                                    }
1608                                    else {
1609                                            key = "rolePermissions_6";
1610                                    }
1611                            }
1612    
1613                            sb.append(key);
1614                    }
1615    
1616                    return sb.toString();
1617            }
1618    
1619            private String _getCondition(String join) {
1620                    if (Validator.isNotNull(join)) {
1621                            int pos = join.indexOf("WHERE");
1622    
1623                            if (pos != -1) {
1624                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
1625                            }
1626                            else {
1627                                    join = StringPool.BLANK;
1628                            }
1629                    }
1630    
1631                    return join;
1632            }
1633    
1634            private long[] _getGroupOrganizationClassNameIds() {
1635                    if (_groupOrganizationClassNameIds == null) {
1636                            _groupOrganizationClassNameIds = new long[] {
1637                                    ClassNameLocalServiceUtil.getClassNameId(Group.class),
1638                                    ClassNameLocalServiceUtil.getClassNameId(Organization.class)
1639                            };
1640                    }
1641    
1642                    return _groupOrganizationClassNameIds;
1643            }
1644    
1645            private Map<String, String> _getJoinMap() {
1646                    if (_joinMap != null) {
1647                            return _joinMap;
1648                    }
1649    
1650                    Map<String, String> joinMap = new HashMap<String, String>();
1651    
1652                    joinMap.put("active", _removeWhere(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1653                    joinMap.put(
1654                            "groupOrg", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1655                    joinMap.put(
1656                            "groupsOrgs", _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1657                    joinMap.put(
1658                            "groupsRoles",
1659                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1660                    joinMap.put(
1661                            "groupsUserGroups",
1662                            _removeWhere(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1663                    joinMap.put(
1664                            "layoutSet", _removeWhere(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1665                    joinMap.put(
1666                            "pageCount", _removeWhere(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1667                    joinMap.put(
1668                            "rolePermissions",
1669                            _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS)));
1670                    joinMap.put(
1671                            "rolePermissions_6",
1672                            _removeWhere(CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1673                    joinMap.put(
1674                            "rolePermissions_6_block",
1675                            _removeWhere(
1676                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1677                    joinMap.put("site", _removeWhere(CustomSQLUtil.get(JOIN_BY_SITE)));
1678                    joinMap.put("type", _removeWhere(CustomSQLUtil.get(JOIN_BY_TYPE)));
1679                    joinMap.put(
1680                            "userGroupRole",
1681                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1682                    joinMap.put(
1683                            "usersGroups",
1684                            _removeWhere(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1685    
1686                    _joinMap = joinMap;
1687    
1688                    return _joinMap;
1689            }
1690    
1691            private Map<String, String> _getWhereMap() {
1692                    if (_whereMap != null) {
1693                            return _whereMap;
1694                    }
1695    
1696                    Map<String, String> whereMap = new HashMap<String, String>();
1697    
1698                    whereMap.put(
1699                            "active", _getCondition(CustomSQLUtil.get(JOIN_BY_ACTIVE)));
1700                    whereMap.put(
1701                            "creatorUserId",
1702                            _getCondition(CustomSQLUtil.get(JOIN_BY_CREATOR_USER_ID)));
1703                    whereMap.put(
1704                            "groupOrg", _getCondition(CustomSQLUtil.get(JOIN_BY_GROUP_ORG)));
1705                    whereMap.put(
1706                            "groupsOrgs",
1707                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ORGS)));
1708                    whereMap.put(
1709                            "groupsRoles",
1710                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_ROLES)));
1711                    whereMap.put(
1712                            "groupsUserGroups",
1713                            _getCondition(CustomSQLUtil.get(JOIN_BY_GROUPS_USER_GROUPS)));
1714                    whereMap.put(
1715                            "layoutSet", _getCondition(CustomSQLUtil.get(JOIN_BY_LAYOUT_SET)));
1716                    whereMap.put(
1717                            "pageCount", _getCondition(CustomSQLUtil.get(JOIN_BY_PAGE_COUNT)));
1718                    whereMap.put(
1719                            "rolePermissions",
1720                            _getCondition(CustomSQLUtil.get(JOIN_BY_ROLE_PERMISSIONS)));
1721                    whereMap.put(
1722                            "rolePermissions_6",
1723                            _getCondition(
1724                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_PERMISSIONS)));
1725                    whereMap.put(
1726                            "rolePermissions_6_block",
1727                            _getCondition(
1728                                    CustomSQLUtil.get(JOIN_BY_ROLE_RESOURCE_TYPE_PERMISSIONS)));
1729                    whereMap.put("site", _getCondition(CustomSQLUtil.get(JOIN_BY_SITE)));
1730                    whereMap.put("type", _getCondition(CustomSQLUtil.get(JOIN_BY_TYPE)));
1731                    whereMap.put(
1732                            "userGroupRole",
1733                            _getCondition(CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE)));
1734                    whereMap.put(
1735                            "usersGroups",
1736                            _getCondition(CustomSQLUtil.get(JOIN_BY_USERS_GROUPS)));
1737    
1738                    _whereMap = whereMap;
1739    
1740                    return _whereMap;
1741            }
1742    
1743            private String _removeWhere(String join) {
1744                    if (Validator.isNotNull(join)) {
1745                            int pos = join.indexOf("WHERE");
1746    
1747                            if (pos != -1) {
1748                                    join = join.substring(0, pos);
1749                            }
1750                    }
1751    
1752                    return join;
1753            }
1754    
1755            private LinkedHashMap<String, Object> _emptyLinkedHashMap =
1756                    new LinkedHashMap<String, Object>(0);
1757            private Map<String, String> _findByC_C_PG_N_DSQLCache =
1758                    new ConcurrentHashMap<String, String>();
1759            private Map<String, String> _findByCompanyIdSQLCache =
1760                    new ConcurrentHashMap<String, String>();
1761            private volatile long[] _groupOrganizationClassNameIds;
1762            private volatile Map<String, String> _joinMap;
1763            private Map<String, String> _replaceJoinAndWhereSQLCache =
1764                    new ConcurrentHashMap<String, String>();
1765            private volatile Map<String, String> _whereMap;
1766    
1767    }