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 * 3));
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(6 +
406                                            (orderByComparator.getOrderByFields().length * 6));
407                    }
408                    else {
409                            query = new StringBundler(3);
410                    }
411    
412                    query.append(_SQL_SELECT_IMAGE_WHERE);
413    
414                    query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
415    
416                    if (orderByComparator != null) {
417                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
418    
419                            if (orderByConditionFields.length > 0) {
420                                    query.append(WHERE_AND);
421                            }
422    
423                            for (int i = 0; i < orderByConditionFields.length; i++) {
424                                    query.append(_ORDER_BY_ENTITY_ALIAS);
425                                    query.append(orderByConditionFields[i]);
426    
427                                    if ((i + 1) < orderByConditionFields.length) {
428                                            if (orderByComparator.isAscending() ^ previous) {
429                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
430                                            }
431                                            else {
432                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
433                                            }
434                                    }
435                                    else {
436                                            if (orderByComparator.isAscending() ^ previous) {
437                                                    query.append(WHERE_GREATER_THAN);
438                                            }
439                                            else {
440                                                    query.append(WHERE_LESSER_THAN);
441                                            }
442                                    }
443                            }
444    
445                            query.append(ORDER_BY_CLAUSE);
446    
447                            String[] orderByFields = orderByComparator.getOrderByFields();
448    
449                            for (int i = 0; i < orderByFields.length; i++) {
450                                    query.append(_ORDER_BY_ENTITY_ALIAS);
451                                    query.append(orderByFields[i]);
452    
453                                    if ((i + 1) < orderByFields.length) {
454                                            if (orderByComparator.isAscending() ^ previous) {
455                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
456                                            }
457                                            else {
458                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
459                                            }
460                                    }
461                                    else {
462                                            if (orderByComparator.isAscending() ^ previous) {
463                                                    query.append(ORDER_BY_ASC);
464                                            }
465                                            else {
466                                                    query.append(ORDER_BY_DESC);
467                                            }
468                                    }
469                            }
470                    }
471                    else {
472                            query.append(ImageModelImpl.ORDER_BY_JPQL);
473                    }
474    
475                    String sql = query.toString();
476    
477                    Query q = session.createQuery(sql);
478    
479                    q.setFirstResult(0);
480                    q.setMaxResults(2);
481    
482                    QueryPos qPos = QueryPos.getInstance(q);
483    
484                    qPos.add(size);
485    
486                    if (orderByComparator != null) {
487                            Object[] values = orderByComparator.getOrderByConditionValues(image);
488    
489                            for (Object value : values) {
490                                    qPos.add(value);
491                            }
492                    }
493    
494                    List<Image> list = q.list();
495    
496                    if (list.size() == 2) {
497                            return list.get(1);
498                    }
499                    else {
500                            return null;
501                    }
502            }
503    
504            /**
505             * Removes all the images where size &lt; &#63; from the database.
506             *
507             * @param size the size
508             */
509            @Override
510            public void removeByLtSize(int size) {
511                    for (Image image : findByLtSize(size, QueryUtil.ALL_POS,
512                                    QueryUtil.ALL_POS, null)) {
513                            remove(image);
514                    }
515            }
516    
517            /**
518             * Returns the number of images where size &lt; &#63;.
519             *
520             * @param size the size
521             * @return the number of matching images
522             */
523            @Override
524            public int countByLtSize(int size) {
525                    FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_LTSIZE;
526    
527                    Object[] finderArgs = new Object[] { size };
528    
529                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
530    
531                    if (count == null) {
532                            StringBundler query = new StringBundler(2);
533    
534                            query.append(_SQL_COUNT_IMAGE_WHERE);
535    
536                            query.append(_FINDER_COLUMN_LTSIZE_SIZE_2);
537    
538                            String sql = query.toString();
539    
540                            Session session = null;
541    
542                            try {
543                                    session = openSession();
544    
545                                    Query q = session.createQuery(sql);
546    
547                                    QueryPos qPos = QueryPos.getInstance(q);
548    
549                                    qPos.add(size);
550    
551                                    count = (Long)q.uniqueResult();
552    
553                                    finderCache.putResult(finderPath, finderArgs, count);
554                            }
555                            catch (Exception e) {
556                                    finderCache.removeResult(finderPath, finderArgs);
557    
558                                    throw processException(e);
559                            }
560                            finally {
561                                    closeSession(session);
562                            }
563                    }
564    
565                    return count.intValue();
566            }
567    
568            private static final String _FINDER_COLUMN_LTSIZE_SIZE_2 = "image.size < ?";
569    
570            public ImagePersistenceImpl() {
571                    setModelClass(Image.class);
572            }
573    
574            /**
575             * Caches the image in the entity cache if it is enabled.
576             *
577             * @param image the image
578             */
579            @Override
580            public void cacheResult(Image image) {
581                    entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
582                            ImageImpl.class, image.getPrimaryKey(), image);
583    
584                    image.resetOriginalValues();
585            }
586    
587            /**
588             * Caches the images in the entity cache if it is enabled.
589             *
590             * @param images the images
591             */
592            @Override
593            public void cacheResult(List<Image> images) {
594                    for (Image image : images) {
595                            if (entityCache.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
596                                                    ImageImpl.class, image.getPrimaryKey()) == null) {
597                                    cacheResult(image);
598                            }
599                            else {
600                                    image.resetOriginalValues();
601                            }
602                    }
603            }
604    
605            /**
606             * Clears the cache for all images.
607             *
608             * <p>
609             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
610             * </p>
611             */
612            @Override
613            public void clearCache() {
614                    entityCache.clearCache(ImageImpl.class);
615    
616                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
617                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
618                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
619            }
620    
621            /**
622             * Clears the cache for the image.
623             *
624             * <p>
625             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
626             * </p>
627             */
628            @Override
629            public void clearCache(Image image) {
630                    entityCache.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
631                            ImageImpl.class, image.getPrimaryKey());
632    
633                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
634                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
635            }
636    
637            @Override
638            public void clearCache(List<Image> images) {
639                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
640                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
641    
642                    for (Image image : images) {
643                            entityCache.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
644                                    ImageImpl.class, image.getPrimaryKey());
645                    }
646            }
647    
648            /**
649             * Creates a new image with the primary key. Does not add the image to the database.
650             *
651             * @param imageId the primary key for the new image
652             * @return the new image
653             */
654            @Override
655            public Image create(long imageId) {
656                    Image image = new ImageImpl();
657    
658                    image.setNew(true);
659                    image.setPrimaryKey(imageId);
660    
661                    image.setCompanyId(companyProvider.getCompanyId());
662    
663                    return image;
664            }
665    
666            /**
667             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
668             *
669             * @param imageId the primary key of the image
670             * @return the image that was removed
671             * @throws NoSuchImageException if a image with the primary key could not be found
672             */
673            @Override
674            public Image remove(long imageId) throws NoSuchImageException {
675                    return remove((Serializable)imageId);
676            }
677    
678            /**
679             * Removes the image with the primary key from the database. Also notifies the appropriate model listeners.
680             *
681             * @param primaryKey the primary key of the image
682             * @return the image that was removed
683             * @throws NoSuchImageException if a image with the primary key could not be found
684             */
685            @Override
686            public Image remove(Serializable primaryKey) throws NoSuchImageException {
687                    Session session = null;
688    
689                    try {
690                            session = openSession();
691    
692                            Image image = (Image)session.get(ImageImpl.class, primaryKey);
693    
694                            if (image == null) {
695                                    if (_log.isWarnEnabled()) {
696                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
697                                    }
698    
699                                    throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
700                                            primaryKey);
701                            }
702    
703                            return remove(image);
704                    }
705                    catch (NoSuchImageException nsee) {
706                            throw nsee;
707                    }
708                    catch (Exception e) {
709                            throw processException(e);
710                    }
711                    finally {
712                            closeSession(session);
713                    }
714            }
715    
716            @Override
717            protected Image removeImpl(Image image) {
718                    image = toUnwrappedModel(image);
719    
720                    Session session = null;
721    
722                    try {
723                            session = openSession();
724    
725                            if (!session.contains(image)) {
726                                    image = (Image)session.get(ImageImpl.class,
727                                                    image.getPrimaryKeyObj());
728                            }
729    
730                            if (image != null) {
731                                    session.delete(image);
732                            }
733                    }
734                    catch (Exception e) {
735                            throw processException(e);
736                    }
737                    finally {
738                            closeSession(session);
739                    }
740    
741                    if (image != null) {
742                            clearCache(image);
743                    }
744    
745                    return image;
746            }
747    
748            @Override
749            public Image updateImpl(Image image) {
750                    image = toUnwrappedModel(image);
751    
752                    boolean isNew = image.isNew();
753    
754                    Session session = null;
755    
756                    try {
757                            session = openSession();
758    
759                            if (image.isNew()) {
760                                    session.save(image);
761    
762                                    image.setNew(false);
763                            }
764                            else {
765                                    image = (Image)session.merge(image);
766                            }
767                    }
768                    catch (Exception e) {
769                            throw processException(e);
770                    }
771                    finally {
772                            closeSession(session);
773                    }
774    
775                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
776    
777                    if (isNew || !ImageModelImpl.COLUMN_BITMASK_ENABLED) {
778                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
779                    }
780    
781                    entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
782                            ImageImpl.class, image.getPrimaryKey(), image, false);
783    
784                    image.resetOriginalValues();
785    
786                    return image;
787            }
788    
789            protected Image toUnwrappedModel(Image image) {
790                    if (image instanceof ImageImpl) {
791                            return image;
792                    }
793    
794                    ImageImpl imageImpl = new ImageImpl();
795    
796                    imageImpl.setNew(image.isNew());
797                    imageImpl.setPrimaryKey(image.getPrimaryKey());
798    
799                    imageImpl.setMvccVersion(image.getMvccVersion());
800                    imageImpl.setImageId(image.getImageId());
801                    imageImpl.setCompanyId(image.getCompanyId());
802                    imageImpl.setModifiedDate(image.getModifiedDate());
803                    imageImpl.setType(image.getType());
804                    imageImpl.setHeight(image.getHeight());
805                    imageImpl.setWidth(image.getWidth());
806                    imageImpl.setSize(image.getSize());
807    
808                    return imageImpl;
809            }
810    
811            /**
812             * Returns the image with the primary key or throws a {@link com.liferay.portal.exception.NoSuchModelException} if it could not be found.
813             *
814             * @param primaryKey the primary key of the image
815             * @return the image
816             * @throws NoSuchImageException if a image with the primary key could not be found
817             */
818            @Override
819            public Image findByPrimaryKey(Serializable primaryKey)
820                    throws NoSuchImageException {
821                    Image image = fetchByPrimaryKey(primaryKey);
822    
823                    if (image == null) {
824                            if (_log.isWarnEnabled()) {
825                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
826                            }
827    
828                            throw new NoSuchImageException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
829                                    primaryKey);
830                    }
831    
832                    return image;
833            }
834    
835            /**
836             * Returns the image with the primary key or throws a {@link NoSuchImageException} if it could not be found.
837             *
838             * @param imageId the primary key of the image
839             * @return the image
840             * @throws NoSuchImageException if a image with the primary key could not be found
841             */
842            @Override
843            public Image findByPrimaryKey(long imageId) throws NoSuchImageException {
844                    return findByPrimaryKey((Serializable)imageId);
845            }
846    
847            /**
848             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
849             *
850             * @param primaryKey the primary key of the image
851             * @return the image, or <code>null</code> if a image with the primary key could not be found
852             */
853            @Override
854            public Image fetchByPrimaryKey(Serializable primaryKey) {
855                    Image image = (Image)entityCache.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
856                                    ImageImpl.class, primaryKey);
857    
858                    if (image == _nullImage) {
859                            return null;
860                    }
861    
862                    if (image == null) {
863                            Session session = null;
864    
865                            try {
866                                    session = openSession();
867    
868                                    image = (Image)session.get(ImageImpl.class, primaryKey);
869    
870                                    if (image != null) {
871                                            cacheResult(image);
872                                    }
873                                    else {
874                                            entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
875                                                    ImageImpl.class, primaryKey, _nullImage);
876                                    }
877                            }
878                            catch (Exception e) {
879                                    entityCache.removeResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
880                                            ImageImpl.class, primaryKey);
881    
882                                    throw processException(e);
883                            }
884                            finally {
885                                    closeSession(session);
886                            }
887                    }
888    
889                    return image;
890            }
891    
892            /**
893             * Returns the image with the primary key or returns <code>null</code> if it could not be found.
894             *
895             * @param imageId the primary key of the image
896             * @return the image, or <code>null</code> if a image with the primary key could not be found
897             */
898            @Override
899            public Image fetchByPrimaryKey(long imageId) {
900                    return fetchByPrimaryKey((Serializable)imageId);
901            }
902    
903            @Override
904            public Map<Serializable, Image> fetchByPrimaryKeys(
905                    Set<Serializable> primaryKeys) {
906                    if (primaryKeys.isEmpty()) {
907                            return Collections.emptyMap();
908                    }
909    
910                    Map<Serializable, Image> map = new HashMap<Serializable, Image>();
911    
912                    if (primaryKeys.size() == 1) {
913                            Iterator<Serializable> iterator = primaryKeys.iterator();
914    
915                            Serializable primaryKey = iterator.next();
916    
917                            Image image = fetchByPrimaryKey(primaryKey);
918    
919                            if (image != null) {
920                                    map.put(primaryKey, image);
921                            }
922    
923                            return map;
924                    }
925    
926                    Set<Serializable> uncachedPrimaryKeys = null;
927    
928                    for (Serializable primaryKey : primaryKeys) {
929                            Image image = (Image)entityCache.getResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
930                                            ImageImpl.class, primaryKey);
931    
932                            if (image == null) {
933                                    if (uncachedPrimaryKeys == null) {
934                                            uncachedPrimaryKeys = new HashSet<Serializable>();
935                                    }
936    
937                                    uncachedPrimaryKeys.add(primaryKey);
938                            }
939                            else {
940                                    map.put(primaryKey, image);
941                            }
942                    }
943    
944                    if (uncachedPrimaryKeys == null) {
945                            return map;
946                    }
947    
948                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
949                                    1);
950    
951                    query.append(_SQL_SELECT_IMAGE_WHERE_PKS_IN);
952    
953                    for (Serializable primaryKey : uncachedPrimaryKeys) {
954                            query.append(String.valueOf(primaryKey));
955    
956                            query.append(StringPool.COMMA);
957                    }
958    
959                    query.setIndex(query.index() - 1);
960    
961                    query.append(StringPool.CLOSE_PARENTHESIS);
962    
963                    String sql = query.toString();
964    
965                    Session session = null;
966    
967                    try {
968                            session = openSession();
969    
970                            Query q = session.createQuery(sql);
971    
972                            for (Image image : (List<Image>)q.list()) {
973                                    map.put(image.getPrimaryKeyObj(), image);
974    
975                                    cacheResult(image);
976    
977                                    uncachedPrimaryKeys.remove(image.getPrimaryKeyObj());
978                            }
979    
980                            for (Serializable primaryKey : uncachedPrimaryKeys) {
981                                    entityCache.putResult(ImageModelImpl.ENTITY_CACHE_ENABLED,
982                                            ImageImpl.class, primaryKey, _nullImage);
983                            }
984                    }
985                    catch (Exception e) {
986                            throw processException(e);
987                    }
988                    finally {
989                            closeSession(session);
990                    }
991    
992                    return map;
993            }
994    
995            /**
996             * Returns all the images.
997             *
998             * @return the images
999             */
1000            @Override
1001            public List<Image> findAll() {
1002                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1003            }
1004    
1005            /**
1006             * Returns a range of all the images.
1007             *
1008             * <p>
1009             * 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.
1010             * </p>
1011             *
1012             * @param start the lower bound of the range of images
1013             * @param end the upper bound of the range of images (not inclusive)
1014             * @return the range of images
1015             */
1016            @Override
1017            public List<Image> findAll(int start, int end) {
1018                    return findAll(start, end, null);
1019            }
1020    
1021            /**
1022             * Returns an ordered range of all the images.
1023             *
1024             * <p>
1025             * 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.
1026             * </p>
1027             *
1028             * @param start the lower bound of the range of images
1029             * @param end the upper bound of the range of images (not inclusive)
1030             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1031             * @return the ordered range of images
1032             */
1033            @Override
1034            public List<Image> findAll(int start, int end,
1035                    OrderByComparator<Image> orderByComparator) {
1036                    return findAll(start, end, orderByComparator, true);
1037            }
1038    
1039            /**
1040             * Returns an ordered range of all the images.
1041             *
1042             * <p>
1043             * 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.
1044             * </p>
1045             *
1046             * @param start the lower bound of the range of images
1047             * @param end the upper bound of the range of images (not inclusive)
1048             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1049             * @param retrieveFromCache whether to retrieve from the finder cache
1050             * @return the ordered range of images
1051             */
1052            @Override
1053            public List<Image> findAll(int start, int end,
1054                    OrderByComparator<Image> orderByComparator, boolean retrieveFromCache) {
1055                    boolean pagination = true;
1056                    FinderPath finderPath = null;
1057                    Object[] finderArgs = null;
1058    
1059                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1060                                    (orderByComparator == null)) {
1061                            pagination = false;
1062                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1063                            finderArgs = FINDER_ARGS_EMPTY;
1064                    }
1065                    else {
1066                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1067                            finderArgs = new Object[] { start, end, orderByComparator };
1068                    }
1069    
1070                    List<Image> list = null;
1071    
1072                    if (retrieveFromCache) {
1073                            list = (List<Image>)finderCache.getResult(finderPath, finderArgs,
1074                                            this);
1075                    }
1076    
1077                    if (list == null) {
1078                            StringBundler query = null;
1079                            String sql = null;
1080    
1081                            if (orderByComparator != null) {
1082                                    query = new StringBundler(2 +
1083                                                    (orderByComparator.getOrderByFields().length * 3));
1084    
1085                                    query.append(_SQL_SELECT_IMAGE);
1086    
1087                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1088                                            orderByComparator);
1089    
1090                                    sql = query.toString();
1091                            }
1092                            else {
1093                                    sql = _SQL_SELECT_IMAGE;
1094    
1095                                    if (pagination) {
1096                                            sql = sql.concat(ImageModelImpl.ORDER_BY_JPQL);
1097                                    }
1098                            }
1099    
1100                            Session session = null;
1101    
1102                            try {
1103                                    session = openSession();
1104    
1105                                    Query q = session.createQuery(sql);
1106    
1107                                    if (!pagination) {
1108                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
1109                                                            end, false);
1110    
1111                                            Collections.sort(list);
1112    
1113                                            list = Collections.unmodifiableList(list);
1114                                    }
1115                                    else {
1116                                            list = (List<Image>)QueryUtil.list(q, getDialect(), start,
1117                                                            end);
1118                                    }
1119    
1120                                    cacheResult(list);
1121    
1122                                    finderCache.putResult(finderPath, finderArgs, list);
1123                            }
1124                            catch (Exception e) {
1125                                    finderCache.removeResult(finderPath, finderArgs);
1126    
1127                                    throw processException(e);
1128                            }
1129                            finally {
1130                                    closeSession(session);
1131                            }
1132                    }
1133    
1134                    return list;
1135            }
1136    
1137            /**
1138             * Removes all the images from the database.
1139             *
1140             */
1141            @Override
1142            public void removeAll() {
1143                    for (Image image : findAll()) {
1144                            remove(image);
1145                    }
1146            }
1147    
1148            /**
1149             * Returns the number of images.
1150             *
1151             * @return the number of images
1152             */
1153            @Override
1154            public int countAll() {
1155                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
1156                                    FINDER_ARGS_EMPTY, this);
1157    
1158                    if (count == null) {
1159                            Session session = null;
1160    
1161                            try {
1162                                    session = openSession();
1163    
1164                                    Query q = session.createQuery(_SQL_COUNT_IMAGE);
1165    
1166                                    count = (Long)q.uniqueResult();
1167    
1168                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
1169                                            count);
1170                            }
1171                            catch (Exception e) {
1172                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
1173                                            FINDER_ARGS_EMPTY);
1174    
1175                                    throw processException(e);
1176                            }
1177                            finally {
1178                                    closeSession(session);
1179                            }
1180                    }
1181    
1182                    return count.intValue();
1183            }
1184    
1185            @Override
1186            public Set<String> getBadColumnNames() {
1187                    return _badColumnNames;
1188            }
1189    
1190            @Override
1191            protected Map<String, Integer> getTableColumnsMap() {
1192                    return ImageModelImpl.TABLE_COLUMNS_MAP;
1193            }
1194    
1195            /**
1196             * Initializes the image persistence.
1197             */
1198            public void afterPropertiesSet() {
1199            }
1200    
1201            public void destroy() {
1202                    entityCache.removeCache(ImageImpl.class.getName());
1203                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1204                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1205                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1206            }
1207    
1208            @BeanReference(type = CompanyProviderWrapper.class)
1209            protected CompanyProvider companyProvider;
1210            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1211            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1212            private static final String _SQL_SELECT_IMAGE = "SELECT image FROM Image image";
1213            private static final String _SQL_SELECT_IMAGE_WHERE_PKS_IN = "SELECT image FROM Image image WHERE imageId IN (";
1214            private static final String _SQL_SELECT_IMAGE_WHERE = "SELECT image FROM Image image WHERE ";
1215            private static final String _SQL_COUNT_IMAGE = "SELECT COUNT(image) FROM Image image";
1216            private static final String _SQL_COUNT_IMAGE_WHERE = "SELECT COUNT(image) FROM Image image WHERE ";
1217            private static final String _ORDER_BY_ENTITY_ALIAS = "image.";
1218            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Image exists with the primary key ";
1219            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Image exists with the key {";
1220            private static final Log _log = LogFactoryUtil.getLog(ImagePersistenceImpl.class);
1221            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1222                                    "type", "size"
1223                            });
1224            private static final Image _nullImage = new ImageImpl() {
1225                            @Override
1226                            public Object clone() {
1227                                    return this;
1228                            }
1229    
1230                            @Override
1231                            public CacheModel<Image> toCacheModel() {
1232                                    return _nullImageCacheModel;
1233                            }
1234                    };
1235    
1236            private static final CacheModel<Image> _nullImageCacheModel = new NullCacheModel();
1237    
1238            private static class NullCacheModel implements CacheModel<Image>, MVCCModel {
1239                    @Override
1240                    public long getMvccVersion() {
1241                            return -1;
1242                    }
1243    
1244                    @Override
1245                    public void setMvccVersion(long mvccVersion) {
1246                    }
1247    
1248                    @Override
1249                    public Image toEntityModel() {
1250                            return _nullImage;
1251                    }
1252            }
1253    }