1   /**
2    * Copyright (c) 2000-2007 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.util.StringUtil;
27  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
28  import com.liferay.portal.spring.hibernate.HibernateUtil;
29  import com.liferay.portlet.shopping.model.impl.ShoppingCouponImpl;
30  import com.liferay.util.dao.hibernate.QueryPos;
31  import com.liferay.util.dao.hibernate.QueryUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import org.hibernate.Hibernate;
37  import org.hibernate.SQLQuery;
38  import org.hibernate.Session;
39  
40  /**
41   * <a href="ShoppingCouponFinder.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ShoppingCouponFinder {
47  
48      public static String COUNT_BY_G_C_C_A_DT =
49          ShoppingCouponFinder.class.getName() + ".countByG_C_C_A_DT";
50  
51      public static String FIND_BY_G_C_C_A_DT =
52          ShoppingCouponFinder.class.getName() + ".findByG_C_C_A_DT";
53  
54      public static int countByG_C_C_A_DT(
55              long groupId, long companyId, String code, boolean active,
56              String discountType, boolean andOperator)
57          throws SystemException {
58  
59          code = StringUtil.upperCase(code);
60  
61          Session session = null;
62  
63          try {
64              session = HibernateUtil.openSession();
65  
66              String sql = CustomSQLUtil.get(COUNT_BY_G_C_C_A_DT);
67  
68              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
69  
70              SQLQuery q = session.createSQLQuery(sql);
71  
72              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
73  
74              QueryPos qPos = QueryPos.getInstance(q);
75  
76              qPos.add(groupId);
77              qPos.add(companyId);
78              qPos.add(code);
79              qPos.add(code);
80              qPos.add(active);
81              qPos.add(discountType);
82              qPos.add(discountType);
83  
84              Iterator itr = q.list().iterator();
85  
86              if (itr.hasNext()) {
87                  Long count = (Long)itr.next();
88  
89                  if (count != null) {
90                      return count.intValue();
91                  }
92              }
93  
94              return 0;
95          }
96          catch (Exception e) {
97              throw new SystemException(e);
98          }
99          finally {
100             HibernateUtil.closeSession(session);
101         }
102     }
103 
104     public static List findByG_C_C_A_DT(
105             long groupId, long companyId, String code, boolean active,
106             String discountType, boolean andOperator, int begin, int end)
107         throws SystemException {
108 
109         code = StringUtil.upperCase(code);
110 
111         Session session = null;
112 
113         try {
114             session = HibernateUtil.openSession();
115 
116             String sql = CustomSQLUtil.get(FIND_BY_G_C_C_A_DT);
117 
118             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
119 
120             SQLQuery q = session.createSQLQuery(sql);
121 
122             q.addEntity("ShoppingCoupon", ShoppingCouponImpl.class);
123 
124             QueryPos qPos = QueryPos.getInstance(q);
125 
126             qPos.add(groupId);
127             qPos.add(companyId);
128             qPos.add(code);
129             qPos.add(code);
130             qPos.add(active);
131             qPos.add(discountType);
132             qPos.add(discountType);
133 
134             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
135         }
136         catch (Exception e) {
137             throw new SystemException(e);
138         }
139         finally {
140             HibernateUtil.closeSession(session);
141         }
142     }
143 
144 }