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