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