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