001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.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.OrderByComparator;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.Organization;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.model.impl.UserImpl;
033    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
034    import com.liferay.util.dao.orm.CustomSQLUtil;
035    
036    import java.util.Iterator;
037    import java.util.LinkedHashMap;
038    import java.util.List;
039    import java.util.Map;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Jon Steer
044     * @author Raymond Augé
045     * @author Connor McKay
046     */
047    public class UserFinderImpl
048            extends BasePersistenceImpl<User> implements UserFinder {
049    
050            public static final String COUNT_BY_USER =
051                    UserFinder.class.getName() + ".countByUser";
052    
053            public static final String COUNT_BY_C_FN_MN_LN_SN_EA_S =
054                    UserFinder.class.getName() + ".countByC_FN_MN_LN_SN_EA_S";
055    
056            public static final String FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES =
057                    UserFinder.class.getName() + ".findByNoAnnouncementsDeliveries";
058    
059            public static final String FIND_BY_NO_CONTACTS =
060                    UserFinder.class.getName() + ".findByNoContacts";
061    
062            public static final String FIND_BY_NO_GROUPS =
063                    UserFinder.class.getName() + ".findByNoGroups";
064    
065            public static final String FIND_BY_C_FN_MN_LN_SN_EA_S =
066                    UserFinder.class.getName() + ".findByC_FN_MN_LN_SN_EA_S";
067    
068            public static final String JOIN_BY_CONTACT_TWITTER_SN =
069                    UserFinder.class.getName() + ".joinByContactTwitterSN";
070    
071            public static final String JOIN_BY_NO_ORGANIZATIONS =
072                    UserFinder.class.getName() + ".joinByNoOrganizations";
073    
074            public static final String JOIN_BY_PERMISSION =
075                    UserFinder.class.getName() + ".joinByPermission";
076    
077            public static final String JOIN_BY_USER_GROUP_ROLE =
078                    UserFinder.class.getName() + ".joinByUserGroupRole";
079    
080            public static final String JOIN_BY_USERS_GROUPS =
081                    UserFinder.class.getName() + ".joinByUsersGroups";
082    
083            public static final String JOIN_BY_USERS_ORGS =
084                    UserFinder.class.getName() + ".joinByUsersOrgs";
085    
086            public static final String JOIN_BY_USERS_ORGS_TREE =
087                    UserFinder.class.getName() + ".joinByUsersOrgsTree";
088    
089            public static final String JOIN_BY_USERS_PASSWORD_POLICIES =
090                    UserFinder.class.getName() + ".joinByUsersPasswordPolicies";
091    
092            public static final String JOIN_BY_USERS_ROLES =
093                    UserFinder.class.getName() + ".joinByUsersRoles";
094    
095            public static final String JOIN_BY_USERS_TEAMS =
096                    UserFinder.class.getName() + ".joinByUsersTeams";
097    
098            public static final String JOIN_BY_USERS_USER_GROUPS =
099                    UserFinder.class.getName() + ".joinByUsersUserGroups";
100    
101            public static final String JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS =
102                    UserFinder.class.getName() + ".joinByAnnouncementsDeliveryEmailOrSms";
103    
104            public static final String JOIN_BY_SOCIAL_MUTUAL_RELATION =
105                    UserFinder.class.getName() + ".joinBySocialMutualRelation";
106    
107            public static final String JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE =
108                    UserFinder.class.getName() + ".joinBySocialMutualRelationType";
109    
110            public static final String JOIN_BY_SOCIAL_RELATION =
111                    UserFinder.class.getName() + ".joinBySocialRelation";
112    
113            public static final String JOIN_BY_SOCIAL_RELATION_TYPE =
114                    UserFinder.class.getName() + ".joinBySocialRelationType";
115    
116            public int countByUser(long userId, LinkedHashMap<String, Object> params)
117                    throws SystemException {
118    
119                    Session session = null;
120    
121                    try {
122                            session = openSession();
123    
124                            String sql = CustomSQLUtil.get(COUNT_BY_USER);
125    
126                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
127                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
128    
129                            SQLQuery q = session.createSQLQuery(sql);
130    
131                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
132    
133                            QueryPos qPos = QueryPos.getInstance(q);
134    
135                            setJoin(qPos, params);
136    
137                            qPos.add(userId);
138    
139                            Iterator<Long> itr = q.iterate();
140    
141                            if (itr.hasNext()) {
142                                    Long count = itr.next();
143    
144                                    if (count != null) {
145                                            return count.intValue();
146                                    }
147                            }
148    
149                            return 0;
150                    }
151                    catch (Exception e) {
152                            throw new SystemException(e);
153                    }
154                    finally {
155                            closeSession(session);
156                    }
157            }
158    
159            public int countByKeywords(
160                            long companyId, String keywords, int status,
161                            LinkedHashMap<String, Object> params)
162                    throws SystemException {
163    
164                    String[] firstNames = null;
165                    String[] middleNames = null;
166                    String[] lastNames = null;
167                    String[] screenNames = null;
168                    String[] emailAddresses = null;
169                    boolean andOperator = false;
170    
171                    if (Validator.isNotNull(keywords)) {
172                            firstNames = CustomSQLUtil.keywords(keywords);
173                            middleNames = CustomSQLUtil.keywords(keywords);
174                            lastNames = CustomSQLUtil.keywords(keywords);
175                            screenNames = CustomSQLUtil.keywords(keywords);
176                            emailAddresses = CustomSQLUtil.keywords(keywords);
177                    }
178                    else {
179                            andOperator = true;
180                    }
181    
182                    return countByC_FN_MN_LN_SN_EA_S(
183                            companyId, firstNames, middleNames, lastNames, screenNames,
184                            emailAddresses, status, params, andOperator);
185            }
186    
187            public int countByC_FN_MN_LN_SN_EA_S(
188                            long companyId, String firstName, String middleName,
189                            String lastName, String screenName, String emailAddress, int status,
190                            LinkedHashMap<String, Object> params, boolean andOperator)
191                    throws SystemException {
192    
193                    String[] firstNames = CustomSQLUtil.keywords(firstName);
194                    String[] middleNames = CustomSQLUtil.keywords(middleName);
195                    String[] lastNames = CustomSQLUtil.keywords(lastName);
196                    String[] screenNames = CustomSQLUtil.keywords(screenName);
197                    String[] emailAddresses = CustomSQLUtil.keywords(emailAddress);
198    
199                    return countByC_FN_MN_LN_SN_EA_S(
200                            companyId, firstNames, middleNames, lastNames, screenNames,
201                            emailAddresses, status, params, andOperator);
202            }
203    
204            public int countByC_FN_MN_LN_SN_EA_S(
205                            long companyId, String[] firstNames, String[] middleNames,
206                            String[] lastNames, String[] screenNames, String[] emailAddresses,
207                            int status, LinkedHashMap<String, Object> params,
208                            boolean andOperator)
209                    throws SystemException {
210    
211                    firstNames = CustomSQLUtil.keywords(firstNames);
212                    middleNames = CustomSQLUtil.keywords(middleNames);
213                    lastNames = CustomSQLUtil.keywords(lastNames);
214                    screenNames = CustomSQLUtil.keywords(screenNames);
215                    emailAddresses = CustomSQLUtil.keywords(emailAddresses);
216    
217                    Session session = null;
218    
219                    try {
220                            session = openSession();
221    
222                            String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_S);
223    
224                            sql = CustomSQLUtil.replaceKeywords(
225                                    sql, "lower(User_.firstName)", StringPool.LIKE, false,
226                                    firstNames);
227                            sql = CustomSQLUtil.replaceKeywords(
228                                    sql, "lower(User_.middleName)", StringPool.LIKE, false,
229                                    middleNames);
230                            sql = CustomSQLUtil.replaceKeywords(
231                                    sql, "lower(User_.lastName)", StringPool.LIKE, false,
232                                    lastNames);
233                            sql = CustomSQLUtil.replaceKeywords(
234                                    sql, "lower(User_.screenName)", StringPool.LIKE, false,
235                                    screenNames);
236                            sql = CustomSQLUtil.replaceKeywords(
237                                    sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
238                                    emailAddresses);
239    
240                            if (status == WorkflowConstants.STATUS_ANY) {
241                                    sql = StringUtil.replace(sql, STATUS_SQL, StringPool.BLANK);
242                            }
243    
244                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
245                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
246                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
247    
248                            SQLQuery q = session.createSQLQuery(sql);
249    
250                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
251    
252                            QueryPos qPos = QueryPos.getInstance(q);
253    
254                            setJoin(qPos, params);
255    
256                            qPos.add(companyId);
257                            qPos.add(false);
258                            qPos.add(firstNames, 2);
259                            qPos.add(middleNames, 2);
260                            qPos.add(lastNames, 2);
261                            qPos.add(screenNames, 2);
262                            qPos.add(emailAddresses, 2);
263    
264                            if (status != WorkflowConstants.STATUS_ANY) {
265                                    qPos.add(status);
266                            }
267    
268                            Iterator<Long> itr = q.iterate();
269    
270                            if (itr.hasNext()) {
271                                    Long count = itr.next();
272    
273                                    if (count != null) {
274                                            return count.intValue();
275                                    }
276                            }
277    
278                            return 0;
279                    }
280                    catch (Exception e) {
281                            throw new SystemException(e);
282                    }
283                    finally {
284                            closeSession(session);
285                    }
286            }
287    
288            public List<User> findByKeywords(
289                            long companyId, String keywords, int status,
290                            LinkedHashMap<String, Object> params, int start, int end,
291                            OrderByComparator obc)
292                    throws SystemException {
293    
294                    String[] firstNames = null;
295                    String[] middleNames = null;
296                    String[] lastNames = null;
297                    String[] screenNames = null;
298                    String[] emailAddresses = null;
299                    boolean andOperator = false;
300    
301                    if (Validator.isNotNull(keywords)) {
302                            firstNames = CustomSQLUtil.keywords(keywords);
303                            middleNames = CustomSQLUtil.keywords(keywords);
304                            lastNames = CustomSQLUtil.keywords(keywords);
305                            screenNames = CustomSQLUtil.keywords(keywords);
306                            emailAddresses = CustomSQLUtil.keywords(keywords);
307                    }
308                    else {
309                            andOperator = true;
310                    }
311    
312                    return findByC_FN_MN_LN_SN_EA_S(
313                            companyId, firstNames, middleNames, lastNames, screenNames,
314                            emailAddresses, status, params, andOperator, start, end, obc);
315            }
316    
317            public List<User> findByNoAnnouncementsDeliveries(String type)
318                    throws SystemException {
319    
320                    Session session = null;
321    
322                    try {
323                            session = openSession();
324    
325                            String sql = CustomSQLUtil.get(FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES);
326    
327                            SQLQuery q = session.createSQLQuery(sql);
328    
329                            q.addEntity("User_", UserImpl.class);
330    
331                            QueryPos qPos = QueryPos.getInstance(q);
332    
333                            qPos.add(type);
334    
335                            return q.list(true);
336                    }
337                    catch (Exception e) {
338                            throw new SystemException(e);
339                    }
340                    finally {
341                            closeSession(session);
342                    }
343            }
344    
345            public List<User> findByNoContacts() throws SystemException {
346                    Session session = null;
347    
348                    try {
349                            session = openSession();
350    
351                            String sql = CustomSQLUtil.get(FIND_BY_NO_CONTACTS);
352    
353                            SQLQuery q = session.createSQLQuery(sql);
354    
355                            q.addEntity("User_", UserImpl.class);
356    
357                            return q.list(true);
358                    }
359                    catch (Exception e) {
360                            throw new SystemException(e);
361                    }
362                    finally {
363                            closeSession(session);
364                    }
365            }
366    
367            public List<User> findByNoGroups() throws SystemException {
368                    Session session = null;
369    
370                    try {
371                            session = openSession();
372    
373                            String sql = CustomSQLUtil.get(FIND_BY_NO_GROUPS);
374    
375                            SQLQuery q = session.createSQLQuery(sql);
376    
377                            q.addEntity("User_", UserImpl.class);
378    
379                            return q.list(true);
380                    }
381                    catch (Exception e) {
382                            throw new SystemException(e);
383                    }
384                    finally {
385                            closeSession(session);
386                    }
387            }
388    
389            public List<User> findByC_FN_MN_LN_SN_EA_S(
390                            long companyId, String firstName, String middleName,
391                            String lastName, String screenName, String emailAddress, int status,
392                            LinkedHashMap<String, Object> params, boolean andOperator,
393                            int start, int end, OrderByComparator obc)
394                    throws SystemException {
395    
396                    String[] firstNames = CustomSQLUtil.keywords(firstName);
397                    String[] middleNames = CustomSQLUtil.keywords(middleName);
398                    String[] lastNames = CustomSQLUtil.keywords(lastName);
399                    String[] screenNames = CustomSQLUtil.keywords(screenName);
400                    String[] emailAddresses = CustomSQLUtil.keywords(emailAddress);
401    
402                    return findByC_FN_MN_LN_SN_EA_S(
403                            companyId, firstNames, middleNames, lastNames, screenNames,
404                            emailAddresses, status, params, andOperator, start, end, obc);
405            }
406    
407            public List<User> findByC_FN_MN_LN_SN_EA_S(
408                            long companyId, String[] firstNames, String[] middleNames,
409                            String[] lastNames, String[] screenNames, String[] emailAddresses,
410                            int status, LinkedHashMap<String, Object> params,
411                            boolean andOperator, int start, int end, OrderByComparator obc)
412                    throws SystemException {
413    
414                    firstNames = CustomSQLUtil.keywords(firstNames);
415                    middleNames = CustomSQLUtil.keywords(middleNames);
416                    lastNames = CustomSQLUtil.keywords(lastNames);
417                    screenNames = CustomSQLUtil.keywords(screenNames);
418                    emailAddresses = CustomSQLUtil.keywords(emailAddresses);
419    
420                    Session session = null;
421    
422                    try {
423                            session = openSession();
424    
425                            String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_S);
426    
427                            sql = CustomSQLUtil.replaceKeywords(
428                                    sql, "lower(User_.firstName)", StringPool.LIKE, false,
429                                    firstNames);
430                            sql = CustomSQLUtil.replaceKeywords(
431                                    sql, "lower(User_.middleName)", StringPool.LIKE, false,
432                                    middleNames);
433                            sql = CustomSQLUtil.replaceKeywords(
434                                    sql, "lower(User_.lastName)", StringPool.LIKE, false,
435                                    lastNames);
436                            sql = CustomSQLUtil.replaceKeywords(
437                                    sql, "lower(User_.screenName)", StringPool.LIKE, false,
438                                    screenNames);
439                            sql = CustomSQLUtil.replaceKeywords(
440                                    sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
441                                    emailAddresses);
442    
443                            if (status == WorkflowConstants.STATUS_ANY) {
444                                    sql = StringUtil.replace(sql, STATUS_SQL, StringPool.BLANK);
445                            }
446    
447                            sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
448                            sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
449                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
450                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
451    
452                            SQLQuery q = session.createSQLQuery(sql);
453    
454                            q.addEntity("User_", UserImpl.class);
455    
456                            QueryPos qPos = QueryPos.getInstance(q);
457    
458                            setJoin(qPos, params);
459    
460                            qPos.add(companyId);
461                            qPos.add(false);
462                            qPos.add(firstNames, 2);
463                            qPos.add(middleNames, 2);
464                            qPos.add(lastNames, 2);
465                            qPos.add(screenNames, 2);
466                            qPos.add(emailAddresses, 2);
467    
468                            if (status != WorkflowConstants.STATUS_ANY) {
469                                    qPos.add(status);
470                            }
471    
472                            return (List<User>)QueryUtil.list(q, getDialect(), start, end);
473                    }
474                    catch (Exception e) {
475                            throw new SystemException(e);
476                    }
477                    finally {
478                            closeSession(session);
479                    }
480            }
481    
482            protected String getJoin(LinkedHashMap<String, Object> params) {
483                    if ((params == null) || params.isEmpty()) {
484                            return StringPool.BLANK;
485                    }
486    
487                    StringBundler sb = new StringBundler(params.size());
488    
489                    Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
490    
491                    while (itr.hasNext()) {
492                            Map.Entry<String, Object> entry = itr.next();
493    
494                            String key = entry.getKey();
495    
496                            if (key.equals("expandoAttributes")) {
497                                    continue;
498                            }
499    
500                            Object value = entry.getValue();
501    
502                            if (Validator.isNotNull(value)) {
503                                    sb.append(getJoin(key, value));
504                            }
505                    }
506    
507                    return sb.toString();
508            }
509    
510            protected String getJoin(String key, Object value) {
511                    String join = StringPool.BLANK;
512    
513                    if (key.equals("contactTwitterSn")) {
514                            join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
515                    }
516                    else if (key.equals("noOrganizations")) {
517                            join = CustomSQLUtil.get(JOIN_BY_NO_ORGANIZATIONS);
518                    }
519                    else if (key.equals("permission")) {
520                            join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
521                    }
522                    else if (key.equals("userGroupRole")) {
523                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
524                    }
525                    else if (key.equals("usersGroups")) {
526                            join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
527                    }
528                    else if (key.equals("usersOrgs")) {
529                            join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
530                    }
531                    else if (key.equals("usersOrgsTree")) {
532                            join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS_TREE);
533                    }
534                    else if (key.equals("usersPasswordPolicies")) {
535                            join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
536                    }
537                    else if (key.equals("usersRoles")) {
538                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
539                    }
540                    else if (key.equals("usersTeams")) {
541                            join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
542                    }
543                    else if (key.equals("usersUserGroups")) {
544                            join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
545                    }
546                    else if (key.equals("announcementsDeliveryEmailOrSms")) {
547                            join = CustomSQLUtil.get(
548                                    JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
549                    }
550                    else if (key.equals("socialMutualRelation")) {
551                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
552                    }
553                    else if (key.equals("socialMutualRelationType")) {
554                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
555                    }
556                    else if (key.equals("socialRelation")) {
557                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
558                    }
559                    else if (key.equals("socialRelationType")) {
560                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
561                    }
562                    else if (value instanceof CustomSQLParam) {
563                            CustomSQLParam customSQLParam = (CustomSQLParam)value;
564    
565                            join = customSQLParam.getSQL();
566                    }
567    
568                    if (Validator.isNotNull(join)) {
569                            int pos = join.indexOf("WHERE");
570    
571                            if (pos != -1) {
572                                    join = join.substring(0, pos);
573                            }
574                    }
575    
576                    return join;
577            }
578    
579            protected String getWhere(LinkedHashMap<String, Object> params) {
580                    if ((params == null) || params.isEmpty()) {
581                            return StringPool.BLANK;
582                    }
583    
584                    StringBundler sb = new StringBundler(params.size());
585    
586                    Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
587    
588                    while (itr.hasNext()) {
589                            Map.Entry<String, Object> entry = itr.next();
590    
591                            String key = entry.getKey();
592    
593                            if (key.equals("expandoAttributes")) {
594                                    continue;
595                            }
596    
597                            Object value = entry.getValue();
598    
599                            if (Validator.isNotNull(value)) {
600                                    sb.append(getWhere(key, value));
601                            }
602                    }
603    
604                    return sb.toString();
605            }
606    
607            protected String getWhere(String key, Object value) {
608                    String join = StringPool.BLANK;
609    
610                    if (key.equals("contactTwitterSn")) {
611                            join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
612                    }
613                    else if (key.equals("noOrganizations")) {
614                            join = CustomSQLUtil.get(JOIN_BY_NO_ORGANIZATIONS);
615                    }
616                    else if (key.equals("permission")) {
617                            join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
618                    }
619                    else if (key.equals("userGroupRole")) {
620                            join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
621                    }
622                    else if (key.equals("usersGroups")) {
623                            join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
624                    }
625                    else if (key.equals("usersOrgs")) {
626                            if (value instanceof Long) {
627                                    join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
628                            }
629                            else if (value instanceof Long[]) {
630                                    Long[] organizationIds = (Long[])value;
631    
632                                    if (organizationIds.length == 0) {
633                                            join = "WHERE ((Users_Orgs.organizationId = -1) ))";
634                                    }
635                                    else {
636                                            StringBundler sb = new StringBundler(
637                                                    organizationIds.length * 2 + 1);
638    
639                                            sb.append("WHERE (");
640    
641                                            for (int i = 0; i < organizationIds.length; i++) {
642                                                    sb.append("(Users_Orgs.organizationId = ?) ");
643    
644                                                    if ((i + 1) < organizationIds.length) {
645                                                            sb.append("OR ");
646                                                    }
647                                            }
648    
649                                            sb.append(")");
650    
651                                            join = sb.toString();
652                                    }
653                            }
654                    }
655                    else if (key.equals("usersOrgsTree")) {
656                            List<Organization> organizationsTree = (List<Organization>)value;
657    
658                            int size = organizationsTree.size();
659    
660                            if (size > 0) {
661                                    StringBundler sb = new StringBundler(size * 2 + 1);
662    
663                                    sb.append("WHERE (");
664    
665                                    for (int i = 0; i < size; i++) {
666                                            sb.append("(Organization_.treePath LIKE ?) ");
667    
668                                            if ((i + 1) < size) {
669                                                    sb.append("OR ");
670                                            }
671                                    }
672    
673                                    sb.append(")");
674    
675                                    join = sb.toString();
676                            }
677                    }
678                    else if (key.equals("usersPasswordPolicies")) {
679                            join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
680                    }
681                    else if (key.equals("usersRoles")) {
682                            join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
683                    }
684                    else if (key.equals("usersTeams")) {
685                            join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
686                    }
687                    else if (key.equals("usersUserGroups")) {
688                            join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
689                    }
690                    else if (key.equals("announcementsDeliveryEmailOrSms")) {
691                            join = CustomSQLUtil.get(
692                                    JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
693                    }
694                    else if (key.equals("socialMutualRelation")) {
695                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
696                    }
697                    else if (key.equals("socialMutualRelationType")) {
698                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
699                    }
700                    else if (key.equals("socialRelation")) {
701                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
702                    }
703                    else if (key.equals("socialRelationType")) {
704                            join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
705                    }
706                    else if (value instanceof CustomSQLParam) {
707                            CustomSQLParam customSQLParam = (CustomSQLParam)value;
708    
709                            join = customSQLParam.getSQL();
710                    }
711    
712                    if (Validator.isNotNull(join)) {
713                            int pos = join.indexOf("WHERE");
714    
715                            if (pos != -1) {
716                                    join = join.substring(pos + 5, join.length()).concat(" AND ");
717                            }
718                            else {
719                                    join = StringPool.BLANK;
720                            }
721                    }
722    
723                    return join;
724            }
725    
726            protected void setJoin(
727                    QueryPos qPos, LinkedHashMap<String, Object> params) {
728    
729                    if (params == null) {
730                            return;
731                    }
732    
733                    for (Map.Entry<String, Object> entry : params.entrySet()) {
734                            String key = entry.getKey();
735    
736                            if (key.equals("expandoAttributes")) {
737                                    continue;
738                            }
739    
740                            Object value = entry.getValue();
741    
742                            if (key.equals("usersOrgsTree")) {
743                                    List<Organization> organizationsTree =
744                                            (List<Organization>)value;
745    
746                                    if (!organizationsTree.isEmpty()) {
747                                            for (Organization organization : organizationsTree) {
748                                                    StringBundler treePath = new StringBundler(5);
749    
750                                                    treePath.append(StringPool.PERCENT);
751                                                    treePath.append(StringPool.SLASH);
752                                                    treePath.append(organization.getOrganizationId());
753                                                    treePath.append(StringPool.SLASH);
754                                                    treePath.append(StringPool.PERCENT);
755    
756                                                    qPos.add(treePath.toString());
757                                            }
758                                    }
759                            }
760                            else if (value instanceof Long) {
761                                    Long valueLong = (Long)value;
762    
763                                    if (Validator.isNotNull(valueLong)) {
764                                            qPos.add(valueLong);
765                                    }
766                            }
767                            else if (value instanceof Long[]) {
768                                    Long[] valueArray = (Long[])value;
769    
770                                    for (Long element : valueArray) {
771                                            if (Validator.isNotNull(element)) {
772                                                    qPos.add(element);
773                                            }
774                                    }
775                            }
776                            else if (value instanceof Long[][]) {
777                                    Long[][] valueDoubleArray = (Long[][])value;
778    
779                                    for (Long[] valueArray : valueDoubleArray) {
780                                            for (Long valueLong : valueArray) {
781                                                    qPos.add(valueLong);
782                                            }
783                                    }
784                            }
785                            else if (value instanceof String) {
786                                    String valueString = (String)value;
787    
788                                    if (Validator.isNotNull(valueString)) {
789                                            qPos.add(valueString);
790                                    }
791                            }
792                            else if (value instanceof String[]) {
793                                    String[] valueArray = (String[])value;
794    
795                                    for (String element : valueArray) {
796                                            if (Validator.isNotNull(element)) {
797                                                    qPos.add(element);
798                                            }
799                                    }
800                            }
801                            else if (value instanceof CustomSQLParam) {
802                                    CustomSQLParam customSQLParam = (CustomSQLParam)value;
803    
804                                    customSQLParam.process(qPos);
805                            }
806                    }
807            }
808    
809            protected static final String STATUS_SQL = "AND (User_.status = ?)";
810    
811    }