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.StringUtil;
026: import com.liferay.portal.model.impl.PasswordPolicyImpl;
027: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
028: import com.liferay.portal.spring.hibernate.HibernateUtil;
029: import com.liferay.util.dao.hibernate.QueryPos;
030: import com.liferay.util.dao.hibernate.QueryUtil;
031:
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.hibernate.Hibernate;
036: import org.hibernate.SQLQuery;
037: import org.hibernate.Session;
038:
039: /**
040: * <a href="PasswordPolicyFinderImpl.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class PasswordPolicyFinderImpl implements PasswordPolicyFinder {
046:
047: public static String COUNT_BY_C_N = PasswordPolicyFinder.class
048: .getName()
049: + ".countByC_N";
050:
051: public static String FIND_BY_C_N = PasswordPolicyFinder.class
052: .getName()
053: + ".findByC_N";
054:
055: public int countByC_N(long companyId, String name)
056: throws SystemException {
057: name = StringUtil.lowerCase(name);
058:
059: Session session = null;
060:
061: try {
062: session = HibernateUtil.openSession();
063:
064: String sql = CustomSQLUtil.get(COUNT_BY_C_N);
065:
066: SQLQuery q = session.createSQLQuery(sql);
067:
068: q.addScalar(HibernateUtil.getCountColumnName(),
069: Hibernate.LONG);
070:
071: QueryPos qPos = QueryPos.getInstance(q);
072:
073: qPos.add(companyId);
074: qPos.add(name);
075: qPos.add(name);
076:
077: Iterator itr = q.list().iterator();
078:
079: if (itr.hasNext()) {
080: Long count = (Long) itr.next();
081:
082: if (count != null) {
083: return count.intValue();
084: }
085: }
086:
087: return 0;
088: } catch (Exception e) {
089: throw new SystemException(e);
090: } finally {
091: HibernateUtil.closeSession(session);
092: }
093: }
094:
095: public List findByC_N(long companyId, String name, int begin,
096: int end, OrderByComparator obc) throws SystemException {
097:
098: name = StringUtil.lowerCase(name);
099:
100: Session session = null;
101:
102: try {
103: session = HibernateUtil.openSession();
104:
105: String sql = CustomSQLUtil.get(FIND_BY_C_N);
106:
107: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
108:
109: SQLQuery q = session.createSQLQuery(sql);
110:
111: q.addEntity("PasswordPolicy", PasswordPolicyImpl.class);
112:
113: QueryPos qPos = QueryPos.getInstance(q);
114:
115: qPos.add(companyId);
116: qPos.add(name);
117: qPos.add(name);
118:
119: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
120: end);
121: } catch (Exception e) {
122: throw new SystemException(e);
123: } finally {
124: HibernateUtil.closeSession(session);
125: }
126: }
127:
128: }
|