001    /**
002     * Copyright (c) 2000-2011 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 implementation for the image service.
053     *
054     * <p>
055     * Never modify or reference this class directly. Always use {@link ImageUtil} to access the image persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate 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. Does not add the image to the database.
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                            BatchSessionUtil.delete(session, image);
217                    }
218                    catch (Exception e) {
219                            throw processException(e);
220                    }
221                    finally {
222                            closeSession(session);
223                    }
224    
225                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226    
227                    EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
228                            ImageImpl.class, image.getPrimaryKey());
229    
230                    return image;
231            }
232    
233            public Image updateImpl(com.liferay.portal.model.Image image, boolean merge)
234                    throws SystemException {
235                    image = toUnwrappedModel(image);
236    
237                    Session session = null;
238    
239                    try {
240                            session = openSession();
241    
242                            BatchSessionUtil.update(session, image, merge);
243    
244                            image.setNew(false);
245                    }
246                    catch (Exception e) {
247                            throw processException(e);
248                    }
249                    finally {
250                            closeSession(session);
251                    }
252    
253                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
254    
255                    EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
256                            ImageImpl.class, image.getPrimaryKey(), image);
257    
258                    return image;
259            }
260    
261            protected Image toUnwrappedModel(Image image) {
262                    if (image instanceof ImageImpl) {
263                            return image;
264                    }
265    
266                    ImageImpl imageImpl = new ImageImpl();
267    
268                    imageImpl.setNew(image.isNew());
269                    imageImpl.setPrimaryKey(image.getPrimaryKey());
270    
271                    imageImpl.setImageId(image.getImageId());
272                    imageImpl.setModifiedDate(image.getModifiedDate());
273                    imageImpl.setText(image.getText());
274                    imageImpl.setType(image.getType());
275                    imageImpl.setHeight(image.getHeight());
276                    imageImpl.setWidth(image.getWidth());
277                    imageImpl.setSize(image.getSize());
278    
279                    return imageImpl;
280            }
281    
282            /**
283             * Finds the image with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
284             *
285             * @param primaryKey the primary key of the image to find
286             * @return the image
287             * @throws com.liferay.portal.NoSuchModelException if a image with the primary key could not be found
288             * @throws SystemException if a system exception occurred
289             */
290            public Image findByPrimaryKey(Serializable primaryKey)
291                    throws NoSuchModelException, SystemException {
292                    return findByPrimaryKey(((Long)primaryKey).longValue());
293            }
294    
295            /**
296             * Finds the image with the primary key or throws a {@link com.liferay.portal.NoSuchImageException} if it could not be found.
297             *
298             * @param imageId the primary key of the image to find
299             * @return the image
300             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
301             * @throws SystemException if a system exception occurred
302             */
303            public Image findByPrimaryKey(long imageId)
304                    throws NoSuchImageException, SystemException {
305                    Image image = fetchByPrimaryKey(imageId);
306    
307                    if (image == null) {
308                            if (_log.isWarnEnabled()) {
309                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + imageId);
310                            }
311    
312                            throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
313                                    imageId);
314                    }
315    
316                    return image;
317            }
318    
319            /**
320             * Finds the image with the primary key or returns <code>null</code> if it could not be found.
321             *
322             * @param primaryKey the primary key of the image to find
323             * @return the image, or <code>null</code> if a image with the primary key could not be found
324             * @throws SystemException if a system exception occurred
325             */
326            public Image fetchByPrimaryKey(Serializable primaryKey)
327                    throws SystemException {
328                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
329            }
330    
331            /**
332             * Finds the image with the primary key or returns <code>null</code> if it could not be found.
333             *
334             * @param imageId the primary key of the image to find
335             * @return the image, or <code>null</code> if a image with the primary key could not be found
336             * @throws SystemException if a system exception occurred
337             */
338            public Image fetchByPrimaryKey(long imageId) throws SystemException {
339                    Image image = (Image)EntityCacheUtil.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
340                                    ImageImpl.class, imageId, this);
341    
342                    if (image == null) {
343                            Session session = null;
344    
345                            try {
346                                    session = openSession();
347    
348                                    image = (Image)session.get(ImageImpl.class, new Long(imageId));
349                            }
350                            catch (Exception e) {
351                                    throw processException(e);
352                            }
353                            finally {
354                                    if (image != null) {
355                                            cacheResult(image);
356                                    }
357    
358                                    closeSession(session);
359                            }
360                    }
361    
362                    return image;
363            }
364    
365            /**
366             * Finds all the images where size &lt; &#63;.
367             *
368             * @param size the size to search with
369             * @return the matching images
370             * @throws SystemException if a system exception occurred
371             */
372            public List<Image> findByLtSize(int size) throws SystemException {
373                    return findByLtSize(size, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
374            }
375    
376            /**
377             * Finds a range of all the images where size &lt; &#63;.
378             *
379             * <p>
380             * 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.
381             * </p>
382             *
383             * @param size the size to search with
384             * @param start the lower bound of the range of images to return
385             * @param end the upper bound of the range of images to return (not inclusive)
386             * @return the range of matching images
387             * @throws SystemException if a system exception occurred
388             */
389            public List<Image> findByLtSize(int size, int start, int end)
390                    throws SystemException {
391                    return findByLtSize(size, start, end, null);
392            }
393    
394            /**
395             * Finds an ordered range of all the images where size &lt; &#63;.
396             *
397             * <p>
398             * 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.
399             * </p>
400             *
401             * @param size the size to search with
402             * @param start the lower bound of the range of images to return
403             * @param end the upper bound of the range of images to return (not inclusive)
404             * @param orderByComparator the comparator to order the results by
405             * @return the ordered range of matching images
406             * @throws SystemException if a system exception occurred
407             */
408            public List<Image> findByLtSize(int size, int start, int end,
409                    OrderByComparator orderByComparator) throws SystemException {
410                    Object[] finderArgs = new Object[] {
411                                    size,
412                                    
413                                    String.valueOf(start), String.valueOf(end),
414                                    String.valueOf(orderByComparator)
415                            };
416    
417                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_LTSIZE,
418                                    finderArgs, this);
419    
420                    if (list == null) {
421                            StringBundler query = null;
422    
423                            if (orderByComparator != null) {
424                                    query = new StringBundler(3 +
425                                                    (orderByComparator.getOrderByFields().length * 3));
426                            }
427                            else {
428                                    query = new StringBundler(3);
429                            }
430    
431                            query.append(_SQL_SELECT_IMAGE_WHERE);
432    
433                            query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
434    
435                            if (orderByComparator != null) {
436                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
437                                            orderByComparator);
438                            }
439    
440                            else {
441                                    query.append(ImageModelImpl.ORDER_BY_JPQL);
442                            }
443    
444                            String sql = query.toString();
445    
446                            Session session = null;
447    
448                            try {
449                                    session = openSession();
450    
451                                    Query q = session.createQuery(sql);
452    
453                                    QueryPos qPos = QueryPos.getInstance(q);
454    
455                                    qPos.add(size);
456    
457                                    list = (List<Image>)QueryUtil.list(q, getDialect(), start, end);
458                            }
459                            catch (Exception e) {
460                                    throw processException(e);
461                            }
462                            finally {
463                                    if (list == null) {
464                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_LTSIZE,
465                                                    finderArgs);
466                                    }
467                                    else {
468                                            cacheResult(list);
469    
470                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_LTSIZE,
471                                                    finderArgs, list);
472                                    }
473    
474                                    closeSession(session);
475                            }
476                    }
477    
478                    return list;
479            }
480    
481            /**
482             * Finds the first image in the ordered set where size &lt; &#63;.
483             *
484             * <p>
485             * 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.
486             * </p>
487             *
488             * @param size the size to search with
489             * @param orderByComparator the comparator to order the set by
490             * @return the first matching image
491             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
492             * @throws SystemException if a system exception occurred
493             */
494            public Image findByLtSize_First(int size,
495                    OrderByComparator orderByComparator)
496                    throws NoSuchImageException, SystemException {
497                    List<Image> list = findByLtSize(size, 0, 1, orderByComparator);
498    
499                    if (list.isEmpty()) {
500                            StringBundler msg = new StringBundler(4);
501    
502                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
503    
504                            msg.append("size=");
505                            msg.append(size);
506    
507                            msg.append(StringPool.CLOSE_CURLY_BRACE);
508    
509                            throw new NoSuchImageException(msg.toString());
510                    }
511                    else {
512                            return list.get(0);
513                    }
514            }
515    
516            /**
517             * Finds the last image in the ordered set where size &lt; &#63;.
518             *
519             * <p>
520             * 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.
521             * </p>
522             *
523             * @param size the size to search with
524             * @param orderByComparator the comparator to order the set by
525             * @return the last matching image
526             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
527             * @throws SystemException if a system exception occurred
528             */
529            public Image findByLtSize_Last(int size, OrderByComparator orderByComparator)
530                    throws NoSuchImageException, SystemException {
531                    int count = countByLtSize(size);
532    
533                    List<Image> list = findByLtSize(size, count - 1, count,
534                                    orderByComparator);
535    
536                    if (list.isEmpty()) {
537                            StringBundler msg = new StringBundler(4);
538    
539                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
540    
541                            msg.append("size=");
542                            msg.append(size);
543    
544                            msg.append(StringPool.CLOSE_CURLY_BRACE);
545    
546                            throw new NoSuchImageException(msg.toString());
547                    }
548                    else {
549                            return list.get(0);
550                    }
551            }
552    
553            /**
554             * Finds the images before and after the current image in the ordered set where size &lt; &#63;.
555             *
556             * <p>
557             * 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.
558             * </p>
559             *
560             * @param imageId the primary key of the current image
561             * @param size the size to search with
562             * @param orderByComparator the comparator to order the set by
563             * @return the previous, current, and next image
564             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
565             * @throws SystemException if a system exception occurred
566             */
567            public Image[] findByLtSize_PrevAndNext(long imageId, int size,
568                    OrderByComparator orderByComparator)
569                    throws NoSuchImageException, SystemException {
570                    Image image = findByPrimaryKey(imageId);
571    
572                    Session session = null;
573    
574                    try {
575                            session = openSession();
576    
577                            Image[] array = new ImageImpl[3];
578    
579                            array[0] = getByLtSize_PrevAndNext(session, image, size,
580                                            orderByComparator, true);
581    
582                            array[1] = image;
583    
584                            array[2] = getByLtSize_PrevAndNext(session, image, size,
585                                            orderByComparator, false);
586    
587                            return array;
588                    }
589                    catch (Exception e) {
590                            throw processException(e);
591                    }
592                    finally {
593                            closeSession(session);
594                    }
595            }
596    
597            protected Image getByLtSize_PrevAndNext(Session session, Image image,
598                    int size, OrderByComparator orderByComparator, boolean previous) {
599                    StringBundler query = null;
600    
601                    if (orderByComparator != null) {
602                            query = new StringBundler(6 +
603                                            (orderByComparator.getOrderByFields().length * 6));
604                    }
605                    else {
606                            query = new StringBundler(3);
607                    }
608    
609                    query.append(_SQL_SELECT_IMAGE_WHERE);
610    
611                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
612    
613                    if (orderByComparator != null) {
614                            String[] orderByFields = orderByComparator.getOrderByFields();
615    
616                            if (orderByFields.length > 0) {
617                                    query.append(WHERE_AND);
618                            }
619    
620                            for (int i = 0; i < orderByFields.length; i++) {
621                                    query.append(_ORDER_BY_ENTITY_ALIAS);
622                                    query.append(orderByFields[i]);
623    
624                                    if ((i + 1) < orderByFields.length) {
625                                            if (orderByComparator.isAscending() ^ previous) {
626                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
627                                            }
628                                            else {
629                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
630                                            }
631                                    }
632                                    else {
633                                            if (orderByComparator.isAscending() ^ previous) {
634                                                    query.append(WHERE_GREATER_THAN);
635                                            }
636                                            else {
637                                                    query.append(WHERE_LESSER_THAN);
638                                            }
639                                    }
640                            }
641    
642                            query.append(ORDER_BY_CLAUSE);
643    
644                            for (int i = 0; i < orderByFields.length; i++) {
645                                    query.append(_ORDER_BY_ENTITY_ALIAS);
646                                    query.append(orderByFields[i]);
647    
648                                    if ((i + 1) < orderByFields.length) {
649                                            if (orderByComparator.isAscending() ^ previous) {
650                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
651                                            }
652                                            else {
653                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
654                                            }
655                                    }
656                                    else {
657                                            if (orderByComparator.isAscending() ^ previous) {
658                                                    query.append(ORDER_BY_ASC);
659                                            }
660                                            else {
661                                                    query.append(ORDER_BY_DESC);
662                                            }
663                                    }
664                            }
665                    }
666    
667                    else {
668                            query.append(ImageModelImpl.ORDER_BY_JPQL);
669                    }
670    
671                    String sql = query.toString();
672    
673                    Query q = session.createQuery(sql);
674    
675                    q.setFirstResult(0);
676                    q.setMaxResults(2);
677    
678                    QueryPos qPos = QueryPos.getInstance(q);
679    
680                    qPos.add(size);
681    
682                    if (orderByComparator != null) {
683                            Object[] values = orderByComparator.getOrderByValues(image);
684    
685                            for (Object value : values) {
686                                    qPos.add(value);
687                            }
688                    }
689    
690                    List<Image> list = q.list();
691    
692                    if (list.size() == 2) {
693                            return list.get(1);
694                    }
695                    else {
696                            return null;
697                    }
698            }
699    
700            /**
701             * Finds all the images.
702             *
703             * @return the images
704             * @throws SystemException if a system exception occurred
705             */
706            public List<Image> findAll() throws SystemException {
707                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
708            }
709    
710            /**
711             * Finds a range of all the images.
712             *
713             * <p>
714             * 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.
715             * </p>
716             *
717             * @param start the lower bound of the range of images to return
718             * @param end the upper bound of the range of images to return (not inclusive)
719             * @return the range of images
720             * @throws SystemException if a system exception occurred
721             */
722            public List<Image> findAll(int start, int end) throws SystemException {
723                    return findAll(start, end, null);
724            }
725    
726            /**
727             * Finds an ordered range of all the images.
728             *
729             * <p>
730             * 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.
731             * </p>
732             *
733             * @param start the lower bound of the range of images to return
734             * @param end the upper bound of the range of images to return (not inclusive)
735             * @param orderByComparator the comparator to order the results by
736             * @return the ordered range of images
737             * @throws SystemException if a system exception occurred
738             */
739            public List<Image> findAll(int start, int end,
740                    OrderByComparator orderByComparator) throws SystemException {
741                    Object[] finderArgs = new Object[] {
742                                    String.valueOf(start), String.valueOf(end),
743                                    String.valueOf(orderByComparator)
744                            };
745    
746                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
747                                    finderArgs, this);
748    
749                    if (list == null) {
750                            StringBundler query = null;
751                            String sql = null;
752    
753                            if (orderByComparator != null) {
754                                    query = new StringBundler(2 +
755                                                    (orderByComparator.getOrderByFields().length * 3));
756    
757                                    query.append(_SQL_SELECT_IMAGE);
758    
759                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
760                                            orderByComparator);
761    
762                                    sql = query.toString();
763                            }
764                            else {
765                                    sql = _SQL_SELECT_IMAGE.concat(ImageModelImpl.ORDER_BY_JPQL);
766                            }
767    
768                            Session session = null;
769    
770                            try {
771                                    session = openSession();
772    
773                                    Query q = session.createQuery(sql);
774    
775                                    if (orderByComparator == null) {
776                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
777                                                            end, false);
778    
779                                            Collections.sort(list);
780                                    }
781                                    else {
782                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
783                                                            end);
784                                    }
785                            }
786                            catch (Exception e) {
787                                    throw processException(e);
788                            }
789                            finally {
790                                    if (list == null) {
791                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
792                                                    finderArgs);
793                                    }
794                                    else {
795                                            cacheResult(list);
796    
797                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
798                                                    list);
799                                    }
800    
801                                    closeSession(session);
802                            }
803                    }
804    
805                    return list;
806            }
807    
808            /**
809             * Removes all the images where size &lt; &#63; from the database.
810             *
811             * @param size the size to search with
812             * @throws SystemException if a system exception occurred
813             */
814            public void removeByLtSize(int size) throws SystemException {
815                    for (Image image : findByLtSize(size)) {
816                            remove(image);
817                    }
818            }
819    
820            /**
821             * Removes all the images from the database.
822             *
823             * @throws SystemException if a system exception occurred
824             */
825            public void removeAll() throws SystemException {
826                    for (Image image : findAll()) {
827                            remove(image);
828                    }
829            }
830    
831            /**
832             * Counts all the images where size &lt; &#63;.
833             *
834             * @param size the size to search with
835             * @return the number of matching images
836             * @throws SystemException if a system exception occurred
837             */
838            public int countByLtSize(int size) throws SystemException {
839                    Object[] finderArgs = new Object[] { size };
840    
841                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LTSIZE,
842                                    finderArgs, this);
843    
844                    if (count == null) {
845                            StringBundler query = new StringBundler(2);
846    
847                            query.append(_SQL_COUNT_IMAGE_WHERE);
848    
849                            query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
850    
851                            String sql = query.toString();
852    
853                            Session session = null;
854    
855                            try {
856                                    session = openSession();
857    
858                                    Query q = session.createQuery(sql);
859    
860                                    QueryPos qPos = QueryPos.getInstance(q);
861    
862                                    qPos.add(size);
863    
864                                    count = (Long)q.uniqueResult();
865                            }
866                            catch (Exception e) {
867                                    throw processException(e);
868                            }
869                            finally {
870                                    if (count == null) {
871                                            count = Long.valueOf(0);
872                                    }
873    
874                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LTSIZE,
875                                            finderArgs, count);
876    
877                                    closeSession(session);
878                            }
879                    }
880    
881                    return count.intValue();
882            }
883    
884            /**
885             * Counts all the images.
886             *
887             * @return the number of images
888             * @throws SystemException if a system exception occurred
889             */
890            public int countAll() throws SystemException {
891                    Object[] finderArgs = new Object[0];
892    
893                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
894                                    finderArgs, this);
895    
896                    if (count == null) {
897                            Session session = null;
898    
899                            try {
900                                    session = openSession();
901    
902                                    Query q = session.createQuery(_SQL_COUNT_IMAGE);
903    
904                                    count = (Long)q.uniqueResult();
905                            }
906                            catch (Exception e) {
907                                    throw processException(e);
908                            }
909                            finally {
910                                    if (count == null) {
911                                            count = Long.valueOf(0);
912                                    }
913    
914                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
915                                            count);
916    
917                                    closeSession(session);
918                            }
919                    }
920    
921                    return count.intValue();
922            }
923    
924            /**
925             * Initializes the image persistence.
926             */
927            public void afterPropertiesSet() {
928                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
929                                            com.liferay.portal.util.PropsUtil.get(
930                                                    "value.object.listener.com.liferay.portal.model.Image")));
931    
932                    if (listenerClassNames.length > 0) {
933                            try {
934                                    List<ModelListener<Image>> listenersList = new ArrayList<ModelListener<Image>>();
935    
936                                    for (String listenerClassName : listenerClassNames) {
937                                            listenersList.add((ModelListener<Image>)InstanceFactory.newInstance(
938                                                            listenerClassName));
939                                    }
940    
941                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
942                            }
943                            catch (Exception e) {
944                                    _log.error(e);
945                            }
946                    }
947            }
948    
949            public void destroy() {
950                    EntityCacheUtil.removeCache(ImageImpl.class.getName());
951                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
952                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
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 = ClusterGroupPersistence.class)
964            protected ClusterGroupPersistence clusterGroupPersistence;
965            @BeanReference(type = CompanyPersistence.class)
966            protected CompanyPersistence companyPersistence;
967            @BeanReference(type = ContactPersistence.class)
968            protected ContactPersistence contactPersistence;
969            @BeanReference(type = CountryPersistence.class)
970            protected CountryPersistence countryPersistence;
971            @BeanReference(type = EmailAddressPersistence.class)
972            protected EmailAddressPersistence emailAddressPersistence;
973            @BeanReference(type = GroupPersistence.class)
974            protected GroupPersistence groupPersistence;
975            @BeanReference(type = ImagePersistence.class)
976            protected ImagePersistence imagePersistence;
977            @BeanReference(type = LayoutPersistence.class)
978            protected LayoutPersistence layoutPersistence;
979            @BeanReference(type = LayoutPrototypePersistence.class)
980            protected LayoutPrototypePersistence layoutPrototypePersistence;
981            @BeanReference(type = LayoutSetPersistence.class)
982            protected LayoutSetPersistence layoutSetPersistence;
983            @BeanReference(type = LayoutSetPrototypePersistence.class)
984            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
985            @BeanReference(type = ListTypePersistence.class)
986            protected ListTypePersistence listTypePersistence;
987            @BeanReference(type = LockPersistence.class)
988            protected LockPersistence lockPersistence;
989            @BeanReference(type = MembershipRequestPersistence.class)
990            protected MembershipRequestPersistence membershipRequestPersistence;
991            @BeanReference(type = OrganizationPersistence.class)
992            protected OrganizationPersistence organizationPersistence;
993            @BeanReference(type = OrgGroupPermissionPersistence.class)
994            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
995            @BeanReference(type = OrgGroupRolePersistence.class)
996            protected OrgGroupRolePersistence orgGroupRolePersistence;
997            @BeanReference(type = OrgLaborPersistence.class)
998            protected OrgLaborPersistence orgLaborPersistence;
999            @BeanReference(type = PasswordPolicyPersistence.class)
1000            protected PasswordPolicyPersistence passwordPolicyPersistence;
1001            @BeanReference(type = PasswordPolicyRelPersistence.class)
1002            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1003            @BeanReference(type = PasswordTrackerPersistence.class)
1004            protected PasswordTrackerPersistence passwordTrackerPersistence;
1005            @BeanReference(type = PermissionPersistence.class)
1006            protected PermissionPersistence permissionPersistence;
1007            @BeanReference(type = PhonePersistence.class)
1008            protected PhonePersistence phonePersistence;
1009            @BeanReference(type = PluginSettingPersistence.class)
1010            protected PluginSettingPersistence pluginSettingPersistence;
1011            @BeanReference(type = PortletPersistence.class)
1012            protected PortletPersistence portletPersistence;
1013            @BeanReference(type = PortletItemPersistence.class)
1014            protected PortletItemPersistence portletItemPersistence;
1015            @BeanReference(type = PortletPreferencesPersistence.class)
1016            protected PortletPreferencesPersistence portletPreferencesPersistence;
1017            @BeanReference(type = RegionPersistence.class)
1018            protected RegionPersistence regionPersistence;
1019            @BeanReference(type = ReleasePersistence.class)
1020            protected ReleasePersistence releasePersistence;
1021            @BeanReference(type = ResourcePersistence.class)
1022            protected ResourcePersistence resourcePersistence;
1023            @BeanReference(type = ResourceActionPersistence.class)
1024            protected ResourceActionPersistence resourceActionPersistence;
1025            @BeanReference(type = ResourceCodePersistence.class)
1026            protected ResourceCodePersistence resourceCodePersistence;
1027            @BeanReference(type = ResourcePermissionPersistence.class)
1028            protected ResourcePermissionPersistence resourcePermissionPersistence;
1029            @BeanReference(type = RolePersistence.class)
1030            protected RolePersistence rolePersistence;
1031            @BeanReference(type = ServiceComponentPersistence.class)
1032            protected ServiceComponentPersistence serviceComponentPersistence;
1033            @BeanReference(type = ShardPersistence.class)
1034            protected ShardPersistence shardPersistence;
1035            @BeanReference(type = SubscriptionPersistence.class)
1036            protected SubscriptionPersistence subscriptionPersistence;
1037            @BeanReference(type = TicketPersistence.class)
1038            protected TicketPersistence ticketPersistence;
1039            @BeanReference(type = TeamPersistence.class)
1040            protected TeamPersistence teamPersistence;
1041            @BeanReference(type = UserPersistence.class)
1042            protected UserPersistence userPersistence;
1043            @BeanReference(type = UserGroupPersistence.class)
1044            protected UserGroupPersistence userGroupPersistence;
1045            @BeanReference(type = UserGroupGroupRolePersistence.class)
1046            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1047            @BeanReference(type = UserGroupRolePersistence.class)
1048            protected UserGroupRolePersistence userGroupRolePersistence;
1049            @BeanReference(type = UserIdMapperPersistence.class)
1050            protected UserIdMapperPersistence userIdMapperPersistence;
1051            @BeanReference(type = UserTrackerPersistence.class)
1052            protected UserTrackerPersistence userTrackerPersistence;
1053            @BeanReference(type = UserTrackerPathPersistence.class)
1054            protected UserTrackerPathPersistence userTrackerPathPersistence;
1055            @BeanReference(type = WebDAVPropsPersistence.class)
1056            protected WebDAVPropsPersistence webDAVPropsPersistence;
1057            @BeanReference(type = WebsitePersistence.class)
1058            protected WebsitePersistence websitePersistence;
1059            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1060            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1061            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1062            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1063            @BeanReference(type = IGImagePersistence.class)
1064            protected IGImagePersistence igImagePersistence;
1065            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1066            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1067            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1068            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1069            private static final String _FINDER_COLUMN_LTSIZE_SIZE_2 = "image.size < ?";
1070            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1071            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1072            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1073            private static Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1074    }