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