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