1   /**
2    * Copyright (c) 2000-2009 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.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   */
49  public class ShoppingOrderFinderImpl
50      extends BasePersistenceImpl implements ShoppingOrderFinder {
51  
52      public static String COUNT_BY_G_C_U_N_PPPS =
53          ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
54  
55      public static String FIND_BY_G_C_U_N_PPPS =
56          ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
57  
58      public int countByG_C_U_N_PPPS(
59              long groupId, long companyId, long userId, String number,
60              String billingFirstName, String billingLastName,
61              String billingEmailAddress, String shippingFirstName,
62              String shippingLastName, String shippingEmailAddress,
63              String ppPaymentStatus, boolean andOperator)
64          throws SystemException {
65  
66          number = StringUtil.upperCase(number);
67  
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
74  
75              if (userId <= 0) {
76                  sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
77              }
78  
79              if (Validator.isNull(ppPaymentStatus)) {
80                  sql = StringUtil.replace(
81                      sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
82  
83                  ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
84              }
85  
86              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
87  
88              SQLQuery q = session.createSQLQuery(sql);
89  
90              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
91  
92              QueryPos qPos = QueryPos.getInstance(q);
93  
94              qPos.add(groupId);
95              qPos.add(companyId);
96  
97              if (userId > 0) {
98                  qPos.add(userId);
99              }
100 
101             qPos.add(number);
102             qPos.add(number);
103             qPos.add(billingFirstName);
104             qPos.add(billingFirstName);
105             qPos.add(billingLastName);
106             qPos.add(billingLastName);
107             qPos.add(billingEmailAddress);
108             qPos.add(billingEmailAddress);
109             qPos.add(shippingFirstName);
110             qPos.add(shippingFirstName);
111             qPos.add(shippingLastName);
112             qPos.add(shippingLastName);
113             qPos.add(shippingEmailAddress);
114             qPos.add(shippingEmailAddress);
115             qPos.add(ppPaymentStatus);
116 
117             Iterator<Long> itr = q.list().iterator();
118 
119             if (itr.hasNext()) {
120                 Long count = itr.next();
121 
122                 if (count != null) {
123                     return count.intValue();
124                 }
125             }
126 
127             return 0;
128         }
129         catch (Exception e) {
130             throw new SystemException(e);
131         }
132         finally {
133             closeSession(session);
134         }
135     }
136 
137     public List<ShoppingOrder> findByG_C_U_N_PPPS(
138             long groupId, long companyId, long userId, String number,
139             String billingFirstName, String billingLastName,
140             String billingEmailAddress, String shippingFirstName,
141             String shippingLastName, String shippingEmailAddress,
142             String ppPaymentStatus, boolean andOperator, int start, int end,
143             OrderByComparator obc)
144         throws SystemException {
145 
146         number = StringUtil.upperCase(number);
147 
148         Session session = null;
149 
150         try {
151             session = openSession();
152 
153             String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
154 
155             if (userId <= 0) {
156                 sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
157             }
158 
159             if (Validator.isNull(ppPaymentStatus)) {
160                 sql = StringUtil.replace(
161                     sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
162 
163                 ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
164             }
165 
166             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
167             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
168 
169             SQLQuery q = session.createSQLQuery(sql);
170 
171             q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
172 
173             QueryPos qPos = QueryPos.getInstance(q);
174 
175             qPos.add(groupId);
176             qPos.add(companyId);
177 
178             if (userId > 0) {
179                 qPos.add(userId);
180             }
181 
182             qPos.add(number);
183             qPos.add(number);
184             qPos.add(billingFirstName);
185             qPos.add(billingFirstName);
186             qPos.add(billingLastName);
187             qPos.add(billingLastName);
188             qPos.add(billingEmailAddress);
189             qPos.add(billingEmailAddress);
190             qPos.add(shippingFirstName);
191             qPos.add(shippingFirstName);
192             qPos.add(shippingLastName);
193             qPos.add(shippingLastName);
194             qPos.add(shippingEmailAddress);
195             qPos.add(shippingEmailAddress);
196             qPos.add(ppPaymentStatus);
197 
198             return (List<ShoppingOrder>)QueryUtil.list(
199                 q, getDialect(), start, end);
200         }
201         catch (Exception e) {
202             throw new SystemException(e);
203         }
204         finally {
205             closeSession(session);
206         }
207     }
208 
209     protected static String USER_ID_SQL = "(userId = ?) AND";
210 
211 }