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