001    /**
002     * Copyright (c) 2000-2012 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.NoSuchModelException;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
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    
050    /**
051     * The persistence implementation for the image service.
052     *
053     * <p>
054     * Caching information and settings can be found in <code>portal.properties</code>
055     * </p>
056     *
057     * @author Brian Wing Shun Chan
058     * @see ImagePersistence
059     * @see ImageUtil
060     * @generated
061     */
062    public class ImagePersistenceImpl extends BasePersistenceImpl<Image>
063            implements ImagePersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * 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.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = ImageImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
075                            ImageModelImpl.FINDER_CACHE_ENABLED, ImageImpl.class,
076                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
078                            ImageModelImpl.FINDER_CACHE_ENABLED, ImageImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
081                            ImageModelImpl.FINDER_CACHE_ENABLED, Long.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_LTSIZE = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
084                            ImageModelImpl.FINDER_CACHE_ENABLED, ImageImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByLtSize",
086                            new String[] {
087                                    Integer.class.getName(),
088                                    
089                            Integer.class.getName(), Integer.class.getName(),
090                                    OrderByComparator.class.getName()
091                            });
092            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_LTSIZE = new FinderPath(ImageModelImpl.ENTITY_CACHE_ENABLED,
093                            ImageModelImpl.FINDER_CACHE_ENABLED, Long.class,
094                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByLtSize",
095                            new String[] { Integer.class.getName() });
096    
097            /**
098             * Returns all the images where size &lt; &#63;.
099             *
100             * @param size the size
101             * @return the matching images
102             * @throws SystemException if a system exception occurred
103             */
104            public List<Image> findByLtSize(int size) throws SystemException {
105                    return findByLtSize(size, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
106            }
107    
108            /**
109             * Returns a range of all the images where size &lt; &#63;.
110             *
111             * <p>
112             * 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.
113             * </p>
114             *
115             * @param size the size
116             * @param start the lower bound of the range of images
117             * @param end the upper bound of the range of images (not inclusive)
118             * @return the range of matching images
119             * @throws SystemException if a system exception occurred
120             */
121            public List<Image> findByLtSize(int size, int start, int end)
122                    throws SystemException {
123                    return findByLtSize(size, start, end, null);
124            }
125    
126            /**
127             * Returns an ordered range of all the images where size &lt; &#63;.
128             *
129             * <p>
130             * 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.
131             * </p>
132             *
133             * @param size the size
134             * @param start the lower bound of the range of images
135             * @param end the upper bound of the range of images (not inclusive)
136             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
137             * @return the ordered range of matching images
138             * @throws SystemException if a system exception occurred
139             */
140            public List<Image> findByLtSize(int size, int start, int end,
141                    OrderByComparator orderByComparator) throws SystemException {
142                    boolean pagination = true;
143                    FinderPath finderPath = null;
144                    Object[] finderArgs = null;
145    
146                    finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_LTSIZE;
147                    finderArgs = new Object[] { size, start, end, orderByComparator };
148    
149                    List<Image> list = (List<Image>)FinderCacheUtil.getResult(finderPath,
150                                    finderArgs, this);
151    
152                    if ((list != null) && !list.isEmpty()) {
153                            for (Image image : list) {
154                                    if ((size != image.getSize())) {
155                                            list = null;
156    
157                                            break;
158                                    }
159                            }
160                    }
161    
162                    if (list == null) {
163                            StringBundler query = null;
164    
165                            if (orderByComparator != null) {
166                                    query = new StringBundler(3 +
167                                                    (orderByComparator.getOrderByFields().length * 3));
168                            }
169                            else {
170                                    query = new StringBundler(3);
171                            }
172    
173                            query.append(_SQL_SELECT_IMAGE_WHERE);
174    
175                            query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
176    
177                            if (orderByComparator != null) {
178                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
179                                            orderByComparator);
180                            }
181                            else
182                             if (pagination) {
183                                    query.append(ImageModelImpl.ORDER_BY_JPQL);
184                            }
185    
186                            String sql = query.toString();
187    
188                            Session session = null;
189    
190                            try {
191                                    session = openSession();
192    
193                                    Query q = session.createQuery(sql);
194    
195                                    QueryPos qPos = QueryPos.getInstance(q);
196    
197                                    qPos.add(size);
198    
199                                    if (!pagination) {
200                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
201                                                            end, false);
202    
203                                            Collections.sort(list);
204    
205                                            list = new UnmodifiableList<Image>(list);
206                                    }
207                                    else {
208                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
209                                                            end);
210                                    }
211    
212                                    cacheResult(list);
213    
214                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
215                            }
216                            catch (Exception e) {
217                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
218    
219                                    throw processException(e);
220                            }
221                            finally {
222                                    closeSession(session);
223                            }
224                    }
225    
226                    return list;
227            }
228    
229            /**
230             * Returns the first image in the ordered set where size &lt; &#63;.
231             *
232             * @param size the size
233             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
234             * @return the first matching image
235             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
236             * @throws SystemException if a system exception occurred
237             */
238            public Image findByLtSize_First(int size,
239                    OrderByComparator orderByComparator)
240                    throws NoSuchImageException, SystemException {
241                    Image image = fetchByLtSize_First(size, orderByComparator);
242    
243                    if (image != null) {
244                            return image;
245                    }
246    
247                    StringBundler msg = new StringBundler(4);
248    
249                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
250    
251                    msg.append("size=");
252                    msg.append(size);
253    
254                    msg.append(StringPool.CLOSE_CURLY_BRACE);
255    
256                    throw new NoSuchImageException(msg.toString());
257            }
258    
259            /**
260             * Returns the first image in the ordered set where size &lt; &#63;.
261             *
262             * @param size the size
263             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
264             * @return the first matching image, or <code>null</code> if a matching image could not be found
265             * @throws SystemException if a system exception occurred
266             */
267            public Image fetchByLtSize_First(int size,
268                    OrderByComparator orderByComparator) throws SystemException {
269                    List<Image> list = findByLtSize(size, 0, 1, orderByComparator);
270    
271                    if (!list.isEmpty()) {
272                            return list.get(0);
273                    }
274    
275                    return null;
276            }
277    
278            /**
279             * Returns the last image in the ordered set where size &lt; &#63;.
280             *
281             * @param size the size
282             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
283             * @return the last matching image
284             * @throws com.liferay.portal.NoSuchImageException if a matching image could not be found
285             * @throws SystemException if a system exception occurred
286             */
287            public Image findByLtSize_Last(int size, OrderByComparator orderByComparator)
288                    throws NoSuchImageException, SystemException {
289                    Image image = fetchByLtSize_Last(size, orderByComparator);
290    
291                    if (image != null) {
292                            return image;
293                    }
294    
295                    StringBundler msg = new StringBundler(4);
296    
297                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
298    
299                    msg.append("size=");
300                    msg.append(size);
301    
302                    msg.append(StringPool.CLOSE_CURLY_BRACE);
303    
304                    throw new NoSuchImageException(msg.toString());
305            }
306    
307            /**
308             * Returns the last image in the ordered set where size &lt; &#63;.
309             *
310             * @param size the size
311             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
312             * @return the last matching image, or <code>null</code> if a matching image could not be found
313             * @throws SystemException if a system exception occurred
314             */
315            public Image fetchByLtSize_Last(int size,
316                    OrderByComparator orderByComparator) throws SystemException {
317                    int count = countByLtSize(size);
318    
319                    List<Image> list = findByLtSize(size, count - 1, count,
320                                    orderByComparator);
321    
322                    if (!list.isEmpty()) {
323                            return list.get(0);
324                    }
325    
326                    return null;
327            }
328    
329            /**
330             * Returns the images before and after the current image in the ordered set where size &lt; &#63;.
331             *
332             * @param imageId the primary key of the current image
333             * @param size the size
334             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
335             * @return the previous, current, and next image
336             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
337             * @throws SystemException if a system exception occurred
338             */
339            public Image[] findByLtSize_PrevAndNext(long imageId, int size,
340                    OrderByComparator orderByComparator)
341                    throws NoSuchImageException, SystemException {
342                    Image image = findByPrimaryKey(imageId);
343    
344                    Session session = null;
345    
346                    try {
347                            session = openSession();
348    
349                            Image[] array = new ImageImpl[3];
350    
351                            array[0] = getByLtSize_PrevAndNext(session, image, size,
352                                            orderByComparator, true);
353    
354                            array[1] = image;
355    
356                            array[2] = getByLtSize_PrevAndNext(session, image, size,
357                                            orderByComparator, false);
358    
359                            return array;
360                    }
361                    catch (Exception e) {
362                            throw processException(e);
363                    }
364                    finally {
365                            closeSession(session);
366                    }
367            }
368    
369            protected Image getByLtSize_PrevAndNext(Session session, Image image,
370                    int size, OrderByComparator orderByComparator, boolean previous) {
371                    StringBundler query = null;
372    
373                    if (orderByComparator != null) {
374                            query = new StringBundler(6 +
375                                            (orderByComparator.getOrderByFields().length * 6));
376                    }
377                    else {
378                            query = new StringBundler(3);
379                    }
380    
381                    query.append(_SQL_SELECT_IMAGE_WHERE);
382    
383                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
384    
385                    if (orderByComparator != null) {
386                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
387    
388                            if (orderByConditionFields.length > 0) {
389                                    query.append(WHERE_AND);
390                            }
391    
392                            for (int i = 0; i < orderByConditionFields.length; i++) {
393                                    query.append(_ORDER_BY_ENTITY_ALIAS);
394                                    query.append(orderByConditionFields[i]);
395    
396                                    if ((i + 1) < orderByConditionFields.length) {
397                                            if (orderByComparator.isAscending() ^ previous) {
398                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
399                                            }
400                                            else {
401                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
402                                            }
403                                    }
404                                    else {
405                                            if (orderByComparator.isAscending() ^ previous) {
406                                                    query.append(WHERE_GREATER_THAN);
407                                            }
408                                            else {
409                                                    query.append(WHERE_LESSER_THAN);
410                                            }
411                                    }
412                            }
413    
414                            query.append(ORDER_BY_CLAUSE);
415    
416                            String[] orderByFields = orderByComparator.getOrderByFields();
417    
418                            for (int i = 0; i < orderByFields.length; i++) {
419                                    query.append(_ORDER_BY_ENTITY_ALIAS);
420                                    query.append(orderByFields[i]);
421    
422                                    if ((i + 1) < orderByFields.length) {
423                                            if (orderByComparator.isAscending() ^ previous) {
424                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
425                                            }
426                                            else {
427                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
428                                            }
429                                    }
430                                    else {
431                                            if (orderByComparator.isAscending() ^ previous) {
432                                                    query.append(ORDER_BY_ASC);
433                                            }
434                                            else {
435                                                    query.append(ORDER_BY_DESC);
436                                            }
437                                    }
438                            }
439                    }
440                    else {
441                            query.append(ImageModelImpl.ORDER_BY_JPQL);
442                    }
443    
444                    String sql = query.toString();
445    
446                    Query q = session.createQuery(sql);
447    
448                    q.setFirstResult(0);
449                    q.setMaxResults(2);
450    
451                    QueryPos qPos = QueryPos.getInstance(q);
452    
453                    qPos.add(size);
454    
455                    if (orderByComparator != null) {
456                            Object[] values = orderByComparator.getOrderByConditionValues(image);
457    
458                            for (Object value : values) {
459                                    qPos.add(value);
460                            }
461                    }
462    
463                    List<Image> list = q.list();
464    
465                    if (list.size() == 2) {
466                            return list.get(1);
467                    }
468                    else {
469                            return null;
470                    }
471            }
472    
473            /**
474             * Removes all the images where size &lt; &#63; from the database.
475             *
476             * @param size the size
477             * @throws SystemException if a system exception occurred
478             */
479            public void removeByLtSize(int size) throws SystemException {
480                    for (Image image : findByLtSize(size, QueryUtil.ALL_POS,
481                                    QueryUtil.ALL_POS, null)) {
482                            remove(image);
483                    }
484            }
485    
486            /**
487             * Returns the number of images where size &lt; &#63;.
488             *
489             * @param size the size
490             * @return the number of matching images
491             * @throws SystemException if a system exception occurred
492             */
493            public int countByLtSize(int size) throws SystemException {
494                    FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_LTSIZE;
495    
496                    Object[] finderArgs = new Object[] { size };
497    
498                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
499                                    this);
500    
501                    if (count == null) {
502                            StringBundler query = new StringBundler(2);
503    
504                            query.append(_SQL_COUNT_IMAGE_WHERE);
505    
506                            query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
507    
508                            String sql = query.toString();
509    
510                            Session session = null;
511    
512                            try {
513                                    session = openSession();
514    
515                                    Query q = session.createQuery(sql);
516    
517                                    QueryPos qPos = QueryPos.getInstance(q);
518    
519                                    qPos.add(size);
520    
521                                    count = (Long)q.uniqueResult();
522    
523                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
524                            }
525                            catch (Exception e) {
526                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
527    
528                                    throw processException(e);
529                            }
530                            finally {
531                                    closeSession(session);
532                            }
533                    }
534    
535                    return count.intValue();
536            }
537    
538            private static final String _FINDER_COLUMN_LTSIZE_SIZE_2 = "image.size < ?";
539    
540            /**
541             * Caches the image in the entity cache if it is enabled.
542             *
543             * @param image the image
544             */
545            public void cacheResult(Image image) {
546                    EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
547                            ImageImpl.class, image.getPrimaryKey(), image);
548    
549                    image.resetOriginalValues();
550            }
551    
552            /**
553             * Caches the images in the entity cache if it is enabled.
554             *
555             * @param images the images
556             */
557            public void cacheResult(List<Image> images) {
558                    for (Image image : images) {
559                            if (EntityCacheUtil.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
560                                                    ImageImpl.class, image.getPrimaryKey()) == null) {
561                                    cacheResult(image);
562                            }
563                            else {
564                                    image.resetOriginalValues();
565                            }
566                    }
567            }
568    
569            /**
570             * Clears the cache for all images.
571             *
572             * <p>
573             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
574             * </p>
575             */
576            @Override
577            public void clearCache() {
578                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
579                            CacheRegistryUtil.clear(ImageImpl.class.getName());
580                    }
581    
582                    EntityCacheUtil.clearCache(ImageImpl.class.getName());
583    
584                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
585                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
586                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
587            }
588    
589            /**
590             * Clears the cache for the image.
591             *
592             * <p>
593             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
594             * </p>
595             */
596            @Override
597            public void clearCache(Image image) {
598                    EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
599                            ImageImpl.class, image.getPrimaryKey());
600    
601                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
602                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
603            }
604    
605            @Override
606            public void clearCache(List<Image> images) {
607                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
608                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
609    
610                    for (Image image : images) {
611                            EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
612                                    ImageImpl.class, image.getPrimaryKey());
613                    }
614            }
615    
616            /**
617             * Creates a new image with the primary key. Does not add the image to the database.
618             *
619             * @param imageId the primary key for the new image
620             * @return the new image
621             */
622            public Image create(long imageId) {
623                    Image image = new ImageImpl();
624    
625                    image.setNew(true);
626                    image.setPrimaryKey(imageId);
627    
628                    return image;
629            }
630    
631            /**
632             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
633             *
634             * @param imageId the primary key of the image
635             * @return the image that was removed
636             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
637             * @throws SystemException if a system exception occurred
638             */
639            public Image remove(long imageId)
640                    throws NoSuchImageException, SystemException {
641                    return remove(Long.valueOf(imageId));
642            }
643    
644            /**
645             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
646             *
647             * @param primaryKey the primary key of the image
648             * @return the image that was removed
649             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
650             * @throws SystemException if a system exception occurred
651             */
652            @Override
653            public Image remove(Serializable primaryKey)
654                    throws NoSuchImageException, SystemException {
655                    Session session = null;
656    
657                    try {
658                            session = openSession();
659    
660                            Image image = (Image)session.get(ImageImpl.class, primaryKey);
661    
662                            if (image == null) {
663                                    if (_log.isWarnEnabled()) {
664                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
665                                    }
666    
667                                    throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
668                                            primaryKey);
669                            }
670    
671                            return remove(image);
672                    }
673                    catch (NoSuchImageException nsee) {
674                            throw nsee;
675                    }
676                    catch (Exception e) {
677                            throw processException(e);
678                    }
679                    finally {
680                            closeSession(session);
681                    }
682            }
683    
684            @Override
685            protected Image removeImpl(Image image) throws SystemException {
686                    image = toUnwrappedModel(image);
687    
688                    Session session = null;
689    
690                    try {
691                            session = openSession();
692    
693                            if (!session.contains(image)) {
694                                    image = (Image)session.get(ImageImpl.class,
695                                                    image.getPrimaryKeyObj());
696                            }
697    
698                            if (image != null) {
699                                    session.delete(image);
700                            }
701                    }
702                    catch (Exception e) {
703                            throw processException(e);
704                    }
705                    finally {
706                            closeSession(session);
707                    }
708    
709                    if (image != null) {
710                            clearCache(image);
711                    }
712    
713                    return image;
714            }
715    
716            @Override
717            public Image updateImpl(com.liferay.portal.model.Image image)
718                    throws SystemException {
719                    image = toUnwrappedModel(image);
720    
721                    boolean isNew = image.isNew();
722    
723                    Session session = null;
724    
725                    try {
726                            session = openSession();
727    
728                            if (image.isNew()) {
729                                    session.save(image);
730    
731                                    image.setNew(false);
732                            }
733                            else {
734                                    session.merge(image);
735                            }
736                    }
737                    catch (Exception e) {
738                            throw processException(e);
739                    }
740                    finally {
741                            closeSession(session);
742                    }
743    
744                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
745    
746                    if (isNew || !ImageModelImpl.COLUMN_BITMASK_ENABLED) {
747                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
748                    }
749    
750                    EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
751                            ImageImpl.class, image.getPrimaryKey(), image);
752    
753                    return image;
754            }
755    
756            protected Image toUnwrappedModel(Image image) {
757                    if (image instanceof ImageImpl) {
758                            return image;
759                    }
760    
761                    ImageImpl imageImpl = new ImageImpl();
762    
763                    imageImpl.setNew(image.isNew());
764                    imageImpl.setPrimaryKey(image.getPrimaryKey());
765    
766                    imageImpl.setImageId(image.getImageId());
767                    imageImpl.setModifiedDate(image.getModifiedDate());
768                    imageImpl.setType(image.getType());
769                    imageImpl.setHeight(image.getHeight());
770                    imageImpl.setWidth(image.getWidth());
771                    imageImpl.setSize(image.getSize());
772    
773                    return imageImpl;
774            }
775    
776            /**
777             * Returns the image with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
778             *
779             * @param primaryKey the primary key of the image
780             * @return the image
781             * @throws com.liferay.portal.NoSuchModelException if a image with the primary key could not be found
782             * @throws SystemException if a system exception occurred
783             */
784            @Override
785            public Image findByPrimaryKey(Serializable primaryKey)
786                    throws NoSuchModelException, SystemException {
787                    return findByPrimaryKey(((Long)primaryKey).longValue());
788            }
789    
790            /**
791             * Returns the image with the primary key or throws a {@link com.liferay.portal.NoSuchImageException} if it could not be found.
792             *
793             * @param imageId the primary key of the image
794             * @return the image
795             * @throws com.liferay.portal.NoSuchImageException if a image with the primary key could not be found
796             * @throws SystemException if a system exception occurred
797             */
798            public Image findByPrimaryKey(long imageId)
799                    throws NoSuchImageException, SystemException {
800                    Image image = fetchByPrimaryKey(imageId);
801    
802                    if (image == null) {
803                            if (_log.isWarnEnabled()) {
804                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + imageId);
805                            }
806    
807                            throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
808                                    imageId);
809                    }
810    
811                    return image;
812            }
813    
814            /**
815             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
816             *
817             * @param primaryKey the primary key of the image
818             * @return the image, or <code>null</code> if a image with the primary key could not be found
819             * @throws SystemException if a system exception occurred
820             */
821            @Override
822            public Image fetchByPrimaryKey(Serializable primaryKey)
823                    throws SystemException {
824                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
825            }
826    
827            /**
828             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
829             *
830             * @param imageId the primary key of the image
831             * @return the image, or <code>null</code> if a image with the primary key could not be found
832             * @throws SystemException if a system exception occurred
833             */
834            public Image fetchByPrimaryKey(long imageId) throws SystemException {
835                    Image image = (Image)EntityCacheUtil.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
836                                    ImageImpl.class, imageId);
837    
838                    if (image == _nullImage) {
839                            return null;
840                    }
841    
842                    if (image == null) {
843                            Session session = null;
844    
845                            try {
846                                    session = openSession();
847    
848                                    image = (Image)session.get(ImageImpl.class,
849                                                    Long.valueOf(imageId));
850    
851                                    if (image != null) {
852                                            cacheResult(image);
853                                    }
854                                    else {
855                                            EntityCacheUtil.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
856                                                    ImageImpl.class, imageId, _nullImage);
857                                    }
858                            }
859                            catch (Exception e) {
860                                    EntityCacheUtil.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
861                                            ImageImpl.class, imageId);
862    
863                                    throw processException(e);
864                            }
865                            finally {
866                                    closeSession(session);
867                            }
868                    }
869    
870                    return image;
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            /**
1041             * Initializes the image persistence.
1042             */
1043            public void afterPropertiesSet() {
1044                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1045                                            com.liferay.portal.util.PropsUtil.get(
1046                                                    "value.object.listener.com.liferay.portal.model.Image")));
1047    
1048                    if (listenerClassNames.length > 0) {
1049                            try {
1050                                    List<ModelListener<Image>> listenersList = new ArrayList<ModelListener<Image>>();
1051    
1052                                    for (String listenerClassName : listenerClassNames) {
1053                                            listenersList.add((ModelListener<Image>)InstanceFactory.newInstance(
1054                                                            listenerClassName));
1055                                    }
1056    
1057                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1058                            }
1059                            catch (Exception e) {
1060                                    _log.error(e);
1061                            }
1062                    }
1063            }
1064    
1065            public void destroy() {
1066                    EntityCacheUtil.removeCache(ImageImpl.class.getName());
1067                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1068                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1069                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1070            }
1071    
1072            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1073            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1074            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1075            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1076            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1077            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1078            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1079            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1080            private static Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1081            private static Image _nullImage = new ImageImpl() {
1082                            @Override
1083                            public Object clone() {
1084                                    return this;
1085                            }
1086    
1087                            @Override
1088                            public CacheModel<Image> toCacheModel() {
1089                                    return _nullImageCacheModel;
1090                            }
1091                    };
1092    
1093            private static CacheModel<Image> _nullImageCacheModel = new CacheModel<Image>() {
1094                            public Image toEntityModel() {
1095                                    return _nullImage;
1096                            }
1097                    };
1098    }