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