1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchImageException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
25  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
26  import com.liferay.portal.kernel.dao.orm.Query;
27  import com.liferay.portal.kernel.dao.orm.QueryPos;
28  import com.liferay.portal.kernel.dao.orm.QueryUtil;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.Image;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.model.impl.ImageImpl;
39  import com.liferay.portal.model.impl.ImageModelImpl;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   * <a href="ImagePersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ImagePersistenceImpl extends BasePersistenceImpl
54      implements ImagePersistence {
55      public Image create(long imageId) {
56          Image image = new ImageImpl();
57  
58          image.setNew(true);
59          image.setPrimaryKey(imageId);
60  
61          return image;
62      }
63  
64      public Image remove(long imageId)
65          throws NoSuchImageException, SystemException {
66          Session session = null;
67  
68          try {
69              session = openSession();
70  
71              Image image = (Image)session.get(ImageImpl.class, new Long(imageId));
72  
73              if (image == null) {
74                  if (_log.isWarnEnabled()) {
75                      _log.warn("No Image exists with the primary key " +
76                          imageId);
77                  }
78  
79                  throw new NoSuchImageException(
80                      "No Image exists with the primary key " + imageId);
81              }
82  
83              return remove(image);
84          }
85          catch (NoSuchImageException nsee) {
86              throw nsee;
87          }
88          catch (Exception e) {
89              throw processException(e);
90          }
91          finally {
92              closeSession(session);
93          }
94      }
95  
96      public Image remove(Image image) throws SystemException {
97          for (ModelListener listener : listeners) {
98              listener.onBeforeRemove(image);
99          }
100 
101         image = removeImpl(image);
102 
103         for (ModelListener listener : listeners) {
104             listener.onAfterRemove(image);
105         }
106 
107         return image;
108     }
109 
110     protected Image removeImpl(Image image) throws SystemException {
111         Session session = null;
112 
113         try {
114             session = openSession();
115 
116             if (BatchSessionUtil.isEnabled()) {
117                 Object staleObject = session.get(ImageImpl.class,
118                         image.getPrimaryKeyObj());
119 
120                 if (staleObject != null) {
121                     session.evict(staleObject);
122                 }
123             }
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     /**
142      * @deprecated Use <code>update(Image image, boolean merge)</code>.
143      */
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     /**
154      * Add, update, or merge, the entity. This method also calls the model
155      * listeners to trigger the proper events associated with adding, deleting,
156      * or updating an entity.
157      *
158      * @param        image the entity to add, update, or merge
159      * @param        merge boolean value for whether to merge the entity. The
160      *                default value is false. Setting merge to true is more
161      *                expensive and should only be true when image is
162      *                transient. See LEP-5473 for a detailed discussion of this
163      *                method.
164      * @return        true if the portlet can be displayed via Ajax
165      */
166     public Image update(Image image, boolean merge) throws SystemException {
167         boolean isNew = image.isNew();
168 
169         for (ModelListener listener : listeners) {
170             if (isNew) {
171                 listener.onBeforeCreate(image);
172             }
173             else {
174                 listener.onBeforeUpdate(image);
175             }
176         }
177 
178         image = updateImpl(image, merge);
179 
180         for (ModelListener listener : listeners) {
181             if (isNew) {
182                 listener.onAfterCreate(image);
183             }
184             else {
185                 listener.onAfterUpdate(image);
186             }
187         }
188 
189         return image;
190     }
191 
192     public Image updateImpl(com.liferay.portal.model.Image image, boolean merge)
193         throws SystemException {
194         Session session = null;
195 
196         try {
197             session = openSession();
198 
199             BatchSessionUtil.update(session, image, merge);
200 
201             image.setNew(false);
202 
203             return image;
204         }
205         catch (Exception e) {
206             throw processException(e);
207         }
208         finally {
209             closeSession(session);
210 
211             FinderCacheUtil.clearCache(Image.class.getName());
212         }
213     }
214 
215     public Image findByPrimaryKey(long imageId)
216         throws NoSuchImageException, SystemException {
217         Image image = fetchByPrimaryKey(imageId);
218 
219         if (image == null) {
220             if (_log.isWarnEnabled()) {
221                 _log.warn("No Image exists with the primary key " + imageId);
222             }
223 
224             throw new NoSuchImageException(
225                 "No Image exists with the primary key " + imageId);
226         }
227 
228         return image;
229     }
230 
231     public Image fetchByPrimaryKey(long imageId) throws SystemException {
232         Session session = null;
233 
234         try {
235             session = openSession();
236 
237             return (Image)session.get(ImageImpl.class, new Long(imageId));
238         }
239         catch (Exception e) {
240             throw processException(e);
241         }
242         finally {
243             closeSession(session);
244         }
245     }
246 
247     public List<Image> findBySize(int size) throws SystemException {
248         boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
249         String finderClassName = Image.class.getName();
250         String finderMethodName = "findBySize";
251         String[] finderParams = new String[] { Integer.class.getName() };
252         Object[] finderArgs = new Object[] { new Integer(size) };
253 
254         Object result = null;
255 
256         if (finderClassNameCacheEnabled) {
257             result = FinderCacheUtil.getResult(finderClassName,
258                     finderMethodName, finderParams, finderArgs, this);
259         }
260 
261         if (result == null) {
262             Session session = null;
263 
264             try {
265                 session = openSession();
266 
267                 StringBuilder query = new StringBuilder();
268 
269                 query.append("FROM com.liferay.portal.model.Image WHERE ");
270 
271                 query.append("size_ < ?");
272 
273                 query.append(" ");
274 
275                 query.append("ORDER BY ");
276 
277                 query.append("imageId ASC");
278 
279                 Query q = session.createQuery(query.toString());
280 
281                 QueryPos qPos = QueryPos.getInstance(q);
282 
283                 qPos.add(size);
284 
285                 List<Image> list = q.list();
286 
287                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
288                     finderClassName, finderMethodName, finderParams,
289                     finderArgs, list);
290 
291                 return list;
292             }
293             catch (Exception e) {
294                 throw processException(e);
295             }
296             finally {
297                 closeSession(session);
298             }
299         }
300         else {
301             return (List<Image>)result;
302         }
303     }
304 
305     public List<Image> findBySize(int size, int start, int end)
306         throws SystemException {
307         return findBySize(size, start, end, null);
308     }
309 
310     public List<Image> findBySize(int size, int start, int end,
311         OrderByComparator obc) throws SystemException {
312         boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
313         String finderClassName = Image.class.getName();
314         String finderMethodName = "findBySize";
315         String[] finderParams = new String[] {
316                 Integer.class.getName(),
317                 
318                 "java.lang.Integer", "java.lang.Integer",
319                 "com.liferay.portal.kernel.util.OrderByComparator"
320             };
321         Object[] finderArgs = new Object[] {
322                 new Integer(size),
323                 
324                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
325             };
326 
327         Object result = null;
328 
329         if (finderClassNameCacheEnabled) {
330             result = FinderCacheUtil.getResult(finderClassName,
331                     finderMethodName, finderParams, finderArgs, this);
332         }
333 
334         if (result == null) {
335             Session session = null;
336 
337             try {
338                 session = openSession();
339 
340                 StringBuilder query = new StringBuilder();
341 
342                 query.append("FROM com.liferay.portal.model.Image WHERE ");
343 
344                 query.append("size_ < ?");
345 
346                 query.append(" ");
347 
348                 if (obc != null) {
349                     query.append("ORDER BY ");
350                     query.append(obc.getOrderBy());
351                 }
352 
353                 else {
354                     query.append("ORDER BY ");
355 
356                     query.append("imageId ASC");
357                 }
358 
359                 Query q = session.createQuery(query.toString());
360 
361                 QueryPos qPos = QueryPos.getInstance(q);
362 
363                 qPos.add(size);
364 
365                 List<Image> list = (List<Image>)QueryUtil.list(q, getDialect(),
366                         start, end);
367 
368                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
369                     finderClassName, finderMethodName, finderParams,
370                     finderArgs, list);
371 
372                 return list;
373             }
374             catch (Exception e) {
375                 throw processException(e);
376             }
377             finally {
378                 closeSession(session);
379             }
380         }
381         else {
382             return (List<Image>)result;
383         }
384     }
385 
386     public Image findBySize_First(int size, OrderByComparator obc)
387         throws NoSuchImageException, SystemException {
388         List<Image> list = findBySize(size, 0, 1, obc);
389 
390         if (list.size() == 0) {
391             StringBuilder msg = new StringBuilder();
392 
393             msg.append("No Image exists with the key {");
394 
395             msg.append("size=" + size);
396 
397             msg.append(StringPool.CLOSE_CURLY_BRACE);
398 
399             throw new NoSuchImageException(msg.toString());
400         }
401         else {
402             return list.get(0);
403         }
404     }
405 
406     public Image findBySize_Last(int size, OrderByComparator obc)
407         throws NoSuchImageException, SystemException {
408         int count = countBySize(size);
409 
410         List<Image> list = findBySize(size, count - 1, count, obc);
411 
412         if (list.size() == 0) {
413             StringBuilder msg = new StringBuilder();
414 
415             msg.append("No Image exists with the key {");
416 
417             msg.append("size=" + size);
418 
419             msg.append(StringPool.CLOSE_CURLY_BRACE);
420 
421             throw new NoSuchImageException(msg.toString());
422         }
423         else {
424             return list.get(0);
425         }
426     }
427 
428     public Image[] findBySize_PrevAndNext(long imageId, int size,
429         OrderByComparator obc) throws NoSuchImageException, SystemException {
430         Image image = findByPrimaryKey(imageId);
431 
432         int count = countBySize(size);
433 
434         Session session = null;
435 
436         try {
437             session = openSession();
438 
439             StringBuilder query = new StringBuilder();
440 
441             query.append("FROM com.liferay.portal.model.Image WHERE ");
442 
443             query.append("size_ < ?");
444 
445             query.append(" ");
446 
447             if (obc != null) {
448                 query.append("ORDER BY ");
449                 query.append(obc.getOrderBy());
450             }
451 
452             else {
453                 query.append("ORDER BY ");
454 
455                 query.append("imageId ASC");
456             }
457 
458             Query q = session.createQuery(query.toString());
459 
460             QueryPos qPos = QueryPos.getInstance(q);
461 
462             qPos.add(size);
463 
464             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, image);
465 
466             Image[] array = new ImageImpl[3];
467 
468             array[0] = (Image)objArray[0];
469             array[1] = (Image)objArray[1];
470             array[2] = (Image)objArray[2];
471 
472             return array;
473         }
474         catch (Exception e) {
475             throw processException(e);
476         }
477         finally {
478             closeSession(session);
479         }
480     }
481 
482     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
483         throws SystemException {
484         Session session = null;
485 
486         try {
487             session = openSession();
488 
489             dynamicQuery.compile(session);
490 
491             return dynamicQuery.list();
492         }
493         catch (Exception e) {
494             throw processException(e);
495         }
496         finally {
497             closeSession(session);
498         }
499     }
500 
501     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
502         int start, int end) throws SystemException {
503         Session session = null;
504 
505         try {
506             session = openSession();
507 
508             dynamicQuery.setLimit(start, end);
509 
510             dynamicQuery.compile(session);
511 
512             return dynamicQuery.list();
513         }
514         catch (Exception e) {
515             throw processException(e);
516         }
517         finally {
518             closeSession(session);
519         }
520     }
521 
522     public List<Image> findAll() throws SystemException {
523         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
524     }
525 
526     public List<Image> findAll(int start, int end) throws SystemException {
527         return findAll(start, end, null);
528     }
529 
530     public List<Image> findAll(int start, int end, OrderByComparator obc)
531         throws SystemException {
532         boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
533         String finderClassName = Image.class.getName();
534         String finderMethodName = "findAll";
535         String[] finderParams = new String[] {
536                 "java.lang.Integer", "java.lang.Integer",
537                 "com.liferay.portal.kernel.util.OrderByComparator"
538             };
539         Object[] finderArgs = new Object[] {
540                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
541             };
542 
543         Object result = null;
544 
545         if (finderClassNameCacheEnabled) {
546             result = FinderCacheUtil.getResult(finderClassName,
547                     finderMethodName, finderParams, finderArgs, this);
548         }
549 
550         if (result == null) {
551             Session session = null;
552 
553             try {
554                 session = openSession();
555 
556                 StringBuilder query = new StringBuilder();
557 
558                 query.append("FROM com.liferay.portal.model.Image ");
559 
560                 if (obc != null) {
561                     query.append("ORDER BY ");
562                     query.append(obc.getOrderBy());
563                 }
564 
565                 else {
566                     query.append("ORDER BY ");
567 
568                     query.append("imageId ASC");
569                 }
570 
571                 Query q = session.createQuery(query.toString());
572 
573                 List<Image> list = null;
574 
575                 if (obc == null) {
576                     list = (List<Image>)QueryUtil.list(q, getDialect(), start,
577                             end, false);
578 
579                     Collections.sort(list);
580                 }
581                 else {
582                     list = (List<Image>)QueryUtil.list(q, getDialect(), start,
583                             end);
584                 }
585 
586                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
587                     finderClassName, finderMethodName, finderParams,
588                     finderArgs, list);
589 
590                 return list;
591             }
592             catch (Exception e) {
593                 throw processException(e);
594             }
595             finally {
596                 closeSession(session);
597             }
598         }
599         else {
600             return (List<Image>)result;
601         }
602     }
603 
604     public void removeBySize(int size) throws SystemException {
605         for (Image image : findBySize(size)) {
606             remove(image);
607         }
608     }
609 
610     public void removeAll() throws SystemException {
611         for (Image image : findAll()) {
612             remove(image);
613         }
614     }
615 
616     public int countBySize(int size) throws SystemException {
617         boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
618         String finderClassName = Image.class.getName();
619         String finderMethodName = "countBySize";
620         String[] finderParams = new String[] { Integer.class.getName() };
621         Object[] finderArgs = new Object[] { new Integer(size) };
622 
623         Object result = null;
624 
625         if (finderClassNameCacheEnabled) {
626             result = FinderCacheUtil.getResult(finderClassName,
627                     finderMethodName, finderParams, finderArgs, this);
628         }
629 
630         if (result == null) {
631             Session session = null;
632 
633             try {
634                 session = openSession();
635 
636                 StringBuilder query = new StringBuilder();
637 
638                 query.append("SELECT COUNT(*) ");
639                 query.append("FROM com.liferay.portal.model.Image WHERE ");
640 
641                 query.append("size_ < ?");
642 
643                 query.append(" ");
644 
645                 Query q = session.createQuery(query.toString());
646 
647                 QueryPos qPos = QueryPos.getInstance(q);
648 
649                 qPos.add(size);
650 
651                 Long count = null;
652 
653                 Iterator<Long> itr = q.list().iterator();
654 
655                 if (itr.hasNext()) {
656                     count = itr.next();
657                 }
658 
659                 if (count == null) {
660                     count = new Long(0);
661                 }
662 
663                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
664                     finderClassName, finderMethodName, finderParams,
665                     finderArgs, count);
666 
667                 return count.intValue();
668             }
669             catch (Exception e) {
670                 throw processException(e);
671             }
672             finally {
673                 closeSession(session);
674             }
675         }
676         else {
677             return ((Long)result).intValue();
678         }
679     }
680 
681     public int countAll() throws SystemException {
682         boolean finderClassNameCacheEnabled = ImageModelImpl.CACHE_ENABLED;
683         String finderClassName = Image.class.getName();
684         String finderMethodName = "countAll";
685         String[] finderParams = new String[] {  };
686         Object[] finderArgs = new Object[] {  };
687 
688         Object result = null;
689 
690         if (finderClassNameCacheEnabled) {
691             result = FinderCacheUtil.getResult(finderClassName,
692                     finderMethodName, finderParams, finderArgs, this);
693         }
694 
695         if (result == null) {
696             Session session = null;
697 
698             try {
699                 session = openSession();
700 
701                 Query q = session.createQuery(
702                         "SELECT COUNT(*) FROM com.liferay.portal.model.Image");
703 
704                 Long count = null;
705 
706                 Iterator<Long> itr = q.list().iterator();
707 
708                 if (itr.hasNext()) {
709                     count = itr.next();
710                 }
711 
712                 if (count == null) {
713                     count = new Long(0);
714                 }
715 
716                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
717                     finderClassName, finderMethodName, finderParams,
718                     finderArgs, count);
719 
720                 return count.intValue();
721             }
722             catch (Exception e) {
723                 throw processException(e);
724             }
725             finally {
726                 closeSession(session);
727             }
728         }
729         else {
730             return ((Long)result).intValue();
731         }
732     }
733 
734     public void afterPropertiesSet() {
735         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
736                     com.liferay.portal.util.PropsUtil.get(
737                         "value.object.listener.com.liferay.portal.model.Image")));
738 
739         if (listenerClassNames.length > 0) {
740             try {
741                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
742 
743                 for (String listenerClassName : listenerClassNames) {
744                     listenersList.add((ModelListener)Class.forName(
745                             listenerClassName).newInstance());
746                 }
747 
748                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
749             }
750             catch (Exception e) {
751                 _log.error(e);
752             }
753         }
754     }
755 
756     private static Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
757 }