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