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