001    /**
002     * Copyright (c) 2000-2013 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 final String COUNT_BY_G_C_U_N_PPPS =
043                    ShoppingOrderFinder.class.getName() + ".countByG_C_U_N_PPPS";
044    
045            public static final String FIND_BY_G_C_U_N_PPPS =
046                    ShoppingOrderFinder.class.getName() + ".findByG_C_U_N_PPPS";
047    
048            @Override
049            public int countByG_C_U_N_PPPS(
050                            long groupId, long companyId, long userId, String number,
051                            String billingFirstName, String billingLastName,
052                            String billingEmailAddress, String shippingFirstName,
053                            String shippingLastName, String shippingEmailAddress,
054                            String ppPaymentStatus, boolean andOperator)
055                    throws SystemException {
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.createSQLQuery(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 obc)
136                    throws SystemException {
137    
138                    number = StringUtil.upperCase(number);
139    
140                    Session session = null;
141    
142                    try {
143                            session = openSession();
144    
145                            String sql = CustomSQLUtil.get(FIND_BY_G_C_U_N_PPPS);
146    
147                            if (userId <= 0) {
148                                    sql = StringUtil.replace(sql, USER_ID_SQL, StringPool.BLANK);
149                            }
150    
151                            if (Validator.isNull(ppPaymentStatus)) {
152                                    sql = StringUtil.replace(
153                                            sql, "ppPaymentStatus = ?", "ppPaymentStatus != ?");
154    
155                                    ppPaymentStatus = ShoppingOrderConstants.STATUS_LATEST;
156                            }
157    
158                            sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
159                            sql = CustomSQLUtil.replaceOrderBy(sql, obc);
160    
161                            SQLQuery q = session.createSQLQuery(sql);
162    
163                            q.addEntity("ShoppingOrder", ShoppingOrderImpl.class);
164    
165                            QueryPos qPos = QueryPos.getInstance(q);
166    
167                            qPos.add(groupId);
168                            qPos.add(companyId);
169    
170                            if (userId > 0) {
171                                    qPos.add(userId);
172                            }
173    
174                            qPos.add(number);
175                            qPos.add(number);
176                            qPos.add(billingFirstName);
177                            qPos.add(billingFirstName);
178                            qPos.add(billingLastName);
179                            qPos.add(billingLastName);
180                            qPos.add(billingEmailAddress);
181                            qPos.add(billingEmailAddress);
182                            qPos.add(shippingFirstName);
183                            qPos.add(shippingFirstName);
184                            qPos.add(shippingLastName);
185                            qPos.add(shippingLastName);
186                            qPos.add(shippingEmailAddress);
187                            qPos.add(shippingEmailAddress);
188                            qPos.add(ppPaymentStatus);
189    
190                            return (List<ShoppingOrder>)QueryUtil.list(
191                                    q, getDialect(), start, end);
192                    }
193                    catch (Exception e) {
194                            throw new SystemException(e);
195                    }
196                    finally {
197                            closeSession(session);
198                    }
199            }
200    
201            protected static final String USER_ID_SQL = "(userId = ?) AND";
202    
203    }