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