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