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.portlet.shopping.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.StringUtil;
024    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
025    import com.liferay.portlet.shopping.model.ShoppingCoupon;
026    import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
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 ShoppingCouponFinderImpl
036            extends BasePersistenceImpl<ShoppingCoupon>
037            implements ShoppingCouponFinder {
038    
039            public static final String COUNT_BY_G_C_C_A_DT =
040                    ShoppingCouponFinder.class.getName() + ".countByG_C_C_A_DT";
041    
042            public static final String FIND_BY_G_C_C_A_DT =
043                    ShoppingCouponFinder.class.getName() + ".findByG_C_C_A_DT";
044    
045            @Override
046            public int countByG_C_C_A_DT(
047                            long groupId, long companyId, String code, boolean active,
048                            String discountType, boolean andOperator)
049                    throws SystemException {
050    
051                    code = StringUtil.upperCase(code);
052    
053                    Session session = null;
054    
055                    try {
056                            session = openSession();
057    
058                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_A_DT);
059    
060                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
061    
062                            SQLQuery q = session.createSQLQuery(sql);
063    
064                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
065    
066                            QueryPos qPos = QueryPos.getInstance(q);
067    
068                            qPos.add(groupId);
069                            qPos.add(companyId);
070                            qPos.add(code);
071                            qPos.add(code);
072                            qPos.add(active);
073                            qPos.add(discountType);
074                            qPos.add(discountType);
075    
076                            Iterator<Long> itr = q.iterate();
077    
078                            if (itr.hasNext()) {
079                                    Long count = itr.next();
080    
081                                    if (count != null) {
082                                            return count.intValue();
083                                    }
084                            }
085    
086                            return 0;
087                    }
088                    catch (Exception e) {
089                            throw new SystemException(e);
090                    }
091                    finally {
092                            closeSession(session);
093                    }
094            }
095    
096            @Override
097            public List<ShoppingCoupon> findByG_C_C_A_DT(
098                            long groupId, long companyId, String code, boolean active,
099                            String discountType, boolean andOperator, int start, int end)
100                    throws SystemException {
101    
102                    code = StringUtil.upperCase(code);
103    
104                    Session session = null;
105    
106                    try {
107                            session = openSession();
108    
109                            String sql = CustomSQLUtil.get(FIND_BY_G_C_C_A_DT);
110    
111                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
112    
113                            SQLQuery q = session.createSQLQuery(sql);
114    
115                            q.addEntity("ShoppingCoupon", ShoppingCouponImpl.class);
116    
117                            QueryPos qPos = QueryPos.getInstance(q);
118    
119                            qPos.add(groupId);
120                            qPos.add(companyId);
121                            qPos.add(code);
122                            qPos.add(code);
123                            qPos.add(active);
124                            qPos.add(discountType);
125                            qPos.add(discountType);
126    
127                            return (List<ShoppingCoupon>)QueryUtil.list(
128                                    q, getDialect(), start, end);
129                    }
130                    catch (Exception e) {
131                            throw new SystemException(e);
132                    }
133                    finally {
134                            closeSession(session);
135                    }
136            }
137    
138    }