001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.shopping.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portlet.shopping.model.ShoppingOrder;
029    import com.liferay.portlet.shopping.model.ShoppingOrderConstants;
030    import com.liferay.portlet.shopping.model.impl.ShoppingOrderImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.util.Iterator;
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class ShoppingOrderFinderImpl
040            extends BasePersistenceImpl<ShoppingOrder> implements ShoppingOrderFinder {
041    
042            public static String COUNT_BY_G_C_U_N_PPPS =
043                    ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
044    
045            public static String FIND_BY_G_C_U_N_PPPS =
046                    ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
047    
048            public int countByG_C_U_N_PPPS(
049                            long groupId, long companyId, long userId, String number,
050                            String billingFirstName, String billingLastName,
051                            String billingEmailAddress, String shippingFirstName,
052                            String shippingLastName, String shippingEmailAddress,
053                            String ppPaymentStatus, boolean andOperator)
054                    throws SystemException {
055    
056                    number = StringUtil.upperCase(number);
057    
058                    Session session = null;
059    
060                    try {
061                            session = openSession();
062    
063                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
064    
065                            if (userId <= 0) {
066                                    sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
067                            }
068    
069                            if (Validator.isNull(ppPaymentStatus)) {
070                                    sql = StringUtil.replace(
071                                            sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
072    
073                                    ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
074                            }
075    
076                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
077    
078                            SQLQuery q = session.createSQLQuery(sql);
079    
080                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
081    
082                            QueryPos qPos = QueryPos.getInstance(q);
083    
084                            qPos.add(groupId);
085                            qPos.add(companyId);
086    
087                            if (userId > 0) {
088                                    qPos.add(userId);
089                            }
090    
091                            qPos.add(number);
092                            qPos.add(number);
093                            qPos.add(billingFirstName);
094                            qPos.add(billingFirstName);
095                            qPos.add(billingLastName);
096                            qPos.add(billingLastName);
097                            qPos.add(billingEmailAddress);
098                            qPos.add(billingEmailAddress);
099                            qPos.add(shippingFirstName);
100                            qPos.add(shippingFirstName);
101                            qPos.add(shippingLastName);
102                            qPos.add(shippingLastName);
103                            qPos.add(shippingEmailAddress);
104                            qPos.add(shippingEmailAddress);
105                            qPos.add(ppPaymentStatus);
106    
107                            Iterator<Long> itr = q.list().iterator();
108    
109                            if (itr.hasNext()) {
110                                    Long count = itr.next();
111    
112                                    if (count != null) {
113                                            return count.intValue();
114                                    }
115                            }
116    
117                            return 0;
118                    }
119                    catch (Exception e) {
120                            throw new SystemException(e);
121                    }
122                    finally {
123                            closeSession(session);
124                    }
125            }
126    
127            public List<ShoppingOrder> findByG_C_U_N_PPPS(
128                            long groupId, long companyId, long userId, String number,
129                            String billingFirstName, String billingLastName,
130                            String billingEmailAddress, String shippingFirstName,
131                            String shippingLastName, String shippingEmailAddress,
132                            String ppPaymentStatus, boolean andOperator, int start, int end,
133                            OrderByComparator obc)
134                    throws SystemException {
135    
136                    number = StringUtil.upperCase(number);
137    
138                    Session session = null;
139    
140                    try {
141                            session = openSession();
142    
143                            String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
144    
145                            if (userId <= 0) {
146                                    sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
147                            }
148    
149                            if (Validator.isNull(ppPaymentStatus)) {
150                                    sql = StringUtil.replace(
151                                            sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
152    
153                                    ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
154                            }
155    
156                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
157                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
158    
159                            SQLQuery q = session.createSQLQuery(sql);
160    
161                            q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
162    
163                            QueryPos qPos = QueryPos.getInstance(q);
164    
165                            qPos.add(groupId);
166                            qPos.add(companyId);
167    
168                            if (userId > 0) {
169                                    qPos.add(userId);
170                            }
171    
172                            qPos.add(number);
173                            qPos.add(number);
174                            qPos.add(billingFirstName);
175                            qPos.add(billingFirstName);
176                            qPos.add(billingLastName);
177                            qPos.add(billingLastName);
178                            qPos.add(billingEmailAddress);
179                            qPos.add(billingEmailAddress);
180                            qPos.add(shippingFirstName);
181                            qPos.add(shippingFirstName);
182                            qPos.add(shippingLastName);
183                            qPos.add(shippingLastName);
184                            qPos.add(shippingEmailAddress);
185                            qPos.add(shippingEmailAddress);
186                            qPos.add(ppPaymentStatus);
187    
188                            return (List<ShoppingOrder>)QueryUtil.list(
189                                    q, getDialect(), start, end);
190                    }
191                    catch (Exception e) {
192                            throw new SystemException(e);
193                    }
194                    finally {
195                            closeSession(session);
196                    }
197            }
198    
199            protected static String USER_ID_SQL = "(userId = ?) AND";
200    
201    }