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