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