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