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 session.delete(image);
126
127 session.flush();
128
129 return image;
130 }
131 catch (Exception e) {
132 throw processException(e);
133 }
134 finally {
135 closeSession(session);
136
137 FinderCacheUtil.clearCache(Image.class.getName());
138 }
139 }
140
141
144 public Image update(Image image) throws SystemException {
145 if (_log.isWarnEnabled()) {
146 _log.warn(
147 "Using the deprecated update(Image image) method. Use update(Image image, boolean merge) instead.");
148 }
149
150 return update(image, false);
151 }
152
153
166 public Image update(Image image, boolean merge) throws SystemException {
167 boolean isNew = image.isNew();
168
169 if (_listeners.length > 0) {
170 for (ModelListener listener : _listeners) {
171 if (isNew) {
172 listener.onBeforeCreate(image);
173 }
174 else {
175 listener.onBeforeUpdate(image);
176 }
177 }
178 }
179
180 image = updateImpl(image, merge);
181
182 if (_listeners.length > 0) {
183 for (ModelListener listener : _listeners) {
184 if (isNew) {
185 listener.onAfterCreate(image);
186 }
187 else {
188 listener.onAfterUpdate(image);
189 }
190 }
191 }
192
193 return image;
194 }
195
196 public Image updateImpl(com.liferay.portal.model.Image image, boolean merge)
197 throws SystemException {
198 Session session = null;
199
200 try {
201 session = openSession();
202
203 if (merge) {
204 session.merge(image);
205 }
206 else {
207 if (image.isNew()) {
208 session.save(image);
209 }
210 }
211
212 session.flush();
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 = (List<Image>)QueryUtil.list(q, getDialect(),
587 start, end);
588
589 if (obc == null) {
590 Collections.sort(list);
591 }
592
593 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
594 finderClassName, finderMethodName, finderParams,
595 finderArgs, list);
596
597 return list;
598 }
599 catch (Exception e) {
600 throw processException(e);
601 }
602 finally {
603 closeSession(session);
604 }
605 }
606 else {
607 return (List<Image>)result;
608 }
609 }
610
611 public void removeBySize(int size) throws SystemException {
612 for (Image image : findBySize(size)) {
613 remove(image);
614 }
615 }
616
617 public void removeAll() throws SystemException {
618 for (Image image : findAll()) {
619 remove(image);
620 }
621 }
622
623 public int countBySize(int size) throws SystemException {
624 boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
625 String finderClassName = Image.class.getName();
626 String finderMethodName = "countBySize";
627 String[] finderParams = new String[] { Integer.class.getName() };
628 Object[] finderArgs = new Object[] { new Integer(size) };
629
630 Object result = null;
631
632 if (finderClassNameCacheEnabled) {
633 result = FinderCacheUtil.getResult(finderClassName,
634 finderMethodName, finderParams, finderArgs, this);
635 }
636
637 if (result == null) {
638 Session session = null;
639
640 try {
641 session = openSession();
642
643 StringBuilder query = new StringBuilder();
644
645 query.append("SELECT COUNT(*) ");
646 query.append("FROM com.liferay.portal.model.Image WHERE ");
647
648 query.append("size_ < ?");
649
650 query.append(" ");
651
652 Query q = session.createQuery(query.toString());
653
654 QueryPos qPos = QueryPos.getInstance(q);
655
656 qPos.add(size);
657
658 Long count = null;
659
660 Iterator<Long> itr = q.list().iterator();
661
662 if (itr.hasNext()) {
663 count = itr.next();
664 }
665
666 if (count == null) {
667 count = new Long(0);
668 }
669
670 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
671 finderClassName, finderMethodName, finderParams,
672 finderArgs, count);
673
674 return count.intValue();
675 }
676 catch (Exception e) {
677 throw processException(e);
678 }
679 finally {
680 closeSession(session);
681 }
682 }
683 else {
684 return ((Long)result).intValue();
685 }
686 }
687
688 public int countAll() throws SystemException {
689 boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
690 String finderClassName = Image.class.getName();
691 String finderMethodName = "countAll";
692 String[] finderParams = new String[] { };
693 Object[] finderArgs = new Object[] { };
694
695 Object result = null;
696
697 if (finderClassNameCacheEnabled) {
698 result = FinderCacheUtil.getResult(finderClassName,
699 finderMethodName, finderParams, finderArgs, this);
700 }
701
702 if (result == null) {
703 Session session = null;
704
705 try {
706 session = openSession();
707
708 Query q = session.createQuery(
709 "SELECT COUNT(*) FROM com.liferay.portal.model.Image");
710
711 Long count = null;
712
713 Iterator<Long> itr = q.list().iterator();
714
715 if (itr.hasNext()) {
716 count = itr.next();
717 }
718
719 if (count == null) {
720 count = new Long(0);
721 }
722
723 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
724 finderClassName, finderMethodName, finderParams,
725 finderArgs, count);
726
727 return count.intValue();
728 }
729 catch (Exception e) {
730 throw processException(e);
731 }
732 finally {
733 closeSession(session);
734 }
735 }
736 else {
737 return ((Long)result).intValue();
738 }
739 }
740
741 public void registerListener(ModelListener listener) {
742 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
743
744 listeners.add(listener);
745
746 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
747 }
748
749 public void unregisterListener(ModelListener listener) {
750 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
751
752 listeners.remove(listener);
753
754 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
755 }
756
757 public void afterPropertiesSet() {
758 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
759 com.liferay.portal.util.PropsUtil.get(
760 "value.object.listener.com.liferay.portal.model.Image")));
761
762 if (listenerClassNames.length > 0) {
763 try {
764 List<ModelListener> listeners = new ArrayList<ModelListener>();
765
766 for (String listenerClassName : listenerClassNames) {
767 listeners.add((ModelListener)Class.forName(
768 listenerClassName).newInstance());
769 }
770
771 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
772 }
773 catch (Exception e) {
774 _log.error(e);
775 }
776 }
777 }
778
779 private static Log _log = LogFactory.getLog(ImagePersistenceImpl.class);
780 private ModelListener[] _listeners = new ModelListener[0];
781 }