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