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