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