001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchImageException;
018    import com.liferay.portal.NoSuchModelException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.Image;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.impl.ImageImpl;
040    import com.liferay.portal.model.impl.ImageModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import com.liferay.portlet.imagegallery.service.persistence.IGImagePersistence;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.List;
050    
051    /**
052     * The persistence for the image service.
053     *
054     * <p>
055     * Never modify this class directly. Modify <code>service.xml</code> and rerun ServiceBuilder to regnerate this class.
056     * </p>
057     *
058     * <p>
059     * Caching information and settings can be found in <code>portal.properties</code>
060     * </p>
061     *
062     * @author Brian Wing Shun Chan
063     * @see ImagePersistence
064     * @see ImageUtil
065     * @generated
066     */
067    public class ImagePersistenceImpl extends BasePersistenceImpl<Image>
068            implements ImagePersistence {
069            public static final String FINDER_CLASS_NAME_ENTITY = ImageImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
071                    ".List";
072            public static final FinderPath FINDER_PATH_FIND_BY_LTSIZE = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
073                            ImageModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
074                            "findByLtSize",
075                            new String[] {
076                                    Integer.class.getName(),
077                                    
078                            "java.lang.Integer", "java.lang.Integer",
079                                    "com.liferay.portal.kernel.util.OrderByComparator"
080                            });
081            public static final FinderPath FINDER_PATH_COUNT_BY_LTSIZE = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
082                            ImageModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
083                            "countByLtSize", new String[] { Integer.class.getName() });
084            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
085                            ImageModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
086                            "findAll", new String[0]);
087            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
088                            ImageModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
089                            "countAll", new String[0]);
090    
091            /**
092             * Caches the image in the entity cache if it is enabled.
093             *
094             * @param image the image to cache
095             */
096            public void cacheResult(Image image) {
097                    EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
098                            ImageImpl.class, image.getPrimaryKey(), image);
099            }
100    
101            /**
102             * Caches the images in the entity cache if it is enabled.
103             *
104             * @param images the images to cache
105             */
106            public void cacheResult(List<Image> images) {
107                    for (Image image : images) {
108                            if (EntityCacheUtil.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
109                                                    ImageImpl.class, image.getPrimaryKey(), this) == null) {
110                                    cacheResult(image);
111                            }
112                    }
113            }
114    
115            /**
116             * Clears the cache for all images.
117             *
118             * <p>
119             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
120             * </p>
121             */
122            public void clearCache() {
123                    CacheRegistryUtil.clear(ImageImpl.class.getName());
124                    EntityCacheUtil.clearCache(ImageImpl.class.getName());
125                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
126                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
127            }
128    
129            /**
130             * Clears the cache for the image.
131             *
132             * <p>
133             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
134             * </p>
135             */
136            public void clearCache(Image image) {
137                    EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
138                            ImageImpl.class, image.getPrimaryKey());
139            }
140    
141            /**
142             * Creates a new image with the primary key.
143             *
144             * @param imageId the primary key for the new image
145             * @return the new image
146             */
147            public Image create(long imageId) {
148                    Image image = new ImageImpl();
149    
150                    image.setNew(true);
151                    image.setPrimaryKey(imageId);
152    
153                    return image;
154            }
155    
156            /**
157             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
158             *
159             * @param primaryKey the primary key of the image to remove
160             * @return the image that was removed
161             * @throws com.liferay.portal.NoSuchModelException if a image with the primary key could not be found
162             * @throws SystemException if a system exception occurred
163             */
164            public Image remove(Serializable primaryKey)
165                    throws NoSuchModelException, SystemException {
166                    return remove(((Long)primaryKey).longValue());
167            }
168    
169            /**
170             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
171             *
172             * @param imageId the primary key of the image to remove
173             * @return the image that was removed
174             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
175             * @throws SystemException if a system exception occurred
176             */
177            public Image remove(long imageId)
178                    throws NoSuchImageException, SystemException {
179                    Session session = null;
180    
181                    try {
182                            session = openSession();
183    
184                            Image image = (Image)session.get(ImageImpl.class, new Long(imageId));
185    
186                            if (image == null) {
187                                    if (_log.isWarnEnabled()) {
188                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + imageId);
189                                    }
190    
191                                    throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
192                                            imageId);
193                            }
194    
195                            return remove(image);
196                    }
197                    catch (NoSuchImageException nsee) {
198                            throw nsee;
199                    }
200                    catch (Exception e) {
201                            throw processException(e);
202                    }
203                    finally {
204                            closeSession(session);
205                    }
206            }
207    
208            protected Image removeImpl(Image image) throws SystemException {
209                    image = toUnwrappedModel(image);
210    
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            if (image.isCachedModel() || BatchSessionUtil.isEnabled()) {
217                                    Object staleObject = session.get(ImageImpl.class,
218                                                    image.getPrimaryKeyObj());
219    
220                                    if (staleObject != null) {
221                                            session.evict(staleObject);
222                                    }
223                            }
224    
225                            session.delete(image);
226    
227                            session.flush();
228                    }
229                    catch (Exception e) {
230                            throw processException(e);
231                    }
232                    finally {
233                            closeSession(session);
234                    }
235    
236                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
237    
238                    EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
239                            ImageImpl.class, image.getPrimaryKey());
240    
241                    return image;
242            }
243    
244            public Image updateImpl(com.liferay.portal.model.Image image, boolean merge)
245                    throws SystemException {
246                    image = toUnwrappedModel(image);
247    
248                    Session session = null;
249    
250                    try {
251                            session = openSession();
252    
253                            BatchSessionUtil.update(session, image, merge);
254    
255                            image.setNew(false);
256                    }
257                    catch (Exception e) {
258                            throw processException(e);
259                    }
260                    finally {
261                            closeSession(session);
262                    }
263    
264                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
265    
266                    EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
267                            ImageImpl.class, image.getPrimaryKey(), image);
268    
269                    return image;
270            }
271    
272            protected Image toUnwrappedModel(Image image) {
273                    if (image instanceof ImageImpl) {
274                            return image;
275                    }
276    
277                    ImageImpl imageImpl = new ImageImpl();
278    
279                    imageImpl.setNew(image.isNew());
280                    imageImpl.setPrimaryKey(image.getPrimaryKey());
281    
282                    imageImpl.setImageId(image.getImageId());
283                    imageImpl.setModifiedDate(image.getModifiedDate());
284                    imageImpl.setText(image.getText());
285                    imageImpl.setType(image.getType());
286                    imageImpl.setHeight(image.getHeight());
287                    imageImpl.setWidth(image.getWidth());
288                    imageImpl.setSize(image.getSize());
289    
290                    return imageImpl;
291            }
292    
293            /**
294             * Finds the image with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
295             *
296             * @param primaryKey the primary key of the image to find
297             * @return the image
298             * @throws com.liferay.portal.NoSuchModelException if a image with the primary key could not be found
299             * @throws SystemException if a system exception occurred
300             */
301            public Image findByPrimaryKey(Serializable primaryKey)
302                    throws NoSuchModelException, SystemException {
303                    return findByPrimaryKey(((Long)primaryKey).longValue());
304            }
305    
306            /**
307             * Finds the image with the primary key or throws a {@link com.liferay.portal.NoSuchImageException} if it could not be found.
308             *
309             * @param imageId the primary key of the image to find
310             * @return the image
311             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
312             * @throws SystemException if a system exception occurred
313             */
314            public Image findByPrimaryKey(long imageId)
315                    throws NoSuchImageException, SystemException {
316                    Image image = fetchByPrimaryKey(imageId);
317    
318                    if (image == null) {
319                            if (_log.isWarnEnabled()) {
320                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + imageId);
321                            }
322    
323                            throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
324                                    imageId);
325                    }
326    
327                    return image;
328            }
329    
330            /**
331             * Finds the image with the primary key or returns <code>null</code> if it could not be found.
332             *
333             * @param primaryKey the primary key of the image to find
334             * @return the image, or <code>null</code> if a image with the primary key could not be found
335             * @throws SystemException if a system exception occurred
336             */
337            public Image fetchByPrimaryKey(Serializable primaryKey)
338                    throws SystemException {
339                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
340            }
341    
342            /**
343             * Finds the image with the primary key or returns <code>null</code> if it could not be found.
344             *
345             * @param imageId the primary key of the image to find
346             * @return the image, or <code>null</code> if a image with the primary key could not be found
347             * @throws SystemException if a system exception occurred
348             */
349            public Image fetchByPrimaryKey(long imageId) throws SystemException {
350                    Image image = (Image)EntityCacheUtil.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
351                                    ImageImpl.class, imageId, this);
352    
353                    if (image == null) {
354                            Session session = null;
355    
356                            try {
357                                    session = openSession();
358    
359                                    image = (Image)session.get(ImageImpl.class, new Long(imageId));
360                            }
361                            catch (Exception e) {
362                                    throw processException(e);
363                            }
364                            finally {
365                                    if (image != null) {
366                                            cacheResult(image);
367                                    }
368    
369                                    closeSession(session);
370                            }
371                    }
372    
373                    return image;
374            }
375    
376            /**
377             * Finds all the images where size &lt; &#63;.
378             *
379             * @param size the size to search with
380             * @return the matching images
381             * @throws SystemException if a system exception occurred
382             */
383            public List<Image> findByLtSize(int size) throws SystemException {
384                    return findByLtSize(size, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
385            }
386    
387            /**
388             * Finds a range of all the images where size &lt; &#63;.
389             *
390             * <p>
391             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
392             * </p>
393             *
394             * @param size the size to search with
395             * @param start the lower bound of the range of images to return
396             * @param end the upper bound of the range of images to return (not inclusive)
397             * @return the range of matching images
398             * @throws SystemException if a system exception occurred
399             */
400            public List<Image> findByLtSize(int size, int start, int end)
401                    throws SystemException {
402                    return findByLtSize(size, start, end, null);
403            }
404    
405            /**
406             * Finds an ordered range of all the images where size &lt; &#63;.
407             *
408             * <p>
409             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
410             * </p>
411             *
412             * @param size the size to search with
413             * @param start the lower bound of the range of images to return
414             * @param end the upper bound of the range of images to return (not inclusive)
415             * @param orderByComparator the comparator to order the results by
416             * @return the ordered range of matching images
417             * @throws SystemException if a system exception occurred
418             */
419            public List<Image> findByLtSize(int size, int start, int end,
420                    OrderByComparator orderByComparator) throws SystemException {
421                    Object[] finderArgs = new Object[] {
422                                    size,
423                                    
424                                    String.valueOf(start), String.valueOf(end),
425                                    String.valueOf(orderByComparator)
426                            };
427    
428                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_LTSIZE,
429                                    finderArgs, this);
430    
431                    if (list == null) {
432                            Session session = null;
433    
434                            try {
435                                    session = openSession();
436    
437                                    StringBundler query = null;
438    
439                                    if (orderByComparator != null) {
440                                            query = new StringBundler(3 +
441                                                            (orderByComparator.getOrderByFields().length * 3));
442                                    }
443                                    else {
444                                            query = new StringBundler(3);
445                                    }
446    
447                                    query.append(_SQL_SELECT_IMAGE_WHERE);
448    
449                                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
450    
451                                    if (orderByComparator != null) {
452                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
453                                                    orderByComparator);
454                                    }
455    
456                                    else {
457                                            query.append(ImageModelImpl.ORDER_BY_JPQL);
458                                    }
459    
460                                    String sql = query.toString();
461    
462                                    Query q = session.createQuery(sql);
463    
464                                    QueryPos qPos = QueryPos.getInstance(q);
465    
466                                    qPos.add(size);
467    
468                                    list = (List<Image>)QueryUtil.list(q, getDialect(), start, end);
469                            }
470                            catch (Exception e) {
471                                    throw processException(e);
472                            }
473                            finally {
474                                    if (list == null) {
475                                            list = new ArrayList<Image>();
476                                    }
477    
478                                    cacheResult(list);
479    
480                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_LTSIZE,
481                                            finderArgs, list);
482    
483                                    closeSession(session);
484                            }
485                    }
486    
487                    return list;
488            }
489    
490            /**
491             * Finds the first image in the ordered set where size &lt; &#63;.
492             *
493             * <p>
494             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
495             * </p>
496             *
497             * @param size the size to search with
498             * @param orderByComparator the comparator to order the set by
499             * @return the first matching image
500             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
501             * @throws SystemException if a system exception occurred
502             */
503            public Image findByLtSize_First(int size,
504                    OrderByComparator orderByComparator)
505                    throws NoSuchImageException, SystemException {
506                    List<Image> list = findByLtSize(size, 0, 1, orderByComparator);
507    
508                    if (list.isEmpty()) {
509                            StringBundler msg = new StringBundler(4);
510    
511                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
512    
513                            msg.append("size=");
514                            msg.append(size);
515    
516                            msg.append(StringPool.CLOSE_CURLY_BRACE);
517    
518                            throw new NoSuchImageException(msg.toString());
519                    }
520                    else {
521                            return list.get(0);
522                    }
523            }
524    
525            /**
526             * Finds the last image in the ordered set where size &lt; &#63;.
527             *
528             * <p>
529             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
530             * </p>
531             *
532             * @param size the size to search with
533             * @param orderByComparator the comparator to order the set by
534             * @return the last matching image
535             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
536             * @throws SystemException if a system exception occurred
537             */
538            public Image findByLtSize_Last(int size, OrderByComparator orderByComparator)
539                    throws NoSuchImageException, SystemException {
540                    int count = countByLtSize(size);
541    
542                    List<Image> list = findByLtSize(size, count - 1, count,
543                                    orderByComparator);
544    
545                    if (list.isEmpty()) {
546                            StringBundler msg = new StringBundler(4);
547    
548                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
549    
550                            msg.append("size=");
551                            msg.append(size);
552    
553                            msg.append(StringPool.CLOSE_CURLY_BRACE);
554    
555                            throw new NoSuchImageException(msg.toString());
556                    }
557                    else {
558                            return list.get(0);
559                    }
560            }
561    
562            /**
563             * Finds the images before and after the current image in the ordered set where size &lt; &#63;.
564             *
565             * <p>
566             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
567             * </p>
568             *
569             * @param imageId the primary key of the current image
570             * @param size the size to search with
571             * @param orderByComparator the comparator to order the set by
572             * @return the previous, current, and next image
573             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
574             * @throws SystemException if a system exception occurred
575             */
576            public Image[] findByLtSize_PrevAndNext(long imageId, int size,
577                    OrderByComparator orderByComparator)
578                    throws NoSuchImageException, SystemException {
579                    Image image = findByPrimaryKey(imageId);
580    
581                    Session session = null;
582    
583                    try {
584                            session = openSession();
585    
586                            Image[] array = new ImageImpl[3];
587    
588                            array[0] = getByLtSize_PrevAndNext(session, image, size,
589                                            orderByComparator, true);
590    
591                            array[1] = image;
592    
593                            array[2] = getByLtSize_PrevAndNext(session, image, size,
594                                            orderByComparator, false);
595    
596                            return array;
597                    }
598                    catch (Exception e) {
599                            throw processException(e);
600                    }
601                    finally {
602                            closeSession(session);
603                    }
604            }
605    
606            protected Image getByLtSize_PrevAndNext(Session session, Image image,
607                    int size, OrderByComparator orderByComparator, boolean previous) {
608                    StringBundler query = null;
609    
610                    if (orderByComparator != null) {
611                            query = new StringBundler(6 +
612                                            (orderByComparator.getOrderByFields().length * 6));
613                    }
614                    else {
615                            query = new StringBundler(3);
616                    }
617    
618                    query.append(_SQL_SELECT_IMAGE_WHERE);
619    
620                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
621    
622                    if (orderByComparator != null) {
623                            String[] orderByFields = orderByComparator.getOrderByFields();
624    
625                            if (orderByFields.length > 0) {
626                                    query.append(WHERE_AND);
627                            }
628    
629                            for (int i = 0; i < orderByFields.length; i++) {
630                                    query.append(_ORDER_BY_ENTITY_ALIAS);
631                                    query.append(orderByFields[i]);
632    
633                                    if ((i + 1) < orderByFields.length) {
634                                            if (orderByComparator.isAscending() ^ previous) {
635                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
636                                            }
637                                            else {
638                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
639                                            }
640                                    }
641                                    else {
642                                            if (orderByComparator.isAscending() ^ previous) {
643                                                    query.append(WHERE_GREATER_THAN);
644                                            }
645                                            else {
646                                                    query.append(WHERE_LESSER_THAN);
647                                            }
648                                    }
649                            }
650    
651                            query.append(ORDER_BY_CLAUSE);
652    
653                            for (int i = 0; i < orderByFields.length; i++) {
654                                    query.append(_ORDER_BY_ENTITY_ALIAS);
655                                    query.append(orderByFields[i]);
656    
657                                    if ((i + 1) < orderByFields.length) {
658                                            if (orderByComparator.isAscending() ^ previous) {
659                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
660                                            }
661                                            else {
662                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
663                                            }
664                                    }
665                                    else {
666                                            if (orderByComparator.isAscending() ^ previous) {
667                                                    query.append(ORDER_BY_ASC);
668                                            }
669                                            else {
670                                                    query.append(ORDER_BY_DESC);
671                                            }
672                                    }
673                            }
674                    }
675    
676                    else {
677                            query.append(ImageModelImpl.ORDER_BY_JPQL);
678                    }
679    
680                    String sql = query.toString();
681    
682                    Query q = session.createQuery(sql);
683    
684                    q.setFirstResult(0);
685                    q.setMaxResults(2);
686    
687                    QueryPos qPos = QueryPos.getInstance(q);
688    
689                    qPos.add(size);
690    
691                    if (orderByComparator != null) {
692                            Object[] values = orderByComparator.getOrderByValues(image);
693    
694                            for (Object value : values) {
695                                    qPos.add(value);
696                            }
697                    }
698    
699                    List<Image> list = q.list();
700    
701                    if (list.size() == 2) {
702                            return list.get(1);
703                    }
704                    else {
705                            return null;
706                    }
707            }
708    
709            /**
710             * Finds all the images.
711             *
712             * @return the images
713             * @throws SystemException if a system exception occurred
714             */
715            public List<Image> findAll() throws SystemException {
716                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
717            }
718    
719            /**
720             * Finds a range of all the images.
721             *
722             * <p>
723             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
724             * </p>
725             *
726             * @param start the lower bound of the range of images to return
727             * @param end the upper bound of the range of images to return (not inclusive)
728             * @return the range of images
729             * @throws SystemException if a system exception occurred
730             */
731            public List<Image> findAll(int start, int end) throws SystemException {
732                    return findAll(start, end, null);
733            }
734    
735            /**
736             * Finds an ordered range of all the images.
737             *
738             * <p>
739             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
740             * </p>
741             *
742             * @param start the lower bound of the range of images to return
743             * @param end the upper bound of the range of images to return (not inclusive)
744             * @param orderByComparator the comparator to order the results by
745             * @return the ordered range of images
746             * @throws SystemException if a system exception occurred
747             */
748            public List<Image> findAll(int start, int end,
749                    OrderByComparator orderByComparator) throws SystemException {
750                    Object[] finderArgs = new Object[] {
751                                    String.valueOf(start), String.valueOf(end),
752                                    String.valueOf(orderByComparator)
753                            };
754    
755                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
756                                    finderArgs, this);
757    
758                    if (list == null) {
759                            Session session = null;
760    
761                            try {
762                                    session = openSession();
763    
764                                    StringBundler query = null;
765                                    String sql = null;
766    
767                                    if (orderByComparator != null) {
768                                            query = new StringBundler(2 +
769                                                            (orderByComparator.getOrderByFields().length * 3));
770    
771                                            query.append(_SQL_SELECT_IMAGE);
772    
773                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
774                                                    orderByComparator);
775    
776                                            sql = query.toString();
777                                    }
778                                    else {
779                                            sql = _SQL_SELECT_IMAGE.concat(ImageModelImpl.ORDER_BY_JPQL);
780                                    }
781    
782                                    Query q = session.createQuery(sql);
783    
784                                    if (orderByComparator == null) {
785                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
786                                                            end, false);
787    
788                                            Collections.sort(list);
789                                    }
790                                    else {
791                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
792                                                            end);
793                                    }
794                            }
795                            catch (Exception e) {
796                                    throw processException(e);
797                            }
798                            finally {
799                                    if (list == null) {
800                                            list = new ArrayList<Image>();
801                                    }
802    
803                                    cacheResult(list);
804    
805                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
806    
807                                    closeSession(session);
808                            }
809                    }
810    
811                    return list;
812            }
813    
814            /**
815             * Removes all the images where size &lt; &#63; from the database.
816             *
817             * @param size the size to search with
818             * @throws SystemException if a system exception occurred
819             */
820            public void removeByLtSize(int size) throws SystemException {
821                    for (Image image : findByLtSize(size)) {
822                            remove(image);
823                    }
824            }
825    
826            /**
827             * Removes all the images from the database.
828             *
829             * @throws SystemException if a system exception occurred
830             */
831            public void removeAll() throws SystemException {
832                    for (Image image : findAll()) {
833                            remove(image);
834                    }
835            }
836    
837            /**
838             * Counts all the images where size &lt; &#63;.
839             *
840             * @param size the size to search with
841             * @return the number of matching images
842             * @throws SystemException if a system exception occurred
843             */
844            public int countByLtSize(int size) throws SystemException {
845                    Object[] finderArgs = new Object[] { size };
846    
847                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LTSIZE,
848                                    finderArgs, this);
849    
850                    if (count == null) {
851                            Session session = null;
852    
853                            try {
854                                    session = openSession();
855    
856                                    StringBundler query = new StringBundler(2);
857    
858                                    query.append(_SQL_COUNT_IMAGE_WHERE);
859    
860                                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
861    
862                                    String sql = query.toString();
863    
864                                    Query q = session.createQuery(sql);
865    
866                                    QueryPos qPos = QueryPos.getInstance(q);
867    
868                                    qPos.add(size);
869    
870                                    count = (Long)q.uniqueResult();
871                            }
872                            catch (Exception e) {
873                                    throw processException(e);
874                            }
875                            finally {
876                                    if (count == null) {
877                                            count = Long.valueOf(0);
878                                    }
879    
880                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LTSIZE,
881                                            finderArgs, count);
882    
883                                    closeSession(session);
884                            }
885                    }
886    
887                    return count.intValue();
888            }
889    
890            /**
891             * Counts all the images.
892             *
893             * @return the number of images
894             * @throws SystemException if a system exception occurred
895             */
896            public int countAll() throws SystemException {
897                    Object[] finderArgs = new Object[0];
898    
899                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
900                                    finderArgs, this);
901    
902                    if (count == null) {
903                            Session session = null;
904    
905                            try {
906                                    session = openSession();
907    
908                                    Query q = session.createQuery(_SQL_COUNT_IMAGE);
909    
910                                    count = (Long)q.uniqueResult();
911                            }
912                            catch (Exception e) {
913                                    throw processException(e);
914                            }
915                            finally {
916                                    if (count == null) {
917                                            count = Long.valueOf(0);
918                                    }
919    
920                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
921                                            count);
922    
923                                    closeSession(session);
924                            }
925                    }
926    
927                    return count.intValue();
928            }
929    
930            /**
931             * Initializes the image persistence.
932             */
933            public void afterPropertiesSet() {
934                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
935                                            com.liferay.portal.util.PropsUtil.get(
936                                                    "value.object.listener.com.liferay.portal.model.Image")));
937    
938                    if (listenerClassNames.length > 0) {
939                            try {
940                                    List<ModelListener<Image>> listenersList = new ArrayList<ModelListener<Image>>();
941    
942                                    for (String listenerClassName : listenerClassNames) {
943                                            listenersList.add((ModelListener<Image>)InstanceFactory.newInstance(
944                                                            listenerClassName));
945                                    }
946    
947                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
948                            }
949                            catch (Exception e) {
950                                    _log.error(e);
951                            }
952                    }
953            }
954    
955            @BeanReference(type = AccountPersistence.class)
956            protected AccountPersistence accountPersistence;
957            @BeanReference(type = AddressPersistence.class)
958            protected AddressPersistence addressPersistence;
959            @BeanReference(type = BrowserTrackerPersistence.class)
960            protected BrowserTrackerPersistence browserTrackerPersistence;
961            @BeanReference(type = ClassNamePersistence.class)
962            protected ClassNamePersistence classNamePersistence;
963            @BeanReference(type = CompanyPersistence.class)
964            protected CompanyPersistence companyPersistence;
965            @BeanReference(type = ContactPersistence.class)
966            protected ContactPersistence contactPersistence;
967            @BeanReference(type = CountryPersistence.class)
968            protected CountryPersistence countryPersistence;
969            @BeanReference(type = EmailAddressPersistence.class)
970            protected EmailAddressPersistence emailAddressPersistence;
971            @BeanReference(type = GroupPersistence.class)
972            protected GroupPersistence groupPersistence;
973            @BeanReference(type = ImagePersistence.class)
974            protected ImagePersistence imagePersistence;
975            @BeanReference(type = LayoutPersistence.class)
976            protected LayoutPersistence layoutPersistence;
977            @BeanReference(type = LayoutPrototypePersistence.class)
978            protected LayoutPrototypePersistence layoutPrototypePersistence;
979            @BeanReference(type = LayoutSetPersistence.class)
980            protected LayoutSetPersistence layoutSetPersistence;
981            @BeanReference(type = LayoutSetPrototypePersistence.class)
982            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
983            @BeanReference(type = ListTypePersistence.class)
984            protected ListTypePersistence listTypePersistence;
985            @BeanReference(type = LockPersistence.class)
986            protected LockPersistence lockPersistence;
987            @BeanReference(type = MembershipRequestPersistence.class)
988            protected MembershipRequestPersistence membershipRequestPersistence;
989            @BeanReference(type = OrganizationPersistence.class)
990            protected OrganizationPersistence organizationPersistence;
991            @BeanReference(type = OrgGroupPermissionPersistence.class)
992            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
993            @BeanReference(type = OrgGroupRolePersistence.class)
994            protected OrgGroupRolePersistence orgGroupRolePersistence;
995            @BeanReference(type = OrgLaborPersistence.class)
996            protected OrgLaborPersistence orgLaborPersistence;
997            @BeanReference(type = PasswordPolicyPersistence.class)
998            protected PasswordPolicyPersistence passwordPolicyPersistence;
999            @BeanReference(type = PasswordPolicyRelPersistence.class)
1000            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1001            @BeanReference(type = PasswordTrackerPersistence.class)
1002            protected PasswordTrackerPersistence passwordTrackerPersistence;
1003            @BeanReference(type = PermissionPersistence.class)
1004            protected PermissionPersistence permissionPersistence;
1005            @BeanReference(type = PhonePersistence.class)
1006            protected PhonePersistence phonePersistence;
1007            @BeanReference(type = PluginSettingPersistence.class)
1008            protected PluginSettingPersistence pluginSettingPersistence;
1009            @BeanReference(type = PortletPersistence.class)
1010            protected PortletPersistence portletPersistence;
1011            @BeanReference(type = PortletItemPersistence.class)
1012            protected PortletItemPersistence portletItemPersistence;
1013            @BeanReference(type = PortletPreferencesPersistence.class)
1014            protected PortletPreferencesPersistence portletPreferencesPersistence;
1015            @BeanReference(type = RegionPersistence.class)
1016            protected RegionPersistence regionPersistence;
1017            @BeanReference(type = ReleasePersistence.class)
1018            protected ReleasePersistence releasePersistence;
1019            @BeanReference(type = ResourcePersistence.class)
1020            protected ResourcePersistence resourcePersistence;
1021            @BeanReference(type = ResourceActionPersistence.class)
1022            protected ResourceActionPersistence resourceActionPersistence;
1023            @BeanReference(type = ResourceCodePersistence.class)
1024            protected ResourceCodePersistence resourceCodePersistence;
1025            @BeanReference(type = ResourcePermissionPersistence.class)
1026            protected ResourcePermissionPersistence resourcePermissionPersistence;
1027            @BeanReference(type = RolePersistence.class)
1028            protected RolePersistence rolePersistence;
1029            @BeanReference(type = ServiceComponentPersistence.class)
1030            protected ServiceComponentPersistence serviceComponentPersistence;
1031            @BeanReference(type = ShardPersistence.class)
1032            protected ShardPersistence shardPersistence;
1033            @BeanReference(type = SubscriptionPersistence.class)
1034            protected SubscriptionPersistence subscriptionPersistence;
1035            @BeanReference(type = TicketPersistence.class)
1036            protected TicketPersistence ticketPersistence;
1037            @BeanReference(type = TeamPersistence.class)
1038            protected TeamPersistence teamPersistence;
1039            @BeanReference(type = UserPersistence.class)
1040            protected UserPersistence userPersistence;
1041            @BeanReference(type = UserGroupPersistence.class)
1042            protected UserGroupPersistence userGroupPersistence;
1043            @BeanReference(type = UserGroupGroupRolePersistence.class)
1044            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1045            @BeanReference(type = UserGroupRolePersistence.class)
1046            protected UserGroupRolePersistence userGroupRolePersistence;
1047            @BeanReference(type = UserIdMapperPersistence.class)
1048            protected UserIdMapperPersistence userIdMapperPersistence;
1049            @BeanReference(type = UserTrackerPersistence.class)
1050            protected UserTrackerPersistence userTrackerPersistence;
1051            @BeanReference(type = UserTrackerPathPersistence.class)
1052            protected UserTrackerPathPersistence userTrackerPathPersistence;
1053            @BeanReference(type = WebDAVPropsPersistence.class)
1054            protected WebDAVPropsPersistence webDAVPropsPersistence;
1055            @BeanReference(type = WebsitePersistence.class)
1056            protected WebsitePersistence websitePersistence;
1057            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1058            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1059            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1060            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1061            @BeanReference(type = IGImagePersistence.class)
1062            protected IGImagePersistence igImagePersistence;
1063            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1064            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1065            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1066            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1067            private static final String _FINDER_COLUMN_LTSIZE_SIZE_2 = "image.size < ?";
1068            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1069            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1070            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1071            private static Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1072    }