1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
36  import com.liferay.portlet.shopping.model.ShoppingOrder;
37  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
38  import com.liferay.util.dao.orm.CustomSQLUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="ShoppingOrderFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class ShoppingOrderFinderImpl
49      extends BasePersistenceImpl implements ShoppingOrderFinder {
50  
51      public static String COUNT_BY_G_C_U_N_PPPS =
52          ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
53  
54      public static String FIND_BY_G_C_U_N_PPPS =
55          ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
56  
57      public int countByG_C_U_N_PPPS(
58              long groupId, long companyId, long userId, String number,
59              String billingFirstName, String billingLastName,
60              String billingEmailAddress, String shippingFirstName,
61              String shippingLastName, String shippingEmailAddress,
62              String ppPaymentStatus, boolean andOperator)
63          throws SystemException {
64  
65          number = StringUtil.upperCase(number);
66  
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
73  
74              if (userId <= 0) {
75                  sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
76              }
77  
78              if (Validator.isNull(ppPaymentStatus)) {
79                  sql = StringUtil.replace(
80                      sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
81  
82                  ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
83              }
84  
85              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
86  
87              SQLQuery q = session.createSQLQuery(sql);
88  
89              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
90  
91              QueryPos qPos = QueryPos.getInstance(q);
92  
93              qPos.add(groupId);
94              qPos.add(companyId);
95  
96              if (userId > 0) {
97                  qPos.add(userId);
98              }
99  
100             qPos.add(number);
101             qPos.add(number);
102             qPos.add(billingFirstName);
103             qPos.add(billingFirstName);
104             qPos.add(billingLastName);
105             qPos.add(billingLastName);
106             qPos.add(billingEmailAddress);
107             qPos.add(billingEmailAddress);
108             qPos.add(shippingFirstName);
109             qPos.add(shippingFirstName);
110             qPos.add(shippingLastName);
111             qPos.add(shippingLastName);
112             qPos.add(shippingEmailAddress);
113             qPos.add(shippingEmailAddress);
114             qPos.add(ppPaymentStatus);
115 
116             Iterator<Long> itr = q.list().iterator();
117 
118             if (itr.hasNext()) {
119                 Long count = itr.next();
120 
121                 if (count != null) {
122                     return count.intValue();
123                 }
124             }
125 
126             return 0;
127         }
128         catch (Exception e) {
129             throw new SystemException(e);
130         }
131         finally {
132             closeSession(session);
133         }
134     }
135 
136     public List<ShoppingOrder> findByG_C_U_N_PPPS(
137             long groupId, long companyId, long userId, String number,
138             String billingFirstName, String billingLastName,
139             String billingEmailAddress, String shippingFirstName,
140             String shippingLastName, String shippingEmailAddress,
141             String ppPaymentStatus, boolean andOperator, int start, int end,
142             OrderByComparator obc)
143         throws SystemException {
144 
145         number = StringUtil.upperCase(number);
146 
147         Session session = null;
148 
149         try {
150             session = openSession();
151 
152             String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
153 
154             if (userId <= 0) {
155                 sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
156             }
157 
158             if (Validator.isNull(ppPaymentStatus)) {
159                 sql = StringUtil.replace(
160                     sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
161 
162                 ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
163             }
164 
165             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
166             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
167 
168             SQLQuery q = session.createSQLQuery(sql);
169 
170             q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
171 
172             QueryPos qPos = QueryPos.getInstance(q);
173 
174             qPos.add(groupId);
175             qPos.add(companyId);
176 
177             if (userId > 0) {
178                 qPos.add(userId);
179             }
180 
181             qPos.add(number);
182             qPos.add(number);
183             qPos.add(billingFirstName);
184             qPos.add(billingFirstName);
185             qPos.add(billingLastName);
186             qPos.add(billingLastName);
187             qPos.add(billingEmailAddress);
188             qPos.add(billingEmailAddress);
189             qPos.add(shippingFirstName);
190             qPos.add(shippingFirstName);
191             qPos.add(shippingLastName);
192             qPos.add(shippingLastName);
193             qPos.add(shippingEmailAddress);
194             qPos.add(shippingEmailAddress);
195             qPos.add(ppPaymentStatus);
196 
197             return (List<ShoppingOrder>)QueryUtil.list(
198                 q, getDialect(), start, end);
199         }
200         catch (Exception e) {
201             throw new SystemException(e);
202         }
203         finally {
204             closeSession(session);
205         }
206     }
207 
208     protected static String USER_ID_SQL = "(userId = ?) AND";
209 
210 }