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