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