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.OrderByComparator;
30 import com.liferay.portal.kernel.util.StringMaker;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.model.Image;
33 import com.liferay.portal.model.impl.ImageImpl;
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
38 import com.liferay.util.dao.hibernate.QueryUtil;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import org.hibernate.Query;
44 import org.hibernate.Session;
45
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.List;
49
50
56 public class ImagePersistenceImpl extends BasePersistence
57 implements ImagePersistence {
58 public Image create(long imageId) {
59 Image image = new ImageImpl();
60 image.setNew(true);
61 image.setPrimaryKey(imageId);
62
63 return image;
64 }
65
66 public Image remove(long imageId)
67 throws NoSuchImageException, SystemException {
68 Session session = null;
69
70 try {
71 session = openSession();
72
73 Image image = (Image)session.get(ImageImpl.class, new Long(imageId));
74
75 if (image == null) {
76 if (_log.isWarnEnabled()) {
77 _log.warn("No Image exists with the primary key " +
78 imageId);
79 }
80
81 throw new NoSuchImageException(
82 "No Image exists with the primary key " + imageId);
83 }
84
85 return remove(image);
86 }
87 catch (NoSuchImageException nsee) {
88 throw nsee;
89 }
90 catch (Exception e) {
91 throw HibernateUtil.processException(e);
92 }
93 finally {
94 closeSession(session);
95 }
96 }
97
98 public Image remove(Image image) throws SystemException {
99 Session session = null;
100
101 try {
102 session = openSession();
103 session.delete(image);
104 session.flush();
105
106 return image;
107 }
108 catch (Exception e) {
109 throw HibernateUtil.processException(e);
110 }
111 finally {
112 closeSession(session);
113 FinderCache.clearCache(Image.class.getName());
114 }
115 }
116
117 public Image update(com.liferay.portal.model.Image image)
118 throws SystemException {
119 return update(image, false);
120 }
121
122 public Image update(com.liferay.portal.model.Image image, boolean merge)
123 throws SystemException {
124 Session session = null;
125
126 try {
127 session = openSession();
128
129 if (merge) {
130 session.merge(image);
131 }
132 else {
133 if (image.isNew()) {
134 session.save(image);
135 }
136 }
137
138 session.flush();
139 image.setNew(false);
140
141 return image;
142 }
143 catch (Exception e) {
144 throw HibernateUtil.processException(e);
145 }
146 finally {
147 closeSession(session);
148 FinderCache.clearCache(Image.class.getName());
149 }
150 }
151
152 public Image findByPrimaryKey(long imageId)
153 throws NoSuchImageException, SystemException {
154 Image image = fetchByPrimaryKey(imageId);
155
156 if (image == null) {
157 if (_log.isWarnEnabled()) {
158 _log.warn("No Image exists with the primary key " + imageId);
159 }
160
161 throw new NoSuchImageException(
162 "No Image exists with the primary key " + imageId);
163 }
164
165 return image;
166 }
167
168 public Image fetchByPrimaryKey(long imageId) throws SystemException {
169 Session session = null;
170
171 try {
172 session = openSession();
173
174 return (Image)session.get(ImageImpl.class, new Long(imageId));
175 }
176 catch (Exception e) {
177 throw HibernateUtil.processException(e);
178 }
179 finally {
180 closeSession(session);
181 }
182 }
183
184 public List findBySize(int size) throws SystemException {
185 String finderClassName = Image.class.getName();
186 String finderMethodName = "findBySize";
187 String[] finderParams = new String[] { Integer.class.getName() };
188 Object[] finderArgs = new Object[] { new Integer(size) };
189 Object result = FinderCache.getResult(finderClassName,
190 finderMethodName, finderParams, finderArgs, getSessionFactory());
191
192 if (result == null) {
193 Session session = null;
194
195 try {
196 session = openSession();
197
198 StringMaker query = new StringMaker();
199 query.append("FROM com.liferay.portal.model.Image WHERE ");
200 query.append("size_ < ?");
201 query.append(" ");
202 query.append("ORDER BY ");
203 query.append("imageId ASC");
204
205 Query q = session.createQuery(query.toString());
206 int queryPos = 0;
207 q.setInteger(queryPos++, size);
208
209 List list = q.list();
210 FinderCache.putResult(finderClassName, finderMethodName,
211 finderParams, finderArgs, list);
212
213 return list;
214 }
215 catch (Exception e) {
216 throw HibernateUtil.processException(e);
217 }
218 finally {
219 closeSession(session);
220 }
221 }
222 else {
223 return (List)result;
224 }
225 }
226
227 public List findBySize(int size, int begin, int end)
228 throws SystemException {
229 return findBySize(size, begin, end, null);
230 }
231
232 public List findBySize(int size, int begin, int end, OrderByComparator obc)
233 throws SystemException {
234 String finderClassName = Image.class.getName();
235 String finderMethodName = "findBySize";
236 String[] finderParams = new String[] {
237 Integer.class.getName(), "java.lang.Integer",
238 "java.lang.Integer",
239 "com.liferay.portal.kernel.util.OrderByComparator"
240 };
241 Object[] finderArgs = new Object[] {
242 new Integer(size), String.valueOf(begin), String.valueOf(end),
243 String.valueOf(obc)
244 };
245 Object result = FinderCache.getResult(finderClassName,
246 finderMethodName, finderParams, finderArgs, getSessionFactory());
247
248 if (result == null) {
249 Session session = null;
250
251 try {
252 session = openSession();
253
254 StringMaker query = new StringMaker();
255 query.append("FROM com.liferay.portal.model.Image WHERE ");
256 query.append("size_ < ?");
257 query.append(" ");
258
259 if (obc != null) {
260 query.append("ORDER BY ");
261 query.append(obc.getOrderBy());
262 }
263 else {
264 query.append("ORDER BY ");
265 query.append("imageId ASC");
266 }
267
268 Query q = session.createQuery(query.toString());
269 int queryPos = 0;
270 q.setInteger(queryPos++, size);
271
272 List list = QueryUtil.list(q, getDialect(), begin, end);
273 FinderCache.putResult(finderClassName, finderMethodName,
274 finderParams, finderArgs, list);
275
276 return list;
277 }
278 catch (Exception e) {
279 throw HibernateUtil.processException(e);
280 }
281 finally {
282 closeSession(session);
283 }
284 }
285 else {
286 return (List)result;
287 }
288 }
289
290 public Image findBySize_First(int size, OrderByComparator obc)
291 throws NoSuchImageException, SystemException {
292 List list = findBySize(size, 0, 1, obc);
293
294 if (list.size() == 0) {
295 StringMaker msg = new StringMaker();
296 msg.append("No Image exists with the key ");
297 msg.append(StringPool.OPEN_CURLY_BRACE);
298 msg.append("size=");
299 msg.append(size);
300 msg.append(StringPool.CLOSE_CURLY_BRACE);
301 throw new NoSuchImageException(msg.toString());
302 }
303 else {
304 return (Image)list.get(0);
305 }
306 }
307
308 public Image findBySize_Last(int size, OrderByComparator obc)
309 throws NoSuchImageException, SystemException {
310 int count = countBySize(size);
311 List list = findBySize(size, count - 1, count, obc);
312
313 if (list.size() == 0) {
314 StringMaker msg = new StringMaker();
315 msg.append("No Image exists with the key ");
316 msg.append(StringPool.OPEN_CURLY_BRACE);
317 msg.append("size=");
318 msg.append(size);
319 msg.append(StringPool.CLOSE_CURLY_BRACE);
320 throw new NoSuchImageException(msg.toString());
321 }
322 else {
323 return (Image)list.get(0);
324 }
325 }
326
327 public Image[] findBySize_PrevAndNext(long imageId, int size,
328 OrderByComparator obc) throws NoSuchImageException, SystemException {
329 Image image = findByPrimaryKey(imageId);
330 int count = countBySize(size);
331 Session session = null;
332
333 try {
334 session = openSession();
335
336 StringMaker query = new StringMaker();
337 query.append("FROM com.liferay.portal.model.Image WHERE ");
338 query.append("size_ < ?");
339 query.append(" ");
340
341 if (obc != null) {
342 query.append("ORDER BY ");
343 query.append(obc.getOrderBy());
344 }
345 else {
346 query.append("ORDER BY ");
347 query.append("imageId ASC");
348 }
349
350 Query q = session.createQuery(query.toString());
351 int queryPos = 0;
352 q.setInteger(queryPos++, size);
353
354 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, image);
355 Image[] array = new ImageImpl[3];
356 array[0] = (Image)objArray[0];
357 array[1] = (Image)objArray[1];
358 array[2] = (Image)objArray[2];
359
360 return array;
361 }
362 catch (Exception e) {
363 throw HibernateUtil.processException(e);
364 }
365 finally {
366 closeSession(session);
367 }
368 }
369
370 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
371 throws SystemException {
372 Session session = null;
373
374 try {
375 session = openSession();
376
377 DynamicQuery query = queryInitializer.initialize(session);
378
379 return query.list();
380 }
381 catch (Exception e) {
382 throw HibernateUtil.processException(e);
383 }
384 finally {
385 closeSession(session);
386 }
387 }
388
389 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
390 int begin, int end) throws SystemException {
391 Session session = null;
392
393 try {
394 session = openSession();
395
396 DynamicQuery query = queryInitializer.initialize(session);
397 query.setLimit(begin, end);
398
399 return query.list();
400 }
401 catch (Exception e) {
402 throw HibernateUtil.processException(e);
403 }
404 finally {
405 closeSession(session);
406 }
407 }
408
409 public List findAll() throws SystemException {
410 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
411 }
412
413 public List findAll(int begin, int end) throws SystemException {
414 return findAll(begin, end, null);
415 }
416
417 public List findAll(int begin, int end, OrderByComparator obc)
418 throws SystemException {
419 String finderClassName = Image.class.getName();
420 String finderMethodName = "findAll";
421 String[] finderParams = new String[] {
422 "java.lang.Integer", "java.lang.Integer",
423 "com.liferay.portal.kernel.util.OrderByComparator"
424 };
425 Object[] finderArgs = new Object[] {
426 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
427 };
428 Object result = FinderCache.getResult(finderClassName,
429 finderMethodName, finderParams, finderArgs, getSessionFactory());
430
431 if (result == null) {
432 Session session = null;
433
434 try {
435 session = openSession();
436
437 StringMaker query = new StringMaker();
438 query.append("FROM com.liferay.portal.model.Image ");
439
440 if (obc != null) {
441 query.append("ORDER BY ");
442 query.append(obc.getOrderBy());
443 }
444 else {
445 query.append("ORDER BY ");
446 query.append("imageId ASC");
447 }
448
449 Query q = session.createQuery(query.toString());
450 List list = QueryUtil.list(q, getDialect(), begin, end);
451
452 if (obc == null) {
453 Collections.sort(list);
454 }
455
456 FinderCache.putResult(finderClassName, finderMethodName,
457 finderParams, finderArgs, list);
458
459 return list;
460 }
461 catch (Exception e) {
462 throw HibernateUtil.processException(e);
463 }
464 finally {
465 closeSession(session);
466 }
467 }
468 else {
469 return (List)result;
470 }
471 }
472
473 public void removeBySize(int size) throws SystemException {
474 Iterator itr = findBySize(size).iterator();
475
476 while (itr.hasNext()) {
477 Image image = (Image)itr.next();
478 remove(image);
479 }
480 }
481
482 public void removeAll() throws SystemException {
483 Iterator itr = findAll().iterator();
484
485 while (itr.hasNext()) {
486 remove((Image)itr.next());
487 }
488 }
489
490 public int countBySize(int size) throws SystemException {
491 String finderClassName = Image.class.getName();
492 String finderMethodName = "countBySize";
493 String[] finderParams = new String[] { Integer.class.getName() };
494 Object[] finderArgs = new Object[] { new Integer(size) };
495 Object result = FinderCache.getResult(finderClassName,
496 finderMethodName, finderParams, finderArgs, getSessionFactory());
497
498 if (result == null) {
499 Session session = null;
500
501 try {
502 session = openSession();
503
504 StringMaker query = new StringMaker();
505 query.append("SELECT COUNT(*) ");
506 query.append("FROM com.liferay.portal.model.Image WHERE ");
507 query.append("size_ < ?");
508 query.append(" ");
509
510 Query q = session.createQuery(query.toString());
511 int queryPos = 0;
512 q.setInteger(queryPos++, size);
513
514 Long count = null;
515 Iterator itr = q.list().iterator();
516
517 if (itr.hasNext()) {
518 count = (Long)itr.next();
519 }
520
521 if (count == null) {
522 count = new Long(0);
523 }
524
525 FinderCache.putResult(finderClassName, finderMethodName,
526 finderParams, finderArgs, count);
527
528 return count.intValue();
529 }
530 catch (Exception e) {
531 throw HibernateUtil.processException(e);
532 }
533 finally {
534 closeSession(session);
535 }
536 }
537 else {
538 return ((Long)result).intValue();
539 }
540 }
541
542 public int countAll() throws SystemException {
543 String finderClassName = Image.class.getName();
544 String finderMethodName = "countAll";
545 String[] finderParams = new String[] { };
546 Object[] finderArgs = new Object[] { };
547 Object result = FinderCache.getResult(finderClassName,
548 finderMethodName, finderParams, finderArgs, getSessionFactory());
549
550 if (result == null) {
551 Session session = null;
552
553 try {
554 session = openSession();
555
556 StringMaker query = new StringMaker();
557 query.append("SELECT COUNT(*) ");
558 query.append("FROM com.liferay.portal.model.Image");
559
560 Query q = session.createQuery(query.toString());
561 Long count = null;
562 Iterator itr = q.list().iterator();
563
564 if (itr.hasNext()) {
565 count = (Long)itr.next();
566 }
567
568 if (count == null) {
569 count = new Long(0);
570 }
571
572 FinderCache.putResult(finderClassName, finderMethodName,
573 finderParams, finderArgs, count);
574
575 return count.intValue();
576 }
577 catch (Exception e) {
578 throw HibernateUtil.processException(e);
579 }
580 finally {
581 closeSession(session);
582 }
583 }
584 else {
585 return ((Long)result).intValue();
586 }
587 }
588
589 protected void initDao() {
590 }
591
592 private static Log _log = LogFactory.getLog(ImagePersistenceImpl.class);
593 }