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