001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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.model.PasswordPolicy;
025    import com.liferay.portal.model.impl.PasswordPolicyImpl;
026    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
027    import com.liferay.util.dao.orm.CustomSQLUtil;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class PasswordPolicyFinderImpl
036            extends BasePersistenceImpl<PasswordPolicy>
037            implements PasswordPolicyFinder {
038    
039            public static final String COUNT_BY_C_N =
040                    PasswordPolicyFinder.class.getName() + ".countByC_N";
041    
042            public static final String FIND_BY_C_N =
043                    PasswordPolicyFinder.class.getName() + ".findByC_N";
044    
045            @Override
046            public int countByC_N(long companyId, String name) throws SystemException {
047                    name = CustomSQLUtil.keywords(name)[0];
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.iterate();
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            @Override
087            public List<PasswordPolicy> findByC_N(
088                            long companyId, String name, int start, int end,
089                            OrderByComparator obc)
090                    throws SystemException {
091    
092                    name = CustomSQLUtil.keywords(name)[0];
093    
094                    Session session = null;
095    
096                    try {
097                            session = openSession();
098    
099                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
100    
101                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
102    
103                            SQLQuery q = session.createSQLQuery(sql);
104    
105                            q.addEntity("PasswordPolicy", PasswordPolicyImpl.class);
106    
107                            QueryPos qPos = QueryPos.getInstance(q);
108    
109                            qPos.add(companyId);
110                            qPos.add(name);
111                            qPos.add(name);
112    
113                            return (List<PasswordPolicy>)QueryUtil.list(
114                                    q, getDialect(), start, end);
115                    }
116                    catch (Exception e) {
117                            throw new SystemException(e);
118                    }
119                    finally {
120                            closeSession(session);
121                    }
122            }
123    
124    }