001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.PasswordPolicy;
026    import com.liferay.portal.model.impl.PasswordPolicyImpl;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.util.dao.orm.CustomSQLUtil;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PasswordPolicyFinderImpl
037            extends BasePersistenceImpl<PasswordPolicy>
038            implements PasswordPolicyFinder {
039    
040            public static String COUNT_BY_C_N =
041                    PasswordPolicyFinder.class.getName() + ".countByC_N";
042    
043            public static String FIND_BY_C_N =
044                    PasswordPolicyFinder.class.getName() + ".findByC_N";
045    
046            public int countByC_N(long companyId, String name) throws SystemException {
047                    name = StringUtil.lowerCase(name);
048    
049                    Session session = null;
050    
051                    try {
052                            session = openSession();
053    
054                            String sql = CustomSQLUtil.get(COUNT_BY_C_N);
055    
056                            SQLQuery q = session.createSQLQuery(sql);
057    
058                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
059    
060                            QueryPos qPos = QueryPos.getInstance(q);
061    
062                            qPos.add(companyId);
063                            qPos.add(name);
064                            qPos.add(name);
065    
066                            Iterator<Long> itr = q.list().iterator();
067    
068                            if (itr.hasNext()) {
069                                    Long count = itr.next();
070    
071                                    if (count != null) {
072                                            return count.intValue();
073                                    }
074                            }
075    
076                            return 0;
077                    }
078                    catch (Exception e) {
079                            throw new SystemException(e);
080                    }
081                    finally {
082                            closeSession(session);
083                    }
084            }
085    
086            public List<PasswordPolicy> findByC_N(
087                            long companyId, String name, int start, int end,
088                            OrderByComparator obc)
089                    throws SystemException {
090    
091                    name = StringUtil.lowerCase(name);
092    
093                    Session session = null;
094    
095                    try {
096                            session = openSession();
097    
098                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
099    
100                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
101    
102                            SQLQuery q = session.createSQLQuery(sql);
103    
104                            q.addEntity("PasswordPolicy", PasswordPolicyImpl.class);
105    
106                            QueryPos qPos = QueryPos.getInstance(q);
107    
108                            qPos.add(companyId);
109                            qPos.add(name);
110                            qPos.add(name);
111    
112                            return (List<PasswordPolicy>)QueryUtil.list(
113                                    q, getDialect(), start, end);
114                    }
115                    catch (Exception e) {
116                            throw new SystemException(e);
117                    }
118                    finally {
119                            closeSession(session);
120                    }
121            }
122    
123    }