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