1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.shopping.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.portlet.shopping.model.ShoppingOrder;
34  import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
35  import com.liferay.util.dao.orm.CustomSQLUtil;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="ShoppingOrderFinderImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ShoppingOrderFinderImpl
47      extends BasePersistenceImpl implements ShoppingOrderFinder {
48  
49      public static String COUNT_BY_G_C_U_N_PPPS =
50          ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
51  
52      public static String FIND_BY_G_C_U_N_PPPS =
53          ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
54  
55      public int countByG_C_U_N_PPPS(
56              long groupId, long companyId, long userId, String number,
57              String billingFirstName, String billingLastName,
58              String billingEmailAddress, String shippingFirstName,
59              String shippingLastName, String shippingEmailAddress,
60              String ppPaymentStatus, boolean andOperator)
61          throws SystemException {
62  
63          number = StringUtil.upperCase(number);
64  
65          Session session = null;
66  
67          try {
68              session = openSession();
69  
70              String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
71  
72              if (userId <= 0) {
73                  sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
74              }
75  
76              if (Validator.isNull(ppPaymentStatus)) {
77                  sql = StringUtil.replace(
78                      sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
79  
80                  ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
81              }
82  
83              sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
84  
85              SQLQuery q = session.createSQLQuery(sql);
86  
87              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
88  
89              QueryPos qPos = QueryPos.getInstance(q);
90  
91              qPos.add(groupId);
92              qPos.add(companyId);
93  
94              if (userId > 0) {
95                  qPos.add(userId);
96              }
97  
98              qPos.add(number);
99              qPos.add(number);
100             qPos.add(billingFirstName);
101             qPos.add(billingFirstName);
102             qPos.add(billingLastName);
103             qPos.add(billingLastName);
104             qPos.add(billingEmailAddress);
105             qPos.add(billingEmailAddress);
106             qPos.add(shippingFirstName);
107             qPos.add(shippingFirstName);
108             qPos.add(shippingLastName);
109             qPos.add(shippingLastName);
110             qPos.add(shippingEmailAddress);
111             qPos.add(shippingEmailAddress);
112             qPos.add(ppPaymentStatus);
113 
114             Iterator<Long> itr = q.list().iterator();
115 
116             if (itr.hasNext()) {
117                 Long count = itr.next();
118 
119                 if (count != null) {
120                     return count.intValue();
121                 }
122             }
123 
124             return 0;
125         }
126         catch (Exception e) {
127             throw new SystemException(e);
128         }
129         finally {
130             closeSession(session);
131         }
132     }
133 
134     public List<ShoppingOrder> findByG_C_U_N_PPPS(
135             long groupId, long companyId, long userId, String number,
136             String billingFirstName, String billingLastName,
137             String billingEmailAddress, String shippingFirstName,
138             String shippingLastName, String shippingEmailAddress,
139             String ppPaymentStatus, boolean andOperator, int start, int end,
140             OrderByComparator obc)
141         throws SystemException {
142 
143         number = StringUtil.upperCase(number);
144 
145         Session session = null;
146 
147         try {
148             session = openSession();
149 
150             String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
151 
152             if (userId <= 0) {
153                 sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
154             }
155 
156             if (Validator.isNull(ppPaymentStatus)) {
157                 sql = StringUtil.replace(
158                     sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
159 
160                 ppPaymentStatus = ShoppingOrderImpl.STATUS_LATEST;
161             }
162 
163             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
164             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
165 
166             SQLQuery q = session.createSQLQuery(sql);
167 
168             q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
169 
170             QueryPos qPos = QueryPos.getInstance(q);
171 
172             qPos.add(groupId);
173             qPos.add(companyId);
174 
175             if (userId > 0) {
176                 qPos.add(userId);
177             }
178 
179             qPos.add(number);
180             qPos.add(number);
181             qPos.add(billingFirstName);
182             qPos.add(billingFirstName);
183             qPos.add(billingLastName);
184             qPos.add(billingLastName);
185             qPos.add(billingEmailAddress);
186             qPos.add(billingEmailAddress);
187             qPos.add(shippingFirstName);
188             qPos.add(shippingFirstName);
189             qPos.add(shippingLastName);
190             qPos.add(shippingLastName);
191             qPos.add(shippingEmailAddress);
192             qPos.add(shippingEmailAddress);
193             qPos.add(ppPaymentStatus);
194 
195             return (List<ShoppingOrder>)QueryUtil.list(
196                 q, getDialect(), start, end);
197         }
198         catch (Exception e) {
199             throw new SystemException(e);
200         }
201         finally {
202             closeSession(session);
203         }
204     }
205 
206     protected static String USER_ID_SQL = "(userId = ?) AND";
207 
208 }