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.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 final String COUNT_BY_C_N =
041                    PasswordPolicyFinder.class.getName() + ".countByC_N";
042    
043            public static final String FIND_BY_C_N =
044                    PasswordPolicyFinder.class.getName() + ".findByC_N";
045    
046            @Override
047            public int countByC_N(long companyId, String name) throws SystemException {
048                    name = StringUtil.lowerCase(name);
049    
050                    Session session = null;
051    
052                    try {
053                            session = openSession();
054    
055                            String sql = CustomSQLUtil.get(COUNT_BY_C_N);
056    
057                            SQLQuery q = session.createSQLQuery(sql);
058    
059                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
060    
061                            QueryPos qPos = QueryPos.getInstance(q);
062    
063                            qPos.add(companyId);
064                            qPos.add(name);
065                            qPos.add(name);
066    
067                            Iterator<Long> itr = q.iterate();
068    
069                            if (itr.hasNext()) {
070                                    Long count = itr.next();
071    
072                                    if (count != null) {
073                                            return count.intValue();
074                                    }
075                            }
076    
077                            return 0;
078                    }
079                    catch (Exception e) {
080                            throw new SystemException(e);
081                    }
082                    finally {
083                            closeSession(session);
084                    }
085            }
086    
087            @Override
088            public List<PasswordPolicy> findByC_N(
089                            long companyId, String name, int start, int end,
090                            OrderByComparator obc)
091                    throws SystemException {
092    
093                    name = StringUtil.lowerCase(name);
094    
095                    Session session = null;
096    
097                    try {
098                            session = openSession();
099    
100                            String sql = CustomSQLUtil.get(FIND_BY_C_N);
101    
102                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
103    
104                            SQLQuery q = session.createSQLQuery(sql);
105    
106                            q.addEntity("PasswordPolicy", PasswordPolicyImpl.class);
107    
108                            QueryPos qPos = QueryPos.getInstance(q);
109    
110                            qPos.add(companyId);
111                            qPos.add(name);
112                            qPos.add(name);
113    
114                            return (List<PasswordPolicy>)QueryUtil.list(
115                                    q, getDialect(), start, end);
116                    }
117                    catch (Exception e) {
118                            throw new SystemException(e);
119                    }
120                    finally {
121                            closeSession(session);
122                    }
123            }
124    
125    }