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