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
54 public class UserFinderImpl extends BasePersistenceImpl implements UserFinder {
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_NO_CONTACTS =
63 UserFinder.class.getName() + ".findByNoContacts";
64
65 public static String FIND_BY_NO_GROUPS =
66 UserFinder.class.getName() + ".findByNoGroups";
67
68 public static String FIND_BY_C_FN_MN_LN_SN_EA_A =
69 UserFinder.class.getName() + ".findByC_FN_MN_LN_SN_EA_A";
70
71 public static String JOIN_BY_CONTACT_TWITTER_SN =
72 UserFinder.class.getName() + ".joinByContactTwitterSN";
73
74 public static String JOIN_BY_PERMISSION =
75 UserFinder.class.getName() + ".joinByPermission";
76
77 public static String JOIN_BY_USER_GROUP_ROLE =
78 UserFinder.class.getName() + ".joinByUserGroupRole";
79
80 public static String JOIN_BY_USERS_GROUPS =
81 UserFinder.class.getName() + ".joinByUsersGroups";
82
83 public static String JOIN_BY_USERS_ORGS =
84 UserFinder.class.getName() + ".joinByUsersOrgs";
85
86 public static String JOIN_BY_USERS_ORGS_TREE =
87 UserFinder.class.getName() + ".joinByUsersOrgsTree";
88
89 public static String JOIN_BY_USERS_PASSWORD_POLICIES =
90 UserFinder.class.getName() + ".joinByUsersPasswordPolicies";
91
92 public static String JOIN_BY_USERS_ROLES =
93 UserFinder.class.getName() + ".joinByUsersRoles";
94
95 public static String JOIN_BY_USERS_USER_GROUPS =
96 UserFinder.class.getName() + ".joinByUsersUserGroups";
97
98 public static String JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS =
99 UserFinder.class.getName() + ".joinByAnnouncementsDeliveryEmailOrSms";
100
101 public static String JOIN_BY_SOCIAL_MUTUAL_RELATION =
102 UserFinder.class.getName() + ".joinBySocialMutualRelation";
103
104 public static String JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE =
105 UserFinder.class.getName() + ".joinBySocialMutualRelationType";
106
107 public static String JOIN_BY_SOCIAL_RELATION =
108 UserFinder.class.getName() + ".joinBySocialRelation";
109
110 public static String JOIN_BY_SOCIAL_RELATION_TYPE =
111 UserFinder.class.getName() + ".joinBySocialRelationType";
112
113 public int countByKeywords(
114 long companyId, String keywords, Boolean active,
115 LinkedHashMap<String, Object> params)
116 throws SystemException {
117
118 String[] firstNames = null;
119 String[] middleNames = null;
120 String[] lastNames = null;
121 String[] screenNames = null;
122 String[] emailAddresses = null;
123 boolean andOperator = false;
124
125 if (Validator.isNotNull(keywords)) {
126 firstNames = CustomSQLUtil.keywords(keywords);
127 middleNames = CustomSQLUtil.keywords(keywords);
128 lastNames = CustomSQLUtil.keywords(keywords);
129 screenNames = CustomSQLUtil.keywords(keywords);
130 emailAddresses = CustomSQLUtil.keywords(keywords);
131 }
132 else {
133 andOperator = true;
134 }
135
136 return countByC_FN_MN_LN_SN_EA_A(
137 companyId, firstNames, middleNames, lastNames, screenNames,
138 emailAddresses, active, params, andOperator);
139 }
140
141 public int countByC_FN_MN_LN_SN_EA_A(
142 long companyId, String firstName, String middleName,
143 String lastName, String screenName, String emailAddress,
144 Boolean active, LinkedHashMap<String, Object> params,
145 boolean andOperator)
146 throws SystemException {
147
148 return countByC_FN_MN_LN_SN_EA_A(
149 companyId, new String[] {firstName}, new String[] {middleName},
150 new String[] {lastName}, new String[] {screenName},
151 new String[] {emailAddress}, active, params, andOperator);
152 }
153
154 public int countByC_FN_MN_LN_SN_EA_A(
155 long companyId, String[] firstNames, String[] middleNames,
156 String[] lastNames, String[] screenNames, String[] emailAddresses,
157 Boolean active, LinkedHashMap<String, Object> params,
158 boolean andOperator)
159 throws SystemException {
160
161 firstNames = CustomSQLUtil.keywords(firstNames);
162 middleNames = CustomSQLUtil.keywords(middleNames);
163 lastNames = CustomSQLUtil.keywords(lastNames);
164 screenNames = CustomSQLUtil.keywords(screenNames);
165 emailAddresses = CustomSQLUtil.keywords(emailAddresses);
166
167 Session session = null;
168
169 try {
170 session = openSession();
171
172 String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_A);
173
174 sql = CustomSQLUtil.replaceKeywords(
175 sql, "lower(User_.firstName)", StringPool.LIKE, false,
176 firstNames);
177 sql = CustomSQLUtil.replaceKeywords(
178 sql, "lower(User_.middleName)", StringPool.LIKE, false,
179 middleNames);
180 sql = CustomSQLUtil.replaceKeywords(
181 sql, "lower(User_.lastName)", StringPool.LIKE, false,
182 lastNames);
183 sql = CustomSQLUtil.replaceKeywords(
184 sql, "lower(User_.screenName)", StringPool.LIKE, false,
185 screenNames);
186 sql = CustomSQLUtil.replaceKeywords(
187 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
188 emailAddresses);
189
190 if (active == null) {
191 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
192 }
193
194 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
195 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
196 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
197
198 SQLQuery q = session.createSQLQuery(sql);
199
200 q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
201
202 QueryPos qPos = QueryPos.getInstance(q);
203
204 setJoin(qPos, params);
205 qPos.add(companyId);
206 qPos.add(false);
207 qPos.add(firstNames, 2);
208 qPos.add(middleNames, 2);
209 qPos.add(lastNames, 2);
210 qPos.add(screenNames, 2);
211 qPos.add(emailAddresses, 2);
212
213 if (active != null) {
214 qPos.add(active);
215 }
216
217 Iterator<Long> itr = q.list().iterator();
218
219 if (itr.hasNext()) {
220 Long count = itr.next();
221
222 if (count != null) {
223 return count.intValue();
224 }
225 }
226
227 return 0;
228 }
229 catch (Exception e) {
230 throw new SystemException(e);
231 }
232 finally {
233 closeSession(session);
234 }
235 }
236
237 public List<User> findByKeywords(
238 long companyId, String keywords, Boolean active,
239 LinkedHashMap<String, Object> params, int start, int end,
240 OrderByComparator obc)
241 throws SystemException {
242
243 String[] firstNames = null;
244 String[] middleNames = null;
245 String[] lastNames = null;
246 String[] screenNames = null;
247 String[] emailAddresses = null;
248 boolean andOperator = false;
249
250 if (Validator.isNotNull(keywords)) {
251 firstNames = CustomSQLUtil.keywords(keywords);
252 middleNames = CustomSQLUtil.keywords(keywords);
253 lastNames = CustomSQLUtil.keywords(keywords);
254 screenNames = CustomSQLUtil.keywords(keywords);
255 emailAddresses = CustomSQLUtil.keywords(keywords);
256 }
257 else {
258 andOperator = true;
259 }
260
261 return findByC_FN_MN_LN_SN_EA_A(
262 companyId, firstNames, middleNames, lastNames, screenNames,
263 emailAddresses, active, params, andOperator, start, end, obc);
264 }
265
266 public List<User> findByNoAnnouncementsDeliveries(String type)
267 throws SystemException {
268
269 Session session = null;
270
271 try {
272 session = openSession();
273
274 String sql = CustomSQLUtil.get(FIND_BY_NO_ANNOUNCEMENTS_DELIVERIES);
275
276 SQLQuery q = session.createSQLQuery(sql);
277
278 q.addEntity("User_", UserImpl.class);
279
280 QueryPos qPos = QueryPos.getInstance(q);
281
282 qPos.add(type);
283
284 return q.list();
285 }
286 catch (Exception e) {
287 throw new SystemException(e);
288 }
289 finally {
290 closeSession(session);
291 }
292 }
293
294 public List<User> findByNoContacts() throws SystemException {
295 Session session = null;
296
297 try {
298 session = openSession();
299
300 String sql = CustomSQLUtil.get(FIND_BY_NO_CONTACTS);
301
302 SQLQuery q = session.createSQLQuery(sql);
303
304 q.addEntity("User_", UserImpl.class);
305
306 return q.list();
307 }
308 catch (Exception e) {
309 throw new SystemException(e);
310 }
311 finally {
312 closeSession(session);
313 }
314 }
315
316 public List<User> findByNoGroups() throws SystemException {
317 Session session = null;
318
319 try {
320 session = openSession();
321
322 String sql = CustomSQLUtil.get(FIND_BY_NO_GROUPS);
323
324 SQLQuery q = session.createSQLQuery(sql);
325
326 q.addEntity("User_", UserImpl.class);
327
328 return q.list();
329 }
330 catch (Exception e) {
331 throw new SystemException(e);
332 }
333 finally {
334 closeSession(session);
335 }
336 }
337
338 public List<User> findByC_FN_MN_LN_SN_EA_A(
339 long companyId, String firstName, String middleName,
340 String lastName, String screenName, String emailAddress,
341 Boolean active, LinkedHashMap<String, Object> params,
342 boolean andOperator, int start, int end, OrderByComparator obc)
343 throws SystemException {
344
345 return findByC_FN_MN_LN_SN_EA_A(
346 companyId, new String[] {firstName}, new String[] {middleName},
347 new String[] {lastName}, new String[] {screenName},
348 new String[] {emailAddress}, active, params, andOperator, start,
349 end, obc);
350 }
351
352 public List<User> findByC_FN_MN_LN_SN_EA_A(
353 long companyId, String[] firstNames, String[] middleNames,
354 String[] lastNames, String[] screenNames, String[] emailAddresses,
355 Boolean active, LinkedHashMap<String, Object> params,
356 boolean andOperator, int start, int end, OrderByComparator obc)
357 throws SystemException {
358
359 firstNames = CustomSQLUtil.keywords(firstNames);
360 middleNames = CustomSQLUtil.keywords(middleNames);
361 lastNames = CustomSQLUtil.keywords(lastNames);
362 screenNames = CustomSQLUtil.keywords(screenNames);
363 emailAddresses = CustomSQLUtil.keywords(emailAddresses);
364
365 Session session = null;
366
367 try {
368 session = openSession();
369
370 String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_A);
371
372 sql = CustomSQLUtil.replaceKeywords(
373 sql, "lower(User_.firstName)", StringPool.LIKE, false,
374 firstNames);
375 sql = CustomSQLUtil.replaceKeywords(
376 sql, "lower(User_.middleName)", StringPool.LIKE, false,
377 middleNames);
378 sql = CustomSQLUtil.replaceKeywords(
379 sql, "lower(User_.lastName)", StringPool.LIKE, false,
380 lastNames);
381 sql = CustomSQLUtil.replaceKeywords(
382 sql, "lower(User_.screenName)", StringPool.LIKE, false,
383 screenNames);
384 sql = CustomSQLUtil.replaceKeywords(
385 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
386 emailAddresses);
387
388 if (active == null) {
389 sql = StringUtil.replace(sql, ACTIVE_SQL, StringPool.BLANK);
390 }
391
392 sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
393 sql = StringUtil.replace(sql, "[$WHERE$]", getWhere(params));
394 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
395 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
396
397 SQLQuery q = session.createSQLQuery(sql);
398
399 q.addEntity("User_", UserImpl.class);
400
401 QueryPos qPos = QueryPos.getInstance(q);
402
403 setJoin(qPos, params);
404 qPos.add(companyId);
405 qPos.add(false);
406 qPos.add(firstNames, 2);
407 qPos.add(middleNames, 2);
408 qPos.add(lastNames, 2);
409 qPos.add(screenNames, 2);
410 qPos.add(emailAddresses, 2);
411
412 if (active != null) {
413 qPos.add(active);
414 }
415
416 return (List<User>)QueryUtil.list(q, getDialect(), start, end);
417 }
418 catch (Exception e) {
419 throw new SystemException(e);
420 }
421 finally {
422 closeSession(session);
423 }
424 }
425
426 protected String getJoin(LinkedHashMap<String, Object> params) {
427 if (params == null) {
428 return StringPool.BLANK;
429 }
430
431 StringBuilder sb = new StringBuilder();
432
433 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
434
435 while (itr.hasNext()) {
436 Map.Entry<String, Object> entry = itr.next();
437
438 String key = entry.getKey();
439 Object value = entry.getValue();
440
441 if (Validator.isNotNull(value)) {
442 sb.append(getJoin(key, value));
443 }
444 }
445
446 return sb.toString();
447 }
448
449 protected String getJoin(String key, Object value) {
450 String join = StringPool.BLANK;
451
452 if (key.equals("contactTwitterSn")) {
453 join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
454 }
455 else if (key.equals("permission")) {
456 join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
457 }
458 else if (key.equals("userGroupRole")) {
459 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
460 }
461 else if (key.equals("usersGroups")) {
462 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
463 }
464 else if (key.equals("usersOrgs")) {
465 join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
466 }
467 else if (key.equals("usersOrgsTree")) {
468 join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS_TREE);
469 }
470 else if (key.equals("usersPasswordPolicies")) {
471 join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
472 }
473 else if (key.equals("usersRoles")) {
474 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
475 }
476 else if (key.equals("usersUserGroups")) {
477 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
478 }
479 else if (key.equals("announcementsDeliveryEmailOrSms")) {
480 join = CustomSQLUtil.get(
481 JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
482 }
483 else if (key.equals("socialMutualRelation")) {
484 join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
485 }
486 else if (key.equals("socialMutualRelationType")) {
487 join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
488 }
489 else if (key.equals("socialRelation")) {
490 join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
491 }
492 else if (key.equals("socialRelationType")) {
493 join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
494 }
495 else if (value instanceof CustomSQLParam) {
496 CustomSQLParam customSQLParam = (CustomSQLParam)value;
497
498 join = customSQLParam.getSQL();
499 }
500
501 if (Validator.isNotNull(join)) {
502 int pos = join.indexOf("WHERE");
503
504 if (pos != -1) {
505 join = join.substring(0, pos);
506 }
507 }
508
509 return join;
510 }
511
512 protected String getWhere(LinkedHashMap<String, Object> params) {
513 if (params == null) {
514 return StringPool.BLANK;
515 }
516
517 StringBuilder sb = new StringBuilder();
518
519 Iterator<Map.Entry<String, Object>> itr = params.entrySet().iterator();
520
521 while (itr.hasNext()) {
522 Map.Entry<String, Object> entry = itr.next();
523
524 String key = entry.getKey();
525 Object value = entry.getValue();
526
527 if (Validator.isNotNull(value)) {
528 sb.append(getWhere(key, value));
529 }
530 }
531
532 return sb.toString();
533 }
534
535 protected String getWhere(String key, Object value) {
536 String join = StringPool.BLANK;
537
538 if (key.equals("contactTwitterSn")) {
539 join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
540 }
541 else if (key.equals("permission")) {
542 join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
543 }
544 else if (key.equals("userGroupRole")) {
545 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
546 }
547 else if (key.equals("usersGroups")) {
548 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
549 }
550 else if (key.equals("usersOrgs")) {
551 join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
552
553 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 }