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