1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.model.impl.UserImpl;
37  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * <a href="UserFinderImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Jon Steer
50   * @author Raymond Augé
51   *
52   */
53  public class UserFinderImpl extends BasePersistenceImpl implements UserFinder {
54  
55      public static String COUNT_BY_C_FN_MN_LN_SN_EA_A =
56          UserFinder.class.getName() + ".countByC_FN_MN_LN_SN_EA_A";
57  
58      public static String FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES =
59          UserFinder.class.getName() + ".findByNoAnnouncementsDeliveries";
60  
61      public static String FIND_BY_C_FN_MN_LN_SN_EA_A =
62          UserFinder.class.getName() + ".findByC_FN_MN_LN_SN_EA_A";
63  
64      public static String JOIN_BY_CONTACT_TWITTER_SN =
65          UserFinder.class.getName() + ".joinByContactTwitterSN";
66  
67      public static String JOIN_BY_PERMISSION =
68          UserFinder.class.getName() + ".joinByPermission";
69  
70      public static String JOIN_BY_USER_GROUP_ROLE =
71          UserFinder.class.getName() + ".joinByUserGroupRole";
72  
73      public static String JOIN_BY_USERS_GROUPS =
74          UserFinder.class.getName() + ".joinByUsersGroups";
75  
76      public static String JOIN_BY_USERS_ORGS =
77          UserFinder.class.getName() + ".joinByUsersOrgs";
78  
79      public static String JOIN_BY_USERS_PASSWORD_POLICIES =
80          UserFinder.class.getName() + ".joinByUsersPasswordPolicies";
81  
82      public static String JOIN_BY_USERS_ROLES =
83          UserFinder.class.getName() + ".joinByUsersRoles";
84  
85      public static String JOIN_BY_USERS_USER_GROUPS =
86          UserFinder.class.getName() + ".joinByUsersUserGroups";
87  
88      public static String JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS =
89          UserFinder.class.getName() + ".joinByAnnouncementsDeliveryEmailOrSms";
90  
91      public static String JOIN_BY_SOCIAL_MUTUAL_RELATION =
92          UserFinder.class.getName() + ".joinBySocialMutualRelation";
93  
94      public static String JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE =
95          UserFinder.class.getName() + ".joinBySocialMutualRelationType";
96  
97      public static String JOIN_BY_SOCIAL_RELATION =
98          UserFinder.class.getName() + ".joinBySocialRelation";
99  
100     public static String JOIN_BY_SOCIAL_RELATION_TYPE =
101         UserFinder.class.getName() + ".joinBySocialRelationType";
102 
103     public int countByKeywords(
104             long companyId, String keywords, Boolean active,
105             LinkedHashMap<String, Object> params)
106         throws SystemException {
107 
108         String[] firstNames = null;
109         String[] middleNames = null;
110         String[] lastNames = null;
111         String[] screenNames = null;
112         String[] emailAddresses = null;
113         boolean andOperator = false;
114 
115         if (Validator.isNotNull(keywords)) {
116             firstNames = CustomSQLUtil.keywords(keywords);
117             middleNames = CustomSQLUtil.keywords(keywords);
118             lastNames = CustomSQLUtil.keywords(keywords);
119             screenNames = CustomSQLUtil.keywords(keywords);
120             emailAddresses = CustomSQLUtil.keywords(keywords);
121         }
122         else {
123             andOperator = true;
124         }
125 
126         return countByC_FN_MN_LN_SN_EA_A(
127             companyId, firstNames, middleNames, lastNames, screenNames,
128             emailAddresses, active, params, andOperator);
129     }
130 
131     public int countByC_FN_MN_LN_SN_EA_A(
132             long companyId, String firstName, String middleName,
133             String lastName, String screenName, String emailAddress,
134             Boolean active, LinkedHashMap<String, Object> params,
135             boolean andOperator)
136         throws SystemException {
137 
138         return countByC_FN_MN_LN_SN_EA_A(
139             companyId, new String[] {firstName}, new String[] {middleName},
140             new String[] {lastName}, new String[] {screenName},
141             new String[] {emailAddress}, active, params, andOperator);
142     }
143 
144     public int countByC_FN_MN_LN_SN_EA_A(
145             long companyId, String[] firstNames, String[] middleNames,
146             String[] lastNames, String[] screenNames, String[] emailAddresses,
147             Boolean active, LinkedHashMap<String, Object> params,
148             boolean andOperator)
149         throws SystemException {
150 
151         firstNames = CustomSQLUtil.keywords(firstNames);
152         middleNames = CustomSQLUtil.keywords(middleNames);
153         lastNames = CustomSQLUtil.keywords(lastNames);
154         screenNames = CustomSQLUtil.keywords(screenNames);
155         emailAddresses = CustomSQLUtil.keywords(emailAddresses);
156 
157         Session session = null;
158 
159         try {
160             session = openSession();
161 
162             String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_A);
163 
164             sql = CustomSQLUtil.replaceKeywords(
165                 sql, "lower(Contact_.firstName)", StringPool.LIKE, false,
166                 firstNames);
167             sql = CustomSQLUtil.replaceKeywords(
168                 sql, "lower(Contact_.middleName)", StringPool.LIKE, false,
169                 middleNames);
170             sql = CustomSQLUtil.replaceKeywords(
171                 sql, "lower(Contact_.lastName)", StringPool.LIKE, false,
172                 lastNames);
173             sql = CustomSQLUtil.replaceKeywords(
174                 sql, "lower(User_.screenName)", StringPool.LIKE, false,
175                 screenNames);
176             sql = CustomSQLUtil.replaceKeywords(
177                 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
178                 emailAddresses);
179 
180             if (active == null) {
181                 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
182             }
183 
184             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
185             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
186             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
187 
188             SQLQuery q = session.createSQLQuery(sql);
189 
190             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
191 
192             QueryPos qPos = QueryPos.getInstance(q);
193 
194             setJoin(qPos, params);
195             qPos.add(companyId);
196             qPos.add(false);
197             qPos.add(firstNames, 2);
198             qPos.add(middleNames, 2);
199             qPos.add(lastNames, 2);
200             qPos.add(screenNames, 2);
201             qPos.add(emailAddresses, 2);
202 
203             if (active != null) {
204                 qPos.add(active);
205             }
206 
207             Iterator<Long> itr = q.list().iterator();
208 
209             if (itr.hasNext()) {
210                 Long count = itr.next();
211 
212                 if (count != null) {
213                     return count.intValue();
214                 }
215             }
216 
217             return 0;
218         }
219         catch (Exception e) {
220             throw new SystemException(e);
221         }
222         finally {
223             closeSession(session);
224         }
225     }
226 
227     public List<User> findByKeywords(
228             long companyId, String keywords, Boolean active,
229             LinkedHashMap<String, Object> params, int start, int end,
230             OrderByComparator obc)
231         throws SystemException {
232 
233         String[] firstNames = null;
234         String[] middleNames = null;
235         String[] lastNames = null;
236         String[] screenNames = null;
237         String[] emailAddresses = null;
238         boolean andOperator = false;
239 
240         if (Validator.isNotNull(keywords)) {
241             firstNames = CustomSQLUtil.keywords(keywords);
242             middleNames = CustomSQLUtil.keywords(keywords);
243             lastNames = CustomSQLUtil.keywords(keywords);
244             screenNames = CustomSQLUtil.keywords(keywords);
245             emailAddresses = CustomSQLUtil.keywords(keywords);
246         }
247         else {
248             andOperator = true;
249         }
250 
251         return findByC_FN_MN_LN_SN_EA_A(
252             companyId, firstNames, middleNames, lastNames, screenNames,
253             emailAddresses, active, params, andOperator, start, end, obc);
254     }
255 
256     public List<User> findByNoAnnouncementsDeliveries(String type)
257         throws SystemException {
258 
259         Session session = null;
260 
261         try {
262             session = openSession();
263 
264             String sql = CustomSQLUtil.get(FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES);
265 
266             SQLQuery q = session.createSQLQuery(sql);
267 
268             q.addEntity("User_", UserImpl.class);
269 
270             QueryPos qPos = QueryPos.getInstance(q);
271 
272             qPos.add(type);
273 
274             return q.list();
275         }
276         catch (Exception e) {
277             throw new SystemException(e);
278         }
279         finally {
280             closeSession(session);
281         }
282     }
283 
284     public List<User> findByC_FN_MN_LN_SN_EA_A(
285             long companyId, String firstName, String middleName,
286             String lastName, String screenName, String emailAddress,
287             Boolean active, LinkedHashMap<String, Object> params,
288             boolean andOperator, int start, int end, OrderByComparator obc)
289         throws SystemException {
290 
291         return findByC_FN_MN_LN_SN_EA_A(
292             companyId, new String[] {firstName}, new String[] {middleName},
293             new String[] {lastName}, new String[] {screenName},
294             new String[] {emailAddress}, active, params, andOperator, start,
295             end, obc);
296     }
297 
298     public List<User> findByC_FN_MN_LN_SN_EA_A(
299             long companyId, String[] firstNames, String[] middleNames,
300             String[] lastNames, String[] screenNames, String[] emailAddresses,
301             Boolean active, LinkedHashMap<String, Object> params,
302             boolean andOperator, int start, int end, OrderByComparator obc)
303         throws SystemException {
304 
305         firstNames = CustomSQLUtil.keywords(firstNames);
306         middleNames = CustomSQLUtil.keywords(middleNames);
307         lastNames = CustomSQLUtil.keywords(lastNames);
308         screenNames = CustomSQLUtil.keywords(screenNames);
309         emailAddresses = CustomSQLUtil.keywords(emailAddresses);
310 
311         Session session = null;
312 
313         try {
314             session = openSession();
315 
316             String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_A);
317 
318             sql = CustomSQLUtil.replaceKeywords(
319                 sql, "lower(Contact_.firstName)", StringPool.LIKE, false,
320                 firstNames);
321             sql = CustomSQLUtil.replaceKeywords(
322                 sql, "lower(Contact_.middleName)", StringPool.LIKE, false,
323                 middleNames);
324             sql = CustomSQLUtil.replaceKeywords(
325                 sql, "lower(Contact_.lastName)", StringPool.LIKE, false,
326                 lastNames);
327             sql = CustomSQLUtil.replaceKeywords(
328                 sql, "lower(User_.screenName)", StringPool.LIKE, false,
329                 screenNames);
330             sql = CustomSQLUtil.replaceKeywords(
331                 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
332                 emailAddresses);
333 
334             if (active == null) {
335                 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
336             }
337 
338             sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
339             sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
340             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
341             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
342 
343             SQLQuery q = session.createSQLQuery(sql);
344 
345             q.addEntity("User_", UserImpl.class);
346 
347             QueryPos qPos = QueryPos.getInstance(q);
348 
349             setJoin(qPos, params);
350             qPos.add(companyId);
351             qPos.add(false);
352             qPos.add(firstNames, 2);
353             qPos.add(middleNames, 2);
354             qPos.add(lastNames, 2);
355             qPos.add(screenNames, 2);
356             qPos.add(emailAddresses, 2);
357 
358             if (active != null) {
359                 qPos.add(active);
360             }
361 
362             return (List<User>)QueryUtil.list(q, getDialect(), start, end);
363         }
364         catch (Exception e) {
365             throw new SystemException(e);
366         }
367         finally {
368             closeSession(session);
369         }
370     }
371 
372     protected String getJoin(LinkedHashMap<String, Object> params) {
373         if (params == null) {
374             return StringPool.BLANK;
375         }
376 
377         StringBuilder sb = new StringBuilder();
378 
379         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
380 
381         while (itr.hasNext()) {
382             Map.Entry<String, Object> entry = itr.next();
383 
384             String key = entry.getKey();
385             Object value = entry.getValue();
386 
387             if (Validator.isNotNull(value)) {
388                 sb.append(getJoin(key));
389             }
390         }
391 
392         return sb.toString();
393     }
394 
395     protected String getJoin(String key) {
396         String join = StringPool.BLANK;
397 
398         if (key.equals("contactTwitterSn")) {
399             join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
400         }
401         else if (key.equals("permission")) {
402             join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
403         }
404         else if (key.equals("userGroupRole")) {
405             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
406         }
407         else if (key.equals("usersGroups")) {
408             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
409         }
410         else if (key.equals("usersOrgs")) {
411             join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
412         }
413         else if (key.equals("usersPasswordPolicies")) {
414             join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
415         }
416         else if (key.equals("usersRoles")) {
417             join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
418         }
419         else if (key.equals("usersUserGroups")) {
420             join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
421         }
422         else if (key.equals("announcementsDeliveryEmailOrSms")) {
423             join = CustomSQLUtil.get(
424                 JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
425         }
426         else if (key.equals("socialMutualRelation")) {
427             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
428         }
429         else if (key.equals("socialMutualRelationType")) {
430             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
431         }
432         else if (key.equals("socialRelation")) {
433             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
434         }
435         else if (key.equals("socialRelationType")) {
436             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
437         }
438 
439         if (Validator.isNotNull(join)) {
440             int pos = join.indexOf("WHERE");
441 
442             if (pos != -1) {
443                 join = join.substring(0, pos);
444             }
445         }
446 
447         return join;
448     }
449 
450     protected String getWhere(LinkedHashMap<String, Object> params) {
451         if (params == null) {
452             return StringPool.BLANK;
453         }
454 
455         StringBuilder sb = new StringBuilder();
456 
457         Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
458 
459         while (itr.hasNext()) {
460             Map.Entry<String, Object> entry = itr.next();
461 
462             String key = entry.getKey();
463             Object value = entry.getValue();
464 
465             if (Validator.isNotNull(value)) {
466                 sb.append(getWhere(key, value));
467             }
468         }
469 
470         return sb.toString();
471     }
472 
473     protected String getWhere(String key, Object value) {
474         String join = StringPool.BLANK;
475 
476         if (key.equals("contactTwitterSn")) {
477             join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
478         }
479         else if (key.equals("permission")) {
480             join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
481         }
482         else if (key.equals("userGroupRole")) {
483             join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
484         }
485         else if (key.equals("usersGroups")) {
486             join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
487         }
488         else if (key.equals("usersOrgs")) {
489             join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
490 
491             if (value instanceof Long[]) {
492                 Long[] organizationIds = (Long[])value;
493 
494                 StringBuilder sb = new StringBuilder();
495 
496                 sb.append("WHERE (");
497 
498                 for (int i = 0; i < organizationIds.length; i++) {
499                     sb.append("(Users_Orgs.organizationId = ?) ");
500 
501                     if ((i + 1) < organizationIds.length) {
502                         sb.append("OR ");
503                     }
504                 }
505 
506                 sb.append(")");
507 
508                 join = sb.toString();
509             }
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("usersUserGroups")) {
518             join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
519         }
520         else if (key.equals("announcementsDeliveryEmailOrSms")) {
521             join = CustomSQLUtil.get(
522                 JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
523         }
524         else if (key.equals("socialMutualRelation")) {
525             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
526         }
527         else if (key.equals("socialMutualRelationType")) {
528             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
529         }
530         else if (key.equals("socialRelation")) {
531             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
532         }
533         else if (key.equals("socialRelationType")) {
534             join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
535         }
536 
537         if (Validator.isNotNull(join)) {
538             int pos = join.indexOf("WHERE");
539 
540             if (pos != -1) {
541                 StringBuilder sb = new StringBuilder();
542 
543                 sb.append(join.substring(pos + 5, join.length()));
544                 sb.append(" AND ");
545 
546                 join = sb.toString();
547             }
548             else {
549                 join = StringPool.BLANK;
550             }
551         }
552 
553         return join;
554     }
555 
556     protected void setJoin(
557         QueryPos qPos, LinkedHashMap<String, Object> params) {
558 
559         if (params != null) {
560             Iterator<Map.Entry<String, Object>> itr =
561                 params.entrySet().iterator();
562 
563             while (itr.hasNext()) {
564                 Map.Entry<String, Object> entry = itr.next();
565 
566                 Object value = entry.getValue();
567 
568                 if (value instanceof Long) {
569                     Long valueLong = (Long)value;
570 
571                     if (Validator.isNotNull(valueLong)) {
572                         qPos.add(valueLong);
573                     }
574                 }
575                 else if (value instanceof Long[]) {
576                     Long[] valueArray = (Long[])value;
577 
578                     for (int i = 0; i < valueArray.length; i++) {
579                         if (Validator.isNotNull(valueArray[i])) {
580                             qPos.add(valueArray[i]);
581                         }
582                     }
583                 }
584                 else if (value instanceof String) {
585                     String valueString = (String)value;
586 
587                     if (Validator.isNotNull(valueString)) {
588                         qPos.add(valueString);
589                     }
590                 }
591                 else if (value instanceof String[]) {
592                     String[] valueArray = (String[])value;
593 
594                     for (int i = 0; i < valueArray.length; i++) {
595                         if (Validator.isNotNull(valueArray[i])) {
596                             qPos.add(valueArray[i]);
597                         }
598                     }
599                 }
600             }
601         }
602     }
603 
604     protected static String ACTIVE_SQL = "AND (User_.active_ = ?)";
605 
606 }