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