1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.OrderByComparator;
27 import com.liferay.portal.kernel.util.StringMaker;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.impl.UserImpl;
32 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
33 import com.liferay.portal.spring.hibernate.HibernateUtil;
34 import com.liferay.util.dao.hibernate.QueryPos;
35 import com.liferay.util.dao.hibernate.QueryUtil;
36
37 import java.util.Iterator;
38 import java.util.LinkedHashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 import org.hibernate.Hibernate;
43 import org.hibernate.SQLQuery;
44 import org.hibernate.Session;
45
46
53 public class UserFinder {
54
55 public static String COUNT_BY_C_FN_MN_LN_SN_EA_A =
56 UserFinder.class.getName() + ".countByC_FN_MN_LN_SN_EA_A";
57
58 public static String FIND_BY_C_FN_MN_LN_SN_EA_A =
59 UserFinder.class.getName() + ".findByC_FN_MN_LN_SN_EA_A";
60
61 public static String JOIN_BY_PERMISSION =
62 UserFinder.class.getName() + ".joinByPermission";
63
64 public static String JOIN_BY_USER_GROUP_ROLE =
65 UserFinder.class.getName() + ".joinByUserGroupRole";
66
67 public static String JOIN_BY_USERS_GROUPS =
68 UserFinder.class.getName() + ".joinByUsersGroups";
69
70 public static String JOIN_BY_USERS_ORGS =
71 UserFinder.class.getName() + ".joinByUsersOrgs";
72
73 public static String JOIN_BY_USERS_PASSWORD_POLICIES =
74 UserFinder.class.getName() + ".joinByUsersPasswordPolicies";
75
76 public static String JOIN_BY_USERS_ROLES =
77 UserFinder.class.getName() + ".joinByUsersRoles";
78
79 public static String JOIN_BY_USERS_USER_GROUPS =
80 UserFinder.class.getName() + ".joinByUsersUserGroups";
81
82 public static int countByKeywords(
83 long companyId, String keywords, Boolean active,
84 LinkedHashMap params)
85 throws SystemException {
86
87 String[] firstNames = null;
88 String[] middleNames = null;
89 String[] lastNames = null;
90 String[] screenNames = null;
91 String[] emailAddresses = null;
92 boolean andOperator = false;
93
94 if (Validator.isNotNull(keywords)) {
95 firstNames = CustomSQLUtil.keywords(keywords);
96 middleNames = CustomSQLUtil.keywords(keywords);
97 lastNames = CustomSQLUtil.keywords(keywords);
98 screenNames = CustomSQLUtil.keywords(keywords);
99 emailAddresses = CustomSQLUtil.keywords(keywords);
100 }
101 else {
102 andOperator = true;
103 }
104
105 return countByC_FN_MN_LN_SN_EA_A(
106 companyId, firstNames, middleNames, lastNames, screenNames,
107 emailAddresses, active, params, andOperator);
108 }
109
110 public static int countByC_FN_MN_LN_SN_EA_A(
111 long companyId, String firstName, String middleName,
112 String lastName, String screenName, String emailAddress,
113 Boolean active, LinkedHashMap params, boolean andOperator)
114 throws SystemException {
115
116 return countByC_FN_MN_LN_SN_EA_A(
117 companyId, new String[] {firstName}, new String[] {middleName},
118 new String[] {lastName}, new String[] {screenName},
119 new String[] {emailAddress}, active, params, andOperator);
120 }
121
122 public static int countByC_FN_MN_LN_SN_EA_A(
123 long companyId, String[] firstNames, String[] middleNames,
124 String[] lastNames, String[] screenNames, String[] emailAddresses,
125 Boolean active, LinkedHashMap params, boolean andOperator)
126 throws SystemException {
127
128 firstNames = CustomSQLUtil.keywords(firstNames);
129 middleNames = CustomSQLUtil.keywords(middleNames);
130 lastNames = CustomSQLUtil.keywords(lastNames);
131 screenNames = CustomSQLUtil.keywords(screenNames);
132 emailAddresses = CustomSQLUtil.keywords(emailAddresses);
133
134 Session session = null;
135
136 try {
137 session = HibernateUtil.openSession();
138
139 String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_A);
140
141 sql = CustomSQLUtil.replaceKeywords(
142 sql, "lower(Contact_.firstName)", StringPool.LIKE, false,
143 firstNames);
144 sql = CustomSQLUtil.replaceKeywords(
145 sql, "lower(Contact_.middleName)", StringPool.LIKE, false,
146 middleNames);
147 sql = CustomSQLUtil.replaceKeywords(
148 sql, "lower(Contact_.lastName)", StringPool.LIKE, false,
149 lastNames);
150 sql = CustomSQLUtil.replaceKeywords(
151 sql, "lower(User_.screenName)", StringPool.LIKE, false,
152 screenNames);
153 sql = CustomSQLUtil.replaceKeywords(
154 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
155 emailAddresses);
156
157 if (active == null) {
158 sql = StringUtil.replace(sql, _ACTIVE_SQL, StringPool.BLANK);
159 }
160
161 sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(params));
162 sql = StringUtil.replace(sql, "[$WHERE$]", _getWhere(params));
163 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
164
165 SQLQuery q = session.createSQLQuery(sql);
166
167 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
168
169 QueryPos qPos = QueryPos.getInstance(q);
170
171 _setJoin(qPos, params);
172 qPos.add(companyId);
173 qPos.add(false);
174 qPos.add(firstNames, 2);
175 qPos.add(middleNames, 2);
176 qPos.add(lastNames, 2);
177 qPos.add(screenNames, 2);
178 qPos.add(emailAddresses, 2);
179
180 if (active != null) {
181 qPos.add(active);
182 }
183
184 Iterator itr = q.list().iterator();
185
186 if (itr.hasNext()) {
187 Long count = (Long)itr.next();
188
189 if (count != null) {
190 return count.intValue();
191 }
192 }
193
194 return 0;
195 }
196 catch (Exception e) {
197 throw new SystemException(e);
198 }
199 finally {
200 HibernateUtil.closeSession(session);
201 }
202 }
203
204 public static List findByKeywords(
205 long companyId, String keywords, Boolean active,
206 LinkedHashMap params, int begin, int end, OrderByComparator obc)
207 throws SystemException {
208
209 String[] firstNames = null;
210 String[] middleNames = null;
211 String[] lastNames = null;
212 String[] screenNames = null;
213 String[] emailAddresses = null;
214 boolean andOperator = false;
215
216 if (Validator.isNotNull(keywords)) {
217 firstNames = CustomSQLUtil.keywords(keywords);
218 middleNames = CustomSQLUtil.keywords(keywords);
219 lastNames = CustomSQLUtil.keywords(keywords);
220 screenNames = CustomSQLUtil.keywords(keywords);
221 emailAddresses = CustomSQLUtil.keywords(keywords);
222 }
223 else {
224 andOperator = true;
225 }
226
227 return findByC_FN_MN_LN_SN_EA_A(
228 companyId, firstNames, middleNames, lastNames, screenNames,
229 emailAddresses, active, params, andOperator, begin, end, obc);
230 }
231
232 public static List findByC_FN_MN_LN_SN_EA_A(
233 long companyId, String firstName, String middleName,
234 String lastName, String screenName, String emailAddress,
235 Boolean active, LinkedHashMap params, boolean andOperator,
236 int begin, int end, OrderByComparator obc)
237 throws SystemException {
238
239 return findByC_FN_MN_LN_SN_EA_A(
240 companyId, new String[] {firstName}, new String[] {middleName},
241 new String[] {lastName}, new String[] {screenName},
242 new String[] {emailAddress}, active, params, andOperator, begin,
243 end, obc);
244 }
245
246 public static List findByC_FN_MN_LN_SN_EA_A(
247 long companyId, String[] firstNames, String[] middleNames,
248 String[] lastNames, String[] screenNames, String[] emailAddresses,
249 Boolean active, LinkedHashMap params, boolean andOperator,
250 int begin, int end, OrderByComparator obc)
251 throws SystemException {
252
253 firstNames = CustomSQLUtil.keywords(firstNames);
254 middleNames = CustomSQLUtil.keywords(middleNames);
255 lastNames = CustomSQLUtil.keywords(lastNames);
256 screenNames = CustomSQLUtil.keywords(screenNames);
257 emailAddresses = CustomSQLUtil.keywords(emailAddresses);
258
259 Session session = null;
260
261 try {
262 session = HibernateUtil.openSession();
263
264 String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_A);
265
266 sql = CustomSQLUtil.replaceKeywords(
267 sql, "lower(Contact_.firstName)", StringPool.LIKE, false,
268 firstNames);
269 sql = CustomSQLUtil.replaceKeywords(
270 sql, "lower(Contact_.middleName)", StringPool.LIKE, false,
271 middleNames);
272 sql = CustomSQLUtil.replaceKeywords(
273 sql, "lower(Contact_.lastName)", StringPool.LIKE, false,
274 lastNames);
275 sql = CustomSQLUtil.replaceKeywords(
276 sql, "lower(User_.screenName)", StringPool.LIKE, false,
277 screenNames);
278 sql = CustomSQLUtil.replaceKeywords(
279 sql, "lower(User_.emailAddress)", StringPool.LIKE, true,
280 emailAddresses);
281
282 if (active == null) {
283 sql = StringUtil.replace(sql, _ACTIVE_SQL, StringPool.BLANK);
284 }
285
286 sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(params));
287 sql = StringUtil.replace(sql, "[$WHERE$]", _getWhere(params));
288 sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
289 sql = CustomSQLUtil.replaceOrderBy(sql, obc);
290
291 SQLQuery q = session.createSQLQuery(sql);
292
293 q.addEntity("User_", UserImpl.class);
294
295 QueryPos qPos = QueryPos.getInstance(q);
296
297 _setJoin(qPos, params);
298 qPos.add(companyId);
299 qPos.add(false);
300 qPos.add(firstNames, 2);
301 qPos.add(middleNames, 2);
302 qPos.add(lastNames, 2);
303 qPos.add(screenNames, 2);
304 qPos.add(emailAddresses, 2);
305
306 if (active != null) {
307 qPos.add(active);
308 }
309
310 return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
311 }
312 catch (Exception e) {
313 throw new SystemException(e);
314 }
315 finally {
316 HibernateUtil.closeSession(session);
317 }
318 }
319
320 private static String _getJoin(LinkedHashMap params) {
321 if (params == null) {
322 return StringPool.BLANK;
323 }
324
325 StringMaker sm = new StringMaker();
326
327 Iterator itr = params.entrySet().iterator();
328
329 while (itr.hasNext()) {
330 Map.Entry entry = (Map.Entry)itr.next();
331
332 String key = (String)entry.getKey();
333 Object value = entry.getValue();
334
335 if (Validator.isNotNull(value)) {
336 sm.append(_getJoin(key));
337 }
338 }
339
340 return sm.toString();
341 }
342
343 private static String _getJoin(String key) {
344 String join = StringPool.BLANK;
345
346 if (key.equals("permission")) {
347 join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
348 }
349 else if (key.equals("userGroupRole")) {
350 join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
351 }
352 else if (key.equals("usersGroups")) {
353 join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
354 }
355 else if (key.equals("usersOrgs")) {
356 join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
357 }
358 else if (key.equals("usersPasswordPolicies")) {
359 join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
360 }
361 else if (key.equals("usersRoles")) {
362 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
363 }
364 else if (key.equals("usersUserGroups")) {
365 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
366 }
367
368 if (Validator.isNotNull(join)) {
369 int pos = join.indexOf("WHERE");
370
371 if (pos != -1) {
372 join = join.substring(0, pos);
373 }
374 }
375
376 return join;
377 }
378
379 private static String _getWhere(LinkedHashMap params) {
380 if (params == null) {
381 return StringPool.BLANK;
382 }
383
384 StringMaker sm = new StringMaker();
385
386 Iterator itr = params.entrySet().iterator();
387
388 while (itr.hasNext()) {
389 Map.Entry entry = (Map.Entry)itr.next();
390
391 String key = (String)entry.getKey();
392 Object value = entry.getValue();
393
394 if (Validator.isNotNull(value)) {
395 sm.append(_getWhere(key));
396 }
397 }
398
399 return sm.toString();
400 }
401
402 private static String _getWhere(String key) {
403 String join = StringPool.BLANK;
404
405 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("usersPasswordPolicies")) {
418 join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
419 }
420 else if (key.equals("usersRoles")) {
421 join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
422 }
423 else if (key.equals("usersUserGroups")) {
424 join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
425 }
426
427 if (Validator.isNotNull(join)) {
428 int pos = join.indexOf("WHERE");
429
430 if (pos != -1) {
431 join = join.substring(pos + 5, join.length()) + " AND ";
432 }
433 }
434
435 return join;
436 }
437
438 private static void _setJoin(QueryPos qPos, LinkedHashMap params) {
439 if (params != null) {
440 Iterator itr = params.entrySet().iterator();
441
442 while (itr.hasNext()) {
443 Map.Entry entry = (Map.Entry)itr.next();
444
445 Object value = entry.getValue();
446
447 if (value instanceof Long) {
448 Long valueLong = (Long)value;
449
450 if (Validator.isNotNull(valueLong)) {
451 qPos.add(valueLong);
452 }
453 }
454 else if (value instanceof Long[]) {
455 Long[] valueArray = (Long[]) value;
456
457 for (int i = 0; i < valueArray.length; i++) {
458 if (Validator.isNotNull(valueArray[i])) {
459 qPos.add(valueArray[i]);
460 }
461 }
462 }
463 else if (value instanceof String) {
464 String valueString = (String)value;
465
466 if (Validator.isNotNull(valueString)) {
467 qPos.add(valueString);
468 }
469 }
470 else if (value instanceof String[]) {
471 String[] valueArray = (String[]) value;
472
473 for (int i = 0; i < valueArray.length; i++) {
474 if (Validator.isNotNull(valueArray[i])) {
475 qPos.add(valueArray[i]);
476 }
477 }
478 }
479 }
480 }
481 }
482
483 private static String _ACTIVE_SQL = "AND (User_.active_ = ?)";
484
485 }