001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.OrderByComparator;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.impl.UserImpl;
030: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
031: import com.liferay.portal.spring.hibernate.HibernateUtil;
032: import com.liferay.util.dao.hibernate.QueryPos;
033: import com.liferay.util.dao.hibernate.QueryUtil;
034:
035: import java.util.Iterator;
036: import java.util.LinkedHashMap;
037: import java.util.List;
038: import java.util.Map;
039:
040: import org.hibernate.Hibernate;
041: import org.hibernate.SQLQuery;
042: import org.hibernate.Session;
043:
044: /**
045: * <a href="UserFinderImpl.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: * @author Jon Steer
049: *
050: */
051: public class UserFinderImpl implements UserFinder {
052:
053: public static String COUNT_BY_C_FN_MN_LN_SN_EA_A = UserFinder.class
054: .getName()
055: + ".countByC_FN_MN_LN_SN_EA_A";
056:
057: public static String FIND_BY_C_FN_MN_LN_SN_EA_A = UserFinder.class
058: .getName()
059: + ".findByC_FN_MN_LN_SN_EA_A";
060:
061: public static String JOIN_BY_PERMISSION = UserFinder.class
062: .getName()
063: + ".joinByPermission";
064:
065: public static String JOIN_BY_USER_GROUP_ROLE = UserFinder.class
066: .getName()
067: + ".joinByUserGroupRole";
068:
069: public static String JOIN_BY_USERS_GROUPS = UserFinder.class
070: .getName()
071: + ".joinByUsersGroups";
072:
073: public static String JOIN_BY_USERS_ORGS = UserFinder.class
074: .getName()
075: + ".joinByUsersOrgs";
076:
077: public static String JOIN_BY_USERS_PASSWORD_POLICIES = UserFinder.class
078: .getName()
079: + ".joinByUsersPasswordPolicies";
080:
081: public static String JOIN_BY_USERS_ROLES = UserFinder.class
082: .getName()
083: + ".joinByUsersRoles";
084:
085: public static String JOIN_BY_USERS_USER_GROUPS = UserFinder.class
086: .getName()
087: + ".joinByUsersUserGroups";
088:
089: public int countByKeywords(long companyId, String keywords,
090: Boolean active, LinkedHashMap params)
091: throws SystemException {
092:
093: String[] firstNames = null;
094: String[] middleNames = null;
095: String[] lastNames = null;
096: String[] screenNames = null;
097: String[] emailAddresses = null;
098: boolean andOperator = false;
099:
100: if (Validator.isNotNull(keywords)) {
101: firstNames = CustomSQLUtil.keywords(keywords);
102: middleNames = CustomSQLUtil.keywords(keywords);
103: lastNames = CustomSQLUtil.keywords(keywords);
104: screenNames = CustomSQLUtil.keywords(keywords);
105: emailAddresses = CustomSQLUtil.keywords(keywords);
106: } else {
107: andOperator = true;
108: }
109:
110: return countByC_FN_MN_LN_SN_EA_A(companyId, firstNames,
111: middleNames, lastNames, screenNames, emailAddresses,
112: active, params, andOperator);
113: }
114:
115: public int countByC_FN_MN_LN_SN_EA_A(long companyId,
116: String firstName, String middleName, String lastName,
117: String screenName, String emailAddress, Boolean active,
118: LinkedHashMap params, boolean andOperator)
119: throws SystemException {
120:
121: return countByC_FN_MN_LN_SN_EA_A(companyId,
122: new String[] { firstName },
123: new String[] { middleName }, new String[] { lastName },
124: new String[] { screenName },
125: new String[] { emailAddress }, active, params,
126: andOperator);
127: }
128:
129: public int countByC_FN_MN_LN_SN_EA_A(long companyId,
130: String[] firstNames, String[] middleNames,
131: String[] lastNames, String[] screenNames,
132: String[] emailAddresses, Boolean active,
133: LinkedHashMap params, boolean andOperator)
134: throws SystemException {
135:
136: firstNames = CustomSQLUtil.keywords(firstNames);
137: middleNames = CustomSQLUtil.keywords(middleNames);
138: lastNames = CustomSQLUtil.keywords(lastNames);
139: screenNames = CustomSQLUtil.keywords(screenNames);
140: emailAddresses = CustomSQLUtil.keywords(emailAddresses);
141:
142: Session session = null;
143:
144: try {
145: session = HibernateUtil.openSession();
146:
147: String sql = CustomSQLUtil.get(COUNT_BY_C_FN_MN_LN_SN_EA_A);
148:
149: sql = CustomSQLUtil.replaceKeywords(sql,
150: "lower(Contact_.firstName)", StringPool.LIKE,
151: false, firstNames);
152: sql = CustomSQLUtil.replaceKeywords(sql,
153: "lower(Contact_.middleName)", StringPool.LIKE,
154: false, middleNames);
155: sql = CustomSQLUtil.replaceKeywords(sql,
156: "lower(Contact_.lastName)", StringPool.LIKE, false,
157: lastNames);
158: sql = CustomSQLUtil.replaceKeywords(sql,
159: "lower(User_.screenName)", StringPool.LIKE, false,
160: screenNames);
161: sql = CustomSQLUtil.replaceKeywords(sql,
162: "lower(User_.emailAddress)", StringPool.LIKE, true,
163: emailAddresses);
164:
165: if (active == null) {
166: sql = StringUtil.replace(sql, ACTIVE_SQL,
167: StringPool.BLANK);
168: }
169:
170: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
171: sql = StringUtil
172: .replace(sql, "[$WHERE$]", getWhere(params));
173: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
174:
175: SQLQuery q = session.createSQLQuery(sql);
176:
177: q.addScalar(HibernateUtil.getCountColumnName(),
178: Hibernate.LONG);
179:
180: QueryPos qPos = QueryPos.getInstance(q);
181:
182: setJoin(qPos, params);
183: qPos.add(companyId);
184: qPos.add(false);
185: qPos.add(firstNames, 2);
186: qPos.add(middleNames, 2);
187: qPos.add(lastNames, 2);
188: qPos.add(screenNames, 2);
189: qPos.add(emailAddresses, 2);
190:
191: if (active != null) {
192: qPos.add(active);
193: }
194:
195: Iterator itr = q.list().iterator();
196:
197: if (itr.hasNext()) {
198: Long count = (Long) itr.next();
199:
200: if (count != null) {
201: return count.intValue();
202: }
203: }
204:
205: return 0;
206: } catch (Exception e) {
207: throw new SystemException(e);
208: } finally {
209: HibernateUtil.closeSession(session);
210: }
211: }
212:
213: public List findByKeywords(long companyId, String keywords,
214: Boolean active, LinkedHashMap params, int begin, int end,
215: OrderByComparator obc) throws SystemException {
216:
217: String[] firstNames = null;
218: String[] middleNames = null;
219: String[] lastNames = null;
220: String[] screenNames = null;
221: String[] emailAddresses = null;
222: boolean andOperator = false;
223:
224: if (Validator.isNotNull(keywords)) {
225: firstNames = CustomSQLUtil.keywords(keywords);
226: middleNames = CustomSQLUtil.keywords(keywords);
227: lastNames = CustomSQLUtil.keywords(keywords);
228: screenNames = CustomSQLUtil.keywords(keywords);
229: emailAddresses = CustomSQLUtil.keywords(keywords);
230: } else {
231: andOperator = true;
232: }
233:
234: return findByC_FN_MN_LN_SN_EA_A(companyId, firstNames,
235: middleNames, lastNames, screenNames, emailAddresses,
236: active, params, andOperator, begin, end, obc);
237: }
238:
239: public List findByC_FN_MN_LN_SN_EA_A(long companyId,
240: String firstName, String middleName, String lastName,
241: String screenName, String emailAddress, Boolean active,
242: LinkedHashMap params, boolean andOperator, int begin,
243: int end, OrderByComparator obc) throws SystemException {
244:
245: return findByC_FN_MN_LN_SN_EA_A(companyId,
246: new String[] { firstName },
247: new String[] { middleName }, new String[] { lastName },
248: new String[] { screenName },
249: new String[] { emailAddress }, active, params,
250: andOperator, begin, end, obc);
251: }
252:
253: public List findByC_FN_MN_LN_SN_EA_A(long companyId,
254: String[] firstNames, String[] middleNames,
255: String[] lastNames, String[] screenNames,
256: String[] emailAddresses, Boolean active,
257: LinkedHashMap params, boolean andOperator, int begin,
258: int end, OrderByComparator obc) throws SystemException {
259:
260: firstNames = CustomSQLUtil.keywords(firstNames);
261: middleNames = CustomSQLUtil.keywords(middleNames);
262: lastNames = CustomSQLUtil.keywords(lastNames);
263: screenNames = CustomSQLUtil.keywords(screenNames);
264: emailAddresses = CustomSQLUtil.keywords(emailAddresses);
265:
266: Session session = null;
267:
268: try {
269: session = HibernateUtil.openSession();
270:
271: String sql = CustomSQLUtil.get(FIND_BY_C_FN_MN_LN_SN_EA_A);
272:
273: sql = CustomSQLUtil.replaceKeywords(sql,
274: "lower(Contact_.firstName)", StringPool.LIKE,
275: false, firstNames);
276: sql = CustomSQLUtil.replaceKeywords(sql,
277: "lower(Contact_.middleName)", StringPool.LIKE,
278: false, middleNames);
279: sql = CustomSQLUtil.replaceKeywords(sql,
280: "lower(Contact_.lastName)", StringPool.LIKE, false,
281: lastNames);
282: sql = CustomSQLUtil.replaceKeywords(sql,
283: "lower(User_.screenName)", StringPool.LIKE, false,
284: screenNames);
285: sql = CustomSQLUtil.replaceKeywords(sql,
286: "lower(User_.emailAddress)", StringPool.LIKE, true,
287: emailAddresses);
288:
289: if (active == null) {
290: sql = StringUtil.replace(sql, ACTIVE_SQL,
291: StringPool.BLANK);
292: }
293:
294: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
295: sql = StringUtil
296: .replace(sql, "[$WHERE$]", getWhere(params));
297: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
298: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
299:
300: SQLQuery q = session.createSQLQuery(sql);
301:
302: q.addEntity("User_", UserImpl.class);
303:
304: QueryPos qPos = QueryPos.getInstance(q);
305:
306: setJoin(qPos, params);
307: qPos.add(companyId);
308: qPos.add(false);
309: qPos.add(firstNames, 2);
310: qPos.add(middleNames, 2);
311: qPos.add(lastNames, 2);
312: qPos.add(screenNames, 2);
313: qPos.add(emailAddresses, 2);
314:
315: if (active != null) {
316: qPos.add(active);
317: }
318:
319: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
320: end);
321: } catch (Exception e) {
322: throw new SystemException(e);
323: } finally {
324: HibernateUtil.closeSession(session);
325: }
326: }
327:
328: protected String getJoin(LinkedHashMap params) {
329: if (params == null) {
330: return StringPool.BLANK;
331: }
332:
333: StringMaker sm = new StringMaker();
334:
335: Iterator itr = params.entrySet().iterator();
336:
337: while (itr.hasNext()) {
338: Map.Entry entry = (Map.Entry) itr.next();
339:
340: String key = (String) entry.getKey();
341: Object value = entry.getValue();
342:
343: if (Validator.isNotNull(value)) {
344: sm.append(getJoin(key));
345: }
346: }
347:
348: return sm.toString();
349: }
350:
351: protected String getJoin(String key) {
352: String join = StringPool.BLANK;
353:
354: if (key.equals("permission")) {
355: join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
356: } else if (key.equals("userGroupRole")) {
357: join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
358: } else if (key.equals("usersGroups")) {
359: join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
360: } else if (key.equals("usersOrgs")) {
361: join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
362: } else if (key.equals("usersPasswordPolicies")) {
363: join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
364: } else if (key.equals("usersRoles")) {
365: join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
366: } else if (key.equals("usersUserGroups")) {
367: join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
368: }
369:
370: if (Validator.isNotNull(join)) {
371: int pos = join.indexOf("WHERE");
372:
373: if (pos != -1) {
374: join = join.substring(0, pos);
375: }
376: }
377:
378: return join;
379: }
380:
381: protected String getWhere(LinkedHashMap params) {
382: if (params == null) {
383: return StringPool.BLANK;
384: }
385:
386: StringMaker sm = new StringMaker();
387:
388: Iterator itr = params.entrySet().iterator();
389:
390: while (itr.hasNext()) {
391: Map.Entry entry = (Map.Entry) itr.next();
392:
393: String key = (String) entry.getKey();
394: Object value = entry.getValue();
395:
396: if (Validator.isNotNull(value)) {
397: sm.append(getWhere(key, value));
398: }
399: }
400:
401: return sm.toString();
402: }
403:
404: protected String getWhere(String key, Object value) {
405: String join = StringPool.BLANK;
406:
407: if (key.equals("permission")) {
408: join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
409: } else if (key.equals("userGroupRole")) {
410: join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
411: } else if (key.equals("usersGroups")) {
412: join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
413: } else if (key.equals("usersOrgs")) {
414: join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
415:
416: if (value instanceof Long[]) {
417: Long[] organizationIds = (Long[]) value;
418:
419: StringMaker sm = new StringMaker();
420:
421: sm.append("WHERE (");
422:
423: for (int i = 0; i < organizationIds.length; i++) {
424: sm.append("(Users_Orgs.organizationId = ?) ");
425:
426: if ((i + 1) < organizationIds.length) {
427: sm.append("OR ");
428: }
429: }
430:
431: sm.append(")");
432:
433: join = sm.toString();
434: }
435: } else if (key.equals("usersPasswordPolicies")) {
436: join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
437: } else if (key.equals("usersRoles")) {
438: join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
439: } else if (key.equals("usersUserGroups")) {
440: join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
441: }
442:
443: if (Validator.isNotNull(join)) {
444: int pos = join.indexOf("WHERE");
445:
446: if (pos != -1) {
447: join = join.substring(pos + 5, join.length()) + " AND ";
448: }
449: }
450:
451: return join;
452: }
453:
454: protected void setJoin(QueryPos qPos, LinkedHashMap params) {
455: if (params != null) {
456: Iterator itr = params.entrySet().iterator();
457:
458: while (itr.hasNext()) {
459: Map.Entry entry = (Map.Entry) itr.next();
460:
461: Object value = entry.getValue();
462:
463: if (value instanceof Long) {
464: Long valueLong = (Long) value;
465:
466: if (Validator.isNotNull(valueLong)) {
467: qPos.add(valueLong);
468: }
469: } else if (value instanceof Long[]) {
470: Long[] valueArray = (Long[]) value;
471:
472: for (int i = 0; i < valueArray.length; i++) {
473: if (Validator.isNotNull(valueArray[i])) {
474: qPos.add(valueArray[i]);
475: }
476: }
477: } else if (value instanceof String) {
478: String valueString = (String) value;
479:
480: if (Validator.isNotNull(valueString)) {
481: qPos.add(valueString);
482: }
483: } else if (value instanceof String[]) {
484: String[] valueArray = (String[]) value;
485:
486: for (int i = 0; i < valueArray.length; i++) {
487: if (Validator.isNotNull(valueArray[i])) {
488: qPos.add(valueArray[i]);
489: }
490: }
491: }
492: }
493: }
494: }
495:
496: protected static String ACTIVE_SQL = "AND (User_.active_ = ?)";
497:
498: }
|