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