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