001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
422    
423                            try {
424                                    session = openSession();
425    
426                                    StringBundler query = null;
427    
428                                    if (orderByComparator != null) {
429                                            query = new StringBundler(3 +
430                                                            (orderByComparator.getOrderByFields().length * 3));
431                                    }
432                                    else {
433                                            query = new StringBundler(3);
434                                    }
435    
436                                    query.append(_SQL_SELECT_IMAGE_WHERE);
437    
438                                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
439    
440                                    if (orderByComparator != null) {
441                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
442                                                    orderByComparator);
443                                    }
444    
445                                    else {
446                                            query.append(ImageModelImpl.ORDER_BY_JPQL);
447                                    }
448    
449                                    String sql = query.toString();
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                                            list = new ArrayList<Image>();
465                                    }
466    
467                                    cacheResult(list);
468    
469                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_LTSIZE,
470                                            finderArgs, list);
471    
472                                    closeSession(session);
473                            }
474                    }
475    
476                    return list;
477            }
478    
479            /**
480             * Finds the first image in the ordered set where size &lt; &#63;.
481             *
482             * <p>
483             * 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.
484             * </p>
485             *
486             * @param size the size to search with
487             * @param orderByComparator the comparator to order the set by
488             * @return the first matching image
489             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
490             * @throws SystemException if a system exception occurred
491             */
492            public Image findByLtSize_First(int size,
493                    OrderByComparator orderByComparator)
494                    throws NoSuchImageException, SystemException {
495                    List<Image> list = findByLtSize(size, 0, 1, orderByComparator);
496    
497                    if (list.isEmpty()) {
498                            StringBundler msg = new StringBundler(4);
499    
500                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
501    
502                            msg.append("size=");
503                            msg.append(size);
504    
505                            msg.append(StringPool.CLOSE_CURLY_BRACE);
506    
507                            throw new NoSuchImageException(msg.toString());
508                    }
509                    else {
510                            return list.get(0);
511                    }
512            }
513    
514            /**
515             * Finds the last image in the ordered set where size &lt; &#63;.
516             *
517             * <p>
518             * 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.
519             * </p>
520             *
521             * @param size the size to search with
522             * @param orderByComparator the comparator to order the set by
523             * @return the last matching image
524             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
525             * @throws SystemException if a system exception occurred
526             */
527            public Image findByLtSize_Last(int size, OrderByComparator orderByComparator)
528                    throws NoSuchImageException, SystemException {
529                    int count = countByLtSize(size);
530    
531                    List<Image> list = findByLtSize(size, count - 1, count,
532                                    orderByComparator);
533    
534                    if (list.isEmpty()) {
535                            StringBundler msg = new StringBundler(4);
536    
537                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
538    
539                            msg.append("size=");
540                            msg.append(size);
541    
542                            msg.append(StringPool.CLOSE_CURLY_BRACE);
543    
544                            throw new NoSuchImageException(msg.toString());
545                    }
546                    else {
547                            return list.get(0);
548                    }
549            }
550    
551            /**
552             * Finds the images before and after the current image in the ordered set where size &lt; &#63;.
553             *
554             * <p>
555             * 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.
556             * </p>
557             *
558             * @param imageId the primary key of the current image
559             * @param size the size to search with
560             * @param orderByComparator the comparator to order the set by
561             * @return the previous, current, and next image
562             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
563             * @throws SystemException if a system exception occurred
564             */
565            public Image[] findByLtSize_PrevAndNext(long imageId, int size,
566                    OrderByComparator orderByComparator)
567                    throws NoSuchImageException, SystemException {
568                    Image image = findByPrimaryKey(imageId);
569    
570                    Session session = null;
571    
572                    try {
573                            session = openSession();
574    
575                            Image[] array = new ImageImpl[3];
576    
577                            array[0] = getByLtSize_PrevAndNext(session, image, size,
578                                            orderByComparator, true);
579    
580                            array[1] = image;
581    
582                            array[2] = getByLtSize_PrevAndNext(session, image, size,
583                                            orderByComparator, false);
584    
585                            return array;
586                    }
587                    catch (Exception e) {
588                            throw processException(e);
589                    }
590                    finally {
591                            closeSession(session);
592                    }
593            }
594    
595            protected Image getByLtSize_PrevAndNext(Session session, Image image,
596                    int size, OrderByComparator orderByComparator, boolean previous) {
597                    StringBundler query = null;
598    
599                    if (orderByComparator != null) {
600                            query = new StringBundler(6 +
601                                            (orderByComparator.getOrderByFields().length * 6));
602                    }
603                    else {
604                            query = new StringBundler(3);
605                    }
606    
607                    query.append(_SQL_SELECT_IMAGE_WHERE);
608    
609                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
610    
611                    if (orderByComparator != null) {
612                            String[] orderByFields = orderByComparator.getOrderByFields();
613    
614                            if (orderByFields.length > 0) {
615                                    query.append(WHERE_AND);
616                            }
617    
618                            for (int i = 0; i < orderByFields.length; i++) {
619                                    query.append(_ORDER_BY_ENTITY_ALIAS);
620                                    query.append(orderByFields[i]);
621    
622                                    if ((i + 1) < orderByFields.length) {
623                                            if (orderByComparator.isAscending() ^ previous) {
624                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
625                                            }
626                                            else {
627                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
628                                            }
629                                    }
630                                    else {
631                                            if (orderByComparator.isAscending() ^ previous) {
632                                                    query.append(WHERE_GREATER_THAN);
633                                            }
634                                            else {
635                                                    query.append(WHERE_LESSER_THAN);
636                                            }
637                                    }
638                            }
639    
640                            query.append(ORDER_BY_CLAUSE);
641    
642                            for (int i = 0; i < orderByFields.length; i++) {
643                                    query.append(_ORDER_BY_ENTITY_ALIAS);
644                                    query.append(orderByFields[i]);
645    
646                                    if ((i + 1) < orderByFields.length) {
647                                            if (orderByComparator.isAscending() ^ previous) {
648                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
649                                            }
650                                            else {
651                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
652                                            }
653                                    }
654                                    else {
655                                            if (orderByComparator.isAscending() ^ previous) {
656                                                    query.append(ORDER_BY_ASC);
657                                            }
658                                            else {
659                                                    query.append(ORDER_BY_DESC);
660                                            }
661                                    }
662                            }
663                    }
664    
665                    else {
666                            query.append(ImageModelImpl.ORDER_BY_JPQL);
667                    }
668    
669                    String sql = query.toString();
670    
671                    Query q = session.createQuery(sql);
672    
673                    q.setFirstResult(0);
674                    q.setMaxResults(2);
675    
676                    QueryPos qPos = QueryPos.getInstance(q);
677    
678                    qPos.add(size);
679    
680                    if (orderByComparator != null) {
681                            Object[] values = orderByComparator.getOrderByValues(image);
682    
683                            for (Object value : values) {
684                                    qPos.add(value);
685                            }
686                    }
687    
688                    List<Image> list = q.list();
689    
690                    if (list.size() == 2) {
691                            return list.get(1);
692                    }
693                    else {
694                            return null;
695                    }
696            }
697    
698            /**
699             * Finds all the images.
700             *
701             * @return the images
702             * @throws SystemException if a system exception occurred
703             */
704            public List<Image> findAll() throws SystemException {
705                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
706            }
707    
708            /**
709             * Finds a range of all the images.
710             *
711             * <p>
712             * 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.
713             * </p>
714             *
715             * @param start the lower bound of the range of images to return
716             * @param end the upper bound of the range of images to return (not inclusive)
717             * @return the range of images
718             * @throws SystemException if a system exception occurred
719             */
720            public List<Image> findAll(int start, int end) throws SystemException {
721                    return findAll(start, end, null);
722            }
723    
724            /**
725             * Finds an ordered range of all the images.
726             *
727             * <p>
728             * 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.
729             * </p>
730             *
731             * @param start the lower bound of the range of images to return
732             * @param end the upper bound of the range of images to return (not inclusive)
733             * @param orderByComparator the comparator to order the results by
734             * @return the ordered range of images
735             * @throws SystemException if a system exception occurred
736             */
737            public List<Image> findAll(int start, int end,
738                    OrderByComparator orderByComparator) throws SystemException {
739                    Object[] finderArgs = new Object[] {
740                                    String.valueOf(start), String.valueOf(end),
741                                    String.valueOf(orderByComparator)
742                            };
743    
744                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
745                                    finderArgs, this);
746    
747                    if (list == null) {
748                            Session session = null;
749    
750                            try {
751                                    session = openSession();
752    
753                                    StringBundler query = null;
754                                    String sql = null;
755    
756                                    if (orderByComparator != null) {
757                                            query = new StringBundler(2 +
758                                                            (orderByComparator.getOrderByFields().length * 3));
759    
760                                            query.append(_SQL_SELECT_IMAGE);
761    
762                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
763                                                    orderByComparator);
764    
765                                            sql = query.toString();
766                                    }
767                                    else {
768                                            sql = _SQL_SELECT_IMAGE.concat(ImageModelImpl.ORDER_BY_JPQL);
769                                    }
770    
771                                    Query q = session.createQuery(sql);
772    
773                                    if (orderByComparator == null) {
774                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
775                                                            end, false);
776    
777                                            Collections.sort(list);
778                                    }
779                                    else {
780                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
781                                                            end);
782                                    }
783                            }
784                            catch (Exception e) {
785                                    throw processException(e);
786                            }
787                            finally {
788                                    if (list == null) {
789                                            list = new ArrayList<Image>();
790                                    }
791    
792                                    cacheResult(list);
793    
794                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
795    
796                                    closeSession(session);
797                            }
798                    }
799    
800                    return list;
801            }
802    
803            /**
804             * Removes all the images where size &lt; &#63; from the database.
805             *
806             * @param size the size to search with
807             * @throws SystemException if a system exception occurred
808             */
809            public void removeByLtSize(int size) throws SystemException {
810                    for (Image image : findByLtSize(size)) {
811                            remove(image);
812                    }
813            }
814    
815            /**
816             * Removes all the images from the database.
817             *
818             * @throws SystemException if a system exception occurred
819             */
820            public void removeAll() throws SystemException {
821                    for (Image image : findAll()) {
822                            remove(image);
823                    }
824            }
825    
826            /**
827             * Counts all the images where size &lt; &#63;.
828             *
829             * @param size the size to search with
830             * @return the number of matching images
831             * @throws SystemException if a system exception occurred
832             */
833            public int countByLtSize(int size) throws SystemException {
834                    Object[] finderArgs = new Object[] { size };
835    
836                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LTSIZE,
837                                    finderArgs, this);
838    
839                    if (count == null) {
840                            Session session = null;
841    
842                            try {
843                                    session = openSession();
844    
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                                    Query q = session.createQuery(sql);
854    
855                                    QueryPos qPos = QueryPos.getInstance(q);
856    
857                                    qPos.add(size);
858    
859                                    count = (Long)q.uniqueResult();
860                            }
861                            catch (Exception e) {
862                                    throw processException(e);
863                            }
864                            finally {
865                                    if (count == null) {
866                                            count = Long.valueOf(0);
867                                    }
868    
869                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LTSIZE,
870                                            finderArgs, count);
871    
872                                    closeSession(session);
873                            }
874                    }
875    
876                    return count.intValue();
877            }
878    
879            /**
880             * Counts all the images.
881             *
882             * @return the number of images
883             * @throws SystemException if a system exception occurred
884             */
885            public int countAll() throws SystemException {
886                    Object[] finderArgs = new Object[0];
887    
888                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
889                                    finderArgs, this);
890    
891                    if (count == null) {
892                            Session session = null;
893    
894                            try {
895                                    session = openSession();
896    
897                                    Query q = session.createQuery(_SQL_COUNT_IMAGE);
898    
899                                    count = (Long)q.uniqueResult();
900                            }
901                            catch (Exception e) {
902                                    throw processException(e);
903                            }
904                            finally {
905                                    if (count == null) {
906                                            count = Long.valueOf(0);
907                                    }
908    
909                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
910                                            count);
911    
912                                    closeSession(session);
913                            }
914                    }
915    
916                    return count.intValue();
917            }
918    
919            /**
920             * Initializes the image persistence.
921             */
922            public void afterPropertiesSet() {
923                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
924                                            com.liferay.portal.util.PropsUtil.get(
925                                                    "value.object.listener.com.liferay.portal.model.Image")));
926    
927                    if (listenerClassNames.length > 0) {
928                            try {
929                                    List<ModelListener<Image>> listenersList = new ArrayList<ModelListener<Image>>();
930    
931                                    for (String listenerClassName : listenerClassNames) {
932                                            listenersList.add((ModelListener<Image>)InstanceFactory.newInstance(
933                                                            listenerClassName));
934                                    }
935    
936                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
937                            }
938                            catch (Exception e) {
939                                    _log.error(e);
940                            }
941                    }
942            }
943    
944            public void destroy() {
945                    EntityCacheUtil.removeCache(ImageImpl.class.getName());
946                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
947                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
948            }
949    
950            @BeanReference(type = AccountPersistence.class)
951            protected AccountPersistence accountPersistence;
952            @BeanReference(type = AddressPersistence.class)
953            protected AddressPersistence addressPersistence;
954            @BeanReference(type = BrowserTrackerPersistence.class)
955            protected BrowserTrackerPersistence browserTrackerPersistence;
956            @BeanReference(type = ClassNamePersistence.class)
957            protected ClassNamePersistence classNamePersistence;
958            @BeanReference(type = ClusterGroupPersistence.class)
959            protected ClusterGroupPersistence clusterGroupPersistence;
960            @BeanReference(type = CompanyPersistence.class)
961            protected CompanyPersistence companyPersistence;
962            @BeanReference(type = ContactPersistence.class)
963            protected ContactPersistence contactPersistence;
964            @BeanReference(type = CountryPersistence.class)
965            protected CountryPersistence countryPersistence;
966            @BeanReference(type = EmailAddressPersistence.class)
967            protected EmailAddressPersistence emailAddressPersistence;
968            @BeanReference(type = GroupPersistence.class)
969            protected GroupPersistence groupPersistence;
970            @BeanReference(type = ImagePersistence.class)
971            protected ImagePersistence imagePersistence;
972            @BeanReference(type = LayoutPersistence.class)
973            protected LayoutPersistence layoutPersistence;
974            @BeanReference(type = LayoutPrototypePersistence.class)
975            protected LayoutPrototypePersistence layoutPrototypePersistence;
976            @BeanReference(type = LayoutSetPersistence.class)
977            protected LayoutSetPersistence layoutSetPersistence;
978            @BeanReference(type = LayoutSetPrototypePersistence.class)
979            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
980            @BeanReference(type = ListTypePersistence.class)
981            protected ListTypePersistence listTypePersistence;
982            @BeanReference(type = LockPersistence.class)
983            protected LockPersistence lockPersistence;
984            @BeanReference(type = MembershipRequestPersistence.class)
985            protected MembershipRequestPersistence membershipRequestPersistence;
986            @BeanReference(type = OrganizationPersistence.class)
987            protected OrganizationPersistence organizationPersistence;
988            @BeanReference(type = OrgGroupPermissionPersistence.class)
989            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
990            @BeanReference(type = OrgGroupRolePersistence.class)
991            protected OrgGroupRolePersistence orgGroupRolePersistence;
992            @BeanReference(type = OrgLaborPersistence.class)
993            protected OrgLaborPersistence orgLaborPersistence;
994            @BeanReference(type = PasswordPolicyPersistence.class)
995            protected PasswordPolicyPersistence passwordPolicyPersistence;
996            @BeanReference(type = PasswordPolicyRelPersistence.class)
997            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
998            @BeanReference(type = PasswordTrackerPersistence.class)
999            protected PasswordTrackerPersistence passwordTrackerPersistence;
1000            @BeanReference(type = PermissionPersistence.class)
1001            protected PermissionPersistence permissionPersistence;
1002            @BeanReference(type = PhonePersistence.class)
1003            protected PhonePersistence phonePersistence;
1004            @BeanReference(type = PluginSettingPersistence.class)
1005            protected PluginSettingPersistence pluginSettingPersistence;
1006            @BeanReference(type = PortletPersistence.class)
1007            protected PortletPersistence portletPersistence;
1008            @BeanReference(type = PortletItemPersistence.class)
1009            protected PortletItemPersistence portletItemPersistence;
1010            @BeanReference(type = PortletPreferencesPersistence.class)
1011            protected PortletPreferencesPersistence portletPreferencesPersistence;
1012            @BeanReference(type = RegionPersistence.class)
1013            protected RegionPersistence regionPersistence;
1014            @BeanReference(type = ReleasePersistence.class)
1015            protected ReleasePersistence releasePersistence;
1016            @BeanReference(type = ResourcePersistence.class)
1017            protected ResourcePersistence resourcePersistence;
1018            @BeanReference(type = ResourceActionPersistence.class)
1019            protected ResourceActionPersistence resourceActionPersistence;
1020            @BeanReference(type = ResourceCodePersistence.class)
1021            protected ResourceCodePersistence resourceCodePersistence;
1022            @BeanReference(type = ResourcePermissionPersistence.class)
1023            protected ResourcePermissionPersistence resourcePermissionPersistence;
1024            @BeanReference(type = RolePersistence.class)
1025            protected RolePersistence rolePersistence;
1026            @BeanReference(type = ServiceComponentPersistence.class)
1027            protected ServiceComponentPersistence serviceComponentPersistence;
1028            @BeanReference(type = ShardPersistence.class)
1029            protected ShardPersistence shardPersistence;
1030            @BeanReference(type = SubscriptionPersistence.class)
1031            protected SubscriptionPersistence subscriptionPersistence;
1032            @BeanReference(type = TicketPersistence.class)
1033            protected TicketPersistence ticketPersistence;
1034            @BeanReference(type = TeamPersistence.class)
1035            protected TeamPersistence teamPersistence;
1036            @BeanReference(type = UserPersistence.class)
1037            protected UserPersistence userPersistence;
1038            @BeanReference(type = UserGroupPersistence.class)
1039            protected UserGroupPersistence userGroupPersistence;
1040            @BeanReference(type = UserGroupGroupRolePersistence.class)
1041            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1042            @BeanReference(type = UserGroupRolePersistence.class)
1043            protected UserGroupRolePersistence userGroupRolePersistence;
1044            @BeanReference(type = UserIdMapperPersistence.class)
1045            protected UserIdMapperPersistence userIdMapperPersistence;
1046            @BeanReference(type = UserTrackerPersistence.class)
1047            protected UserTrackerPersistence userTrackerPersistence;
1048            @BeanReference(type = UserTrackerPathPersistence.class)
1049            protected UserTrackerPathPersistence userTrackerPathPersistence;
1050            @BeanReference(type = WebDAVPropsPersistence.class)
1051            protected WebDAVPropsPersistence webDAVPropsPersistence;
1052            @BeanReference(type = WebsitePersistence.class)
1053            protected WebsitePersistence websitePersistence;
1054            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1055            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1056            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1057            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1058            @BeanReference(type = IGImagePersistence.class)
1059            protected IGImagePersistence igImagePersistence;
1060            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1061            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1062            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1063            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1064            private static final String _FINDER_COLUMN_LTSIZE_SIZE_2 = "image.size < ?";
1065            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1066            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1067            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1068            private static Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1069    }