1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.shopping.service.persistence;
24  
25  import com.liferay.portal.NoSuchModelException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.annotation.BeanReference;
28  import com.liferay.portal.kernel.cache.CacheRegistry;
29  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32  import com.liferay.portal.kernel.dao.orm.FinderPath;
33  import com.liferay.portal.kernel.dao.orm.Query;
34  import com.liferay.portal.kernel.dao.orm.QueryPos;
35  import com.liferay.portal.kernel.dao.orm.QueryUtil;
36  import com.liferay.portal.kernel.dao.orm.Session;
37  import com.liferay.portal.kernel.log.Log;
38  import com.liferay.portal.kernel.log.LogFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.StringBundler;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.model.ModelListener;
45  import com.liferay.portal.service.persistence.BatchSessionUtil;
46  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
47  
48  import com.liferay.portlet.shopping.NoSuchCartException;
49  import com.liferay.portlet.shopping.model.ShoppingCart;
50  import com.liferay.portlet.shopping.model.impl.ShoppingCartImpl;
51  import com.liferay.portlet.shopping.model.impl.ShoppingCartModelImpl;
52  
53  import java.io.Serializable;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.List;
58  
59  /**
60   * <a href="ShoppingCartPersistenceImpl.java.html"><b><i>View Source</i></b></a>
61   *
62   * <p>
63   * ServiceBuilder generated this class. Modifications in this class will be
64   * overwritten the next time is generated.
65   * </p>
66   *
67   * @author    Brian Wing Shun Chan
68   * @see       ShoppingCartPersistence
69   * @see       ShoppingCartUtil
70   * @generated
71   */
72  public class ShoppingCartPersistenceImpl extends BasePersistenceImpl<ShoppingCart>
73      implements ShoppingCartPersistence {
74      public static final String FINDER_CLASS_NAME_ENTITY = ShoppingCartImpl.class.getName();
75      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
76          ".List";
77      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
78              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79              "findByGroupId", new String[] { Long.class.getName() });
80      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
81              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
82              "findByGroupId",
83              new String[] {
84                  Long.class.getName(),
85                  
86              "java.lang.Integer", "java.lang.Integer",
87                  "com.liferay.portal.kernel.util.OrderByComparator"
88              });
89      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
90              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
91              "countByGroupId", new String[] { Long.class.getName() });
92      public static final FinderPath FINDER_PATH_FIND_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
93              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
94              "findByUserId", new String[] { Long.class.getName() });
95      public static final FinderPath FINDER_PATH_FIND_BY_OBC_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
96              ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
97              "findByUserId",
98              new String[] {
99                  Long.class.getName(),
100                 
101             "java.lang.Integer", "java.lang.Integer",
102                 "com.liferay.portal.kernel.util.OrderByComparator"
103             });
104     public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
105             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
106             "countByUserId", new String[] { Long.class.getName() });
107     public static final FinderPath FINDER_PATH_FETCH_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
108             ShoppingCartModelImpl.FINDER_CACHE_ENABLED,
109             FINDER_CLASS_NAME_ENTITY, "fetchByG_U",
110             new String[] { Long.class.getName(), Long.class.getName() });
111     public static final FinderPath FINDER_PATH_COUNT_BY_G_U = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
112             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113             "countByG_U",
114             new String[] { Long.class.getName(), Long.class.getName() });
115     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
116             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
117             "findAll", new String[0]);
118     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
119             ShoppingCartModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
120             "countAll", new String[0]);
121 
122     public void cacheResult(ShoppingCart shoppingCart) {
123         EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
124             ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
125 
126         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
127             new Object[] {
128                 new Long(shoppingCart.getGroupId()),
129                 new Long(shoppingCart.getUserId())
130             }, shoppingCart);
131     }
132 
133     public void cacheResult(List<ShoppingCart> shoppingCarts) {
134         for (ShoppingCart shoppingCart : shoppingCarts) {
135             if (EntityCacheUtil.getResult(
136                         ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
137                         ShoppingCartImpl.class, shoppingCart.getPrimaryKey(),
138                         this) == null) {
139                 cacheResult(shoppingCart);
140             }
141         }
142     }
143 
144     public void clearCache() {
145         CacheRegistry.clear(ShoppingCartImpl.class.getName());
146         EntityCacheUtil.clearCache(ShoppingCartImpl.class.getName());
147         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
148         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
149     }
150 
151     public ShoppingCart create(long cartId) {
152         ShoppingCart shoppingCart = new ShoppingCartImpl();
153 
154         shoppingCart.setNew(true);
155         shoppingCart.setPrimaryKey(cartId);
156 
157         return shoppingCart;
158     }
159 
160     public ShoppingCart remove(Serializable primaryKey)
161         throws NoSuchModelException, SystemException {
162         return remove(((Long)primaryKey).longValue());
163     }
164 
165     public ShoppingCart remove(long cartId)
166         throws NoSuchCartException, SystemException {
167         Session session = null;
168 
169         try {
170             session = openSession();
171 
172             ShoppingCart shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
173                     new Long(cartId));
174 
175             if (shoppingCart == null) {
176                 if (_log.isWarnEnabled()) {
177                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + cartId);
178                 }
179 
180                 throw new NoSuchCartException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
181                     cartId);
182             }
183 
184             return remove(shoppingCart);
185         }
186         catch (NoSuchCartException nsee) {
187             throw nsee;
188         }
189         catch (Exception e) {
190             throw processException(e);
191         }
192         finally {
193             closeSession(session);
194         }
195     }
196 
197     public ShoppingCart remove(ShoppingCart shoppingCart)
198         throws SystemException {
199         for (ModelListener<ShoppingCart> listener : listeners) {
200             listener.onBeforeRemove(shoppingCart);
201         }
202 
203         shoppingCart = removeImpl(shoppingCart);
204 
205         for (ModelListener<ShoppingCart> listener : listeners) {
206             listener.onAfterRemove(shoppingCart);
207         }
208 
209         return shoppingCart;
210     }
211 
212     protected ShoppingCart removeImpl(ShoppingCart shoppingCart)
213         throws SystemException {
214         shoppingCart = toUnwrappedModel(shoppingCart);
215 
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             if (shoppingCart.isCachedModel() || BatchSessionUtil.isEnabled()) {
222                 Object staleObject = session.get(ShoppingCartImpl.class,
223                         shoppingCart.getPrimaryKeyObj());
224 
225                 if (staleObject != null) {
226                     session.evict(staleObject);
227                 }
228             }
229 
230             session.delete(shoppingCart);
231 
232             session.flush();
233         }
234         catch (Exception e) {
235             throw processException(e);
236         }
237         finally {
238             closeSession(session);
239         }
240 
241         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
242 
243         ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
244 
245         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
246             new Object[] {
247                 new Long(shoppingCartModelImpl.getOriginalGroupId()),
248                 new Long(shoppingCartModelImpl.getOriginalUserId())
249             });
250 
251         EntityCacheUtil.removeResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
252             ShoppingCartImpl.class, shoppingCart.getPrimaryKey());
253 
254         return shoppingCart;
255     }
256 
257     /**
258      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
259      */
260     public ShoppingCart update(ShoppingCart shoppingCart)
261         throws SystemException {
262         if (_log.isWarnEnabled()) {
263             _log.warn(
264                 "Using the deprecated update(ShoppingCart shoppingCart) method. Use update(ShoppingCart shoppingCart, boolean merge) instead.");
265         }
266 
267         return update(shoppingCart, false);
268     }
269 
270     public ShoppingCart updateImpl(
271         com.liferay.portlet.shopping.model.ShoppingCart shoppingCart,
272         boolean merge) throws SystemException {
273         shoppingCart = toUnwrappedModel(shoppingCart);
274 
275         boolean isNew = shoppingCart.isNew();
276 
277         ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
278 
279         Session session = null;
280 
281         try {
282             session = openSession();
283 
284             BatchSessionUtil.update(session, shoppingCart, merge);
285 
286             shoppingCart.setNew(false);
287         }
288         catch (Exception e) {
289             throw processException(e);
290         }
291         finally {
292             closeSession(session);
293         }
294 
295         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
296 
297         EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
298             ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
299 
300         if (!isNew &&
301                 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
302                 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
303             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
304                 new Object[] {
305                     new Long(shoppingCartModelImpl.getOriginalGroupId()),
306                     new Long(shoppingCartModelImpl.getOriginalUserId())
307                 });
308         }
309 
310         if (isNew ||
311                 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
312                 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
313             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
314                 new Object[] {
315                     new Long(shoppingCart.getGroupId()),
316                     new Long(shoppingCart.getUserId())
317                 }, shoppingCart);
318         }
319 
320         return shoppingCart;
321     }
322 
323     protected ShoppingCart toUnwrappedModel(ShoppingCart shoppingCart) {
324         if (shoppingCart instanceof ShoppingCartImpl) {
325             return shoppingCart;
326         }
327 
328         ShoppingCartImpl shoppingCartImpl = new ShoppingCartImpl();
329 
330         shoppingCartImpl.setNew(shoppingCart.isNew());
331         shoppingCartImpl.setPrimaryKey(shoppingCart.getPrimaryKey());
332 
333         shoppingCartImpl.setCartId(shoppingCart.getCartId());
334         shoppingCartImpl.setGroupId(shoppingCart.getGroupId());
335         shoppingCartImpl.setCompanyId(shoppingCart.getCompanyId());
336         shoppingCartImpl.setUserId(shoppingCart.getUserId());
337         shoppingCartImpl.setUserName(shoppingCart.getUserName());
338         shoppingCartImpl.setCreateDate(shoppingCart.getCreateDate());
339         shoppingCartImpl.setModifiedDate(shoppingCart.getModifiedDate());
340         shoppingCartImpl.setItemIds(shoppingCart.getItemIds());
341         shoppingCartImpl.setCouponCodes(shoppingCart.getCouponCodes());
342         shoppingCartImpl.setAltShipping(shoppingCart.getAltShipping());
343         shoppingCartImpl.setInsure(shoppingCart.isInsure());
344 
345         return shoppingCartImpl;
346     }
347 
348     public ShoppingCart findByPrimaryKey(Serializable primaryKey)
349         throws NoSuchModelException, SystemException {
350         return findByPrimaryKey(((Long)primaryKey).longValue());
351     }
352 
353     public ShoppingCart findByPrimaryKey(long cartId)
354         throws NoSuchCartException, SystemException {
355         ShoppingCart shoppingCart = fetchByPrimaryKey(cartId);
356 
357         if (shoppingCart == null) {
358             if (_log.isWarnEnabled()) {
359                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + cartId);
360             }
361 
362             throw new NoSuchCartException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
363                 cartId);
364         }
365 
366         return shoppingCart;
367     }
368 
369     public ShoppingCart fetchByPrimaryKey(Serializable primaryKey)
370         throws SystemException {
371         return fetchByPrimaryKey(((Long)primaryKey).longValue());
372     }
373 
374     public ShoppingCart fetchByPrimaryKey(long cartId)
375         throws SystemException {
376         ShoppingCart shoppingCart = (ShoppingCart)EntityCacheUtil.getResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
377                 ShoppingCartImpl.class, cartId, this);
378 
379         if (shoppingCart == null) {
380             Session session = null;
381 
382             try {
383                 session = openSession();
384 
385                 shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
386                         new Long(cartId));
387             }
388             catch (Exception e) {
389                 throw processException(e);
390             }
391             finally {
392                 if (shoppingCart != null) {
393                     cacheResult(shoppingCart);
394                 }
395 
396                 closeSession(session);
397             }
398         }
399 
400         return shoppingCart;
401     }
402 
403     public List<ShoppingCart> findByGroupId(long groupId)
404         throws SystemException {
405         Object[] finderArgs = new Object[] { new Long(groupId) };
406 
407         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
408                 finderArgs, this);
409 
410         if (list == null) {
411             Session session = null;
412 
413             try {
414                 session = openSession();
415 
416                 StringBundler query = new StringBundler(2);
417 
418                 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
419 
420                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
421 
422                 String sql = query.toString();
423 
424                 Query q = session.createQuery(sql);
425 
426                 QueryPos qPos = QueryPos.getInstance(q);
427 
428                 qPos.add(groupId);
429 
430                 list = q.list();
431             }
432             catch (Exception e) {
433                 throw processException(e);
434             }
435             finally {
436                 if (list == null) {
437                     list = new ArrayList<ShoppingCart>();
438                 }
439 
440                 cacheResult(list);
441 
442                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
443                     finderArgs, list);
444 
445                 closeSession(session);
446             }
447         }
448 
449         return list;
450     }
451 
452     public List<ShoppingCart> findByGroupId(long groupId, int start, int end)
453         throws SystemException {
454         return findByGroupId(groupId, start, end, null);
455     }
456 
457     public List<ShoppingCart> findByGroupId(long groupId, int start, int end,
458         OrderByComparator obc) throws SystemException {
459         Object[] finderArgs = new Object[] {
460                 new Long(groupId),
461                 
462                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
463             };
464 
465         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
466                 finderArgs, this);
467 
468         if (list == null) {
469             Session session = null;
470 
471             try {
472                 session = openSession();
473 
474                 StringBundler query = null;
475 
476                 if (obc != null) {
477                     query = new StringBundler(3 +
478                             (obc.getOrderByFields().length * 3));
479                 }
480                 else {
481                     query = new StringBundler(2);
482                 }
483 
484                 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
485 
486                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
487 
488                 if (obc != null) {
489                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
490                 }
491 
492                 String sql = query.toString();
493 
494                 Query q = session.createQuery(sql);
495 
496                 QueryPos qPos = QueryPos.getInstance(q);
497 
498                 qPos.add(groupId);
499 
500                 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
501                         start, end);
502             }
503             catch (Exception e) {
504                 throw processException(e);
505             }
506             finally {
507                 if (list == null) {
508                     list = new ArrayList<ShoppingCart>();
509                 }
510 
511                 cacheResult(list);
512 
513                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
514                     finderArgs, list);
515 
516                 closeSession(session);
517             }
518         }
519 
520         return list;
521     }
522 
523     public ShoppingCart findByGroupId_First(long groupId, OrderByComparator obc)
524         throws NoSuchCartException, SystemException {
525         List<ShoppingCart> list = findByGroupId(groupId, 0, 1, obc);
526 
527         if (list.isEmpty()) {
528             StringBundler msg = new StringBundler(4);
529 
530             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
531 
532             msg.append("groupId=");
533             msg.append(groupId);
534 
535             msg.append(StringPool.CLOSE_CURLY_BRACE);
536 
537             throw new NoSuchCartException(msg.toString());
538         }
539         else {
540             return list.get(0);
541         }
542     }
543 
544     public ShoppingCart findByGroupId_Last(long groupId, OrderByComparator obc)
545         throws NoSuchCartException, SystemException {
546         int count = countByGroupId(groupId);
547 
548         List<ShoppingCart> list = findByGroupId(groupId, count - 1, count, obc);
549 
550         if (list.isEmpty()) {
551             StringBundler msg = new StringBundler(4);
552 
553             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
554 
555             msg.append("groupId=");
556             msg.append(groupId);
557 
558             msg.append(StringPool.CLOSE_CURLY_BRACE);
559 
560             throw new NoSuchCartException(msg.toString());
561         }
562         else {
563             return list.get(0);
564         }
565     }
566 
567     public ShoppingCart[] findByGroupId_PrevAndNext(long cartId, long groupId,
568         OrderByComparator obc) throws NoSuchCartException, SystemException {
569         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
570 
571         int count = countByGroupId(groupId);
572 
573         Session session = null;
574 
575         try {
576             session = openSession();
577 
578             StringBundler query = null;
579 
580             if (obc != null) {
581                 query = new StringBundler(3 +
582                         (obc.getOrderByFields().length * 3));
583             }
584             else {
585                 query = new StringBundler(2);
586             }
587 
588             query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
589 
590             query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
591 
592             if (obc != null) {
593                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
594             }
595 
596             String sql = query.toString();
597 
598             Query q = session.createQuery(sql);
599 
600             QueryPos qPos = QueryPos.getInstance(q);
601 
602             qPos.add(groupId);
603 
604             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
605                     shoppingCart);
606 
607             ShoppingCart[] array = new ShoppingCartImpl[3];
608 
609             array[0] = (ShoppingCart)objArray[0];
610             array[1] = (ShoppingCart)objArray[1];
611             array[2] = (ShoppingCart)objArray[2];
612 
613             return array;
614         }
615         catch (Exception e) {
616             throw processException(e);
617         }
618         finally {
619             closeSession(session);
620         }
621     }
622 
623     public List<ShoppingCart> findByUserId(long userId)
624         throws SystemException {
625         Object[] finderArgs = new Object[] { new Long(userId) };
626 
627         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
628                 finderArgs, this);
629 
630         if (list == null) {
631             Session session = null;
632 
633             try {
634                 session = openSession();
635 
636                 StringBundler query = new StringBundler(2);
637 
638                 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
639 
640                 query.append(_FINDER_COLUMN_USERID_USERID_2);
641 
642                 String sql = query.toString();
643 
644                 Query q = session.createQuery(sql);
645 
646                 QueryPos qPos = QueryPos.getInstance(q);
647 
648                 qPos.add(userId);
649 
650                 list = q.list();
651             }
652             catch (Exception e) {
653                 throw processException(e);
654             }
655             finally {
656                 if (list == null) {
657                     list = new ArrayList<ShoppingCart>();
658                 }
659 
660                 cacheResult(list);
661 
662                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
663                     finderArgs, list);
664 
665                 closeSession(session);
666             }
667         }
668 
669         return list;
670     }
671 
672     public List<ShoppingCart> findByUserId(long userId, int start, int end)
673         throws SystemException {
674         return findByUserId(userId, start, end, null);
675     }
676 
677     public List<ShoppingCart> findByUserId(long userId, int start, int end,
678         OrderByComparator obc) throws SystemException {
679         Object[] finderArgs = new Object[] {
680                 new Long(userId),
681                 
682                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
683             };
684 
685         List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_USERID,
686                 finderArgs, this);
687 
688         if (list == null) {
689             Session session = null;
690 
691             try {
692                 session = openSession();
693 
694                 StringBundler query = null;
695 
696                 if (obc != null) {
697                     query = new StringBundler(3 +
698                             (obc.getOrderByFields().length * 3));
699                 }
700                 else {
701                     query = new StringBundler(2);
702                 }
703 
704                 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
705 
706                 query.append(_FINDER_COLUMN_USERID_USERID_2);
707 
708                 if (obc != null) {
709                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
710                 }
711 
712                 String sql = query.toString();
713 
714                 Query q = session.createQuery(sql);
715 
716                 QueryPos qPos = QueryPos.getInstance(q);
717 
718                 qPos.add(userId);
719 
720                 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
721                         start, end);
722             }
723             catch (Exception e) {
724                 throw processException(e);
725             }
726             finally {
727                 if (list == null) {
728                     list = new ArrayList<ShoppingCart>();
729                 }
730 
731                 cacheResult(list);
732 
733                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_USERID,
734                     finderArgs, list);
735 
736                 closeSession(session);
737             }
738         }
739 
740         return list;
741     }
742 
743     public ShoppingCart findByUserId_First(long userId, OrderByComparator obc)
744         throws NoSuchCartException, SystemException {
745         List<ShoppingCart> list = findByUserId(userId, 0, 1, obc);
746 
747         if (list.isEmpty()) {
748             StringBundler msg = new StringBundler(4);
749 
750             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
751 
752             msg.append("userId=");
753             msg.append(userId);
754 
755             msg.append(StringPool.CLOSE_CURLY_BRACE);
756 
757             throw new NoSuchCartException(msg.toString());
758         }
759         else {
760             return list.get(0);
761         }
762     }
763 
764     public ShoppingCart findByUserId_Last(long userId, OrderByComparator obc)
765         throws NoSuchCartException, SystemException {
766         int count = countByUserId(userId);
767 
768         List<ShoppingCart> list = findByUserId(userId, count - 1, count, obc);
769 
770         if (list.isEmpty()) {
771             StringBundler msg = new StringBundler(4);
772 
773             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
774 
775             msg.append("userId=");
776             msg.append(userId);
777 
778             msg.append(StringPool.CLOSE_CURLY_BRACE);
779 
780             throw new NoSuchCartException(msg.toString());
781         }
782         else {
783             return list.get(0);
784         }
785     }
786 
787     public ShoppingCart[] findByUserId_PrevAndNext(long cartId, long userId,
788         OrderByComparator obc) throws NoSuchCartException, SystemException {
789         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
790 
791         int count = countByUserId(userId);
792 
793         Session session = null;
794 
795         try {
796             session = openSession();
797 
798             StringBundler query = null;
799 
800             if (obc != null) {
801                 query = new StringBundler(3 +
802                         (obc.getOrderByFields().length * 3));
803             }
804             else {
805                 query = new StringBundler(2);
806             }
807 
808             query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
809 
810             query.append(_FINDER_COLUMN_USERID_USERID_2);
811 
812             if (obc != null) {
813                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
814             }
815 
816             String sql = query.toString();
817 
818             Query q = session.createQuery(sql);
819 
820             QueryPos qPos = QueryPos.getInstance(q);
821 
822             qPos.add(userId);
823 
824             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
825                     shoppingCart);
826 
827             ShoppingCart[] array = new ShoppingCartImpl[3];
828 
829             array[0] = (ShoppingCart)objArray[0];
830             array[1] = (ShoppingCart)objArray[1];
831             array[2] = (ShoppingCart)objArray[2];
832 
833             return array;
834         }
835         catch (Exception e) {
836             throw processException(e);
837         }
838         finally {
839             closeSession(session);
840         }
841     }
842 
843     public ShoppingCart findByG_U(long groupId, long userId)
844         throws NoSuchCartException, SystemException {
845         ShoppingCart shoppingCart = fetchByG_U(groupId, userId);
846 
847         if (shoppingCart == null) {
848             StringBundler msg = new StringBundler(6);
849 
850             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
851 
852             msg.append("groupId=");
853             msg.append(groupId);
854 
855             msg.append(", userId=");
856             msg.append(userId);
857 
858             msg.append(StringPool.CLOSE_CURLY_BRACE);
859 
860             if (_log.isWarnEnabled()) {
861                 _log.warn(msg.toString());
862             }
863 
864             throw new NoSuchCartException(msg.toString());
865         }
866 
867         return shoppingCart;
868     }
869 
870     public ShoppingCart fetchByG_U(long groupId, long userId)
871         throws SystemException {
872         return fetchByG_U(groupId, userId, true);
873     }
874 
875     public ShoppingCart fetchByG_U(long groupId, long userId,
876         boolean retrieveFromCache) throws SystemException {
877         Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
878 
879         Object result = null;
880 
881         if (retrieveFromCache) {
882             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_U,
883                     finderArgs, this);
884         }
885 
886         if (result == null) {
887             Session session = null;
888 
889             try {
890                 session = openSession();
891 
892                 StringBundler query = new StringBundler(3);
893 
894                 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
895 
896                 query.append(_FINDER_COLUMN_G_U_GROUPID_2);
897 
898                 query.append(_FINDER_COLUMN_G_U_USERID_2);
899 
900                 String sql = query.toString();
901 
902                 Query q = session.createQuery(sql);
903 
904                 QueryPos qPos = QueryPos.getInstance(q);
905 
906                 qPos.add(groupId);
907 
908                 qPos.add(userId);
909 
910                 List<ShoppingCart> list = q.list();
911 
912                 result = list;
913 
914                 ShoppingCart shoppingCart = null;
915 
916                 if (list.isEmpty()) {
917                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
918                         finderArgs, list);
919                 }
920                 else {
921                     shoppingCart = list.get(0);
922 
923                     cacheResult(shoppingCart);
924 
925                     if ((shoppingCart.getGroupId() != groupId) ||
926                             (shoppingCart.getUserId() != userId)) {
927                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
928                             finderArgs, shoppingCart);
929                     }
930                 }
931 
932                 return shoppingCart;
933             }
934             catch (Exception e) {
935                 throw processException(e);
936             }
937             finally {
938                 if (result == null) {
939                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
940                         finderArgs, new ArrayList<ShoppingCart>());
941                 }
942 
943                 closeSession(session);
944             }
945         }
946         else {
947             if (result instanceof List<?>) {
948                 return null;
949             }
950             else {
951                 return (ShoppingCart)result;
952             }
953         }
954     }
955 
956     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
957         throws SystemException {
958         Session session = null;
959 
960         try {
961             session = openSession();
962 
963             dynamicQuery.compile(session);
964 
965             return dynamicQuery.list();
966         }
967         catch (Exception e) {
968             throw processException(e);
969         }
970         finally {
971             closeSession(session);
972         }
973     }
974 
975     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
976         int start, int end) throws SystemException {
977         Session session = null;
978 
979         try {
980             session = openSession();
981 
982             dynamicQuery.setLimit(start, end);
983 
984             dynamicQuery.compile(session);
985 
986             return dynamicQuery.list();
987         }
988         catch (Exception e) {
989             throw processException(e);
990         }
991         finally {
992             closeSession(session);
993         }
994     }
995 
996     public List<ShoppingCart> findAll() throws SystemException {
997         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
998     }
999 
1000    public List<ShoppingCart> findAll(int start, int end)
1001        throws SystemException {
1002        return findAll(start, end, null);
1003    }
1004
1005    public List<ShoppingCart> findAll(int start, int end, OrderByComparator obc)
1006        throws SystemException {
1007        Object[] finderArgs = new Object[] {
1008                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1009            };
1010
1011        List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1012                finderArgs, this);
1013
1014        if (list == null) {
1015            Session session = null;
1016
1017            try {
1018                session = openSession();
1019
1020                StringBundler query = null;
1021                String sql = null;
1022
1023                if (obc != null) {
1024                    query = new StringBundler(2 +
1025                            (obc.getOrderByFields().length * 3));
1026
1027                    query.append(_SQL_SELECT_SHOPPINGCART);
1028
1029                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1030
1031                    sql = query.toString();
1032                }
1033
1034                sql = _SQL_SELECT_SHOPPINGCART;
1035
1036                Query q = session.createQuery(sql);
1037
1038                if (obc == null) {
1039                    list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1040                            start, end, false);
1041
1042                    Collections.sort(list);
1043                }
1044                else {
1045                    list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1046                            start, end);
1047                }
1048            }
1049            catch (Exception e) {
1050                throw processException(e);
1051            }
1052            finally {
1053                if (list == null) {
1054                    list = new ArrayList<ShoppingCart>();
1055                }
1056
1057                cacheResult(list);
1058
1059                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1060
1061                closeSession(session);
1062            }
1063        }
1064
1065        return list;
1066    }
1067
1068    public void removeByGroupId(long groupId) throws SystemException {
1069        for (ShoppingCart shoppingCart : findByGroupId(groupId)) {
1070            remove(shoppingCart);
1071        }
1072    }
1073
1074    public void removeByUserId(long userId) throws SystemException {
1075        for (ShoppingCart shoppingCart : findByUserId(userId)) {
1076            remove(shoppingCart);
1077        }
1078    }
1079
1080    public void removeByG_U(long groupId, long userId)
1081        throws NoSuchCartException, SystemException {
1082        ShoppingCart shoppingCart = findByG_U(groupId, userId);
1083
1084        remove(shoppingCart);
1085    }
1086
1087    public void removeAll() throws SystemException {
1088        for (ShoppingCart shoppingCart : findAll()) {
1089            remove(shoppingCart);
1090        }
1091    }
1092
1093    public int countByGroupId(long groupId) throws SystemException {
1094        Object[] finderArgs = new Object[] { new Long(groupId) };
1095
1096        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1097                finderArgs, this);
1098
1099        if (count == null) {
1100            Session session = null;
1101
1102            try {
1103                session = openSession();
1104
1105                StringBundler query = new StringBundler(2);
1106
1107                query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1108
1109                query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1110
1111                String sql = query.toString();
1112
1113                Query q = session.createQuery(sql);
1114
1115                QueryPos qPos = QueryPos.getInstance(q);
1116
1117                qPos.add(groupId);
1118
1119                count = (Long)q.uniqueResult();
1120            }
1121            catch (Exception e) {
1122                throw processException(e);
1123            }
1124            finally {
1125                if (count == null) {
1126                    count = Long.valueOf(0);
1127                }
1128
1129                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1130                    finderArgs, count);
1131
1132                closeSession(session);
1133            }
1134        }
1135
1136        return count.intValue();
1137    }
1138
1139    public int countByUserId(long userId) throws SystemException {
1140        Object[] finderArgs = new Object[] { new Long(userId) };
1141
1142        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
1143                finderArgs, this);
1144
1145        if (count == null) {
1146            Session session = null;
1147
1148            try {
1149                session = openSession();
1150
1151                StringBundler query = new StringBundler(2);
1152
1153                query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1154
1155                query.append(_FINDER_COLUMN_USERID_USERID_2);
1156
1157                String sql = query.toString();
1158
1159                Query q = session.createQuery(sql);
1160
1161                QueryPos qPos = QueryPos.getInstance(q);
1162
1163                qPos.add(userId);
1164
1165                count = (Long)q.uniqueResult();
1166            }
1167            catch (Exception e) {
1168                throw processException(e);
1169            }
1170            finally {
1171                if (count == null) {
1172                    count = Long.valueOf(0);
1173                }
1174
1175                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
1176                    finderArgs, count);
1177
1178                closeSession(session);
1179            }
1180        }
1181
1182        return count.intValue();
1183    }
1184
1185    public int countByG_U(long groupId, long userId) throws SystemException {
1186        Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
1187
1188        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_U,
1189                finderArgs, this);
1190
1191        if (count == null) {
1192            Session session = null;
1193
1194            try {
1195                session = openSession();
1196
1197                StringBundler query = new StringBundler(3);
1198
1199                query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1200
1201                query.append(_FINDER_COLUMN_G_U_GROUPID_2);
1202
1203                query.append(_FINDER_COLUMN_G_U_USERID_2);
1204
1205                String sql = query.toString();
1206
1207                Query q = session.createQuery(sql);
1208
1209                QueryPos qPos = QueryPos.getInstance(q);
1210
1211                qPos.add(groupId);
1212
1213                qPos.add(userId);
1214
1215                count = (Long)q.uniqueResult();
1216            }
1217            catch (Exception e) {
1218                throw processException(e);
1219            }
1220            finally {
1221                if (count == null) {
1222                    count = Long.valueOf(0);
1223                }
1224
1225                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_U, finderArgs,
1226                    count);
1227
1228                closeSession(session);
1229            }
1230        }
1231
1232        return count.intValue();
1233    }
1234
1235    public int countAll() throws SystemException {
1236        Object[] finderArgs = new Object[0];
1237
1238        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1239                finderArgs, this);
1240
1241        if (count == null) {
1242            Session session = null;
1243
1244            try {
1245                session = openSession();
1246
1247                Query q = session.createQuery(_SQL_COUNT_SHOPPINGCART);
1248
1249                count = (Long)q.uniqueResult();
1250            }
1251            catch (Exception e) {
1252                throw processException(e);
1253            }
1254            finally {
1255                if (count == null) {
1256                    count = Long.valueOf(0);
1257                }
1258
1259                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1260                    count);
1261
1262                closeSession(session);
1263            }
1264        }
1265
1266        return count.intValue();
1267    }
1268
1269    public void afterPropertiesSet() {
1270        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1271                    com.liferay.portal.util.PropsUtil.get(
1272                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCart")));
1273
1274        if (listenerClassNames.length > 0) {
1275            try {
1276                List<ModelListener<ShoppingCart>> listenersList = new ArrayList<ModelListener<ShoppingCart>>();
1277
1278                for (String listenerClassName : listenerClassNames) {
1279                    listenersList.add((ModelListener<ShoppingCart>)Class.forName(
1280                            listenerClassName).newInstance());
1281                }
1282
1283                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1284            }
1285            catch (Exception e) {
1286                _log.error(e);
1287            }
1288        }
1289    }
1290
1291    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence")
1292    protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1293    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence")
1294    protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1295    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence")
1296    protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1297    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence")
1298    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1299    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence")
1300    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1301    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence")
1302    protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1303    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence")
1304    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1305    @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence")
1306    protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1307    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1308    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1309    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1310    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1311    private static final String _SQL_SELECT_SHOPPINGCART = "SELECT shoppingCart FROM ShoppingCart shoppingCart";
1312    private static final String _SQL_SELECT_SHOPPINGCART_WHERE = "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ";
1313    private static final String _SQL_COUNT_SHOPPINGCART = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart";
1314    private static final String _SQL_COUNT_SHOPPINGCART_WHERE = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart WHERE ";
1315    private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCart.groupId = ?";
1316    private static final String _FINDER_COLUMN_USERID_USERID_2 = "shoppingCart.userId = ?";
1317    private static final String _FINDER_COLUMN_G_U_GROUPID_2 = "shoppingCart.groupId = ? AND ";
1318    private static final String _FINDER_COLUMN_G_U_USERID_2 = "shoppingCart.userId = ?";
1319    private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCart.";
1320    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCart exists with the primary key ";
1321    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCart exists with the key {";
1322    private static Log _log = LogFactoryUtil.getLog(ShoppingCartPersistenceImpl.class);
1323}