001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.trash.service.persistence;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
018    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
019    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderPath;
021    import com.liferay.portal.kernel.dao.orm.Query;
022    import com.liferay.portal.kernel.dao.orm.QueryPos;
023    import com.liferay.portal.kernel.dao.orm.QueryUtil;
024    import com.liferay.portal.kernel.dao.orm.Session;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.InstanceFactory;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.UnmodifiableList;
035    import com.liferay.portal.model.CacheModel;
036    import com.liferay.portal.model.ModelListener;
037    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
038    
039    import com.liferay.portlet.trash.NoSuchVersionException;
040    import com.liferay.portlet.trash.model.TrashVersion;
041    import com.liferay.portlet.trash.model.impl.TrashVersionImpl;
042    import com.liferay.portlet.trash.model.impl.TrashVersionModelImpl;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * The persistence implementation for the trash version service.
052     *
053     * <p>
054     * Caching information and settings can be found in <code>portal.properties</code>
055     * </p>
056     *
057     * @author Brian Wing Shun Chan
058     * @see TrashVersionPersistence
059     * @see TrashVersionUtil
060     * @generated
061     */
062    public class TrashVersionPersistenceImpl extends BasePersistenceImpl<TrashVersion>
063            implements TrashVersionPersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * Never modify or reference this class directly. Always use {@link TrashVersionUtil} to access the trash version persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = TrashVersionImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
075                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
076                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
078                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
081                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, Long.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ENTRYID = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
084                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByEntryId",
086                            new String[] {
087                                    Long.class.getName(),
088                                    
089                            Integer.class.getName(), Integer.class.getName(),
090                                    OrderByComparator.class.getName()
091                            });
092            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID =
093                    new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
094                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
095                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByEntryId",
096                            new String[] { Long.class.getName() },
097                            TrashVersionModelImpl.ENTRYID_COLUMN_BITMASK);
098            public static final FinderPath FINDER_PATH_COUNT_BY_ENTRYID = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
099                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, Long.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByEntryId",
101                            new String[] { Long.class.getName() });
102    
103            /**
104             * Returns all the trash versions where entryId = &#63;.
105             *
106             * @param entryId the entry ID
107             * @return the matching trash versions
108             * @throws SystemException if a system exception occurred
109             */
110            public List<TrashVersion> findByEntryId(long entryId)
111                    throws SystemException {
112                    return findByEntryId(entryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
113            }
114    
115            /**
116             * Returns a range of all the trash versions where entryId = &#63;.
117             *
118             * <p>
119             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
120             * </p>
121             *
122             * @param entryId the entry ID
123             * @param start the lower bound of the range of trash versions
124             * @param end the upper bound of the range of trash versions (not inclusive)
125             * @return the range of matching trash versions
126             * @throws SystemException if a system exception occurred
127             */
128            public List<TrashVersion> findByEntryId(long entryId, int start, int end)
129                    throws SystemException {
130                    return findByEntryId(entryId, start, end, null);
131            }
132    
133            /**
134             * Returns an ordered range of all the trash versions where entryId = &#63;.
135             *
136             * <p>
137             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. 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.
138             * </p>
139             *
140             * @param entryId the entry ID
141             * @param start the lower bound of the range of trash versions
142             * @param end the upper bound of the range of trash versions (not inclusive)
143             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
144             * @return the ordered range of matching trash versions
145             * @throws SystemException if a system exception occurred
146             */
147            public List<TrashVersion> findByEntryId(long entryId, int start, int end,
148                    OrderByComparator orderByComparator) throws SystemException {
149                    boolean pagination = true;
150                    FinderPath finderPath = null;
151                    Object[] finderArgs = null;
152    
153                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
154                                    (orderByComparator == null)) {
155                            pagination = false;
156                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID;
157                            finderArgs = new Object[] { entryId };
158                    }
159                    else {
160                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ENTRYID;
161                            finderArgs = new Object[] { entryId, start, end, orderByComparator };
162                    }
163    
164                    List<TrashVersion> list = (List<TrashVersion>)FinderCacheUtil.getResult(finderPath,
165                                    finderArgs, this);
166    
167                    if ((list != null) && !list.isEmpty()) {
168                            for (TrashVersion trashVersion : list) {
169                                    if ((entryId != trashVersion.getEntryId())) {
170                                            list = null;
171    
172                                            break;
173                                    }
174                            }
175                    }
176    
177                    if (list == null) {
178                            StringBundler query = null;
179    
180                            if (orderByComparator != null) {
181                                    query = new StringBundler(3 +
182                                                    (orderByComparator.getOrderByFields().length * 3));
183                            }
184                            else {
185                                    query = new StringBundler(3);
186                            }
187    
188                            query.append(_SQL_SELECT_TRASHVERSION_WHERE);
189    
190                            query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
191    
192                            if (orderByComparator != null) {
193                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
194                                            orderByComparator);
195                            }
196                            else
197                             if (pagination) {
198                                    query.append(TrashVersionModelImpl.ORDER_BY_JPQL);
199                            }
200    
201                            String sql = query.toString();
202    
203                            Session session = null;
204    
205                            try {
206                                    session = openSession();
207    
208                                    Query q = session.createQuery(sql);
209    
210                                    QueryPos qPos = QueryPos.getInstance(q);
211    
212                                    qPos.add(entryId);
213    
214                                    if (!pagination) {
215                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
216                                                            start, end, false);
217    
218                                            Collections.sort(list);
219    
220                                            list = new UnmodifiableList<TrashVersion>(list);
221                                    }
222                                    else {
223                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
224                                                            start, end);
225                                    }
226    
227                                    cacheResult(list);
228    
229                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
230                            }
231                            catch (Exception e) {
232                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
233    
234                                    throw processException(e);
235                            }
236                            finally {
237                                    closeSession(session);
238                            }
239                    }
240    
241                    return list;
242            }
243    
244            /**
245             * Returns the first trash version in the ordered set where entryId = &#63;.
246             *
247             * @param entryId the entry ID
248             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
249             * @return the first matching trash version
250             * @throws com.liferay.portlet.trash.NoSuchVersionException if a matching trash version could not be found
251             * @throws SystemException if a system exception occurred
252             */
253            public TrashVersion findByEntryId_First(long entryId,
254                    OrderByComparator orderByComparator)
255                    throws NoSuchVersionException, SystemException {
256                    TrashVersion trashVersion = fetchByEntryId_First(entryId,
257                                    orderByComparator);
258    
259                    if (trashVersion != null) {
260                            return trashVersion;
261                    }
262    
263                    StringBundler msg = new StringBundler(4);
264    
265                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
266    
267                    msg.append("entryId=");
268                    msg.append(entryId);
269    
270                    msg.append(StringPool.CLOSE_CURLY_BRACE);
271    
272                    throw new NoSuchVersionException(msg.toString());
273            }
274    
275            /**
276             * Returns the first trash version in the ordered set where entryId = &#63;.
277             *
278             * @param entryId the entry ID
279             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
280             * @return the first matching trash version, or <code>null</code> if a matching trash version could not be found
281             * @throws SystemException if a system exception occurred
282             */
283            public TrashVersion fetchByEntryId_First(long entryId,
284                    OrderByComparator orderByComparator) throws SystemException {
285                    List<TrashVersion> list = findByEntryId(entryId, 0, 1, orderByComparator);
286    
287                    if (!list.isEmpty()) {
288                            return list.get(0);
289                    }
290    
291                    return null;
292            }
293    
294            /**
295             * Returns the last trash version in the ordered set where entryId = &#63;.
296             *
297             * @param entryId the entry ID
298             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
299             * @return the last matching trash version
300             * @throws com.liferay.portlet.trash.NoSuchVersionException if a matching trash version could not be found
301             * @throws SystemException if a system exception occurred
302             */
303            public TrashVersion findByEntryId_Last(long entryId,
304                    OrderByComparator orderByComparator)
305                    throws NoSuchVersionException, SystemException {
306                    TrashVersion trashVersion = fetchByEntryId_Last(entryId,
307                                    orderByComparator);
308    
309                    if (trashVersion != null) {
310                            return trashVersion;
311                    }
312    
313                    StringBundler msg = new StringBundler(4);
314    
315                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
316    
317                    msg.append("entryId=");
318                    msg.append(entryId);
319    
320                    msg.append(StringPool.CLOSE_CURLY_BRACE);
321    
322                    throw new NoSuchVersionException(msg.toString());
323            }
324    
325            /**
326             * Returns the last trash version in the ordered set where entryId = &#63;.
327             *
328             * @param entryId the entry ID
329             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
330             * @return the last matching trash version, or <code>null</code> if a matching trash version could not be found
331             * @throws SystemException if a system exception occurred
332             */
333            public TrashVersion fetchByEntryId_Last(long entryId,
334                    OrderByComparator orderByComparator) throws SystemException {
335                    int count = countByEntryId(entryId);
336    
337                    List<TrashVersion> list = findByEntryId(entryId, count - 1, count,
338                                    orderByComparator);
339    
340                    if (!list.isEmpty()) {
341                            return list.get(0);
342                    }
343    
344                    return null;
345            }
346    
347            /**
348             * Returns the trash versions before and after the current trash version in the ordered set where entryId = &#63;.
349             *
350             * @param versionId the primary key of the current trash version
351             * @param entryId the entry ID
352             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
353             * @return the previous, current, and next trash version
354             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
355             * @throws SystemException if a system exception occurred
356             */
357            public TrashVersion[] findByEntryId_PrevAndNext(long versionId,
358                    long entryId, OrderByComparator orderByComparator)
359                    throws NoSuchVersionException, SystemException {
360                    TrashVersion trashVersion = findByPrimaryKey(versionId);
361    
362                    Session session = null;
363    
364                    try {
365                            session = openSession();
366    
367                            TrashVersion[] array = new TrashVersionImpl[3];
368    
369                            array[0] = getByEntryId_PrevAndNext(session, trashVersion, entryId,
370                                            orderByComparator, true);
371    
372                            array[1] = trashVersion;
373    
374                            array[2] = getByEntryId_PrevAndNext(session, trashVersion, entryId,
375                                            orderByComparator, false);
376    
377                            return array;
378                    }
379                    catch (Exception e) {
380                            throw processException(e);
381                    }
382                    finally {
383                            closeSession(session);
384                    }
385            }
386    
387            protected TrashVersion getByEntryId_PrevAndNext(Session session,
388                    TrashVersion trashVersion, long entryId,
389                    OrderByComparator orderByComparator, boolean previous) {
390                    StringBundler query = null;
391    
392                    if (orderByComparator != null) {
393                            query = new StringBundler(6 +
394                                            (orderByComparator.getOrderByFields().length * 6));
395                    }
396                    else {
397                            query = new StringBundler(3);
398                    }
399    
400                    query.append(_SQL_SELECT_TRASHVERSION_WHERE);
401    
402                    query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
403    
404                    if (orderByComparator != null) {
405                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
406    
407                            if (orderByConditionFields.length > 0) {
408                                    query.append(WHERE_AND);
409                            }
410    
411                            for (int i = 0; i < orderByConditionFields.length; i++) {
412                                    query.append(_ORDER_BY_ENTITY_ALIAS);
413                                    query.append(orderByConditionFields[i]);
414    
415                                    if ((i + 1) < orderByConditionFields.length) {
416                                            if (orderByComparator.isAscending() ^ previous) {
417                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
418                                            }
419                                            else {
420                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
421                                            }
422                                    }
423                                    else {
424                                            if (orderByComparator.isAscending() ^ previous) {
425                                                    query.append(WHERE_GREATER_THAN);
426                                            }
427                                            else {
428                                                    query.append(WHERE_LESSER_THAN);
429                                            }
430                                    }
431                            }
432    
433                            query.append(ORDER_BY_CLAUSE);
434    
435                            String[] orderByFields = orderByComparator.getOrderByFields();
436    
437                            for (int i = 0; i < orderByFields.length; i++) {
438                                    query.append(_ORDER_BY_ENTITY_ALIAS);
439                                    query.append(orderByFields[i]);
440    
441                                    if ((i + 1) < orderByFields.length) {
442                                            if (orderByComparator.isAscending() ^ previous) {
443                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
444                                            }
445                                            else {
446                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
447                                            }
448                                    }
449                                    else {
450                                            if (orderByComparator.isAscending() ^ previous) {
451                                                    query.append(ORDER_BY_ASC);
452                                            }
453                                            else {
454                                                    query.append(ORDER_BY_DESC);
455                                            }
456                                    }
457                            }
458                    }
459                    else {
460                            query.append(TrashVersionModelImpl.ORDER_BY_JPQL);
461                    }
462    
463                    String sql = query.toString();
464    
465                    Query q = session.createQuery(sql);
466    
467                    q.setFirstResult(0);
468                    q.setMaxResults(2);
469    
470                    QueryPos qPos = QueryPos.getInstance(q);
471    
472                    qPos.add(entryId);
473    
474                    if (orderByComparator != null) {
475                            Object[] values = orderByComparator.getOrderByConditionValues(trashVersion);
476    
477                            for (Object value : values) {
478                                    qPos.add(value);
479                            }
480                    }
481    
482                    List<TrashVersion> list = q.list();
483    
484                    if (list.size() == 2) {
485                            return list.get(1);
486                    }
487                    else {
488                            return null;
489                    }
490            }
491    
492            /**
493             * Removes all the trash versions where entryId = &#63; from the database.
494             *
495             * @param entryId the entry ID
496             * @throws SystemException if a system exception occurred
497             */
498            public void removeByEntryId(long entryId) throws SystemException {
499                    for (TrashVersion trashVersion : findByEntryId(entryId,
500                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
501                            remove(trashVersion);
502                    }
503            }
504    
505            /**
506             * Returns the number of trash versions where entryId = &#63;.
507             *
508             * @param entryId the entry ID
509             * @return the number of matching trash versions
510             * @throws SystemException if a system exception occurred
511             */
512            public int countByEntryId(long entryId) throws SystemException {
513                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ENTRYID;
514    
515                    Object[] finderArgs = new Object[] { entryId };
516    
517                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
518                                    this);
519    
520                    if (count == null) {
521                            StringBundler query = new StringBundler(2);
522    
523                            query.append(_SQL_COUNT_TRASHVERSION_WHERE);
524    
525                            query.append(_FINDER_COLUMN_ENTRYID_ENTRYID_2);
526    
527                            String sql = query.toString();
528    
529                            Session session = null;
530    
531                            try {
532                                    session = openSession();
533    
534                                    Query q = session.createQuery(sql);
535    
536                                    QueryPos qPos = QueryPos.getInstance(q);
537    
538                                    qPos.add(entryId);
539    
540                                    count = (Long)q.uniqueResult();
541    
542                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
543                            }
544                            catch (Exception e) {
545                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
546    
547                                    throw processException(e);
548                            }
549                            finally {
550                                    closeSession(session);
551                            }
552                    }
553    
554                    return count.intValue();
555            }
556    
557            private static final String _FINDER_COLUMN_ENTRYID_ENTRYID_2 = "trashVersion.entryId = ?";
558            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
559                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
560                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C",
561                            new String[] {
562                                    Long.class.getName(), Long.class.getName(),
563                                    
564                            Integer.class.getName(), Integer.class.getName(),
565                                    OrderByComparator.class.getName()
566                            });
567            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
568                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, TrashVersionImpl.class,
569                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C",
570                            new String[] { Long.class.getName(), Long.class.getName() },
571                            TrashVersionModelImpl.CLASSNAMEID_COLUMN_BITMASK |
572                            TrashVersionModelImpl.CLASSPK_COLUMN_BITMASK);
573            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
574                            TrashVersionModelImpl.FINDER_CACHE_ENABLED, Long.class,
575                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
576                            new String[] { Long.class.getName(), Long.class.getName() });
577    
578            /**
579             * Returns all the trash versions where classNameId = &#63; and classPK = &#63;.
580             *
581             * @param classNameId the class name ID
582             * @param classPK the class p k
583             * @return the matching trash versions
584             * @throws SystemException if a system exception occurred
585             */
586            public List<TrashVersion> findByC_C(long classNameId, long classPK)
587                    throws SystemException {
588                    return findByC_C(classNameId, classPK, QueryUtil.ALL_POS,
589                            QueryUtil.ALL_POS, null);
590            }
591    
592            /**
593             * Returns a range of all the trash versions where classNameId = &#63; and classPK = &#63;.
594             *
595             * <p>
596             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. 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.
597             * </p>
598             *
599             * @param classNameId the class name ID
600             * @param classPK the class p k
601             * @param start the lower bound of the range of trash versions
602             * @param end the upper bound of the range of trash versions (not inclusive)
603             * @return the range of matching trash versions
604             * @throws SystemException if a system exception occurred
605             */
606            public List<TrashVersion> findByC_C(long classNameId, long classPK,
607                    int start, int end) throws SystemException {
608                    return findByC_C(classNameId, classPK, start, end, null);
609            }
610    
611            /**
612             * Returns an ordered range of all the trash versions where classNameId = &#63; and classPK = &#63;.
613             *
614             * <p>
615             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. 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.
616             * </p>
617             *
618             * @param classNameId the class name ID
619             * @param classPK the class p k
620             * @param start the lower bound of the range of trash versions
621             * @param end the upper bound of the range of trash versions (not inclusive)
622             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
623             * @return the ordered range of matching trash versions
624             * @throws SystemException if a system exception occurred
625             */
626            public List<TrashVersion> findByC_C(long classNameId, long classPK,
627                    int start, int end, OrderByComparator orderByComparator)
628                    throws SystemException {
629                    boolean pagination = true;
630                    FinderPath finderPath = null;
631                    Object[] finderArgs = null;
632    
633                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
634                                    (orderByComparator == null)) {
635                            pagination = false;
636                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C;
637                            finderArgs = new Object[] { classNameId, classPK };
638                    }
639                    else {
640                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C;
641                            finderArgs = new Object[] {
642                                            classNameId, classPK,
643                                            
644                                            start, end, orderByComparator
645                                    };
646                    }
647    
648                    List<TrashVersion> list = (List<TrashVersion>)FinderCacheUtil.getResult(finderPath,
649                                    finderArgs, this);
650    
651                    if ((list != null) && !list.isEmpty()) {
652                            for (TrashVersion trashVersion : list) {
653                                    if ((classNameId != trashVersion.getClassNameId()) ||
654                                                    (classPK != trashVersion.getClassPK())) {
655                                            list = null;
656    
657                                            break;
658                                    }
659                            }
660                    }
661    
662                    if (list == null) {
663                            StringBundler query = null;
664    
665                            if (orderByComparator != null) {
666                                    query = new StringBundler(4 +
667                                                    (orderByComparator.getOrderByFields().length * 3));
668                            }
669                            else {
670                                    query = new StringBundler(4);
671                            }
672    
673                            query.append(_SQL_SELECT_TRASHVERSION_WHERE);
674    
675                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
676    
677                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
678    
679                            if (orderByComparator != null) {
680                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
681                                            orderByComparator);
682                            }
683                            else
684                             if (pagination) {
685                                    query.append(TrashVersionModelImpl.ORDER_BY_JPQL);
686                            }
687    
688                            String sql = query.toString();
689    
690                            Session session = null;
691    
692                            try {
693                                    session = openSession();
694    
695                                    Query q = session.createQuery(sql);
696    
697                                    QueryPos qPos = QueryPos.getInstance(q);
698    
699                                    qPos.add(classNameId);
700    
701                                    qPos.add(classPK);
702    
703                                    if (!pagination) {
704                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
705                                                            start, end, false);
706    
707                                            Collections.sort(list);
708    
709                                            list = new UnmodifiableList<TrashVersion>(list);
710                                    }
711                                    else {
712                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
713                                                            start, end);
714                                    }
715    
716                                    cacheResult(list);
717    
718                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
719                            }
720                            catch (Exception e) {
721                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
722    
723                                    throw processException(e);
724                            }
725                            finally {
726                                    closeSession(session);
727                            }
728                    }
729    
730                    return list;
731            }
732    
733            /**
734             * Returns the first trash version in the ordered set where classNameId = &#63; and classPK = &#63;.
735             *
736             * @param classNameId the class name ID
737             * @param classPK the class p k
738             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
739             * @return the first matching trash version
740             * @throws com.liferay.portlet.trash.NoSuchVersionException if a matching trash version could not be found
741             * @throws SystemException if a system exception occurred
742             */
743            public TrashVersion findByC_C_First(long classNameId, long classPK,
744                    OrderByComparator orderByComparator)
745                    throws NoSuchVersionException, SystemException {
746                    TrashVersion trashVersion = fetchByC_C_First(classNameId, classPK,
747                                    orderByComparator);
748    
749                    if (trashVersion != null) {
750                            return trashVersion;
751                    }
752    
753                    StringBundler msg = new StringBundler(6);
754    
755                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
756    
757                    msg.append("classNameId=");
758                    msg.append(classNameId);
759    
760                    msg.append(", classPK=");
761                    msg.append(classPK);
762    
763                    msg.append(StringPool.CLOSE_CURLY_BRACE);
764    
765                    throw new NoSuchVersionException(msg.toString());
766            }
767    
768            /**
769             * Returns the first trash version in the ordered set where classNameId = &#63; and classPK = &#63;.
770             *
771             * @param classNameId the class name ID
772             * @param classPK the class p k
773             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
774             * @return the first matching trash version, or <code>null</code> if a matching trash version could not be found
775             * @throws SystemException if a system exception occurred
776             */
777            public TrashVersion fetchByC_C_First(long classNameId, long classPK,
778                    OrderByComparator orderByComparator) throws SystemException {
779                    List<TrashVersion> list = findByC_C(classNameId, classPK, 0, 1,
780                                    orderByComparator);
781    
782                    if (!list.isEmpty()) {
783                            return list.get(0);
784                    }
785    
786                    return null;
787            }
788    
789            /**
790             * Returns the last trash version in the ordered set where classNameId = &#63; and classPK = &#63;.
791             *
792             * @param classNameId the class name ID
793             * @param classPK the class p k
794             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
795             * @return the last matching trash version
796             * @throws com.liferay.portlet.trash.NoSuchVersionException if a matching trash version could not be found
797             * @throws SystemException if a system exception occurred
798             */
799            public TrashVersion findByC_C_Last(long classNameId, long classPK,
800                    OrderByComparator orderByComparator)
801                    throws NoSuchVersionException, SystemException {
802                    TrashVersion trashVersion = fetchByC_C_Last(classNameId, classPK,
803                                    orderByComparator);
804    
805                    if (trashVersion != null) {
806                            return trashVersion;
807                    }
808    
809                    StringBundler msg = new StringBundler(6);
810    
811                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
812    
813                    msg.append("classNameId=");
814                    msg.append(classNameId);
815    
816                    msg.append(", classPK=");
817                    msg.append(classPK);
818    
819                    msg.append(StringPool.CLOSE_CURLY_BRACE);
820    
821                    throw new NoSuchVersionException(msg.toString());
822            }
823    
824            /**
825             * Returns the last trash version in the ordered set where classNameId = &#63; and classPK = &#63;.
826             *
827             * @param classNameId the class name ID
828             * @param classPK the class p k
829             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
830             * @return the last matching trash version, or <code>null</code> if a matching trash version could not be found
831             * @throws SystemException if a system exception occurred
832             */
833            public TrashVersion fetchByC_C_Last(long classNameId, long classPK,
834                    OrderByComparator orderByComparator) throws SystemException {
835                    int count = countByC_C(classNameId, classPK);
836    
837                    List<TrashVersion> list = findByC_C(classNameId, classPK, count - 1,
838                                    count, orderByComparator);
839    
840                    if (!list.isEmpty()) {
841                            return list.get(0);
842                    }
843    
844                    return null;
845            }
846    
847            /**
848             * Returns the trash versions before and after the current trash version in the ordered set where classNameId = &#63; and classPK = &#63;.
849             *
850             * @param versionId the primary key of the current trash version
851             * @param classNameId the class name ID
852             * @param classPK the class p k
853             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
854             * @return the previous, current, and next trash version
855             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
856             * @throws SystemException if a system exception occurred
857             */
858            public TrashVersion[] findByC_C_PrevAndNext(long versionId,
859                    long classNameId, long classPK, OrderByComparator orderByComparator)
860                    throws NoSuchVersionException, SystemException {
861                    TrashVersion trashVersion = findByPrimaryKey(versionId);
862    
863                    Session session = null;
864    
865                    try {
866                            session = openSession();
867    
868                            TrashVersion[] array = new TrashVersionImpl[3];
869    
870                            array[0] = getByC_C_PrevAndNext(session, trashVersion, classNameId,
871                                            classPK, orderByComparator, true);
872    
873                            array[1] = trashVersion;
874    
875                            array[2] = getByC_C_PrevAndNext(session, trashVersion, classNameId,
876                                            classPK, orderByComparator, false);
877    
878                            return array;
879                    }
880                    catch (Exception e) {
881                            throw processException(e);
882                    }
883                    finally {
884                            closeSession(session);
885                    }
886            }
887    
888            protected TrashVersion getByC_C_PrevAndNext(Session session,
889                    TrashVersion trashVersion, long classNameId, long classPK,
890                    OrderByComparator orderByComparator, boolean previous) {
891                    StringBundler query = null;
892    
893                    if (orderByComparator != null) {
894                            query = new StringBundler(6 +
895                                            (orderByComparator.getOrderByFields().length * 6));
896                    }
897                    else {
898                            query = new StringBundler(3);
899                    }
900    
901                    query.append(_SQL_SELECT_TRASHVERSION_WHERE);
902    
903                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
904    
905                    query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
906    
907                    if (orderByComparator != null) {
908                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
909    
910                            if (orderByConditionFields.length > 0) {
911                                    query.append(WHERE_AND);
912                            }
913    
914                            for (int i = 0; i < orderByConditionFields.length; i++) {
915                                    query.append(_ORDER_BY_ENTITY_ALIAS);
916                                    query.append(orderByConditionFields[i]);
917    
918                                    if ((i + 1) < orderByConditionFields.length) {
919                                            if (orderByComparator.isAscending() ^ previous) {
920                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
921                                            }
922                                            else {
923                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
924                                            }
925                                    }
926                                    else {
927                                            if (orderByComparator.isAscending() ^ previous) {
928                                                    query.append(WHERE_GREATER_THAN);
929                                            }
930                                            else {
931                                                    query.append(WHERE_LESSER_THAN);
932                                            }
933                                    }
934                            }
935    
936                            query.append(ORDER_BY_CLAUSE);
937    
938                            String[] orderByFields = orderByComparator.getOrderByFields();
939    
940                            for (int i = 0; i < orderByFields.length; i++) {
941                                    query.append(_ORDER_BY_ENTITY_ALIAS);
942                                    query.append(orderByFields[i]);
943    
944                                    if ((i + 1) < orderByFields.length) {
945                                            if (orderByComparator.isAscending() ^ previous) {
946                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
947                                            }
948                                            else {
949                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
950                                            }
951                                    }
952                                    else {
953                                            if (orderByComparator.isAscending() ^ previous) {
954                                                    query.append(ORDER_BY_ASC);
955                                            }
956                                            else {
957                                                    query.append(ORDER_BY_DESC);
958                                            }
959                                    }
960                            }
961                    }
962                    else {
963                            query.append(TrashVersionModelImpl.ORDER_BY_JPQL);
964                    }
965    
966                    String sql = query.toString();
967    
968                    Query q = session.createQuery(sql);
969    
970                    q.setFirstResult(0);
971                    q.setMaxResults(2);
972    
973                    QueryPos qPos = QueryPos.getInstance(q);
974    
975                    qPos.add(classNameId);
976    
977                    qPos.add(classPK);
978    
979                    if (orderByComparator != null) {
980                            Object[] values = orderByComparator.getOrderByConditionValues(trashVersion);
981    
982                            for (Object value : values) {
983                                    qPos.add(value);
984                            }
985                    }
986    
987                    List<TrashVersion> list = q.list();
988    
989                    if (list.size() == 2) {
990                            return list.get(1);
991                    }
992                    else {
993                            return null;
994                    }
995            }
996    
997            /**
998             * Removes all the trash versions where classNameId = &#63; and classPK = &#63; from the database.
999             *
1000             * @param classNameId the class name ID
1001             * @param classPK the class p k
1002             * @throws SystemException if a system exception occurred
1003             */
1004            public void removeByC_C(long classNameId, long classPK)
1005                    throws SystemException {
1006                    for (TrashVersion trashVersion : findByC_C(classNameId, classPK,
1007                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1008                            remove(trashVersion);
1009                    }
1010            }
1011    
1012            /**
1013             * Returns the number of trash versions where classNameId = &#63; and classPK = &#63;.
1014             *
1015             * @param classNameId the class name ID
1016             * @param classPK the class p k
1017             * @return the number of matching trash versions
1018             * @throws SystemException if a system exception occurred
1019             */
1020            public int countByC_C(long classNameId, long classPK)
1021                    throws SystemException {
1022                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
1023    
1024                    Object[] finderArgs = new Object[] { classNameId, classPK };
1025    
1026                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1027                                    this);
1028    
1029                    if (count == null) {
1030                            StringBundler query = new StringBundler(3);
1031    
1032                            query.append(_SQL_COUNT_TRASHVERSION_WHERE);
1033    
1034                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1035    
1036                            query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1037    
1038                            String sql = query.toString();
1039    
1040                            Session session = null;
1041    
1042                            try {
1043                                    session = openSession();
1044    
1045                                    Query q = session.createQuery(sql);
1046    
1047                                    QueryPos qPos = QueryPos.getInstance(q);
1048    
1049                                    qPos.add(classNameId);
1050    
1051                                    qPos.add(classPK);
1052    
1053                                    count = (Long)q.uniqueResult();
1054    
1055                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1056                            }
1057                            catch (Exception e) {
1058                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1059    
1060                                    throw processException(e);
1061                            }
1062                            finally {
1063                                    closeSession(session);
1064                            }
1065                    }
1066    
1067                    return count.intValue();
1068            }
1069    
1070            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "trashVersion.classNameId = ? AND ";
1071            private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "trashVersion.classPK = ?";
1072    
1073            /**
1074             * Caches the trash version in the entity cache if it is enabled.
1075             *
1076             * @param trashVersion the trash version
1077             */
1078            public void cacheResult(TrashVersion trashVersion) {
1079                    EntityCacheUtil.putResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1080                            TrashVersionImpl.class, trashVersion.getPrimaryKey(), trashVersion);
1081    
1082                    trashVersion.resetOriginalValues();
1083            }
1084    
1085            /**
1086             * Caches the trash versions in the entity cache if it is enabled.
1087             *
1088             * @param trashVersions the trash versions
1089             */
1090            public void cacheResult(List<TrashVersion> trashVersions) {
1091                    for (TrashVersion trashVersion : trashVersions) {
1092                            if (EntityCacheUtil.getResult(
1093                                                    TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1094                                                    TrashVersionImpl.class, trashVersion.getPrimaryKey()) == null) {
1095                                    cacheResult(trashVersion);
1096                            }
1097                            else {
1098                                    trashVersion.resetOriginalValues();
1099                            }
1100                    }
1101            }
1102    
1103            /**
1104             * Clears the cache for all trash versions.
1105             *
1106             * <p>
1107             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1108             * </p>
1109             */
1110            @Override
1111            public void clearCache() {
1112                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1113                            CacheRegistryUtil.clear(TrashVersionImpl.class.getName());
1114                    }
1115    
1116                    EntityCacheUtil.clearCache(TrashVersionImpl.class.getName());
1117    
1118                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1119                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1120                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1121            }
1122    
1123            /**
1124             * Clears the cache for the trash version.
1125             *
1126             * <p>
1127             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1128             * </p>
1129             */
1130            @Override
1131            public void clearCache(TrashVersion trashVersion) {
1132                    EntityCacheUtil.removeResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1133                            TrashVersionImpl.class, trashVersion.getPrimaryKey());
1134    
1135                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1136                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1137            }
1138    
1139            @Override
1140            public void clearCache(List<TrashVersion> trashVersions) {
1141                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1142                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1143    
1144                    for (TrashVersion trashVersion : trashVersions) {
1145                            EntityCacheUtil.removeResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1146                                    TrashVersionImpl.class, trashVersion.getPrimaryKey());
1147                    }
1148            }
1149    
1150            /**
1151             * Creates a new trash version with the primary key. Does not add the trash version to the database.
1152             *
1153             * @param versionId the primary key for the new trash version
1154             * @return the new trash version
1155             */
1156            public TrashVersion create(long versionId) {
1157                    TrashVersion trashVersion = new TrashVersionImpl();
1158    
1159                    trashVersion.setNew(true);
1160                    trashVersion.setPrimaryKey(versionId);
1161    
1162                    return trashVersion;
1163            }
1164    
1165            /**
1166             * Removes the trash version with the primary key from the database. Also notifies the appropriate model listeners.
1167             *
1168             * @param versionId the primary key of the trash version
1169             * @return the trash version that was removed
1170             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
1171             * @throws SystemException if a system exception occurred
1172             */
1173            public TrashVersion remove(long versionId)
1174                    throws NoSuchVersionException, SystemException {
1175                    return remove((Serializable)versionId);
1176            }
1177    
1178            /**
1179             * Removes the trash version with the primary key from the database. Also notifies the appropriate model listeners.
1180             *
1181             * @param primaryKey the primary key of the trash version
1182             * @return the trash version that was removed
1183             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
1184             * @throws SystemException if a system exception occurred
1185             */
1186            @Override
1187            public TrashVersion remove(Serializable primaryKey)
1188                    throws NoSuchVersionException, SystemException {
1189                    Session session = null;
1190    
1191                    try {
1192                            session = openSession();
1193    
1194                            TrashVersion trashVersion = (TrashVersion)session.get(TrashVersionImpl.class,
1195                                            primaryKey);
1196    
1197                            if (trashVersion == null) {
1198                                    if (_log.isWarnEnabled()) {
1199                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1200                                    }
1201    
1202                                    throw new NoSuchVersionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1203                                            primaryKey);
1204                            }
1205    
1206                            return remove(trashVersion);
1207                    }
1208                    catch (NoSuchVersionException nsee) {
1209                            throw nsee;
1210                    }
1211                    catch (Exception e) {
1212                            throw processException(e);
1213                    }
1214                    finally {
1215                            closeSession(session);
1216                    }
1217            }
1218    
1219            @Override
1220            protected TrashVersion removeImpl(TrashVersion trashVersion)
1221                    throws SystemException {
1222                    trashVersion = toUnwrappedModel(trashVersion);
1223    
1224                    Session session = null;
1225    
1226                    try {
1227                            session = openSession();
1228    
1229                            if (!session.contains(trashVersion)) {
1230                                    trashVersion = (TrashVersion)session.get(TrashVersionImpl.class,
1231                                                    trashVersion.getPrimaryKeyObj());
1232                            }
1233    
1234                            if (trashVersion != null) {
1235                                    session.delete(trashVersion);
1236                            }
1237                    }
1238                    catch (Exception e) {
1239                            throw processException(e);
1240                    }
1241                    finally {
1242                            closeSession(session);
1243                    }
1244    
1245                    if (trashVersion != null) {
1246                            clearCache(trashVersion);
1247                    }
1248    
1249                    return trashVersion;
1250            }
1251    
1252            @Override
1253            public TrashVersion updateImpl(
1254                    com.liferay.portlet.trash.model.TrashVersion trashVersion)
1255                    throws SystemException {
1256                    trashVersion = toUnwrappedModel(trashVersion);
1257    
1258                    boolean isNew = trashVersion.isNew();
1259    
1260                    TrashVersionModelImpl trashVersionModelImpl = (TrashVersionModelImpl)trashVersion;
1261    
1262                    Session session = null;
1263    
1264                    try {
1265                            session = openSession();
1266    
1267                            if (trashVersion.isNew()) {
1268                                    session.save(trashVersion);
1269    
1270                                    trashVersion.setNew(false);
1271                            }
1272                            else {
1273                                    session.merge(trashVersion);
1274                            }
1275                    }
1276                    catch (Exception e) {
1277                            throw processException(e);
1278                    }
1279                    finally {
1280                            closeSession(session);
1281                    }
1282    
1283                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1284    
1285                    if (isNew || !TrashVersionModelImpl.COLUMN_BITMASK_ENABLED) {
1286                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1287                    }
1288    
1289                    else {
1290                            if ((trashVersionModelImpl.getColumnBitmask() &
1291                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID.getColumnBitmask()) != 0) {
1292                                    Object[] args = new Object[] {
1293                                                    trashVersionModelImpl.getOriginalEntryId()
1294                                            };
1295    
1296                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
1297                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID,
1298                                            args);
1299    
1300                                    args = new Object[] { trashVersionModelImpl.getEntryId() };
1301    
1302                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ENTRYID, args);
1303                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ENTRYID,
1304                                            args);
1305                            }
1306    
1307                            if ((trashVersionModelImpl.getColumnBitmask() &
1308                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
1309                                    Object[] args = new Object[] {
1310                                                    trashVersionModelImpl.getOriginalClassNameId(),
1311                                                    trashVersionModelImpl.getOriginalClassPK()
1312                                            };
1313    
1314                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1315                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1316                                            args);
1317    
1318                                    args = new Object[] {
1319                                                    trashVersionModelImpl.getClassNameId(),
1320                                                    trashVersionModelImpl.getClassPK()
1321                                            };
1322    
1323                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1324                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1325                                            args);
1326                            }
1327                    }
1328    
1329                    EntityCacheUtil.putResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1330                            TrashVersionImpl.class, trashVersion.getPrimaryKey(), trashVersion);
1331    
1332                    return trashVersion;
1333            }
1334    
1335            protected TrashVersion toUnwrappedModel(TrashVersion trashVersion) {
1336                    if (trashVersion instanceof TrashVersionImpl) {
1337                            return trashVersion;
1338                    }
1339    
1340                    TrashVersionImpl trashVersionImpl = new TrashVersionImpl();
1341    
1342                    trashVersionImpl.setNew(trashVersion.isNew());
1343                    trashVersionImpl.setPrimaryKey(trashVersion.getPrimaryKey());
1344    
1345                    trashVersionImpl.setVersionId(trashVersion.getVersionId());
1346                    trashVersionImpl.setEntryId(trashVersion.getEntryId());
1347                    trashVersionImpl.setClassNameId(trashVersion.getClassNameId());
1348                    trashVersionImpl.setClassPK(trashVersion.getClassPK());
1349                    trashVersionImpl.setStatus(trashVersion.getStatus());
1350    
1351                    return trashVersionImpl;
1352            }
1353    
1354            /**
1355             * Returns the trash version with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1356             *
1357             * @param primaryKey the primary key of the trash version
1358             * @return the trash version
1359             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
1360             * @throws SystemException if a system exception occurred
1361             */
1362            @Override
1363            public TrashVersion findByPrimaryKey(Serializable primaryKey)
1364                    throws NoSuchVersionException, SystemException {
1365                    TrashVersion trashVersion = fetchByPrimaryKey(primaryKey);
1366    
1367                    if (trashVersion == null) {
1368                            if (_log.isWarnEnabled()) {
1369                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1370                            }
1371    
1372                            throw new NoSuchVersionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1373                                    primaryKey);
1374                    }
1375    
1376                    return trashVersion;
1377            }
1378    
1379            /**
1380             * Returns the trash version with the primary key or throws a {@link com.liferay.portlet.trash.NoSuchVersionException} if it could not be found.
1381             *
1382             * @param versionId the primary key of the trash version
1383             * @return the trash version
1384             * @throws com.liferay.portlet.trash.NoSuchVersionException if a trash version with the primary key could not be found
1385             * @throws SystemException if a system exception occurred
1386             */
1387            public TrashVersion findByPrimaryKey(long versionId)
1388                    throws NoSuchVersionException, SystemException {
1389                    return findByPrimaryKey((Serializable)versionId);
1390            }
1391    
1392            /**
1393             * Returns the trash version with the primary key or returns <code>null</code> if it could not be found.
1394             *
1395             * @param primaryKey the primary key of the trash version
1396             * @return the trash version, or <code>null</code> if a trash version with the primary key could not be found
1397             * @throws SystemException if a system exception occurred
1398             */
1399            @Override
1400            public TrashVersion fetchByPrimaryKey(Serializable primaryKey)
1401                    throws SystemException {
1402                    TrashVersion trashVersion = (TrashVersion)EntityCacheUtil.getResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1403                                    TrashVersionImpl.class, primaryKey);
1404    
1405                    if (trashVersion == _nullTrashVersion) {
1406                            return null;
1407                    }
1408    
1409                    if (trashVersion == null) {
1410                            Session session = null;
1411    
1412                            try {
1413                                    session = openSession();
1414    
1415                                    trashVersion = (TrashVersion)session.get(TrashVersionImpl.class,
1416                                                    primaryKey);
1417    
1418                                    if (trashVersion != null) {
1419                                            cacheResult(trashVersion);
1420                                    }
1421                                    else {
1422                                            EntityCacheUtil.putResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1423                                                    TrashVersionImpl.class, primaryKey, _nullTrashVersion);
1424                                    }
1425                            }
1426                            catch (Exception e) {
1427                                    EntityCacheUtil.removeResult(TrashVersionModelImpl.ENTITY_CACHE_ENABLED,
1428                                            TrashVersionImpl.class, primaryKey);
1429    
1430                                    throw processException(e);
1431                            }
1432                            finally {
1433                                    closeSession(session);
1434                            }
1435                    }
1436    
1437                    return trashVersion;
1438            }
1439    
1440            /**
1441             * Returns the trash version with the primary key or returns <code>null</code> if it could not be found.
1442             *
1443             * @param versionId the primary key of the trash version
1444             * @return the trash version, or <code>null</code> if a trash version with the primary key could not be found
1445             * @throws SystemException if a system exception occurred
1446             */
1447            public TrashVersion fetchByPrimaryKey(long versionId)
1448                    throws SystemException {
1449                    return fetchByPrimaryKey((Serializable)versionId);
1450            }
1451    
1452            /**
1453             * Returns all the trash versions.
1454             *
1455             * @return the trash versions
1456             * @throws SystemException if a system exception occurred
1457             */
1458            public List<TrashVersion> findAll() throws SystemException {
1459                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1460            }
1461    
1462            /**
1463             * Returns a range of all the trash versions.
1464             *
1465             * <p>
1466             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. 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.
1467             * </p>
1468             *
1469             * @param start the lower bound of the range of trash versions
1470             * @param end the upper bound of the range of trash versions (not inclusive)
1471             * @return the range of trash versions
1472             * @throws SystemException if a system exception occurred
1473             */
1474            public List<TrashVersion> findAll(int start, int end)
1475                    throws SystemException {
1476                    return findAll(start, end, null);
1477            }
1478    
1479            /**
1480             * Returns an ordered range of all the trash versions.
1481             *
1482             * <p>
1483             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.trash.model.impl.TrashVersionModelImpl}. 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.
1484             * </p>
1485             *
1486             * @param start the lower bound of the range of trash versions
1487             * @param end the upper bound of the range of trash versions (not inclusive)
1488             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1489             * @return the ordered range of trash versions
1490             * @throws SystemException if a system exception occurred
1491             */
1492            public List<TrashVersion> findAll(int start, int end,
1493                    OrderByComparator orderByComparator) throws SystemException {
1494                    boolean pagination = true;
1495                    FinderPath finderPath = null;
1496                    Object[] finderArgs = null;
1497    
1498                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1499                                    (orderByComparator == null)) {
1500                            pagination = false;
1501                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1502                            finderArgs = FINDER_ARGS_EMPTY;
1503                    }
1504                    else {
1505                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1506                            finderArgs = new Object[] { start, end, orderByComparator };
1507                    }
1508    
1509                    List<TrashVersion> list = (List<TrashVersion>)FinderCacheUtil.getResult(finderPath,
1510                                    finderArgs, this);
1511    
1512                    if (list == null) {
1513                            StringBundler query = null;
1514                            String sql = null;
1515    
1516                            if (orderByComparator != null) {
1517                                    query = new StringBundler(2 +
1518                                                    (orderByComparator.getOrderByFields().length * 3));
1519    
1520                                    query.append(_SQL_SELECT_TRASHVERSION);
1521    
1522                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1523                                            orderByComparator);
1524    
1525                                    sql = query.toString();
1526                            }
1527                            else {
1528                                    sql = _SQL_SELECT_TRASHVERSION;
1529    
1530                                    if (pagination) {
1531                                            sql = sql.concat(TrashVersionModelImpl.ORDER_BY_JPQL);
1532                                    }
1533                            }
1534    
1535                            Session session = null;
1536    
1537                            try {
1538                                    session = openSession();
1539    
1540                                    Query q = session.createQuery(sql);
1541    
1542                                    if (!pagination) {
1543                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
1544                                                            start, end, false);
1545    
1546                                            Collections.sort(list);
1547    
1548                                            list = new UnmodifiableList<TrashVersion>(list);
1549                                    }
1550                                    else {
1551                                            list = (List<TrashVersion>)QueryUtil.list(q, getDialect(),
1552                                                            start, end);
1553                                    }
1554    
1555                                    cacheResult(list);
1556    
1557                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1558                            }
1559                            catch (Exception e) {
1560                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1561    
1562                                    throw processException(e);
1563                            }
1564                            finally {
1565                                    closeSession(session);
1566                            }
1567                    }
1568    
1569                    return list;
1570            }
1571    
1572            /**
1573             * Removes all the trash versions from the database.
1574             *
1575             * @throws SystemException if a system exception occurred
1576             */
1577            public void removeAll() throws SystemException {
1578                    for (TrashVersion trashVersion : findAll()) {
1579                            remove(trashVersion);
1580                    }
1581            }
1582    
1583            /**
1584             * Returns the number of trash versions.
1585             *
1586             * @return the number of trash versions
1587             * @throws SystemException if a system exception occurred
1588             */
1589            public int countAll() throws SystemException {
1590                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1591                                    FINDER_ARGS_EMPTY, this);
1592    
1593                    if (count == null) {
1594                            Session session = null;
1595    
1596                            try {
1597                                    session = openSession();
1598    
1599                                    Query q = session.createQuery(_SQL_COUNT_TRASHVERSION);
1600    
1601                                    count = (Long)q.uniqueResult();
1602    
1603                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1604                                            FINDER_ARGS_EMPTY, count);
1605                            }
1606                            catch (Exception e) {
1607                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1608                                            FINDER_ARGS_EMPTY);
1609    
1610                                    throw processException(e);
1611                            }
1612                            finally {
1613                                    closeSession(session);
1614                            }
1615                    }
1616    
1617                    return count.intValue();
1618            }
1619    
1620            /**
1621             * Initializes the trash version persistence.
1622             */
1623            public void afterPropertiesSet() {
1624                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1625                                            com.liferay.portal.util.PropsUtil.get(
1626                                                    "value.object.listener.com.liferay.portlet.trash.model.TrashVersion")));
1627    
1628                    if (listenerClassNames.length > 0) {
1629                            try {
1630                                    List<ModelListener<TrashVersion>> listenersList = new ArrayList<ModelListener<TrashVersion>>();
1631    
1632                                    for (String listenerClassName : listenerClassNames) {
1633                                            listenersList.add((ModelListener<TrashVersion>)InstanceFactory.newInstance(
1634                                                            getClassLoader(), listenerClassName));
1635                                    }
1636    
1637                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1638                            }
1639                            catch (Exception e) {
1640                                    _log.error(e);
1641                            }
1642                    }
1643            }
1644    
1645            public void destroy() {
1646                    EntityCacheUtil.removeCache(TrashVersionImpl.class.getName());
1647                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1648                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1649                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1650            }
1651    
1652            private static final String _SQL_SELECT_TRASHVERSION = "SELECT trashVersion FROM TrashVersion trashVersion";
1653            private static final String _SQL_SELECT_TRASHVERSION_WHERE = "SELECT trashVersion FROM TrashVersion trashVersion WHERE ";
1654            private static final String _SQL_COUNT_TRASHVERSION = "SELECT COUNT(trashVersion) FROM TrashVersion trashVersion";
1655            private static final String _SQL_COUNT_TRASHVERSION_WHERE = "SELECT COUNT(trashVersion) FROM TrashVersion trashVersion WHERE ";
1656            private static final String _ORDER_BY_ENTITY_ALIAS = "trashVersion.";
1657            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No TrashVersion exists with the primary key ";
1658            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No TrashVersion exists with the key {";
1659            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1660            private static Log _log = LogFactoryUtil.getLog(TrashVersionPersistenceImpl.class);
1661            private static TrashVersion _nullTrashVersion = new TrashVersionImpl() {
1662                            @Override
1663                            public Object clone() {
1664                                    return this;
1665                            }
1666    
1667                            @Override
1668                            public CacheModel<TrashVersion> toCacheModel() {
1669                                    return _nullTrashVersionCacheModel;
1670                            }
1671                    };
1672    
1673            private static CacheModel<TrashVersion> _nullTrashVersionCacheModel = new CacheModel<TrashVersion>() {
1674                            public TrashVersion toEntityModel() {
1675                                    return _nullTrashVersion;
1676                            }
1677                    };
1678    }