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