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