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