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