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