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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.model.CacheModel;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextThreadLocal;
034    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
035    
036    import com.liferay.portlet.shopping.NoSuchCartException;
037    import com.liferay.portlet.shopping.model.ShoppingCart;
038    import com.liferay.portlet.shopping.model.impl.ShoppingCartImpl;
039    import com.liferay.portlet.shopping.model.impl.ShoppingCartModelImpl;
040    import com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence;
041    
042    import java.io.Serializable;
043    
044    import java.util.Collections;
045    import java.util.Date;
046    import java.util.HashMap;
047    import java.util.HashSet;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    import java.util.Set;
052    
053    /**
054     * The persistence implementation for the shopping cart service.
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see ShoppingCartPersistence
062     * @see com.liferay.portlet.shopping.service.persistence.ShoppingCartUtil
063     * @generated
064     */
065    @ProviderType
066    public class ShoppingCartPersistenceImpl extends BasePersistenceImpl<ShoppingCart>
067            implements ShoppingCartPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * Never modify or reference this class directly. Always use {@link ShoppingCartUtil} to access the shopping cart persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
072             */
073            public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCartImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List1";
076            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List2";
078            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
079                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
082                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
085                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, Long.class,
086                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
087            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
088                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByGroupId",
090                            new String[] {
091                                    Long.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
097                    new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
098                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
100                            new String[] { Long.class.getName() },
101                            ShoppingCartModelImpl.GROUPID_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
103                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
105                            new String[] { Long.class.getName() });
106    
107            /**
108             * Returns all the shopping carts where groupId = &#63;.
109             *
110             * @param groupId the group ID
111             * @return the matching shopping carts
112             */
113            @Override
114            public List<ShoppingCart> findByGroupId(long groupId) {
115                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
116            }
117    
118            /**
119             * Returns a range of all the shopping carts where groupId = &#63;.
120             *
121             * <p>
122             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
123             * </p>
124             *
125             * @param groupId the group ID
126             * @param start the lower bound of the range of shopping carts
127             * @param end the upper bound of the range of shopping carts (not inclusive)
128             * @return the range of matching shopping carts
129             */
130            @Override
131            public List<ShoppingCart> findByGroupId(long groupId, int start, int end) {
132                    return findByGroupId(groupId, start, end, null);
133            }
134    
135            /**
136             * Returns an ordered range of all the shopping carts where groupId = &#63;.
137             *
138             * <p>
139             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
140             * </p>
141             *
142             * @param groupId the group ID
143             * @param start the lower bound of the range of shopping carts
144             * @param end the upper bound of the range of shopping carts (not inclusive)
145             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
146             * @return the ordered range of matching shopping carts
147             */
148            @Override
149            public List<ShoppingCart> findByGroupId(long groupId, int start, int end,
150                    OrderByComparator<ShoppingCart> orderByComparator) {
151                    boolean pagination = true;
152                    FinderPath finderPath = null;
153                    Object[] finderArgs = null;
154    
155                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
156                                    (orderByComparator == null)) {
157                            pagination = false;
158                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
159                            finderArgs = new Object[] { groupId };
160                    }
161                    else {
162                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
163                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
164                    }
165    
166                    List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(finderPath,
167                                    finderArgs, this);
168    
169                    if ((list != null) && !list.isEmpty()) {
170                            for (ShoppingCart shoppingCart : list) {
171                                    if ((groupId != shoppingCart.getGroupId())) {
172                                            list = null;
173    
174                                            break;
175                                    }
176                            }
177                    }
178    
179                    if (list == null) {
180                            StringBundler query = null;
181    
182                            if (orderByComparator != null) {
183                                    query = new StringBundler(3 +
184                                                    (orderByComparator.getOrderByFields().length * 3));
185                            }
186                            else {
187                                    query = new StringBundler(3);
188                            }
189    
190                            query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
191    
192                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
193    
194                            if (orderByComparator != null) {
195                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
196                                            orderByComparator);
197                            }
198                            else
199                             if (pagination) {
200                                    query.append(ShoppingCartModelImpl.ORDER_BY_JPQL);
201                            }
202    
203                            String sql = query.toString();
204    
205                            Session session = null;
206    
207                            try {
208                                    session = openSession();
209    
210                                    Query q = session.createQuery(sql);
211    
212                                    QueryPos qPos = QueryPos.getInstance(q);
213    
214                                    qPos.add(groupId);
215    
216                                    if (!pagination) {
217                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
218                                                            start, end, false);
219    
220                                            Collections.sort(list);
221    
222                                            list = Collections.unmodifiableList(list);
223                                    }
224                                    else {
225                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
226                                                            start, end);
227                                    }
228    
229                                    cacheResult(list);
230    
231                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
232                            }
233                            catch (Exception e) {
234                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
235    
236                                    throw processException(e);
237                            }
238                            finally {
239                                    closeSession(session);
240                            }
241                    }
242    
243                    return list;
244            }
245    
246            /**
247             * Returns the first shopping cart in the ordered set where groupId = &#63;.
248             *
249             * @param groupId the group ID
250             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
251             * @return the first matching shopping cart
252             * @throws NoSuchCartException if a matching shopping cart could not be found
253             */
254            @Override
255            public ShoppingCart findByGroupId_First(long groupId,
256                    OrderByComparator<ShoppingCart> orderByComparator)
257                    throws NoSuchCartException {
258                    ShoppingCart shoppingCart = fetchByGroupId_First(groupId,
259                                    orderByComparator);
260    
261                    if (shoppingCart != null) {
262                            return shoppingCart;
263                    }
264    
265                    StringBundler msg = new StringBundler(4);
266    
267                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
268    
269                    msg.append("groupId=");
270                    msg.append(groupId);
271    
272                    msg.append(StringPool.CLOSE_CURLY_BRACE);
273    
274                    throw new NoSuchCartException(msg.toString());
275            }
276    
277            /**
278             * Returns the first shopping cart in the ordered set where groupId = &#63;.
279             *
280             * @param groupId the group ID
281             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
282             * @return the first matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
283             */
284            @Override
285            public ShoppingCart fetchByGroupId_First(long groupId,
286                    OrderByComparator<ShoppingCart> orderByComparator) {
287                    List<ShoppingCart> list = findByGroupId(groupId, 0, 1, orderByComparator);
288    
289                    if (!list.isEmpty()) {
290                            return list.get(0);
291                    }
292    
293                    return null;
294            }
295    
296            /**
297             * Returns the last shopping cart in the ordered set where groupId = &#63;.
298             *
299             * @param groupId the group ID
300             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
301             * @return the last matching shopping cart
302             * @throws NoSuchCartException if a matching shopping cart could not be found
303             */
304            @Override
305            public ShoppingCart findByGroupId_Last(long groupId,
306                    OrderByComparator<ShoppingCart> orderByComparator)
307                    throws NoSuchCartException {
308                    ShoppingCart shoppingCart = fetchByGroupId_Last(groupId,
309                                    orderByComparator);
310    
311                    if (shoppingCart != null) {
312                            return shoppingCart;
313                    }
314    
315                    StringBundler msg = new StringBundler(4);
316    
317                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
318    
319                    msg.append("groupId=");
320                    msg.append(groupId);
321    
322                    msg.append(StringPool.CLOSE_CURLY_BRACE);
323    
324                    throw new NoSuchCartException(msg.toString());
325            }
326    
327            /**
328             * Returns the last shopping cart in the ordered set where groupId = &#63;.
329             *
330             * @param groupId the group ID
331             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
332             * @return the last matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
333             */
334            @Override
335            public ShoppingCart fetchByGroupId_Last(long groupId,
336                    OrderByComparator<ShoppingCart> orderByComparator) {
337                    int count = countByGroupId(groupId);
338    
339                    if (count == 0) {
340                            return null;
341                    }
342    
343                    List<ShoppingCart> list = findByGroupId(groupId, count - 1, count,
344                                    orderByComparator);
345    
346                    if (!list.isEmpty()) {
347                            return list.get(0);
348                    }
349    
350                    return null;
351            }
352    
353            /**
354             * Returns the shopping carts before and after the current shopping cart in the ordered set where groupId = &#63;.
355             *
356             * @param cartId the primary key of the current shopping cart
357             * @param groupId the group ID
358             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
359             * @return the previous, current, and next shopping cart
360             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
361             */
362            @Override
363            public ShoppingCart[] findByGroupId_PrevAndNext(long cartId, long groupId,
364                    OrderByComparator<ShoppingCart> orderByComparator)
365                    throws NoSuchCartException {
366                    ShoppingCart shoppingCart = findByPrimaryKey(cartId);
367    
368                    Session session = null;
369    
370                    try {
371                            session = openSession();
372    
373                            ShoppingCart[] array = new ShoppingCartImpl[3];
374    
375                            array[0] = getByGroupId_PrevAndNext(session, shoppingCart, groupId,
376                                            orderByComparator, true);
377    
378                            array[1] = shoppingCart;
379    
380                            array[2] = getByGroupId_PrevAndNext(session, shoppingCart, groupId,
381                                            orderByComparator, false);
382    
383                            return array;
384                    }
385                    catch (Exception e) {
386                            throw processException(e);
387                    }
388                    finally {
389                            closeSession(session);
390                    }
391            }
392    
393            protected ShoppingCart getByGroupId_PrevAndNext(Session session,
394                    ShoppingCart shoppingCart, long groupId,
395                    OrderByComparator<ShoppingCart> orderByComparator, boolean previous) {
396                    StringBundler query = null;
397    
398                    if (orderByComparator != null) {
399                            query = new StringBundler(6 +
400                                            (orderByComparator.getOrderByFields().length * 6));
401                    }
402                    else {
403                            query = new StringBundler(3);
404                    }
405    
406                    query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
407    
408                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
409    
410                    if (orderByComparator != null) {
411                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
412    
413                            if (orderByConditionFields.length > 0) {
414                                    query.append(WHERE_AND);
415                            }
416    
417                            for (int i = 0; i < orderByConditionFields.length; i++) {
418                                    query.append(_ORDER_BY_ENTITY_ALIAS);
419                                    query.append(orderByConditionFields[i]);
420    
421                                    if ((i + 1) < orderByConditionFields.length) {
422                                            if (orderByComparator.isAscending() ^ previous) {
423                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
424                                            }
425                                            else {
426                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
427                                            }
428                                    }
429                                    else {
430                                            if (orderByComparator.isAscending() ^ previous) {
431                                                    query.append(WHERE_GREATER_THAN);
432                                            }
433                                            else {
434                                                    query.append(WHERE_LESSER_THAN);
435                                            }
436                                    }
437                            }
438    
439                            query.append(ORDER_BY_CLAUSE);
440    
441                            String[] orderByFields = orderByComparator.getOrderByFields();
442    
443                            for (int i = 0; i < orderByFields.length; i++) {
444                                    query.append(_ORDER_BY_ENTITY_ALIAS);
445                                    query.append(orderByFields[i]);
446    
447                                    if ((i + 1) < orderByFields.length) {
448                                            if (orderByComparator.isAscending() ^ previous) {
449                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
450                                            }
451                                            else {
452                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
453                                            }
454                                    }
455                                    else {
456                                            if (orderByComparator.isAscending() ^ previous) {
457                                                    query.append(ORDER_BY_ASC);
458                                            }
459                                            else {
460                                                    query.append(ORDER_BY_DESC);
461                                            }
462                                    }
463                            }
464                    }
465                    else {
466                            query.append(ShoppingCartModelImpl.ORDER_BY_JPQL);
467                    }
468    
469                    String sql = query.toString();
470    
471                    Query q = session.createQuery(sql);
472    
473                    q.setFirstResult(0);
474                    q.setMaxResults(2);
475    
476                    QueryPos qPos = QueryPos.getInstance(q);
477    
478                    qPos.add(groupId);
479    
480                    if (orderByComparator != null) {
481                            Object[] values = orderByComparator.getOrderByConditionValues(shoppingCart);
482    
483                            for (Object value : values) {
484                                    qPos.add(value);
485                            }
486                    }
487    
488                    List<ShoppingCart> list = q.list();
489    
490                    if (list.size() == 2) {
491                            return list.get(1);
492                    }
493                    else {
494                            return null;
495                    }
496            }
497    
498            /**
499             * Removes all the shopping carts where groupId = &#63; from the database.
500             *
501             * @param groupId the group ID
502             */
503            @Override
504            public void removeByGroupId(long groupId) {
505                    for (ShoppingCart shoppingCart : findByGroupId(groupId,
506                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
507                            remove(shoppingCart);
508                    }
509            }
510    
511            /**
512             * Returns the number of shopping carts where groupId = &#63;.
513             *
514             * @param groupId the group ID
515             * @return the number of matching shopping carts
516             */
517            @Override
518            public int countByGroupId(long groupId) {
519                    FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUPID;
520    
521                    Object[] finderArgs = new Object[] { groupId };
522    
523                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
524                                    this);
525    
526                    if (count == null) {
527                            StringBundler query = new StringBundler(2);
528    
529                            query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
530    
531                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
532    
533                            String sql = query.toString();
534    
535                            Session session = null;
536    
537                            try {
538                                    session = openSession();
539    
540                                    Query q = session.createQuery(sql);
541    
542                                    QueryPos qPos = QueryPos.getInstance(q);
543    
544                                    qPos.add(groupId);
545    
546                                    count = (Long)q.uniqueResult();
547    
548                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
549                            }
550                            catch (Exception e) {
551                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
552    
553                                    throw processException(e);
554                            }
555                            finally {
556                                    closeSession(session);
557                            }
558                    }
559    
560                    return count.intValue();
561            }
562    
563            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCart.groupId = ?";
564            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
565                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
566                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
567                            new String[] {
568                                    Long.class.getName(),
569                                    
570                            Integer.class.getName(), Integer.class.getName(),
571                                    OrderByComparator.class.getName()
572                            });
573            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
574                    new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
575                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
576                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
577                            new String[] { Long.class.getName() },
578                            ShoppingCartModelImpl.USERID_COLUMN_BITMASK);
579            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
580                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, Long.class,
581                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
582                            new String[] { Long.class.getName() });
583    
584            /**
585             * Returns all the shopping carts where userId = &#63;.
586             *
587             * @param userId the user ID
588             * @return the matching shopping carts
589             */
590            @Override
591            public List<ShoppingCart> findByUserId(long userId) {
592                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
593            }
594    
595            /**
596             * Returns a range of all the shopping carts where userId = &#63;.
597             *
598             * <p>
599             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
600             * </p>
601             *
602             * @param userId the user ID
603             * @param start the lower bound of the range of shopping carts
604             * @param end the upper bound of the range of shopping carts (not inclusive)
605             * @return the range of matching shopping carts
606             */
607            @Override
608            public List<ShoppingCart> findByUserId(long userId, int start, int end) {
609                    return findByUserId(userId, start, end, null);
610            }
611    
612            /**
613             * Returns an ordered range of all the shopping carts where userId = &#63;.
614             *
615             * <p>
616             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
617             * </p>
618             *
619             * @param userId the user ID
620             * @param start the lower bound of the range of shopping carts
621             * @param end the upper bound of the range of shopping carts (not inclusive)
622             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
623             * @return the ordered range of matching shopping carts
624             */
625            @Override
626            public List<ShoppingCart> findByUserId(long userId, int start, int end,
627                    OrderByComparator<ShoppingCart> orderByComparator) {
628                    boolean pagination = true;
629                    FinderPath finderPath = null;
630                    Object[] finderArgs = null;
631    
632                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
633                                    (orderByComparator == null)) {
634                            pagination = false;
635                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
636                            finderArgs = new Object[] { userId };
637                    }
638                    else {
639                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
640                            finderArgs = new Object[] { userId, start, end, orderByComparator };
641                    }
642    
643                    List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(finderPath,
644                                    finderArgs, this);
645    
646                    if ((list != null) && !list.isEmpty()) {
647                            for (ShoppingCart shoppingCart : list) {
648                                    if ((userId != shoppingCart.getUserId())) {
649                                            list = null;
650    
651                                            break;
652                                    }
653                            }
654                    }
655    
656                    if (list == null) {
657                            StringBundler query = null;
658    
659                            if (orderByComparator != null) {
660                                    query = new StringBundler(3 +
661                                                    (orderByComparator.getOrderByFields().length * 3));
662                            }
663                            else {
664                                    query = new StringBundler(3);
665                            }
666    
667                            query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
668    
669                            query.append(_FINDER_COLUMN_USERID_USERID_2);
670    
671                            if (orderByComparator != null) {
672                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
673                                            orderByComparator);
674                            }
675                            else
676                             if (pagination) {
677                                    query.append(ShoppingCartModelImpl.ORDER_BY_JPQL);
678                            }
679    
680                            String sql = query.toString();
681    
682                            Session session = null;
683    
684                            try {
685                                    session = openSession();
686    
687                                    Query q = session.createQuery(sql);
688    
689                                    QueryPos qPos = QueryPos.getInstance(q);
690    
691                                    qPos.add(userId);
692    
693                                    if (!pagination) {
694                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
695                                                            start, end, false);
696    
697                                            Collections.sort(list);
698    
699                                            list = Collections.unmodifiableList(list);
700                                    }
701                                    else {
702                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
703                                                            start, end);
704                                    }
705    
706                                    cacheResult(list);
707    
708                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
709                            }
710                            catch (Exception e) {
711                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
712    
713                                    throw processException(e);
714                            }
715                            finally {
716                                    closeSession(session);
717                            }
718                    }
719    
720                    return list;
721            }
722    
723            /**
724             * Returns the first shopping cart in the ordered set where userId = &#63;.
725             *
726             * @param userId the user ID
727             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
728             * @return the first matching shopping cart
729             * @throws NoSuchCartException if a matching shopping cart could not be found
730             */
731            @Override
732            public ShoppingCart findByUserId_First(long userId,
733                    OrderByComparator<ShoppingCart> orderByComparator)
734                    throws NoSuchCartException {
735                    ShoppingCart shoppingCart = fetchByUserId_First(userId,
736                                    orderByComparator);
737    
738                    if (shoppingCart != null) {
739                            return shoppingCart;
740                    }
741    
742                    StringBundler msg = new StringBundler(4);
743    
744                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
745    
746                    msg.append("userId=");
747                    msg.append(userId);
748    
749                    msg.append(StringPool.CLOSE_CURLY_BRACE);
750    
751                    throw new NoSuchCartException(msg.toString());
752            }
753    
754            /**
755             * Returns the first shopping cart in the ordered set where userId = &#63;.
756             *
757             * @param userId the user ID
758             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
759             * @return the first matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
760             */
761            @Override
762            public ShoppingCart fetchByUserId_First(long userId,
763                    OrderByComparator<ShoppingCart> orderByComparator) {
764                    List<ShoppingCart> list = findByUserId(userId, 0, 1, orderByComparator);
765    
766                    if (!list.isEmpty()) {
767                            return list.get(0);
768                    }
769    
770                    return null;
771            }
772    
773            /**
774             * Returns the last shopping cart in the ordered set where userId = &#63;.
775             *
776             * @param userId the user ID
777             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
778             * @return the last matching shopping cart
779             * @throws NoSuchCartException if a matching shopping cart could not be found
780             */
781            @Override
782            public ShoppingCart findByUserId_Last(long userId,
783                    OrderByComparator<ShoppingCart> orderByComparator)
784                    throws NoSuchCartException {
785                    ShoppingCart shoppingCart = fetchByUserId_Last(userId, orderByComparator);
786    
787                    if (shoppingCart != null) {
788                            return shoppingCart;
789                    }
790    
791                    StringBundler msg = new StringBundler(4);
792    
793                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
794    
795                    msg.append("userId=");
796                    msg.append(userId);
797    
798                    msg.append(StringPool.CLOSE_CURLY_BRACE);
799    
800                    throw new NoSuchCartException(msg.toString());
801            }
802    
803            /**
804             * Returns the last shopping cart in the ordered set where userId = &#63;.
805             *
806             * @param userId the user ID
807             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
808             * @return the last matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
809             */
810            @Override
811            public ShoppingCart fetchByUserId_Last(long userId,
812                    OrderByComparator<ShoppingCart> orderByComparator) {
813                    int count = countByUserId(userId);
814    
815                    if (count == 0) {
816                            return null;
817                    }
818    
819                    List<ShoppingCart> list = findByUserId(userId, count - 1, count,
820                                    orderByComparator);
821    
822                    if (!list.isEmpty()) {
823                            return list.get(0);
824                    }
825    
826                    return null;
827            }
828    
829            /**
830             * Returns the shopping carts before and after the current shopping cart in the ordered set where userId = &#63;.
831             *
832             * @param cartId the primary key of the current shopping cart
833             * @param userId the user ID
834             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
835             * @return the previous, current, and next shopping cart
836             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
837             */
838            @Override
839            public ShoppingCart[] findByUserId_PrevAndNext(long cartId, long userId,
840                    OrderByComparator<ShoppingCart> orderByComparator)
841                    throws NoSuchCartException {
842                    ShoppingCart shoppingCart = findByPrimaryKey(cartId);
843    
844                    Session session = null;
845    
846                    try {
847                            session = openSession();
848    
849                            ShoppingCart[] array = new ShoppingCartImpl[3];
850    
851                            array[0] = getByUserId_PrevAndNext(session, shoppingCart, userId,
852                                            orderByComparator, true);
853    
854                            array[1] = shoppingCart;
855    
856                            array[2] = getByUserId_PrevAndNext(session, shoppingCart, userId,
857                                            orderByComparator, false);
858    
859                            return array;
860                    }
861                    catch (Exception e) {
862                            throw processException(e);
863                    }
864                    finally {
865                            closeSession(session);
866                    }
867            }
868    
869            protected ShoppingCart getByUserId_PrevAndNext(Session session,
870                    ShoppingCart shoppingCart, long userId,
871                    OrderByComparator<ShoppingCart> orderByComparator, boolean previous) {
872                    StringBundler query = null;
873    
874                    if (orderByComparator != null) {
875                            query = new StringBundler(6 +
876                                            (orderByComparator.getOrderByFields().length * 6));
877                    }
878                    else {
879                            query = new StringBundler(3);
880                    }
881    
882                    query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
883    
884                    query.append(_FINDER_COLUMN_USERID_USERID_2);
885    
886                    if (orderByComparator != null) {
887                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
888    
889                            if (orderByConditionFields.length > 0) {
890                                    query.append(WHERE_AND);
891                            }
892    
893                            for (int i = 0; i < orderByConditionFields.length; i++) {
894                                    query.append(_ORDER_BY_ENTITY_ALIAS);
895                                    query.append(orderByConditionFields[i]);
896    
897                                    if ((i + 1) < orderByConditionFields.length) {
898                                            if (orderByComparator.isAscending() ^ previous) {
899                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
900                                            }
901                                            else {
902                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
903                                            }
904                                    }
905                                    else {
906                                            if (orderByComparator.isAscending() ^ previous) {
907                                                    query.append(WHERE_GREATER_THAN);
908                                            }
909                                            else {
910                                                    query.append(WHERE_LESSER_THAN);
911                                            }
912                                    }
913                            }
914    
915                            query.append(ORDER_BY_CLAUSE);
916    
917                            String[] orderByFields = orderByComparator.getOrderByFields();
918    
919                            for (int i = 0; i < orderByFields.length; i++) {
920                                    query.append(_ORDER_BY_ENTITY_ALIAS);
921                                    query.append(orderByFields[i]);
922    
923                                    if ((i + 1) < orderByFields.length) {
924                                            if (orderByComparator.isAscending() ^ previous) {
925                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
926                                            }
927                                            else {
928                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
929                                            }
930                                    }
931                                    else {
932                                            if (orderByComparator.isAscending() ^ previous) {
933                                                    query.append(ORDER_BY_ASC);
934                                            }
935                                            else {
936                                                    query.append(ORDER_BY_DESC);
937                                            }
938                                    }
939                            }
940                    }
941                    else {
942                            query.append(ShoppingCartModelImpl.ORDER_BY_JPQL);
943                    }
944    
945                    String sql = query.toString();
946    
947                    Query q = session.createQuery(sql);
948    
949                    q.setFirstResult(0);
950                    q.setMaxResults(2);
951    
952                    QueryPos qPos = QueryPos.getInstance(q);
953    
954                    qPos.add(userId);
955    
956                    if (orderByComparator != null) {
957                            Object[] values = orderByComparator.getOrderByConditionValues(shoppingCart);
958    
959                            for (Object value : values) {
960                                    qPos.add(value);
961                            }
962                    }
963    
964                    List<ShoppingCart> list = q.list();
965    
966                    if (list.size() == 2) {
967                            return list.get(1);
968                    }
969                    else {
970                            return null;
971                    }
972            }
973    
974            /**
975             * Removes all the shopping carts where userId = &#63; from the database.
976             *
977             * @param userId the user ID
978             */
979            @Override
980            public void removeByUserId(long userId) {
981                    for (ShoppingCart shoppingCart : findByUserId(userId,
982                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
983                            remove(shoppingCart);
984                    }
985            }
986    
987            /**
988             * Returns the number of shopping carts where userId = &#63;.
989             *
990             * @param userId the user ID
991             * @return the number of matching shopping carts
992             */
993            @Override
994            public int countByUserId(long userId) {
995                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
996    
997                    Object[] finderArgs = new Object[] { userId };
998    
999                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1000                                    this);
1001    
1002                    if (count == null) {
1003                            StringBundler query = new StringBundler(2);
1004    
1005                            query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1006    
1007                            query.append(_FINDER_COLUMN_USERID_USERID_2);
1008    
1009                            String sql = query.toString();
1010    
1011                            Session session = null;
1012    
1013                            try {
1014                                    session = openSession();
1015    
1016                                    Query q = session.createQuery(sql);
1017    
1018                                    QueryPos qPos = QueryPos.getInstance(q);
1019    
1020                                    qPos.add(userId);
1021    
1022                                    count = (Long)q.uniqueResult();
1023    
1024                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1025                            }
1026                            catch (Exception e) {
1027                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1028    
1029                                    throw processException(e);
1030                            }
1031                            finally {
1032                                    closeSession(session);
1033                            }
1034                    }
1035    
1036                    return count.intValue();
1037            }
1038    
1039            private static final String _FINDER_COLUMN_USERID_USERID_2 = "shoppingCart.userId = ?";
1040            public static final FinderPath FINDER_PATH_FETCH_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1041                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, ShoppingCartImpl.class,
1042                            FINDER_CLASS_NAME_ENTITY, "fetchByG_U",
1043                            new String[] { Long.class.getName(), Long.class.getName() },
1044                            ShoppingCartModelImpl.GROUPID_COLUMN_BITMASK |
1045                            ShoppingCartModelImpl.USERID_COLUMN_BITMASK);
1046            public static final FinderPath FINDER_PATH_COUNT_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1047                            ShoppingCartModelImpl.FINDER_CACHE_ENABLED, Long.class,
1048                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_U",
1049                            new String[] { Long.class.getName(), Long.class.getName() });
1050    
1051            /**
1052             * Returns the shopping cart where groupId = &#63; and userId = &#63; or throws a {@link NoSuchCartException} if it could not be found.
1053             *
1054             * @param groupId the group ID
1055             * @param userId the user ID
1056             * @return the matching shopping cart
1057             * @throws NoSuchCartException if a matching shopping cart could not be found
1058             */
1059            @Override
1060            public ShoppingCart findByG_U(long groupId, long userId)
1061                    throws NoSuchCartException {
1062                    ShoppingCart shoppingCart = fetchByG_U(groupId, userId);
1063    
1064                    if (shoppingCart == null) {
1065                            StringBundler msg = new StringBundler(6);
1066    
1067                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1068    
1069                            msg.append("groupId=");
1070                            msg.append(groupId);
1071    
1072                            msg.append(", userId=");
1073                            msg.append(userId);
1074    
1075                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1076    
1077                            if (_log.isWarnEnabled()) {
1078                                    _log.warn(msg.toString());
1079                            }
1080    
1081                            throw new NoSuchCartException(msg.toString());
1082                    }
1083    
1084                    return shoppingCart;
1085            }
1086    
1087            /**
1088             * Returns the shopping cart where groupId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1089             *
1090             * @param groupId the group ID
1091             * @param userId the user ID
1092             * @return the matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
1093             */
1094            @Override
1095            public ShoppingCart fetchByG_U(long groupId, long userId) {
1096                    return fetchByG_U(groupId, userId, true);
1097            }
1098    
1099            /**
1100             * Returns the shopping cart where groupId = &#63; and userId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1101             *
1102             * @param groupId the group ID
1103             * @param userId the user ID
1104             * @param retrieveFromCache whether to use the finder cache
1105             * @return the matching shopping cart, or <code>null</code> if a matching shopping cart could not be found
1106             */
1107            @Override
1108            public ShoppingCart fetchByG_U(long groupId, long userId,
1109                    boolean retrieveFromCache) {
1110                    Object[] finderArgs = new Object[] { groupId, userId };
1111    
1112                    Object result = null;
1113    
1114                    if (retrieveFromCache) {
1115                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_U,
1116                                            finderArgs, this);
1117                    }
1118    
1119                    if (result instanceof ShoppingCart) {
1120                            ShoppingCart shoppingCart = (ShoppingCart)result;
1121    
1122                            if ((groupId != shoppingCart.getGroupId()) ||
1123                                            (userId != shoppingCart.getUserId())) {
1124                                    result = null;
1125                            }
1126                    }
1127    
1128                    if (result == null) {
1129                            StringBundler query = new StringBundler(4);
1130    
1131                            query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
1132    
1133                            query.append(_FINDER_COLUMN_G_U_GROUPID_2);
1134    
1135                            query.append(_FINDER_COLUMN_G_U_USERID_2);
1136    
1137                            String sql = query.toString();
1138    
1139                            Session session = null;
1140    
1141                            try {
1142                                    session = openSession();
1143    
1144                                    Query q = session.createQuery(sql);
1145    
1146                                    QueryPos qPos = QueryPos.getInstance(q);
1147    
1148                                    qPos.add(groupId);
1149    
1150                                    qPos.add(userId);
1151    
1152                                    List<ShoppingCart> list = q.list();
1153    
1154                                    if (list.isEmpty()) {
1155                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
1156                                                    finderArgs, list);
1157                                    }
1158                                    else {
1159                                            ShoppingCart shoppingCart = list.get(0);
1160    
1161                                            result = shoppingCart;
1162    
1163                                            cacheResult(shoppingCart);
1164    
1165                                            if ((shoppingCart.getGroupId() != groupId) ||
1166                                                            (shoppingCart.getUserId() != userId)) {
1167                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
1168                                                            finderArgs, shoppingCart);
1169                                            }
1170                                    }
1171                            }
1172                            catch (Exception e) {
1173                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
1174                                            finderArgs);
1175    
1176                                    throw processException(e);
1177                            }
1178                            finally {
1179                                    closeSession(session);
1180                            }
1181                    }
1182    
1183                    if (result instanceof List<?>) {
1184                            return null;
1185                    }
1186                    else {
1187                            return (ShoppingCart)result;
1188                    }
1189            }
1190    
1191            /**
1192             * Removes the shopping cart where groupId = &#63; and userId = &#63; from the database.
1193             *
1194             * @param groupId the group ID
1195             * @param userId the user ID
1196             * @return the shopping cart that was removed
1197             */
1198            @Override
1199            public ShoppingCart removeByG_U(long groupId, long userId)
1200                    throws NoSuchCartException {
1201                    ShoppingCart shoppingCart = findByG_U(groupId, userId);
1202    
1203                    return remove(shoppingCart);
1204            }
1205    
1206            /**
1207             * Returns the number of shopping carts where groupId = &#63; and userId = &#63;.
1208             *
1209             * @param groupId the group ID
1210             * @param userId the user ID
1211             * @return the number of matching shopping carts
1212             */
1213            @Override
1214            public int countByG_U(long groupId, long userId) {
1215                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_U;
1216    
1217                    Object[] finderArgs = new Object[] { groupId, userId };
1218    
1219                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1220                                    this);
1221    
1222                    if (count == null) {
1223                            StringBundler query = new StringBundler(3);
1224    
1225                            query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1226    
1227                            query.append(_FINDER_COLUMN_G_U_GROUPID_2);
1228    
1229                            query.append(_FINDER_COLUMN_G_U_USERID_2);
1230    
1231                            String sql = query.toString();
1232    
1233                            Session session = null;
1234    
1235                            try {
1236                                    session = openSession();
1237    
1238                                    Query q = session.createQuery(sql);
1239    
1240                                    QueryPos qPos = QueryPos.getInstance(q);
1241    
1242                                    qPos.add(groupId);
1243    
1244                                    qPos.add(userId);
1245    
1246                                    count = (Long)q.uniqueResult();
1247    
1248                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1249                            }
1250                            catch (Exception e) {
1251                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1252    
1253                                    throw processException(e);
1254                            }
1255                            finally {
1256                                    closeSession(session);
1257                            }
1258                    }
1259    
1260                    return count.intValue();
1261            }
1262    
1263            private static final String _FINDER_COLUMN_G_U_GROUPID_2 = "shoppingCart.groupId = ? AND ";
1264            private static final String _FINDER_COLUMN_G_U_USERID_2 = "shoppingCart.userId = ?";
1265    
1266            public ShoppingCartPersistenceImpl() {
1267                    setModelClass(ShoppingCart.class);
1268            }
1269    
1270            /**
1271             * Caches the shopping cart in the entity cache if it is enabled.
1272             *
1273             * @param shoppingCart the shopping cart
1274             */
1275            @Override
1276            public void cacheResult(ShoppingCart shoppingCart) {
1277                    EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1278                            ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
1279    
1280                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
1281                            new Object[] { shoppingCart.getGroupId(), shoppingCart.getUserId() },
1282                            shoppingCart);
1283    
1284                    shoppingCart.resetOriginalValues();
1285            }
1286    
1287            /**
1288             * Caches the shopping carts in the entity cache if it is enabled.
1289             *
1290             * @param shoppingCarts the shopping carts
1291             */
1292            @Override
1293            public void cacheResult(List<ShoppingCart> shoppingCarts) {
1294                    for (ShoppingCart shoppingCart : shoppingCarts) {
1295                            if (EntityCacheUtil.getResult(
1296                                                    ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1297                                                    ShoppingCartImpl.class, shoppingCart.getPrimaryKey()) == null) {
1298                                    cacheResult(shoppingCart);
1299                            }
1300                            else {
1301                                    shoppingCart.resetOriginalValues();
1302                            }
1303                    }
1304            }
1305    
1306            /**
1307             * Clears the cache for all shopping carts.
1308             *
1309             * <p>
1310             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1311             * </p>
1312             */
1313            @Override
1314            public void clearCache() {
1315                    EntityCacheUtil.clearCache(ShoppingCartImpl.class);
1316    
1317                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1318                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1319                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1320            }
1321    
1322            /**
1323             * Clears the cache for the shopping cart.
1324             *
1325             * <p>
1326             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1327             * </p>
1328             */
1329            @Override
1330            public void clearCache(ShoppingCart shoppingCart) {
1331                    EntityCacheUtil.removeResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1332                            ShoppingCartImpl.class, shoppingCart.getPrimaryKey());
1333    
1334                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1335                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1336    
1337                    clearUniqueFindersCache(shoppingCart);
1338            }
1339    
1340            @Override
1341            public void clearCache(List<ShoppingCart> shoppingCarts) {
1342                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1343                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1344    
1345                    for (ShoppingCart shoppingCart : shoppingCarts) {
1346                            EntityCacheUtil.removeResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1347                                    ShoppingCartImpl.class, shoppingCart.getPrimaryKey());
1348    
1349                            clearUniqueFindersCache(shoppingCart);
1350                    }
1351            }
1352    
1353            protected void cacheUniqueFindersCache(ShoppingCart shoppingCart) {
1354                    if (shoppingCart.isNew()) {
1355                            Object[] args = new Object[] {
1356                                            shoppingCart.getGroupId(), shoppingCart.getUserId()
1357                                    };
1358    
1359                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_U, args,
1360                                    Long.valueOf(1));
1361                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U, args,
1362                                    shoppingCart);
1363                    }
1364                    else {
1365                            ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
1366    
1367                            if ((shoppingCartModelImpl.getColumnBitmask() &
1368                                            FINDER_PATH_FETCH_BY_G_U.getColumnBitmask()) != 0) {
1369                                    Object[] args = new Object[] {
1370                                                    shoppingCart.getGroupId(), shoppingCart.getUserId()
1371                                            };
1372    
1373                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_U, args,
1374                                            Long.valueOf(1));
1375                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U, args,
1376                                            shoppingCart);
1377                            }
1378                    }
1379            }
1380    
1381            protected void clearUniqueFindersCache(ShoppingCart shoppingCart) {
1382                    ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
1383    
1384                    Object[] args = new Object[] {
1385                                    shoppingCart.getGroupId(), shoppingCart.getUserId()
1386                            };
1387    
1388                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_U, args);
1389                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U, args);
1390    
1391                    if ((shoppingCartModelImpl.getColumnBitmask() &
1392                                    FINDER_PATH_FETCH_BY_G_U.getColumnBitmask()) != 0) {
1393                            args = new Object[] {
1394                                            shoppingCartModelImpl.getOriginalGroupId(),
1395                                            shoppingCartModelImpl.getOriginalUserId()
1396                                    };
1397    
1398                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_U, args);
1399                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U, args);
1400                    }
1401            }
1402    
1403            /**
1404             * Creates a new shopping cart with the primary key. Does not add the shopping cart to the database.
1405             *
1406             * @param cartId the primary key for the new shopping cart
1407             * @return the new shopping cart
1408             */
1409            @Override
1410            public ShoppingCart create(long cartId) {
1411                    ShoppingCart shoppingCart = new ShoppingCartImpl();
1412    
1413                    shoppingCart.setNew(true);
1414                    shoppingCart.setPrimaryKey(cartId);
1415    
1416                    return shoppingCart;
1417            }
1418    
1419            /**
1420             * Removes the shopping cart with the primary key from the database. Also notifies the appropriate model listeners.
1421             *
1422             * @param cartId the primary key of the shopping cart
1423             * @return the shopping cart that was removed
1424             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
1425             */
1426            @Override
1427            public ShoppingCart remove(long cartId) throws NoSuchCartException {
1428                    return remove((Serializable)cartId);
1429            }
1430    
1431            /**
1432             * Removes the shopping cart with the primary key from the database. Also notifies the appropriate model listeners.
1433             *
1434             * @param primaryKey the primary key of the shopping cart
1435             * @return the shopping cart that was removed
1436             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
1437             */
1438            @Override
1439            public ShoppingCart remove(Serializable primaryKey)
1440                    throws NoSuchCartException {
1441                    Session session = null;
1442    
1443                    try {
1444                            session = openSession();
1445    
1446                            ShoppingCart shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
1447                                            primaryKey);
1448    
1449                            if (shoppingCart == null) {
1450                                    if (_log.isWarnEnabled()) {
1451                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1452                                    }
1453    
1454                                    throw new NoSuchCartException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1455                                            primaryKey);
1456                            }
1457    
1458                            return remove(shoppingCart);
1459                    }
1460                    catch (NoSuchCartException nsee) {
1461                            throw nsee;
1462                    }
1463                    catch (Exception e) {
1464                            throw processException(e);
1465                    }
1466                    finally {
1467                            closeSession(session);
1468                    }
1469            }
1470    
1471            @Override
1472            protected ShoppingCart removeImpl(ShoppingCart shoppingCart) {
1473                    shoppingCart = toUnwrappedModel(shoppingCart);
1474    
1475                    Session session = null;
1476    
1477                    try {
1478                            session = openSession();
1479    
1480                            if (!session.contains(shoppingCart)) {
1481                                    shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
1482                                                    shoppingCart.getPrimaryKeyObj());
1483                            }
1484    
1485                            if (shoppingCart != null) {
1486                                    session.delete(shoppingCart);
1487                            }
1488                    }
1489                    catch (Exception e) {
1490                            throw processException(e);
1491                    }
1492                    finally {
1493                            closeSession(session);
1494                    }
1495    
1496                    if (shoppingCart != null) {
1497                            clearCache(shoppingCart);
1498                    }
1499    
1500                    return shoppingCart;
1501            }
1502    
1503            @Override
1504            public ShoppingCart updateImpl(ShoppingCart shoppingCart) {
1505                    shoppingCart = toUnwrappedModel(shoppingCart);
1506    
1507                    boolean isNew = shoppingCart.isNew();
1508    
1509                    ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
1510    
1511                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
1512    
1513                    Date now = new Date();
1514    
1515                    if (isNew && (shoppingCart.getCreateDate() == null)) {
1516                            if (serviceContext == null) {
1517                                    shoppingCart.setCreateDate(now);
1518                            }
1519                            else {
1520                                    shoppingCart.setCreateDate(serviceContext.getCreateDate(now));
1521                            }
1522                    }
1523    
1524                    if (!shoppingCartModelImpl.hasSetModifiedDate()) {
1525                            if (serviceContext == null) {
1526                                    shoppingCart.setModifiedDate(now);
1527                            }
1528                            else {
1529                                    shoppingCart.setModifiedDate(serviceContext.getModifiedDate(now));
1530                            }
1531                    }
1532    
1533                    Session session = null;
1534    
1535                    try {
1536                            session = openSession();
1537    
1538                            if (shoppingCart.isNew()) {
1539                                    session.save(shoppingCart);
1540    
1541                                    shoppingCart.setNew(false);
1542                            }
1543                            else {
1544                                    session.merge(shoppingCart);
1545                            }
1546                    }
1547                    catch (Exception e) {
1548                            throw processException(e);
1549                    }
1550                    finally {
1551                            closeSession(session);
1552                    }
1553    
1554                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1555    
1556                    if (isNew || !ShoppingCartModelImpl.COLUMN_BITMASK_ENABLED) {
1557                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1558                    }
1559    
1560                    else {
1561                            if ((shoppingCartModelImpl.getColumnBitmask() &
1562                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
1563                                    Object[] args = new Object[] {
1564                                                    shoppingCartModelImpl.getOriginalGroupId()
1565                                            };
1566    
1567                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1568                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1569                                            args);
1570    
1571                                    args = new Object[] { shoppingCartModelImpl.getGroupId() };
1572    
1573                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1574                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1575                                            args);
1576                            }
1577    
1578                            if ((shoppingCartModelImpl.getColumnBitmask() &
1579                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
1580                                    Object[] args = new Object[] {
1581                                                    shoppingCartModelImpl.getOriginalUserId()
1582                                            };
1583    
1584                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
1585                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
1586                                            args);
1587    
1588                                    args = new Object[] { shoppingCartModelImpl.getUserId() };
1589    
1590                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
1591                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
1592                                            args);
1593                            }
1594                    }
1595    
1596                    EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1597                            ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart,
1598                            false);
1599    
1600                    clearUniqueFindersCache(shoppingCart);
1601                    cacheUniqueFindersCache(shoppingCart);
1602    
1603                    shoppingCart.resetOriginalValues();
1604    
1605                    return shoppingCart;
1606            }
1607    
1608            protected ShoppingCart toUnwrappedModel(ShoppingCart shoppingCart) {
1609                    if (shoppingCart instanceof ShoppingCartImpl) {
1610                            return shoppingCart;
1611                    }
1612    
1613                    ShoppingCartImpl shoppingCartImpl = new ShoppingCartImpl();
1614    
1615                    shoppingCartImpl.setNew(shoppingCart.isNew());
1616                    shoppingCartImpl.setPrimaryKey(shoppingCart.getPrimaryKey());
1617    
1618                    shoppingCartImpl.setCartId(shoppingCart.getCartId());
1619                    shoppingCartImpl.setGroupId(shoppingCart.getGroupId());
1620                    shoppingCartImpl.setCompanyId(shoppingCart.getCompanyId());
1621                    shoppingCartImpl.setUserId(shoppingCart.getUserId());
1622                    shoppingCartImpl.setUserName(shoppingCart.getUserName());
1623                    shoppingCartImpl.setCreateDate(shoppingCart.getCreateDate());
1624                    shoppingCartImpl.setModifiedDate(shoppingCart.getModifiedDate());
1625                    shoppingCartImpl.setItemIds(shoppingCart.getItemIds());
1626                    shoppingCartImpl.setCouponCodes(shoppingCart.getCouponCodes());
1627                    shoppingCartImpl.setAltShipping(shoppingCart.getAltShipping());
1628                    shoppingCartImpl.setInsure(shoppingCart.isInsure());
1629    
1630                    return shoppingCartImpl;
1631            }
1632    
1633            /**
1634             * Returns the shopping cart with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1635             *
1636             * @param primaryKey the primary key of the shopping cart
1637             * @return the shopping cart
1638             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
1639             */
1640            @Override
1641            public ShoppingCart findByPrimaryKey(Serializable primaryKey)
1642                    throws NoSuchCartException {
1643                    ShoppingCart shoppingCart = fetchByPrimaryKey(primaryKey);
1644    
1645                    if (shoppingCart == null) {
1646                            if (_log.isWarnEnabled()) {
1647                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1648                            }
1649    
1650                            throw new NoSuchCartException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1651                                    primaryKey);
1652                    }
1653    
1654                    return shoppingCart;
1655            }
1656    
1657            /**
1658             * Returns the shopping cart with the primary key or throws a {@link NoSuchCartException} if it could not be found.
1659             *
1660             * @param cartId the primary key of the shopping cart
1661             * @return the shopping cart
1662             * @throws NoSuchCartException if a shopping cart with the primary key could not be found
1663             */
1664            @Override
1665            public ShoppingCart findByPrimaryKey(long cartId)
1666                    throws NoSuchCartException {
1667                    return findByPrimaryKey((Serializable)cartId);
1668            }
1669    
1670            /**
1671             * Returns the shopping cart with the primary key or returns <code>null</code> if it could not be found.
1672             *
1673             * @param primaryKey the primary key of the shopping cart
1674             * @return the shopping cart, or <code>null</code> if a shopping cart with the primary key could not be found
1675             */
1676            @Override
1677            public ShoppingCart fetchByPrimaryKey(Serializable primaryKey) {
1678                    ShoppingCart shoppingCart = (ShoppingCart)EntityCacheUtil.getResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1679                                    ShoppingCartImpl.class, primaryKey);
1680    
1681                    if (shoppingCart == _nullShoppingCart) {
1682                            return null;
1683                    }
1684    
1685                    if (shoppingCart == null) {
1686                            Session session = null;
1687    
1688                            try {
1689                                    session = openSession();
1690    
1691                                    shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
1692                                                    primaryKey);
1693    
1694                                    if (shoppingCart != null) {
1695                                            cacheResult(shoppingCart);
1696                                    }
1697                                    else {
1698                                            EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1699                                                    ShoppingCartImpl.class, primaryKey, _nullShoppingCart);
1700                                    }
1701                            }
1702                            catch (Exception e) {
1703                                    EntityCacheUtil.removeResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1704                                            ShoppingCartImpl.class, primaryKey);
1705    
1706                                    throw processException(e);
1707                            }
1708                            finally {
1709                                    closeSession(session);
1710                            }
1711                    }
1712    
1713                    return shoppingCart;
1714            }
1715    
1716            /**
1717             * Returns the shopping cart with the primary key or returns <code>null</code> if it could not be found.
1718             *
1719             * @param cartId the primary key of the shopping cart
1720             * @return the shopping cart, or <code>null</code> if a shopping cart with the primary key could not be found
1721             */
1722            @Override
1723            public ShoppingCart fetchByPrimaryKey(long cartId) {
1724                    return fetchByPrimaryKey((Serializable)cartId);
1725            }
1726    
1727            @Override
1728            public Map<Serializable, ShoppingCart> fetchByPrimaryKeys(
1729                    Set<Serializable> primaryKeys) {
1730                    if (primaryKeys.isEmpty()) {
1731                            return Collections.emptyMap();
1732                    }
1733    
1734                    Map<Serializable, ShoppingCart> map = new HashMap<Serializable, ShoppingCart>();
1735    
1736                    if (primaryKeys.size() == 1) {
1737                            Iterator<Serializable> iterator = primaryKeys.iterator();
1738    
1739                            Serializable primaryKey = iterator.next();
1740    
1741                            ShoppingCart shoppingCart = fetchByPrimaryKey(primaryKey);
1742    
1743                            if (shoppingCart != null) {
1744                                    map.put(primaryKey, shoppingCart);
1745                            }
1746    
1747                            return map;
1748                    }
1749    
1750                    Set<Serializable> uncachedPrimaryKeys = null;
1751    
1752                    for (Serializable primaryKey : primaryKeys) {
1753                            ShoppingCart shoppingCart = (ShoppingCart)EntityCacheUtil.getResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1754                                            ShoppingCartImpl.class, primaryKey);
1755    
1756                            if (shoppingCart == null) {
1757                                    if (uncachedPrimaryKeys == null) {
1758                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1759                                    }
1760    
1761                                    uncachedPrimaryKeys.add(primaryKey);
1762                            }
1763                            else {
1764                                    map.put(primaryKey, shoppingCart);
1765                            }
1766                    }
1767    
1768                    if (uncachedPrimaryKeys == null) {
1769                            return map;
1770                    }
1771    
1772                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1773                                    1);
1774    
1775                    query.append(_SQL_SELECT_SHOPPINGCART_WHERE_PKS_IN);
1776    
1777                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1778                            query.append(String.valueOf(primaryKey));
1779    
1780                            query.append(StringPool.COMMA);
1781                    }
1782    
1783                    query.setIndex(query.index() - 1);
1784    
1785                    query.append(StringPool.CLOSE_PARENTHESIS);
1786    
1787                    String sql = query.toString();
1788    
1789                    Session session = null;
1790    
1791                    try {
1792                            session = openSession();
1793    
1794                            Query q = session.createQuery(sql);
1795    
1796                            for (ShoppingCart shoppingCart : (List<ShoppingCart>)q.list()) {
1797                                    map.put(shoppingCart.getPrimaryKeyObj(), shoppingCart);
1798    
1799                                    cacheResult(shoppingCart);
1800    
1801                                    uncachedPrimaryKeys.remove(shoppingCart.getPrimaryKeyObj());
1802                            }
1803    
1804                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1805                                    EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
1806                                            ShoppingCartImpl.class, primaryKey, _nullShoppingCart);
1807                            }
1808                    }
1809                    catch (Exception e) {
1810                            throw processException(e);
1811                    }
1812                    finally {
1813                            closeSession(session);
1814                    }
1815    
1816                    return map;
1817            }
1818    
1819            /**
1820             * Returns all the shopping carts.
1821             *
1822             * @return the shopping carts
1823             */
1824            @Override
1825            public List<ShoppingCart> findAll() {
1826                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1827            }
1828    
1829            /**
1830             * Returns a range of all the shopping carts.
1831             *
1832             * <p>
1833             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1834             * </p>
1835             *
1836             * @param start the lower bound of the range of shopping carts
1837             * @param end the upper bound of the range of shopping carts (not inclusive)
1838             * @return the range of shopping carts
1839             */
1840            @Override
1841            public List<ShoppingCart> findAll(int start, int end) {
1842                    return findAll(start, end, null);
1843            }
1844    
1845            /**
1846             * Returns an ordered range of all the shopping carts.
1847             *
1848             * <p>
1849             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link ShoppingCartModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1850             * </p>
1851             *
1852             * @param start the lower bound of the range of shopping carts
1853             * @param end the upper bound of the range of shopping carts (not inclusive)
1854             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1855             * @return the ordered range of shopping carts
1856             */
1857            @Override
1858            public List<ShoppingCart> findAll(int start, int end,
1859                    OrderByComparator<ShoppingCart> orderByComparator) {
1860                    boolean pagination = true;
1861                    FinderPath finderPath = null;
1862                    Object[] finderArgs = null;
1863    
1864                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1865                                    (orderByComparator == null)) {
1866                            pagination = false;
1867                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1868                            finderArgs = FINDER_ARGS_EMPTY;
1869                    }
1870                    else {
1871                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1872                            finderArgs = new Object[] { start, end, orderByComparator };
1873                    }
1874    
1875                    List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(finderPath,
1876                                    finderArgs, this);
1877    
1878                    if (list == null) {
1879                            StringBundler query = null;
1880                            String sql = null;
1881    
1882                            if (orderByComparator != null) {
1883                                    query = new StringBundler(2 +
1884                                                    (orderByComparator.getOrderByFields().length * 3));
1885    
1886                                    query.append(_SQL_SELECT_SHOPPINGCART);
1887    
1888                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1889                                            orderByComparator);
1890    
1891                                    sql = query.toString();
1892                            }
1893                            else {
1894                                    sql = _SQL_SELECT_SHOPPINGCART;
1895    
1896                                    if (pagination) {
1897                                            sql = sql.concat(ShoppingCartModelImpl.ORDER_BY_JPQL);
1898                                    }
1899                            }
1900    
1901                            Session session = null;
1902    
1903                            try {
1904                                    session = openSession();
1905    
1906                                    Query q = session.createQuery(sql);
1907    
1908                                    if (!pagination) {
1909                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1910                                                            start, end, false);
1911    
1912                                            Collections.sort(list);
1913    
1914                                            list = Collections.unmodifiableList(list);
1915                                    }
1916                                    else {
1917                                            list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1918                                                            start, end);
1919                                    }
1920    
1921                                    cacheResult(list);
1922    
1923                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1924                            }
1925                            catch (Exception e) {
1926                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1927    
1928                                    throw processException(e);
1929                            }
1930                            finally {
1931                                    closeSession(session);
1932                            }
1933                    }
1934    
1935                    return list;
1936            }
1937    
1938            /**
1939             * Removes all the shopping carts from the database.
1940             *
1941             */
1942            @Override
1943            public void removeAll() {
1944                    for (ShoppingCart shoppingCart : findAll()) {
1945                            remove(shoppingCart);
1946                    }
1947            }
1948    
1949            /**
1950             * Returns the number of shopping carts.
1951             *
1952             * @return the number of shopping carts
1953             */
1954            @Override
1955            public int countAll() {
1956                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1957                                    FINDER_ARGS_EMPTY, this);
1958    
1959                    if (count == null) {
1960                            Session session = null;
1961    
1962                            try {
1963                                    session = openSession();
1964    
1965                                    Query q = session.createQuery(_SQL_COUNT_SHOPPINGCART);
1966    
1967                                    count = (Long)q.uniqueResult();
1968    
1969                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1970                                            FINDER_ARGS_EMPTY, count);
1971                            }
1972                            catch (Exception e) {
1973                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1974                                            FINDER_ARGS_EMPTY);
1975    
1976                                    throw processException(e);
1977                            }
1978                            finally {
1979                                    closeSession(session);
1980                            }
1981                    }
1982    
1983                    return count.intValue();
1984            }
1985    
1986            /**
1987             * Initializes the shopping cart persistence.
1988             */
1989            public void afterPropertiesSet() {
1990            }
1991    
1992            public void destroy() {
1993                    EntityCacheUtil.removeCache(ShoppingCartImpl.class.getName());
1994                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1995                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1996                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1997            }
1998    
1999            private static final String _SQL_SELECT_SHOPPINGCART = "SELECT shoppingCart FROM ShoppingCart shoppingCart";
2000            private static final String _SQL_SELECT_SHOPPINGCART_WHERE_PKS_IN = "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE cartId IN (";
2001            private static final String _SQL_SELECT_SHOPPINGCART_WHERE = "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ";
2002            private static final String _SQL_COUNT_SHOPPINGCART = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart";
2003            private static final String _SQL_COUNT_SHOPPINGCART_WHERE = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart WHERE ";
2004            private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCart.";
2005            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCart exists with the primary key ";
2006            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCart exists with the key {";
2007            private static final Log _log = LogFactoryUtil.getLog(ShoppingCartPersistenceImpl.class);
2008            private static final ShoppingCart _nullShoppingCart = new ShoppingCartImpl() {
2009                            @Override
2010                            public Object clone() {
2011                                    return this;
2012                            }
2013    
2014                            @Override
2015                            public CacheModel<ShoppingCart> toCacheModel() {
2016                                    return _nullShoppingCartCacheModel;
2017                            }
2018                    };
2019    
2020            private static final CacheModel<ShoppingCart> _nullShoppingCartCacheModel = new CacheModel<ShoppingCart>() {
2021                            @Override
2022                            public ShoppingCart toEntityModel() {
2023                                    return _nullShoppingCart;
2024                            }
2025                    };
2026    }