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