001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 String COUNT_BY_G_C_C_A_DT =
040                    ShoppingCouponFinder.class.getName() + ".countByG_C_C_A_DT";
041    
042            public static String FIND_BY_G_C_C_A_DT =
043                    ShoppingCouponFinder.class.getName() + ".findByG_C_C_A_DT";
044    
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 = StringUtil.upperCase(code);
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.list().iterator();
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            public List<ShoppingCoupon> findByG_C_C_A_DT(
096                            long groupId, long companyId, String code, boolean active,
097                            String discountType, boolean andOperator, int start, int end)
098                    throws SystemException {
099    
100                    code = StringUtil.upperCase(code);
101    
102                    Session session = null;
103    
104                    try {
105                            session = openSession();
106    
107                            String sql = CustomSQLUtil.get(FIND_BY_G_C_C_A_DT);
108    
109                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
110    
111                            SQLQuery q = session.createSQLQuery(sql);
112    
113                            q.addEntity("ShoppingCoupon", ShoppingCouponImpl.class);
114    
115                            QueryPos qPos = QueryPos.getInstance(q);
116    
117                            qPos.add(groupId);
118                            qPos.add(companyId);
119                            qPos.add(code);
120                            qPos.add(code);
121                            qPos.add(active);
122                            qPos.add(discountType);
123                            qPos.add(discountType);
124    
125                            return (List<ShoppingCoupon>)QueryUtil.list(
126                                    q, getDialect(), start, end);
127                    }
128                    catch (Exception e) {
129                            throw new SystemException(e);
130                    }
131                    finally {
132                            closeSession(session);
133                    }
134            }
135    
136    }