1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.shopping.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.OrderByComparator;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.shopping.model.ShoppingOrder;
29  import com.liferay.portlet.shopping.model.ShoppingOrderConstants;
30  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
31  import com.liferay.util.dao.orm.CustomSQLUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="ShoppingOrderFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class ShoppingOrderFinderImpl
42      extends BasePersistenceImpl<ShoppingOrder> implements ShoppingOrderFinder {
43  
44      public static String COUNT_BY_G_C_U_N_PPPS =
45          ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
46  
47      public static String FIND_BY_G_C_U_N_PPPS =
48          ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
49  
50      public int countByG_C_U_N_PPPS(
51              long groupId, long companyId, long userId, String number,
52              String billingFirstName, String billingLastName,
53              String billingEmailAddress, String shippingFirstName,
54              String shippingLastName, String shippingEmailAddress,
55              String ppPaymentStatus, boolean andOperator)
56          throws SystemException {
57  
58          number = StringUtil.upperCase(number);
59  
60          Session session = null;
61  
62          try {
63              session = openSession();
64  
65              String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
66  
67              if (userId <= 0) {
68                  sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
69              }
70  
71              if (Validator.isNull(ppPaymentStatus)) {
72                  sql = StringUtil.replace(
73                      sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
74  
75                  ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
76              }
77  
78              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
79  
80              SQLQuery q = session.createSQLQuery(sql);
81  
82              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
83  
84              QueryPos qPos = QueryPos.getInstance(q);
85  
86              qPos.add(groupId);
87              qPos.add(companyId);
88  
89              if (userId > 0) {
90                  qPos.add(userId);
91              }
92  
93              qPos.add(number);
94              qPos.add(number);
95              qPos.add(billingFirstName);
96              qPos.add(billingFirstName);
97              qPos.add(billingLastName);
98              qPos.add(billingLastName);
99              qPos.add(billingEmailAddress);
100             qPos.add(billingEmailAddress);
101             qPos.add(shippingFirstName);
102             qPos.add(shippingFirstName);
103             qPos.add(shippingLastName);
104             qPos.add(shippingLastName);
105             qPos.add(shippingEmailAddress);
106             qPos.add(shippingEmailAddress);
107             qPos.add(ppPaymentStatus);
108 
109             Iterator<Long> itr = q.list().iterator();
110 
111             if (itr.hasNext()) {
112                 Long count = itr.next();
113 
114                 if (count != null) {
115                     return count.intValue();
116                 }
117             }
118 
119             return 0;
120         }
121         catch (Exception e) {
122             throw new SystemException(e);
123         }
124         finally {
125             closeSession(session);
126         }
127     }
128 
129     public List<ShoppingOrder> findByG_C_U_N_PPPS(
130             long groupId, long companyId, long userId, String number,
131             String billingFirstName, String billingLastName,
132             String billingEmailAddress, String shippingFirstName,
133             String shippingLastName, String shippingEmailAddress,
134             String ppPaymentStatus, boolean andOperator, int start, int end,
135             OrderByComparator obc)
136         throws SystemException {
137 
138         number = StringUtil.upperCase(number);
139 
140         Session session = null;
141 
142         try {
143             session = openSession();
144 
145             String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
146 
147             if (userId <= 0) {
148                 sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
149             }
150 
151             if (Validator.isNull(ppPaymentStatus)) {
152                 sql = StringUtil.replace(
153                     sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
154 
155                 ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
156             }
157 
158             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
159             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
160 
161             SQLQuery q = session.createSQLQuery(sql);
162 
163             q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
164 
165             QueryPos qPos = QueryPos.getInstance(q);
166 
167             qPos.add(groupId);
168             qPos.add(companyId);
169 
170             if (userId > 0) {
171                 qPos.add(userId);
172             }
173 
174             qPos.add(number);
175             qPos.add(number);
176             qPos.add(billingFirstName);
177             qPos.add(billingFirstName);
178             qPos.add(billingLastName);
179             qPos.add(billingLastName);
180             qPos.add(billingEmailAddress);
181             qPos.add(billingEmailAddress);
182             qPos.add(shippingFirstName);
183             qPos.add(shippingFirstName);
184             qPos.add(shippingLastName);
185             qPos.add(shippingLastName);
186             qPos.add(shippingEmailAddress);
187             qPos.add(shippingEmailAddress);
188             qPos.add(ppPaymentStatus);
189 
190             return (List<ShoppingOrder>)QueryUtil.list(
191                 q, getDialect(), start, end);
192         }
193         catch (Exception e) {
194             throw new SystemException(e);
195         }
196         finally {
197             closeSession(session);
198         }
199     }
200 
201     protected static String USER_ID_SQL = "(userId = ?) AND";
202 
203 }