1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.imagegallery.NoSuchImageException;
36  import com.liferay.portlet.imagegallery.model.IGImage;
37  import com.liferay.portlet.imagegallery.model.impl.IGImageImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="IGImagePersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class IGImagePersistenceImpl extends BasePersistence
58      implements IGImagePersistence {
59      public IGImage create(long imageId) {
60          IGImage igImage = new IGImageImpl();
61          igImage.setNew(true);
62          igImage.setPrimaryKey(imageId);
63  
64          return igImage;
65      }
66  
67      public IGImage remove(long imageId)
68          throws NoSuchImageException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              IGImage igImage = (IGImage)session.get(IGImageImpl.class,
75                      new Long(imageId));
76  
77              if (igImage == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No IGImage exists with the primary key " +
80                          imageId);
81                  }
82  
83                  throw new NoSuchImageException(
84                      "No IGImage exists with the primary key " + imageId);
85              }
86  
87              return remove(igImage);
88          }
89          catch (NoSuchImageException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public IGImage remove(IGImage igImage) throws SystemException {
101         Session session = null;
102 
103         try {
104             session = openSession();
105             session.delete(igImage);
106             session.flush();
107 
108             return igImage;
109         }
110         catch (Exception e) {
111             throw HibernateUtil.processException(e);
112         }
113         finally {
114             closeSession(session);
115             FinderCache.clearCache(IGImage.class.getName());
116         }
117     }
118 
119     public IGImage update(
120         com.liferay.portlet.imagegallery.model.IGImage igImage)
121         throws SystemException {
122         return update(igImage, false);
123     }
124 
125     public IGImage update(
126         com.liferay.portlet.imagegallery.model.IGImage igImage, boolean merge)
127         throws SystemException {
128         Session session = null;
129 
130         try {
131             session = openSession();
132 
133             if (merge) {
134                 session.merge(igImage);
135             }
136             else {
137                 if (igImage.isNew()) {
138                     session.save(igImage);
139                 }
140             }
141 
142             session.flush();
143             igImage.setNew(false);
144 
145             return igImage;
146         }
147         catch (Exception e) {
148             throw HibernateUtil.processException(e);
149         }
150         finally {
151             closeSession(session);
152             FinderCache.clearCache(IGImage.class.getName());
153         }
154     }
155 
156     public IGImage findByPrimaryKey(long imageId)
157         throws NoSuchImageException, SystemException {
158         IGImage igImage = fetchByPrimaryKey(imageId);
159 
160         if (igImage == null) {
161             if (_log.isWarnEnabled()) {
162                 _log.warn("No IGImage exists with the primary key " + imageId);
163             }
164 
165             throw new NoSuchImageException(
166                 "No IGImage exists with the primary key " + imageId);
167         }
168 
169         return igImage;
170     }
171 
172     public IGImage fetchByPrimaryKey(long imageId) throws SystemException {
173         Session session = null;
174 
175         try {
176             session = openSession();
177 
178             return (IGImage)session.get(IGImageImpl.class, new Long(imageId));
179         }
180         catch (Exception e) {
181             throw HibernateUtil.processException(e);
182         }
183         finally {
184             closeSession(session);
185         }
186     }
187 
188     public List findByFolderId(long folderId) throws SystemException {
189         String finderClassName = IGImage.class.getName();
190         String finderMethodName = "findByFolderId";
191         String[] finderParams = new String[] { Long.class.getName() };
192         Object[] finderArgs = new Object[] { new Long(folderId) };
193         Object result = FinderCache.getResult(finderClassName,
194                 finderMethodName, finderParams, finderArgs, getSessionFactory());
195 
196         if (result == null) {
197             Session session = null;
198 
199             try {
200                 session = openSession();
201 
202                 StringMaker query = new StringMaker();
203                 query.append(
204                     "FROM com.liferay.portlet.imagegallery.model.IGImage WHERE ");
205                 query.append("folderId = ?");
206                 query.append(" ");
207                 query.append("ORDER BY ");
208                 query.append("imageId ASC");
209 
210                 Query q = session.createQuery(query.toString());
211                 int queryPos = 0;
212                 q.setLong(queryPos++, folderId);
213 
214                 List list = q.list();
215                 FinderCache.putResult(finderClassName, finderMethodName,
216                     finderParams, finderArgs, list);
217 
218                 return list;
219             }
220             catch (Exception e) {
221                 throw HibernateUtil.processException(e);
222             }
223             finally {
224                 closeSession(session);
225             }
226         }
227         else {
228             return (List)result;
229         }
230     }
231 
232     public List findByFolderId(long folderId, int begin, int end)
233         throws SystemException {
234         return findByFolderId(folderId, begin, end, null);
235     }
236 
237     public List findByFolderId(long folderId, int begin, int end,
238         OrderByComparator obc) throws SystemException {
239         String finderClassName = IGImage.class.getName();
240         String finderMethodName = "findByFolderId";
241         String[] finderParams = new String[] {
242                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
243                 "com.liferay.portal.kernel.util.OrderByComparator"
244             };
245         Object[] finderArgs = new Object[] {
246                 new Long(folderId), String.valueOf(begin), String.valueOf(end),
247                 String.valueOf(obc)
248             };
249         Object result = FinderCache.getResult(finderClassName,
250                 finderMethodName, finderParams, finderArgs, getSessionFactory());
251 
252         if (result == null) {
253             Session session = null;
254 
255             try {
256                 session = openSession();
257 
258                 StringMaker query = new StringMaker();
259                 query.append(
260                     "FROM com.liferay.portlet.imagegallery.model.IGImage WHERE ");
261                 query.append("folderId = ?");
262                 query.append(" ");
263 
264                 if (obc != null) {
265                     query.append("ORDER BY ");
266                     query.append(obc.getOrderBy());
267                 }
268                 else {
269                     query.append("ORDER BY ");
270                     query.append("imageId ASC");
271                 }
272 
273                 Query q = session.createQuery(query.toString());
274                 int queryPos = 0;
275                 q.setLong(queryPos++, folderId);
276 
277                 List list = QueryUtil.list(q, getDialect(), begin, end);
278                 FinderCache.putResult(finderClassName, finderMethodName,
279                     finderParams, 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 IGImage findByFolderId_First(long folderId, OrderByComparator obc)
296         throws NoSuchImageException, SystemException {
297         List list = findByFolderId(folderId, 0, 1, obc);
298 
299         if (list.size() == 0) {
300             StringMaker msg = new StringMaker();
301             msg.append("No IGImage exists with the key ");
302             msg.append(StringPool.OPEN_CURLY_BRACE);
303             msg.append("folderId=");
304             msg.append(folderId);
305             msg.append(StringPool.CLOSE_CURLY_BRACE);
306             throw new NoSuchImageException(msg.toString());
307         }
308         else {
309             return (IGImage)list.get(0);
310         }
311     }
312 
313     public IGImage findByFolderId_Last(long folderId, OrderByComparator obc)
314         throws NoSuchImageException, SystemException {
315         int count = countByFolderId(folderId);
316         List list = findByFolderId(folderId, count - 1, count, obc);
317 
318         if (list.size() == 0) {
319             StringMaker msg = new StringMaker();
320             msg.append("No IGImage exists with the key ");
321             msg.append(StringPool.OPEN_CURLY_BRACE);
322             msg.append("folderId=");
323             msg.append(folderId);
324             msg.append(StringPool.CLOSE_CURLY_BRACE);
325             throw new NoSuchImageException(msg.toString());
326         }
327         else {
328             return (IGImage)list.get(0);
329         }
330     }
331 
332     public IGImage[] findByFolderId_PrevAndNext(long imageId, long folderId,
333         OrderByComparator obc) throws NoSuchImageException, SystemException {
334         IGImage igImage = findByPrimaryKey(imageId);
335         int count = countByFolderId(folderId);
336         Session session = null;
337 
338         try {
339             session = openSession();
340 
341             StringMaker query = new StringMaker();
342             query.append(
343                 "FROM com.liferay.portlet.imagegallery.model.IGImage WHERE ");
344             query.append("folderId = ?");
345             query.append(" ");
346 
347             if (obc != null) {
348                 query.append("ORDER BY ");
349                 query.append(obc.getOrderBy());
350             }
351             else {
352                 query.append("ORDER BY ");
353                 query.append("imageId ASC");
354             }
355 
356             Query q = session.createQuery(query.toString());
357             int queryPos = 0;
358             q.setLong(queryPos++, folderId);
359 
360             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, igImage);
361             IGImage[] array = new IGImageImpl[3];
362             array[0] = (IGImage)objArray[0];
363             array[1] = (IGImage)objArray[1];
364             array[2] = (IGImage)objArray[2];
365 
366             return array;
367         }
368         catch (Exception e) {
369             throw HibernateUtil.processException(e);
370         }
371         finally {
372             closeSession(session);
373         }
374     }
375 
376     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
377         throws SystemException {
378         Session session = null;
379 
380         try {
381             session = openSession();
382 
383             DynamicQuery query = queryInitializer.initialize(session);
384 
385             return query.list();
386         }
387         catch (Exception e) {
388             throw HibernateUtil.processException(e);
389         }
390         finally {
391             closeSession(session);
392         }
393     }
394 
395     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
396         int begin, int end) throws SystemException {
397         Session session = null;
398 
399         try {
400             session = openSession();
401 
402             DynamicQuery query = queryInitializer.initialize(session);
403             query.setLimit(begin, end);
404 
405             return query.list();
406         }
407         catch (Exception e) {
408             throw HibernateUtil.processException(e);
409         }
410         finally {
411             closeSession(session);
412         }
413     }
414 
415     public List findAll() throws SystemException {
416         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
417     }
418 
419     public List findAll(int begin, int end) throws SystemException {
420         return findAll(begin, end, null);
421     }
422 
423     public List findAll(int begin, int end, OrderByComparator obc)
424         throws SystemException {
425         String finderClassName = IGImage.class.getName();
426         String finderMethodName = "findAll";
427         String[] finderParams = new String[] {
428                 "java.lang.Integer", "java.lang.Integer",
429                 "com.liferay.portal.kernel.util.OrderByComparator"
430             };
431         Object[] finderArgs = new Object[] {
432                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
433             };
434         Object result = FinderCache.getResult(finderClassName,
435                 finderMethodName, finderParams, finderArgs, getSessionFactory());
436 
437         if (result == null) {
438             Session session = null;
439 
440             try {
441                 session = openSession();
442 
443                 StringMaker query = new StringMaker();
444                 query.append(
445                     "FROM com.liferay.portlet.imagegallery.model.IGImage ");
446 
447                 if (obc != null) {
448                     query.append("ORDER BY ");
449                     query.append(obc.getOrderBy());
450                 }
451                 else {
452                     query.append("ORDER BY ");
453                     query.append("imageId ASC");
454                 }
455 
456                 Query q = session.createQuery(query.toString());
457                 List list = QueryUtil.list(q, getDialect(), begin, end);
458 
459                 if (obc == null) {
460                     Collections.sort(list);
461                 }
462 
463                 FinderCache.putResult(finderClassName, finderMethodName,
464                     finderParams, finderArgs, list);
465 
466                 return list;
467             }
468             catch (Exception e) {
469                 throw HibernateUtil.processException(e);
470             }
471             finally {
472                 closeSession(session);
473             }
474         }
475         else {
476             return (List)result;
477         }
478     }
479 
480     public void removeByFolderId(long folderId) throws SystemException {
481         Iterator itr = findByFolderId(folderId).iterator();
482 
483         while (itr.hasNext()) {
484             IGImage igImage = (IGImage)itr.next();
485             remove(igImage);
486         }
487     }
488 
489     public void removeAll() throws SystemException {
490         Iterator itr = findAll().iterator();
491 
492         while (itr.hasNext()) {
493             remove((IGImage)itr.next());
494         }
495     }
496 
497     public int countByFolderId(long folderId) throws SystemException {
498         String finderClassName = IGImage.class.getName();
499         String finderMethodName = "countByFolderId";
500         String[] finderParams = new String[] { Long.class.getName() };
501         Object[] finderArgs = new Object[] { new Long(folderId) };
502         Object result = FinderCache.getResult(finderClassName,
503                 finderMethodName, finderParams, finderArgs, getSessionFactory());
504 
505         if (result == null) {
506             Session session = null;
507 
508             try {
509                 session = openSession();
510 
511                 StringMaker query = new StringMaker();
512                 query.append("SELECT COUNT(*) ");
513                 query.append(
514                     "FROM com.liferay.portlet.imagegallery.model.IGImage WHERE ");
515                 query.append("folderId = ?");
516                 query.append(" ");
517 
518                 Query q = session.createQuery(query.toString());
519                 int queryPos = 0;
520                 q.setLong(queryPos++, folderId);
521 
522                 Long count = null;
523                 Iterator itr = q.list().iterator();
524 
525                 if (itr.hasNext()) {
526                     count = (Long)itr.next();
527                 }
528 
529                 if (count == null) {
530                     count = new Long(0);
531                 }
532 
533                 FinderCache.putResult(finderClassName, finderMethodName,
534                     finderParams, finderArgs, count);
535 
536                 return count.intValue();
537             }
538             catch (Exception e) {
539                 throw HibernateUtil.processException(e);
540             }
541             finally {
542                 closeSession(session);
543             }
544         }
545         else {
546             return ((Long)result).intValue();
547         }
548     }
549 
550     public int countAll() throws SystemException {
551         String finderClassName = IGImage.class.getName();
552         String finderMethodName = "countAll";
553         String[] finderParams = new String[] {  };
554         Object[] finderArgs = new Object[] {  };
555         Object result = FinderCache.getResult(finderClassName,
556                 finderMethodName, finderParams, finderArgs, getSessionFactory());
557 
558         if (result == null) {
559             Session session = null;
560 
561             try {
562                 session = openSession();
563 
564                 StringMaker query = new StringMaker();
565                 query.append("SELECT COUNT(*) ");
566                 query.append(
567                     "FROM com.liferay.portlet.imagegallery.model.IGImage");
568 
569                 Query q = session.createQuery(query.toString());
570                 Long count = null;
571                 Iterator itr = q.list().iterator();
572 
573                 if (itr.hasNext()) {
574                     count = (Long)itr.next();
575                 }
576 
577                 if (count == null) {
578                     count = new Long(0);
579                 }
580 
581                 FinderCache.putResult(finderClassName, finderMethodName,
582                     finderParams, finderArgs, count);
583 
584                 return count.intValue();
585             }
586             catch (Exception e) {
587                 throw HibernateUtil.processException(e);
588             }
589             finally {
590                 closeSession(session);
591             }
592         }
593         else {
594             return ((Long)result).intValue();
595         }
596     }
597 
598     protected void initDao() {
599     }
600 
601     private static Log _log = LogFactory.getLog(IGImagePersistenceImpl.class);
602 }