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