1
22
23 package com.liferay.portlet.shopping.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28 import com.liferay.portal.kernel.dao.orm.Query;
29 import com.liferay.portal.kernel.dao.orm.QueryPos;
30 import com.liferay.portal.kernel.dao.orm.QueryUtil;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.util.GetterUtil;
33 import com.liferay.portal.kernel.util.ListUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.model.ModelListener;
38 import com.liferay.portal.service.persistence.BatchSessionUtil;
39 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40
41 import com.liferay.portlet.shopping.NoSuchItemPriceException;
42 import com.liferay.portlet.shopping.model.ShoppingItemPrice;
43 import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceImpl;
44 import com.liferay.portlet.shopping.model.impl.ShoppingItemPriceModelImpl;
45
46 import org.apache.commons.logging.Log;
47 import org.apache.commons.logging.LogFactory;
48
49 import java.util.ArrayList;
50 import java.util.Collections;
51 import java.util.Iterator;
52 import java.util.List;
53
54
60 public class ShoppingItemPricePersistenceImpl extends BasePersistenceImpl
61 implements ShoppingItemPricePersistence {
62 public ShoppingItemPrice create(long itemPriceId) {
63 ShoppingItemPrice shoppingItemPrice = new ShoppingItemPriceImpl();
64
65 shoppingItemPrice.setNew(true);
66 shoppingItemPrice.setPrimaryKey(itemPriceId);
67
68 return shoppingItemPrice;
69 }
70
71 public ShoppingItemPrice remove(long itemPriceId)
72 throws NoSuchItemPriceException, SystemException {
73 Session session = null;
74
75 try {
76 session = openSession();
77
78 ShoppingItemPrice shoppingItemPrice = (ShoppingItemPrice)session.get(ShoppingItemPriceImpl.class,
79 new Long(itemPriceId));
80
81 if (shoppingItemPrice == null) {
82 if (_log.isWarnEnabled()) {
83 _log.warn(
84 "No ShoppingItemPrice exists with the primary key " +
85 itemPriceId);
86 }
87
88 throw new NoSuchItemPriceException(
89 "No ShoppingItemPrice exists with the primary key " +
90 itemPriceId);
91 }
92
93 return remove(shoppingItemPrice);
94 }
95 catch (NoSuchItemPriceException nsee) {
96 throw nsee;
97 }
98 catch (Exception e) {
99 throw processException(e);
100 }
101 finally {
102 closeSession(session);
103 }
104 }
105
106 public ShoppingItemPrice remove(ShoppingItemPrice shoppingItemPrice)
107 throws SystemException {
108 if (_listeners.length > 0) {
109 for (ModelListener listener : _listeners) {
110 listener.onBeforeRemove(shoppingItemPrice);
111 }
112 }
113
114 shoppingItemPrice = removeImpl(shoppingItemPrice);
115
116 if (_listeners.length > 0) {
117 for (ModelListener listener : _listeners) {
118 listener.onAfterRemove(shoppingItemPrice);
119 }
120 }
121
122 return shoppingItemPrice;
123 }
124
125 protected ShoppingItemPrice removeImpl(ShoppingItemPrice shoppingItemPrice)
126 throws SystemException {
127 Session session = null;
128
129 try {
130 session = openSession();
131
132 if (BatchSessionUtil.isEnabled()) {
133 Object staleObject = session.get(ShoppingItemPriceImpl.class,
134 shoppingItemPrice.getPrimaryKeyObj());
135
136 if (staleObject != null) {
137 session.evict(staleObject);
138 }
139 }
140
141 session.delete(shoppingItemPrice);
142
143 session.flush();
144
145 return shoppingItemPrice;
146 }
147 catch (Exception e) {
148 throw processException(e);
149 }
150 finally {
151 closeSession(session);
152
153 FinderCacheUtil.clearCache(ShoppingItemPrice.class.getName());
154 }
155 }
156
157
160 public ShoppingItemPrice update(ShoppingItemPrice shoppingItemPrice)
161 throws SystemException {
162 if (_log.isWarnEnabled()) {
163 _log.warn(
164 "Using the deprecated update(ShoppingItemPrice shoppingItemPrice) method. Use update(ShoppingItemPrice shoppingItemPrice, boolean merge) instead.");
165 }
166
167 return update(shoppingItemPrice, false);
168 }
169
170
183 public ShoppingItemPrice update(ShoppingItemPrice shoppingItemPrice,
184 boolean merge) throws SystemException {
185 boolean isNew = shoppingItemPrice.isNew();
186
187 if (_listeners.length > 0) {
188 for (ModelListener listener : _listeners) {
189 if (isNew) {
190 listener.onBeforeCreate(shoppingItemPrice);
191 }
192 else {
193 listener.onBeforeUpdate(shoppingItemPrice);
194 }
195 }
196 }
197
198 shoppingItemPrice = updateImpl(shoppingItemPrice, merge);
199
200 if (_listeners.length > 0) {
201 for (ModelListener listener : _listeners) {
202 if (isNew) {
203 listener.onAfterCreate(shoppingItemPrice);
204 }
205 else {
206 listener.onAfterUpdate(shoppingItemPrice);
207 }
208 }
209 }
210
211 return shoppingItemPrice;
212 }
213
214 public ShoppingItemPrice updateImpl(
215 com.liferay.portlet.shopping.model.ShoppingItemPrice shoppingItemPrice,
216 boolean merge) throws SystemException {
217 Session session = null;
218
219 try {
220 session = openSession();
221
222 BatchSessionUtil.update(session, shoppingItemPrice, merge);
223
224 shoppingItemPrice.setNew(false);
225
226 return shoppingItemPrice;
227 }
228 catch (Exception e) {
229 throw processException(e);
230 }
231 finally {
232 closeSession(session);
233
234 FinderCacheUtil.clearCache(ShoppingItemPrice.class.getName());
235 }
236 }
237
238 public ShoppingItemPrice findByPrimaryKey(long itemPriceId)
239 throws NoSuchItemPriceException, SystemException {
240 ShoppingItemPrice shoppingItemPrice = fetchByPrimaryKey(itemPriceId);
241
242 if (shoppingItemPrice == null) {
243 if (_log.isWarnEnabled()) {
244 _log.warn("No ShoppingItemPrice exists with the primary key " +
245 itemPriceId);
246 }
247
248 throw new NoSuchItemPriceException(
249 "No ShoppingItemPrice exists with the primary key " +
250 itemPriceId);
251 }
252
253 return shoppingItemPrice;
254 }
255
256 public ShoppingItemPrice fetchByPrimaryKey(long itemPriceId)
257 throws SystemException {
258 Session session = null;
259
260 try {
261 session = openSession();
262
263 return (ShoppingItemPrice)session.get(ShoppingItemPriceImpl.class,
264 new Long(itemPriceId));
265 }
266 catch (Exception e) {
267 throw processException(e);
268 }
269 finally {
270 closeSession(session);
271 }
272 }
273
274 public List<ShoppingItemPrice> findByItemId(long itemId)
275 throws SystemException {
276 boolean finderClassNameCacheEnabled = ShoppingItemPriceModelImpl.CACHE_ENABLED;
277 String finderClassName = ShoppingItemPrice.class.getName();
278 String finderMethodName = "findByItemId";
279 String[] finderParams = new String[] { Long.class.getName() };
280 Object[] finderArgs = new Object[] { new Long(itemId) };
281
282 Object result = null;
283
284 if (finderClassNameCacheEnabled) {
285 result = FinderCacheUtil.getResult(finderClassName,
286 finderMethodName, finderParams, finderArgs, this);
287 }
288
289 if (result == null) {
290 Session session = null;
291
292 try {
293 session = openSession();
294
295 StringBuilder query = new StringBuilder();
296
297 query.append(
298 "FROM com.liferay.portlet.shopping.model.ShoppingItemPrice WHERE ");
299
300 query.append("itemId = ?");
301
302 query.append(" ");
303
304 query.append("ORDER BY ");
305
306 query.append("itemId ASC, ");
307 query.append("itemPriceId ASC");
308
309 Query q = session.createQuery(query.toString());
310
311 QueryPos qPos = QueryPos.getInstance(q);
312
313 qPos.add(itemId);
314
315 List<ShoppingItemPrice> list = q.list();
316
317 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
318 finderClassName, finderMethodName, finderParams,
319 finderArgs, list);
320
321 return list;
322 }
323 catch (Exception e) {
324 throw processException(e);
325 }
326 finally {
327 closeSession(session);
328 }
329 }
330 else {
331 return (List<ShoppingItemPrice>)result;
332 }
333 }
334
335 public List<ShoppingItemPrice> findByItemId(long itemId, int start, int end)
336 throws SystemException {
337 return findByItemId(itemId, start, end, null);
338 }
339
340 public List<ShoppingItemPrice> findByItemId(long itemId, int start,
341 int end, OrderByComparator obc) throws SystemException {
342 boolean finderClassNameCacheEnabled = ShoppingItemPriceModelImpl.CACHE_ENABLED;
343 String finderClassName = ShoppingItemPrice.class.getName();
344 String finderMethodName = "findByItemId";
345 String[] finderParams = new String[] {
346 Long.class.getName(),
347
348 "java.lang.Integer", "java.lang.Integer",
349 "com.liferay.portal.kernel.util.OrderByComparator"
350 };
351 Object[] finderArgs = new Object[] {
352 new Long(itemId),
353
354 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
355 };
356
357 Object result = null;
358
359 if (finderClassNameCacheEnabled) {
360 result = FinderCacheUtil.getResult(finderClassName,
361 finderMethodName, finderParams, finderArgs, this);
362 }
363
364 if (result == null) {
365 Session session = null;
366
367 try {
368 session = openSession();
369
370 StringBuilder query = new StringBuilder();
371
372 query.append(
373 "FROM com.liferay.portlet.shopping.model.ShoppingItemPrice WHERE ");
374
375 query.append("itemId = ?");
376
377 query.append(" ");
378
379 if (obc != null) {
380 query.append("ORDER BY ");
381 query.append(obc.getOrderBy());
382 }
383
384 else {
385 query.append("ORDER BY ");
386
387 query.append("itemId ASC, ");
388 query.append("itemPriceId ASC");
389 }
390
391 Query q = session.createQuery(query.toString());
392
393 QueryPos qPos = QueryPos.getInstance(q);
394
395 qPos.add(itemId);
396
397 List<ShoppingItemPrice> list = (List<ShoppingItemPrice>)QueryUtil.list(q,
398 getDialect(), start, end);
399
400 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
401 finderClassName, finderMethodName, finderParams,
402 finderArgs, list);
403
404 return list;
405 }
406 catch (Exception e) {
407 throw processException(e);
408 }
409 finally {
410 closeSession(session);
411 }
412 }
413 else {
414 return (List<ShoppingItemPrice>)result;
415 }
416 }
417
418 public ShoppingItemPrice findByItemId_First(long itemId,
419 OrderByComparator obc) throws NoSuchItemPriceException, SystemException {
420 List<ShoppingItemPrice> list = findByItemId(itemId, 0, 1, obc);
421
422 if (list.size() == 0) {
423 StringBuilder msg = new StringBuilder();
424
425 msg.append("No ShoppingItemPrice exists with the key {");
426
427 msg.append("itemId=" + itemId);
428
429 msg.append(StringPool.CLOSE_CURLY_BRACE);
430
431 throw new NoSuchItemPriceException(msg.toString());
432 }
433 else {
434 return list.get(0);
435 }
436 }
437
438 public ShoppingItemPrice findByItemId_Last(long itemId,
439 OrderByComparator obc) throws NoSuchItemPriceException, SystemException {
440 int count = countByItemId(itemId);
441
442 List<ShoppingItemPrice> list = findByItemId(itemId, count - 1, count,
443 obc);
444
445 if (list.size() == 0) {
446 StringBuilder msg = new StringBuilder();
447
448 msg.append("No ShoppingItemPrice exists with the key {");
449
450 msg.append("itemId=" + itemId);
451
452 msg.append(StringPool.CLOSE_CURLY_BRACE);
453
454 throw new NoSuchItemPriceException(msg.toString());
455 }
456 else {
457 return list.get(0);
458 }
459 }
460
461 public ShoppingItemPrice[] findByItemId_PrevAndNext(long itemPriceId,
462 long itemId, OrderByComparator obc)
463 throws NoSuchItemPriceException, SystemException {
464 ShoppingItemPrice shoppingItemPrice = findByPrimaryKey(itemPriceId);
465
466 int count = countByItemId(itemId);
467
468 Session session = null;
469
470 try {
471 session = openSession();
472
473 StringBuilder query = new StringBuilder();
474
475 query.append(
476 "FROM com.liferay.portlet.shopping.model.ShoppingItemPrice WHERE ");
477
478 query.append("itemId = ?");
479
480 query.append(" ");
481
482 if (obc != null) {
483 query.append("ORDER BY ");
484 query.append(obc.getOrderBy());
485 }
486
487 else {
488 query.append("ORDER BY ");
489
490 query.append("itemId ASC, ");
491 query.append("itemPriceId ASC");
492 }
493
494 Query q = session.createQuery(query.toString());
495
496 QueryPos qPos = QueryPos.getInstance(q);
497
498 qPos.add(itemId);
499
500 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
501 shoppingItemPrice);
502
503 ShoppingItemPrice[] array = new ShoppingItemPriceImpl[3];
504
505 array[0] = (ShoppingItemPrice)objArray[0];
506 array[1] = (ShoppingItemPrice)objArray[1];
507 array[2] = (ShoppingItemPrice)objArray[2];
508
509 return array;
510 }
511 catch (Exception e) {
512 throw processException(e);
513 }
514 finally {
515 closeSession(session);
516 }
517 }
518
519 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
520 throws SystemException {
521 Session session = null;
522
523 try {
524 session = openSession();
525
526 dynamicQuery.compile(session);
527
528 return dynamicQuery.list();
529 }
530 catch (Exception e) {
531 throw processException(e);
532 }
533 finally {
534 closeSession(session);
535 }
536 }
537
538 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
539 int start, int end) throws SystemException {
540 Session session = null;
541
542 try {
543 session = openSession();
544
545 dynamicQuery.setLimit(start, end);
546
547 dynamicQuery.compile(session);
548
549 return dynamicQuery.list();
550 }
551 catch (Exception e) {
552 throw processException(e);
553 }
554 finally {
555 closeSession(session);
556 }
557 }
558
559 public List<ShoppingItemPrice> findAll() throws SystemException {
560 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
561 }
562
563 public List<ShoppingItemPrice> findAll(int start, int end)
564 throws SystemException {
565 return findAll(start, end, null);
566 }
567
568 public List<ShoppingItemPrice> findAll(int start, int end,
569 OrderByComparator obc) throws SystemException {
570 boolean finderClassNameCacheEnabled = ShoppingItemPriceModelImpl.CACHE_ENABLED;
571 String finderClassName = ShoppingItemPrice.class.getName();
572 String finderMethodName = "findAll";
573 String[] finderParams = new String[] {
574 "java.lang.Integer", "java.lang.Integer",
575 "com.liferay.portal.kernel.util.OrderByComparator"
576 };
577 Object[] finderArgs = new Object[] {
578 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
579 };
580
581 Object result = null;
582
583 if (finderClassNameCacheEnabled) {
584 result = FinderCacheUtil.getResult(finderClassName,
585 finderMethodName, finderParams, finderArgs, this);
586 }
587
588 if (result == null) {
589 Session session = null;
590
591 try {
592 session = openSession();
593
594 StringBuilder query = new StringBuilder();
595
596 query.append(
597 "FROM com.liferay.portlet.shopping.model.ShoppingItemPrice ");
598
599 if (obc != null) {
600 query.append("ORDER BY ");
601 query.append(obc.getOrderBy());
602 }
603
604 else {
605 query.append("ORDER BY ");
606
607 query.append("itemId ASC, ");
608 query.append("itemPriceId ASC");
609 }
610
611 Query q = session.createQuery(query.toString());
612
613 List<ShoppingItemPrice> list = null;
614
615 if (obc == null) {
616 list = (List<ShoppingItemPrice>)QueryUtil.list(q,
617 getDialect(), start, end, false);
618
619 Collections.sort(list);
620 }
621 else {
622 list = (List<ShoppingItemPrice>)QueryUtil.list(q,
623 getDialect(), start, end);
624 }
625
626 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
627 finderClassName, finderMethodName, finderParams,
628 finderArgs, list);
629
630 return list;
631 }
632 catch (Exception e) {
633 throw processException(e);
634 }
635 finally {
636 closeSession(session);
637 }
638 }
639 else {
640 return (List<ShoppingItemPrice>)result;
641 }
642 }
643
644 public void removeByItemId(long itemId) throws SystemException {
645 for (ShoppingItemPrice shoppingItemPrice : findByItemId(itemId)) {
646 remove(shoppingItemPrice);
647 }
648 }
649
650 public void removeAll() throws SystemException {
651 for (ShoppingItemPrice shoppingItemPrice : findAll()) {
652 remove(shoppingItemPrice);
653 }
654 }
655
656 public int countByItemId(long itemId) throws SystemException {
657 boolean finderClassNameCacheEnabled = ShoppingItemPriceModelImpl.CACHE_ENABLED;
658 String finderClassName = ShoppingItemPrice.class.getName();
659 String finderMethodName = "countByItemId";
660 String[] finderParams = new String[] { Long.class.getName() };
661 Object[] finderArgs = new Object[] { new Long(itemId) };
662
663 Object result = null;
664
665 if (finderClassNameCacheEnabled) {
666 result = FinderCacheUtil.getResult(finderClassName,
667 finderMethodName, finderParams, finderArgs, this);
668 }
669
670 if (result == null) {
671 Session session = null;
672
673 try {
674 session = openSession();
675
676 StringBuilder query = new StringBuilder();
677
678 query.append("SELECT COUNT(*) ");
679 query.append(
680 "FROM com.liferay.portlet.shopping.model.ShoppingItemPrice WHERE ");
681
682 query.append("itemId = ?");
683
684 query.append(" ");
685
686 Query q = session.createQuery(query.toString());
687
688 QueryPos qPos = QueryPos.getInstance(q);
689
690 qPos.add(itemId);
691
692 Long count = null;
693
694 Iterator<Long> itr = q.list().iterator();
695
696 if (itr.hasNext()) {
697 count = itr.next();
698 }
699
700 if (count == null) {
701 count = new Long(0);
702 }
703
704 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
705 finderClassName, finderMethodName, finderParams,
706 finderArgs, count);
707
708 return count.intValue();
709 }
710 catch (Exception e) {
711 throw processException(e);
712 }
713 finally {
714 closeSession(session);
715 }
716 }
717 else {
718 return ((Long)result).intValue();
719 }
720 }
721
722 public int countAll() throws SystemException {
723 boolean finderClassNameCacheEnabled = ShoppingItemPriceModelImpl.CACHE_ENABLED;
724 String finderClassName = ShoppingItemPrice.class.getName();
725 String finderMethodName = "countAll";
726 String[] finderParams = new String[] { };
727 Object[] finderArgs = new Object[] { };
728
729 Object result = null;
730
731 if (finderClassNameCacheEnabled) {
732 result = FinderCacheUtil.getResult(finderClassName,
733 finderMethodName, finderParams, finderArgs, this);
734 }
735
736 if (result == null) {
737 Session session = null;
738
739 try {
740 session = openSession();
741
742 Query q = session.createQuery(
743 "SELECT COUNT(*) FROM com.liferay.portlet.shopping.model.ShoppingItemPrice");
744
745 Long count = null;
746
747 Iterator<Long> itr = q.list().iterator();
748
749 if (itr.hasNext()) {
750 count = itr.next();
751 }
752
753 if (count == null) {
754 count = new Long(0);
755 }
756
757 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
758 finderClassName, finderMethodName, finderParams,
759 finderArgs, count);
760
761 return count.intValue();
762 }
763 catch (Exception e) {
764 throw processException(e);
765 }
766 finally {
767 closeSession(session);
768 }
769 }
770 else {
771 return ((Long)result).intValue();
772 }
773 }
774
775 public void registerListener(ModelListener listener) {
776 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
777
778 listeners.add(listener);
779
780 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
781 }
782
783 public void unregisterListener(ModelListener listener) {
784 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
785
786 listeners.remove(listener);
787
788 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
789 }
790
791 public void afterPropertiesSet() {
792 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
793 com.liferay.portal.util.PropsUtil.get(
794 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingItemPrice")));
795
796 if (listenerClassNames.length > 0) {
797 try {
798 List<ModelListener> listeners = new ArrayList<ModelListener>();
799
800 for (String listenerClassName : listenerClassNames) {
801 listeners.add((ModelListener)Class.forName(
802 listenerClassName).newInstance());
803 }
804
805 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
806 }
807 catch (Exception e) {
808 _log.error(e);
809 }
810 }
811 }
812
813 private static Log _log = LogFactory.getLog(ShoppingItemPricePersistenceImpl.class);
814 private ModelListener[] _listeners = new ModelListener[0];
815 }