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