001    /**
002     * Copyright (c) 2000-present 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.impl;
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.portlet.shopping.service.persistence.ShoppingOrderFinder;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class ShoppingOrderFinderImpl
041            extends BasePersistenceImpl<ShoppingOrder> implements ShoppingOrderFinder {
042    
043            public static final String COUNT_BY_G_C_U_N_PPPS =
044                    ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
045    
046            public static final String FIND_BY_G_C_U_N_PPPS =
047                    ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
048    
049            @Override
050            public int countByG_C_U_N_PPPS(
051                    long groupId, long companyId, long userId, String number,
052                    String billingFirstName, String billingLastName,
053                    String billingEmailAddress, String shippingFirstName,
054                    String shippingLastName, String shippingEmailAddress,
055                    String ppPaymentStatus, boolean andOperator) {
056    
057                    number = StringUtil.upperCase(number);
058    
059                    Session session = null;
060    
061                    try {
062                            session = openSession();
063    
064                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_U_N_PPPS);
065    
066                            if (userId <= 0) {
067                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
068                            }
069    
070                            if (Validator.isNull(ppPaymentStatus)) {
071                                    sql = StringUtil.replace(
072                                            sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
073    
074                                    ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
075                            }
076    
077                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
078    
079                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
080    
081                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
082    
083                            QueryPos qPos = QueryPos.getInstance(q);
084    
085                            qPos.add(groupId);
086                            qPos.add(companyId);
087    
088                            if (userId > 0) {
089                                    qPos.add(userId);
090                            }
091    
092                            qPos.add(number);
093                            qPos.add(number);
094                            qPos.add(billingFirstName);
095                            qPos.add(billingFirstName);
096                            qPos.add(billingLastName);
097                            qPos.add(billingLastName);
098                            qPos.add(billingEmailAddress);
099                            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.iterate();
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            @Override
129            public List<ShoppingOrder> findByG_C_U_N_PPPS(
130                    long groupId, long companyId, long userId, String number,
131                    String billingFirstName, String billingLastName,
132                    String billingEmailAddress, String shippingFirstName,
133                    String shippingLastName, String shippingEmailAddress,
134                    String ppPaymentStatus, boolean andOperator, int start, int end,
135                    OrderByComparator<ShoppingOrder> obc) {
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 = ShoppingOrderConstants.STATUS_LATEST;
155                            }
156    
157                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
158                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
159    
160                            SQLQuery q = session.createSynchronizedSQLQuery(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            private static final String _USER_ID_SQL = "(userId = ?) AND";
201    
202    }