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