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