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