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.setCompanyId(image.getCompanyId());
797                    imageImpl.setModifiedDate(image.getModifiedDate());
798                    imageImpl.setType(image.getType());
799                    imageImpl.setHeight(image.getHeight());
800                    imageImpl.setWidth(image.getWidth());
801                    imageImpl.setSize(image.getSize());
802    
803                    return imageImpl;
804            }
805    
806            /**
807             * Returns the image with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
808             *
809             * @param primaryKey the primary key of the image
810             * @return the image
811             * @throws NoSuchImageException if a image with the primary key could not be found
812             */
813            @Override
814            public Image findByPrimaryKey(Serializable primaryKey)
815                    throws NoSuchImageException {
816                    Image image = fetchByPrimaryKey(primaryKey);
817    
818                    if (image == null) {
819                            if (_log.isWarnEnabled()) {
820                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
821                            }
822    
823                            throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
824                                    primaryKey);
825                    }
826    
827                    return image;
828            }
829    
830            /**
831             * Returns the image with the primary key or throws a {@link NoSuchImageException} if it could not be found.
832             *
833             * @param imageId the primary key of the image
834             * @return the image
835             * @throws NoSuchImageException if a image with the primary key could not be found
836             */
837            @Override
838            public Image findByPrimaryKey(long imageId) throws NoSuchImageException {
839                    return findByPrimaryKey((Serializable)imageId);
840            }
841    
842            /**
843             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
844             *
845             * @param primaryKey the primary key of the image
846             * @return the image, or <code>null</code> if a image with the primary key could not be found
847             */
848            @Override
849            public Image fetchByPrimaryKey(Serializable primaryKey) {
850                    Image image = (Image)entityCache.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
851                                    ImageImpl.class, primaryKey);
852    
853                    if (image == _nullImage) {
854                            return null;
855                    }
856    
857                    if (image == null) {
858                            Session session = null;
859    
860                            try {
861                                    session = openSession();
862    
863                                    image = (Image)session.get(ImageImpl.class, primaryKey);
864    
865                                    if (image != null) {
866                                            cacheResult(image);
867                                    }
868                                    else {
869                                            entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
870                                                    ImageImpl.class, primaryKey, _nullImage);
871                                    }
872                            }
873                            catch (Exception e) {
874                                    entityCache.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
875                                            ImageImpl.class, primaryKey);
876    
877                                    throw processException(e);
878                            }
879                            finally {
880                                    closeSession(session);
881                            }
882                    }
883    
884                    return image;
885            }
886    
887            /**
888             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
889             *
890             * @param imageId the primary key of the image
891             * @return the image, or <code>null</code> if a image with the primary key could not be found
892             */
893            @Override
894            public Image fetchByPrimaryKey(long imageId) {
895                    return fetchByPrimaryKey((Serializable)imageId);
896            }
897    
898            @Override
899            public Map<Serializable, Image> fetchByPrimaryKeys(
900                    Set<Serializable> primaryKeys) {
901                    if (primaryKeys.isEmpty()) {
902                            return Collections.emptyMap();
903                    }
904    
905                    Map<Serializable, Image> map = new HashMap<Serializable, Image>();
906    
907                    if (primaryKeys.size() == 1) {
908                            Iterator<Serializable> iterator = primaryKeys.iterator();
909    
910                            Serializable primaryKey = iterator.next();
911    
912                            Image image = fetchByPrimaryKey(primaryKey);
913    
914                            if (image != null) {
915                                    map.put(primaryKey, image);
916                            }
917    
918                            return map;
919                    }
920    
921                    Set<Serializable> uncachedPrimaryKeys = null;
922    
923                    for (Serializable primaryKey : primaryKeys) {
924                            Image image = (Image)entityCache.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
925                                            ImageImpl.class, primaryKey);
926    
927                            if (image == null) {
928                                    if (uncachedPrimaryKeys == null) {
929                                            uncachedPrimaryKeys = new HashSet<Serializable>();
930                                    }
931    
932                                    uncachedPrimaryKeys.add(primaryKey);
933                            }
934                            else {
935                                    map.put(primaryKey, image);
936                            }
937                    }
938    
939                    if (uncachedPrimaryKeys == null) {
940                            return map;
941                    }
942    
943                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
944                                    1);
945    
946                    query.append(_SQL_SELECT_IMAGE_WHERE_PKS_IN);
947    
948                    for (Serializable primaryKey : uncachedPrimaryKeys) {
949                            query.append(String.valueOf(primaryKey));
950    
951                            query.append(StringPool.COMMA);
952                    }
953    
954                    query.setIndex(query.index() - 1);
955    
956                    query.append(StringPool.CLOSE_PARENTHESIS);
957    
958                    String sql = query.toString();
959    
960                    Session session = null;
961    
962                    try {
963                            session = openSession();
964    
965                            Query q = session.createQuery(sql);
966    
967                            for (Image image : (List<Image>)q.list()) {
968                                    map.put(image.getPrimaryKeyObj(), image);
969    
970                                    cacheResult(image);
971    
972                                    uncachedPrimaryKeys.remove(image.getPrimaryKeyObj());
973                            }
974    
975                            for (Serializable primaryKey : uncachedPrimaryKeys) {
976                                    entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
977                                            ImageImpl.class, primaryKey, _nullImage);
978                            }
979                    }
980                    catch (Exception e) {
981                            throw processException(e);
982                    }
983                    finally {
984                            closeSession(session);
985                    }
986    
987                    return map;
988            }
989    
990            /**
991             * Returns all the images.
992             *
993             * @return the images
994             */
995            @Override
996            public List<Image> findAll() {
997                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
998            }
999    
1000            /**
1001             * Returns a range of all the images.
1002             *
1003             * <p>
1004             * 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.
1005             * </p>
1006             *
1007             * @param start the lower bound of the range of images
1008             * @param end the upper bound of the range of images (not inclusive)
1009             * @return the range of images
1010             */
1011            @Override
1012            public List<Image> findAll(int start, int end) {
1013                    return findAll(start, end, null);
1014            }
1015    
1016            /**
1017             * Returns an ordered range of all the images.
1018             *
1019             * <p>
1020             * 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.
1021             * </p>
1022             *
1023             * @param start the lower bound of the range of images
1024             * @param end the upper bound of the range of images (not inclusive)
1025             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1026             * @return the ordered range of images
1027             */
1028            @Override
1029            public List<Image> findAll(int start, int end,
1030                    OrderByComparator<Image> orderByComparator) {
1031                    return findAll(start, end, orderByComparator, true);
1032            }
1033    
1034            /**
1035             * Returns an ordered range of all the images.
1036             *
1037             * <p>
1038             * 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.
1039             * </p>
1040             *
1041             * @param start the lower bound of the range of images
1042             * @param end the upper bound of the range of images (not inclusive)
1043             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1044             * @param retrieveFromCache whether to retrieve from the finder cache
1045             * @return the ordered range of images
1046             */
1047            @Override
1048            public List<Image> findAll(int start, int end,
1049                    OrderByComparator<Image> orderByComparator, boolean retrieveFromCache) {
1050                    boolean pagination = true;
1051                    FinderPath finderPath = null;
1052                    Object[] finderArgs = null;
1053    
1054                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1055                                    (orderByComparator == null)) {
1056                            pagination = false;
1057                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1058                            finderArgs = FINDER_ARGS_EMPTY;
1059                    }
1060                    else {
1061                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1062                            finderArgs = new Object[] { start, end, orderByComparator };
1063                    }
1064    
1065                    List<Image> list = null;
1066    
1067                    if (retrieveFromCache) {
1068                            list = (List<Image>)finderCache.getResult(finderPath, finderArgs,
1069                                            this);
1070                    }
1071    
1072                    if (list == null) {
1073                            StringBundler query = null;
1074                            String sql = null;
1075    
1076                            if (orderByComparator != null) {
1077                                    query = new StringBundler(2 +
1078                                                    (orderByComparator.getOrderByFields().length * 3));
1079    
1080                                    query.append(_SQL_SELECT_IMAGE);
1081    
1082                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1083                                            orderByComparator);
1084    
1085                                    sql = query.toString();
1086                            }
1087                            else {
1088                                    sql = _SQL_SELECT_IMAGE;
1089    
1090                                    if (pagination) {
1091                                            sql = sql.concat(ImageModelImpl.ORDER_BY_JPQL);
1092                                    }
1093                            }
1094    
1095                            Session session = null;
1096    
1097                            try {
1098                                    session = openSession();
1099    
1100                                    Query q = session.createQuery(sql);
1101    
1102                                    if (!pagination) {
1103                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
1104                                                            end, false);
1105    
1106                                            Collections.sort(list);
1107    
1108                                            list = Collections.unmodifiableList(list);
1109                                    }
1110                                    else {
1111                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
1112                                                            end);
1113                                    }
1114    
1115                                    cacheResult(list);
1116    
1117                                    finderCache.putResult(finderPath, finderArgs, list);
1118                            }
1119                            catch (Exception e) {
1120                                    finderCache.removeResult(finderPath, finderArgs);
1121    
1122                                    throw processException(e);
1123                            }
1124                            finally {
1125                                    closeSession(session);
1126                            }
1127                    }
1128    
1129                    return list;
1130            }
1131    
1132            /**
1133             * Removes all the images from the database.
1134             *
1135             */
1136            @Override
1137            public void removeAll() {
1138                    for (Image image : findAll()) {
1139                            remove(image);
1140                    }
1141            }
1142    
1143            /**
1144             * Returns the number of images.
1145             *
1146             * @return the number of images
1147             */
1148            @Override
1149            public int countAll() {
1150                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
1151                                    FINDER_ARGS_EMPTY, this);
1152    
1153                    if (count == null) {
1154                            Session session = null;
1155    
1156                            try {
1157                                    session = openSession();
1158    
1159                                    Query q = session.createQuery(_SQL_COUNT_IMAGE);
1160    
1161                                    count = (Long)q.uniqueResult();
1162    
1163                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
1164                                            count);
1165                            }
1166                            catch (Exception e) {
1167                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
1168                                            FINDER_ARGS_EMPTY);
1169    
1170                                    throw processException(e);
1171                            }
1172                            finally {
1173                                    closeSession(session);
1174                            }
1175                    }
1176    
1177                    return count.intValue();
1178            }
1179    
1180            @Override
1181            public Set<String> getBadColumnNames() {
1182                    return _badColumnNames;
1183            }
1184    
1185            @Override
1186            protected Map<String, Integer> getTableColumnsMap() {
1187                    return ImageModelImpl.TABLE_COLUMNS_MAP;
1188            }
1189    
1190            /**
1191             * Initializes the image persistence.
1192             */
1193            public void afterPropertiesSet() {
1194            }
1195    
1196            public void destroy() {
1197                    entityCache.removeCache(ImageImpl.class.getName());
1198                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1199                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1200                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1201            }
1202    
1203            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1204            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1205            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1206            private static final String _SQL_SELECT_IMAGE_WHERE_PKS_IN = "SELECT image FROM Image image WHERE imageId IN (";
1207            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1208            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1209            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1210            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1211            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1212            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1213            private static final Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1214            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1215                                    "type", "size"
1216                            });
1217            private static final Image _nullImage = new ImageImpl() {
1218                            @Override
1219                            public Object clone() {
1220                                    return this;
1221                            }
1222    
1223                            @Override
1224                            public CacheModel<Image> toCacheModel() {
1225                                    return _nullImageCacheModel;
1226                            }
1227                    };
1228    
1229            private static final CacheModel<Image> _nullImageCacheModel = new NullCacheModel();
1230    
1231            private static class NullCacheModel implements CacheModel<Image>, MVCCModel {
1232                    @Override
1233                    public long getMvccVersion() {
1234                            return -1;
1235                    }
1236    
1237                    @Override
1238                    public void setMvccVersion(long mvccVersion) {
1239                    }
1240    
1241                    @Override
1242                    public Image toEntityModel() {
1243                            return _nullImage;
1244                    }
1245            }
1246    }