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