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