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