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