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