1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.CustomSQLParam;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.exception.SystemException;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.model.impl.UserImpl;
31  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.Iterator;
35  import java.util.LinkedHashMap;
36  import java.util.List;
37  import java.util.Map;
38  
39  /**
40   * <a href="UserFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Jon Steer
44   * @author Raymond Augé
45   */
46  public class UserFinderImpl
47      extends BasePersistenceImpl<User> implements UserFinder {
48  
49      public static String COUNT_BY_USER =
50          UserFinder.class.getName() + ".countByUser";
51  
52      public static String COUNT_BY_C_FN_MN_LN_SN_EA_A =
53          UserFinder.class.getName() + ".countByC_FN_MN_LN_SN_EA_A";
54  
55      public static String FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES =
56          UserFinder.class.getName() + ".findByNoAnnouncementsDeliveries";
57  
58      public static String FIND_BY_NO_CONTACTS =
59          UserFinder.class.getName() + ".findByNoContacts";
60  
61      public static String FIND_BY_NO_GROUPS =
62          UserFinder.class.getName() + ".findByNoGroups";
63  
64      public static String FIND_BY_C_FN_MN_LN_SN_EA_A =
65          UserFinder.class.getName() + ".findByC_FN_MN_LN_SN_EA_A";
66  
67      public static String JOIN_BY_CONTACT_TWITTER_SN =
68          UserFinder.class.getName() + ".joinByContactTwitterSN";
69  
70      public static String JOIN_BY_PERMISSION =
71          UserFinder.class.getName() + ".joinByPermission";
72  
73      public static String JOIN_BY_USER_GROUP_ROLE =
74          UserFinder.class.getName() + ".joinByUserGroupRole";
75  
76      public static String JOIN_BY_USERS_GROUPS =
77          UserFinder.class.getName() + ".joinByUsersGroups";
78  
79      public static String JOIN_BY_USERS_ORGS =
80          UserFinder.class.getName() + ".joinByUsersOrgs";
81  
82      public static String JOIN_BY_USERS_ORGS_TREE =
83          UserFinder.class.getName() + ".joinByUsersOrgsTree";
84  
85      public static String JOIN_BY_USERS_PASSWORD_POLICIES =
86          UserFinder.class.getName() + ".joinByUsersPasswordPolicies";
87  
88      public static String JOIN_BY_USERS_ROLES =
89          UserFinder.class.getName() + ".joinByUsersRoles";
90  
91      public static String JOIN_BY_USERS_USER_GROUPS =
92          UserFinder.class.getName() + ".joinByUsersUserGroups";
93  
94      public static String JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS =
95          UserFinder.class.getName() + ".joinByAnnouncementsDeliveryEmailOrSms";
96  
97      public static String JOIN_BY_SOCIAL_MUTUAL_RELATION =
98          UserFinder.class.getName() + ".joinBySocialMutualRelation";
99  
100     public static String JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE =
101         UserFinder.class.getName() + ".joinBySocialMutualRelationType";
102 
103     public static String JOIN_BY_SOCIAL_RELATION =
104         UserFinder.class.getName() + ".joinBySocialRelation";
105 
106     public static String JOIN_BY_SOCIAL_RELATION_TYPE =
107         UserFinder.class.getName() + ".joinBySocialRelationType";
108 
109     public int countByUser(long userId, LinkedHashMap<String, Object> params)
110         throws SystemException {
111 
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             String sql = CustomSQLUtil.get(COUNT_BY_USER);
118 
119             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
120             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
121 
122             SQLQuery q = session.createSQLQuery(sql);
123 
124             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
125 
126             QueryPos qPos = QueryPos.getInstance(q);
127 
128             setJoin(qPos, params);
129             qPos.add(userId);
130 
131             Iterator<Long> itr = q.list().iterator();
132 
133             if (itr.hasNext()) {
134                 Long count = itr.next();
135 
136                 if (count != null) {
137                     return count.intValue();
138                 }
139             }
140 
141             return 0;
142         }
143         catch (Exception e) {
144             throw new SystemException(e);
145         }
146         finally {
147             closeSession(session);
148         }
149     }
150 
151     public int countByKeywords(
152             long companyId, String keywords, Boolean active,
153             LinkedHashMap<String, Object> params)
154         throws SystemException {
155 
156         String[] firstNames = null;
157         String[] middleNames = null;
158         String[] lastNames = null;
159         String[] screenNames = null;
160         String[] emailAddresses = null;
161         boolean andOperator = false;
162 
163         if (Validator.isNotNull(keywords)) {
164             firstNames = CustomSQLUtil.keywords(keywords);
165             middleNames = CustomSQLUtil.keywords(keywords);
166             lastNames = CustomSQLUtil.keywords(keywords);
167             screenNames = CustomSQLUtil.keywords(keywords);
168             emailAddresses = CustomSQLUtil.keywords(keywords);
169         }
170         else {
171             andOperator = true;
172         }
173 
174         return countByC_FN_MN_LN_SN_EA_A(
175             companyId, firstNames, middleNames, lastNames, screenNames,
176             emailAddresses, active, params, andOperator);
177     }
178 
179     public int countByC_FN_MN_LN_SN_EA_A(
180             long companyId, String firstName, String middleName,
181             String lastName, String screenName, String emailAddress,
182             Boolean active, LinkedHashMap<String, Object> params,
183             boolean andOperator)
184         throws SystemException {
185 
186         return countByC_FN_MN_LN_SN_EA_A(
187             companyId, new String[] {firstName}, new String[] {middleName},
188             new String[] {lastName}, new String[] {screenName},
189             new String[] {emailAddress}, active, params, andOperator);
190     }
191 
192     public int countByC_FN_MN_LN_SN_EA_A(
193             long companyId, String[] firstNames, String[] middleNames,
194             String[] lastNames, String[] screenNames, String[] emailAddresses,
195             Boolean active, LinkedHashMap<String, Object> params,
196             boolean andOperator)
197         throws SystemException {
198 
199         firstNames = CustomSQLUtil.keywords(firstNames);
200         middleNames = CustomSQLUtil.keywords(middleNames);
201         lastNames = CustomSQLUtil.keywords(lastNames);
202         screenNames = CustomSQLUtil.keywords(screenNames);
203         emailAddresses = CustomSQLUtil.keywords(emailAddresses);
204 
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_A);
211 
212             sql = CustomSQLUtil.replaceKeywords(
213                 sql, "lower(User_.firstName)", StringPool.LIKE, false,
214                 firstNames);
215             sql = CustomSQLUtil.replaceKeywords(
216                 sql, "lower(User_.middleName)", StringPool.LIKE, false,
217                 middleNames);
218             sql = CustomSQLUtil.replaceKeywords(
219                 sql, "lower(User_.lastName)", StringPool.LIKE, false,
220                 lastNames);
221             sql = CustomSQLUtil.replaceKeywords(
222                 sql, "lower(User_.screenName)", StringPool.LIKE, false,
223                 screenNames);
224             sql = CustomSQLUtil.replaceKeywords(
225                 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
226                 emailAddresses);
227 
228             if (active == null) {
229                 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
230             }
231 
232             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
233             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
234             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
235 
236             SQLQuery q = session.createSQLQuery(sql);
237 
238             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
239 
240             QueryPos qPos = QueryPos.getInstance(q);
241 
242             setJoin(qPos, params);
243             qPos.add(companyId);
244             qPos.add(false);
245             qPos.add(firstNames, 2);
246             qPos.add(middleNames, 2);
247             qPos.add(lastNames, 2);
248             qPos.add(screenNames, 2);
249             qPos.add(emailAddresses, 2);
250 
251             if (active != null) {
252                 qPos.add(active);
253             }
254 
255             Iterator<Long> itr = q.list().iterator();
256 
257             if (itr.hasNext()) {
258                 Long count = itr.next();
259 
260                 if (count != null) {
261                     return count.intValue();
262                 }
263             }
264 
265             return 0;
266         }
267         catch (Exception e) {
268             throw new SystemException(e);
269         }
270         finally {
271             closeSession(session);
272         }
273     }
274 
275     public List<User> findByKeywords(
276             long companyId, String keywords, Boolean active,
277             LinkedHashMap<String, Object> params, int start, int end,
278             OrderByComparator obc)
279         throws SystemException {
280 
281         String[] firstNames = null;
282         String[] middleNames = null;
283         String[] lastNames = null;
284         String[] screenNames = null;
285         String[] emailAddresses = null;
286         boolean andOperator = false;
287 
288         if (Validator.isNotNull(keywords)) {
289             firstNames = CustomSQLUtil.keywords(keywords);
290             middleNames = CustomSQLUtil.keywords(keywords);
291             lastNames = CustomSQLUtil.keywords(keywords);
292             screenNames = CustomSQLUtil.keywords(keywords);
293             emailAddresses = CustomSQLUtil.keywords(keywords);
294         }
295         else {
296             andOperator = true;
297         }
298 
299         return findByC_FN_MN_LN_SN_EA_A(
300             companyId, firstNames, middleNames, lastNames, screenNames,
301             emailAddresses, active, params, andOperator, start, end, obc);
302     }
303 
304     public List<User> findByNoAnnouncementsDeliveries(String type)
305         throws SystemException {
306 
307         Session session = null;
308 
309         try {
310             session = openSession();
311 
312             String sql = CustomSQLUtil.get(FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES);
313 
314             SQLQuery q = session.createSQLQuery(sql);
315 
316             q.addEntity("User_", UserImpl.class);
317 
318             QueryPos qPos = QueryPos.getInstance(q);
319 
320             qPos.add(type);
321 
322             return q.list();
323         }
324         catch (Exception e) {
325             throw new SystemException(e);
326         }
327         finally {
328             closeSession(session);
329         }
330     }
331 
332     public List<User> findByNoContacts() throws SystemException {
333         Session session = null;
334 
335         try {
336             session = openSession();
337 
338             String sql = CustomSQLUtil.get(FIND_BY_NO_CONTACTS);
339 
340             SQLQuery q = session.createSQLQuery(sql);
341 
342             q.addEntity("User_", UserImpl.class);
343 
344             return q.list();
345         }
346         catch (Exception e) {
347             throw new SystemException(e);
348         }
349         finally {
350             closeSession(session);
351         }
352     }
353 
354     public List<User> findByNoGroups() throws SystemException {
355         Session session = null;
356 
357         try {
358             session = openSession();
359 
360             String sql = CustomSQLUtil.get(FIND_BY_NO_GROUPS);
361 
362             SQLQuery q = session.createSQLQuery(sql);
363 
364             q.addEntity("User_", UserImpl.class);
365 
366             return q.list();
367         }
368         catch (Exception e) {
369             throw new SystemException(e);
370         }
371         finally {
372             closeSession(session);
373         }
374     }
375 
376     public List<User> findByC_FN_MN_LN_SN_EA_A(
377             long companyId, String firstName, String middleName,
378             String lastName, String screenName, String emailAddress,
379             Boolean active, LinkedHashMap<String, Object> params,
380             boolean andOperator, int start, int end, OrderByComparator obc)
381         throws SystemException {
382 
383         return findByC_FN_MN_LN_SN_EA_A(
384             companyId, new String[] {firstName}, new String[] {middleName},
385             new String[] {lastName}, new String[] {screenName},
386             new String[] {emailAddress}, active, params, andOperator, start,
387             end, obc);
388     }
389 
390     public List<User> findByC_FN_MN_LN_SN_EA_A(
391             long companyId, String[] firstNames, String[] middleNames,
392             String[] lastNames, String[] screenNames, String[] emailAddresses,
393             Boolean active, LinkedHashMap<String, Object> params,
394             boolean andOperator, int start, int end, OrderByComparator obc)
395         throws SystemException {
396 
397         firstNames = CustomSQLUtil.keywords(firstNames);
398         middleNames = CustomSQLUtil.keywords(middleNames);
399         lastNames = CustomSQLUtil.keywords(lastNames);
400         screenNames = CustomSQLUtil.keywords(screenNames);
401         emailAddresses = CustomSQLUtil.keywords(emailAddresses);
402 
403         Session session = null;
404 
405         try {
406             session = openSession();
407 
408             String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_A);
409 
410             sql = CustomSQLUtil.replaceKeywords(
411                 sql, "lower(User_.firstName)", StringPool.LIKE, false,
412                 firstNames);
413             sql = CustomSQLUtil.replaceKeywords(
414                 sql, "lower(User_.middleName)", StringPool.LIKE, false,
415                 middleNames);
416             sql = CustomSQLUtil.replaceKeywords(
417                 sql, "lower(User_.lastName)", StringPool.LIKE, false,
418                 lastNames);
419             sql = CustomSQLUtil.replaceKeywords(
420                 sql, "lower(User_.screenName)", StringPool.LIKE, false,
421                 screenNames);
422             sql = CustomSQLUtil.replaceKeywords(
423                 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
424                 emailAddresses);
425 
426             if (active == null) {
427                 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
428             }
429 
430             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
431             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
432             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
433             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
434 
435             SQLQuery q = session.createSQLQuery(sql);
436 
437             q.addEntity("User_", UserImpl.class);
438 
439             QueryPos qPos = QueryPos.getInstance(q);
440 
441             setJoin(qPos, params);
442             qPos.add(companyId);
443             qPos.add(false);
444             qPos.add(firstNames, 2);
445             qPos.add(middleNames, 2);
446             qPos.add(lastNames, 2);
447             qPos.add(screenNames, 2);
448             qPos.add(emailAddresses, 2);
449 
450             if (active != null) {
451                 qPos.add(active);
452             }
453 
454             return (List<User>)QueryUtil.list(q, getDialect(), start, end);
455         }
456         catch (Exception e) {
457             throw new SystemException(e);
458         }
459         finally {
460             closeSession(session);
461         }
462     }
463 
464     protected String getJoin(LinkedHashMap<String, Object> params) {
465         if ((params == null) || params.isEmpty()) {
466             return StringPool.BLANK;
467         }
468 
469         StringBundler sb = new StringBundler(params.size());
470 
471         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
472 
473         while (itr.hasNext()) {
474             Map.Entry<String, Object> entry = itr.next();
475 
476             String key = entry.getKey();
477             Object value = entry.getValue();
478 
479             if (Validator.isNotNull(value)) {
480                 sb.append(getJoin(key, value));
481             }
482         }
483 
484         return sb.toString();
485     }
486 
487     protected String getJoin(String key, Object value) {
488         String join = StringPool.BLANK;
489 
490         if (key.equals("contactTwitterSn")) {
491             join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
492         }
493         else if (key.equals("permission")) {
494             join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
495         }
496         else if (key.equals("userGroupRole")) {
497             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
498         }
499         else if (key.equals("usersGroups")) {
500             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
501         }
502         else if (key.equals("usersOrgs")) {
503             join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
504         }
505         else if (key.equals("usersOrgsTree")) {
506             join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS_TREE);
507         }
508         else if (key.equals("usersPasswordPolicies")) {
509             join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
510         }
511         else if (key.equals("usersRoles")) {
512             join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
513         }
514         else if (key.equals("usersUserGroups")) {
515             join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
516         }
517         else if (key.equals("announcementsDeliveryEmailOrSms")) {
518             join = CustomSQLUtil.get(
519                 JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
520         }
521         else if (key.equals("socialMutualRelation")) {
522             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
523         }
524         else if (key.equals("socialMutualRelationType")) {
525             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
526         }
527         else if (key.equals("socialRelation")) {
528             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
529         }
530         else if (key.equals("socialRelationType")) {
531             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
532         }
533         else if (value instanceof CustomSQLParam) {
534             CustomSQLParam customSQLParam = (CustomSQLParam)value;
535 
536             join = customSQLParam.getSQL();
537         }
538 
539         if (Validator.isNotNull(join)) {
540             int pos = join.indexOf("WHERE");
541 
542             if (pos != -1) {
543                 join = join.substring(0, pos);
544             }
545         }
546 
547         return join;
548     }
549 
550     protected String getWhere(LinkedHashMap<String, Object> params) {
551         if ((params == null) || params.isEmpty()) {
552             return StringPool.BLANK;
553         }
554 
555         StringBundler sb = new StringBundler(params.size());
556 
557         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
558 
559         while (itr.hasNext()) {
560             Map.Entry<String, Object> entry = itr.next();
561 
562             String key = entry.getKey();
563             Object value = entry.getValue();
564 
565             if (Validator.isNotNull(value)) {
566                 sb.append(getWhere(key, value));
567             }
568         }
569 
570         return sb.toString();
571     }
572 
573     protected String getWhere(String key, Object value) {
574         String join = StringPool.BLANK;
575 
576         if (key.equals("contactTwitterSn")) {
577             join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
578         }
579         else if (key.equals("permission")) {
580             join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
581         }
582         else if (key.equals("userGroupRole")) {
583             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
584         }
585         else if (key.equals("usersGroups")) {
586             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
587         }
588         else if (key.equals("usersOrgs")) {
589             if (value instanceof Long) {
590                 join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
591             }
592             else if (value instanceof Long[]) {
593                 Long[] organizationIds = (Long[])value;
594 
595                 if (organizationIds.length == 0) {
596                     join = "WHERE ((Users_Orgs.organizationId = -1) ))";
597                 }
598                 else {
599                     StringBundler sb = new StringBundler(
600                         organizationIds.length * 2 + 1);
601 
602                     sb.append("WHERE (");
603 
604                     for (int i = 0; i < organizationIds.length; i++) {
605                         sb.append("(Users_Orgs.organizationId = ?) ");
606 
607                         if ((i + 1) < organizationIds.length) {
608                             sb.append("OR ");
609                         }
610                     }
611 
612                     sb.append(")");
613 
614                     join = sb.toString();
615                 }
616             }
617         }
618         else if (key.equals("usersOrgsTree")) {
619             Long[][] leftAndRightOrganizationIds = (Long[][])value;
620 
621             if (leftAndRightOrganizationIds.length > 0) {
622                 StringBundler sb = new StringBundler(
623                     leftAndRightOrganizationIds.length * 2 + 1);
624 
625                 sb.append("WHERE (");
626 
627                 for (int i = 0; i < leftAndRightOrganizationIds.length; i++) {
628                     sb.append(
629                         "(Organization_.leftOrganizationId BETWEEN ? AND ?) ");
630 
631                     if ((i + 1) < leftAndRightOrganizationIds.length) {
632                         sb.append("OR ");
633                     }
634                 }
635 
636                 sb.append(")");
637 
638                 join = sb.toString();
639             }
640         }
641         else if (key.equals("usersPasswordPolicies")) {
642             join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
643         }
644         else if (key.equals("usersRoles")) {
645             join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
646         }
647         else if (key.equals("usersUserGroups")) {
648             join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
649         }
650         else if (key.equals("announcementsDeliveryEmailOrSms")) {
651             join = CustomSQLUtil.get(
652                 JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
653         }
654         else if (key.equals("socialMutualRelation")) {
655             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
656         }
657         else if (key.equals("socialMutualRelationType")) {
658             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
659         }
660         else if (key.equals("socialRelation")) {
661             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
662         }
663         else if (key.equals("socialRelationType")) {
664             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
665         }
666         else if (value instanceof CustomSQLParam) {
667             CustomSQLParam customSQLParam = (CustomSQLParam)value;
668 
669             join = customSQLParam.getSQL();
670         }
671 
672         if (Validator.isNotNull(join)) {
673             int pos = join.indexOf("WHERE");
674 
675             if (pos != -1) {
676                 join = join.substring(pos + 5, join.length()).concat(" AND ");
677             }
678             else {
679                 join = StringPool.BLANK;
680             }
681         }
682 
683         return join;
684     }
685 
686     protected void setJoin(
687         QueryPos qPos, LinkedHashMap<String, Object> params) {
688 
689         if (params != null) {
690             Iterator<Map.Entry<String, Object>> itr =
691                 params.entrySet().iterator();
692 
693             while (itr.hasNext()) {
694                 Map.Entry<String, Object> entry = itr.next();
695 
696                 Object value = entry.getValue();
697 
698                 if (value instanceof Long) {
699                     Long valueLong = (Long)value;
700 
701                     if (Validator.isNotNull(valueLong)) {
702                         qPos.add(valueLong);
703                     }
704                 }
705                 else if (value instanceof Long[]) {
706                     Long[] valueArray = (Long[])value;
707 
708                     for (int i = 0; i < valueArray.length; i++) {
709                         if (Validator.isNotNull(valueArray[i])) {
710                             qPos.add(valueArray[i]);
711                         }
712                     }
713                 }
714                 else if (value instanceof Long[][]) {
715                     Long[][] valueDoubleArray = (Long[][])value;
716 
717                     for (Long[] valueArray : valueDoubleArray) {
718                         for (Long valueLong : valueArray) {
719                             qPos.add(valueLong);
720                         }
721                     }
722                 }
723                 else if (value instanceof String) {
724                     String valueString = (String)value;
725 
726                     if (Validator.isNotNull(valueString)) {
727                         qPos.add(valueString);
728                     }
729                 }
730                 else if (value instanceof String[]) {
731                     String[] valueArray = (String[])value;
732 
733                     for (int i = 0; i < valueArray.length; i++) {
734                         if (Validator.isNotNull(valueArray[i])) {
735                             qPos.add(valueArray[i]);
736                         }
737                     }
738                 }
739                 else if (value instanceof CustomSQLParam) {
740                     CustomSQLParam customSQLParam = (CustomSQLParam)value;
741 
742                     customSQLParam.process(qPos);
743                 }
744             }
745         }
746     }
747 
748     protected static String ACTIVE_SQL = "AND (User_.active_ = ?)";
749 
750 }