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