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