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