1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.portlet.shopping.model.ShoppingCoupon;
34  import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
35  import com.liferay.util.dao.orm.CustomSQLUtil;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="ShoppingCouponFinderImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ShoppingCouponFinderImpl
47      extends BasePersistenceImpl implements ShoppingCouponFinder {
48  
49      public static String COUNT_BY_G_C_C_A_DT =
50          ShoppingCouponFinder.class.getName() + ".countByG_C_C_A_DT";
51  
52      public static String FIND_BY_G_C_C_A_DT =
53          ShoppingCouponFinder.class.getName() + ".findByG_C_C_A_DT";
54  
55      public int countByG_C_C_A_DT(
56              long groupId, long companyId, String code, boolean active,
57              String discountType, boolean andOperator)
58          throws SystemException {
59  
60          code = StringUtil.upperCase(code);
61  
62          Session session = null;
63  
64          try {
65              session = openSession();
66  
67              String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_A_DT);
68  
69              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
70  
71              SQLQuery q = session.createSQLQuery(sql);
72  
73              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
74  
75              QueryPos qPos = QueryPos.getInstance(q);
76  
77              qPos.add(groupId);
78              qPos.add(companyId);
79              qPos.add(code);
80              qPos.add(code);
81              qPos.add(active);
82              qPos.add(discountType);
83              qPos.add(discountType);
84  
85              Iterator<Long> itr = q.list().iterator();
86  
87              if (itr.hasNext()) {
88                  Long count = itr.next();
89  
90                  if (count != null) {
91                      return count.intValue();
92                  }
93              }
94  
95              return 0;
96          }
97          catch (Exception e) {
98              throw new SystemException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public List<ShoppingCoupon> findByG_C_C_A_DT(
106             long groupId, long companyId, String code, boolean active,
107             String discountType, boolean andOperator, int start, int end)
108         throws SystemException {
109 
110         code = StringUtil.upperCase(code);
111 
112         Session session = null;
113 
114         try {
115             session = openSession();
116 
117             String sql = CustomSQLUtil.get(FIND_BY_G_C_C_A_DT);
118 
119             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addEntity("ShoppingCoupon", ShoppingCouponImpl.class);
124 
125             QueryPos qPos = QueryPos.getInstance(q);
126 
127             qPos.add(groupId);
128             qPos.add(companyId);
129             qPos.add(code);
130             qPos.add(code);
131             qPos.add(active);
132             qPos.add(discountType);
133             qPos.add(discountType);
134 
135             return (List<ShoppingCoupon>)QueryUtil.list(
136                 q, getDialect(), start, end);
137         }
138         catch (Exception e) {
139             throw new SystemException(e);
140         }
141         finally {
142             closeSession(session);
143         }
144     }
145 
146 }