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