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