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