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