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            public Ticket findByKey(String key)
104                    throws NoSuchTicketException, SystemException {
105                    Ticket ticket = fetchByKey(key);
106    
107                    if (ticket == null) {
108                            StringBundler msg = new StringBundler(4);
109    
110                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
111    
112                            msg.append("key=");
113                            msg.append(key);
114    
115                            msg.append(StringPool.CLOSE_CURLY_BRACE);
116    
117                            if (_log.isWarnEnabled()) {
118                                    _log.warn(msg.toString());
119                            }
120    
121                            throw new NoSuchTicketException(msg.toString());
122                    }
123    
124                    return ticket;
125            }
126    
127            /**
128             * Returns the ticket where key = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
129             *
130             * @param key the key
131             * @return the matching ticket, or <code>null</code> if a matching ticket could not be found
132             * @throws SystemException if a system exception occurred
133             */
134            public Ticket fetchByKey(String key) throws SystemException {
135                    return fetchByKey(key, true);
136            }
137    
138            /**
139             * Returns the ticket where key = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
140             *
141             * @param key the key
142             * @param retrieveFromCache whether to use the finder cache
143             * @return the matching ticket, or <code>null</code> if a matching ticket could not be found
144             * @throws SystemException if a system exception occurred
145             */
146            public Ticket fetchByKey(String key, boolean retrieveFromCache)
147                    throws SystemException {
148                    Object[] finderArgs = new Object[] { key };
149    
150                    Object result = null;
151    
152                    if (retrieveFromCache) {
153                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_KEY,
154                                            finderArgs, this);
155                    }
156    
157                    if (result instanceof Ticket) {
158                            Ticket ticket = (Ticket)result;
159    
160                            if (!Validator.equals(key, ticket.getKey())) {
161                                    result = null;
162                            }
163                    }
164    
165                    if (result == null) {
166                            StringBundler query = new StringBundler(3);
167    
168                            query.append(_SQL_SELECT_TICKET_WHERE);
169    
170                            boolean bindKey = false;
171    
172                            if (key == null) {
173                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
174                            }
175                            else if (key.equals(StringPool.BLANK)) {
176                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
177                            }
178                            else {
179                                    bindKey = true;
180    
181                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
182                            }
183    
184                            String sql = query.toString();
185    
186                            Session session = null;
187    
188                            try {
189                                    session = openSession();
190    
191                                    Query q = session.createQuery(sql);
192    
193                                    QueryPos qPos = QueryPos.getInstance(q);
194    
195                                    if (bindKey) {
196                                            qPos.add(key);
197                                    }
198    
199                                    List<Ticket> list = q.list();
200    
201                                    if (list.isEmpty()) {
202                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
203                                                    finderArgs, list);
204                                    }
205                                    else {
206                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
207                                                    _log.warn(
208                                                            "TicketPersistenceImpl.fetchByKey(String, boolean) with parameters (" +
209                                                            StringUtil.merge(finderArgs) +
210                                                            ") 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.");
211                                            }
212    
213                                            Ticket ticket = list.get(0);
214    
215                                            result = ticket;
216    
217                                            cacheResult(ticket);
218    
219                                            if ((ticket.getKey() == null) ||
220                                                            !ticket.getKey().equals(key)) {
221                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
222                                                            finderArgs, ticket);
223                                            }
224                                    }
225                            }
226                            catch (Exception e) {
227                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY,
228                                            finderArgs);
229    
230                                    throw processException(e);
231                            }
232                            finally {
233                                    closeSession(session);
234                            }
235                    }
236    
237                    if (result instanceof List<?>) {
238                            return null;
239                    }
240                    else {
241                            return (Ticket)result;
242                    }
243            }
244    
245            /**
246             * Removes the ticket where key = &#63; from the database.
247             *
248             * @param key the key
249             * @return the ticket that was removed
250             * @throws SystemException if a system exception occurred
251             */
252            public Ticket removeByKey(String key)
253                    throws NoSuchTicketException, SystemException {
254                    Ticket ticket = findByKey(key);
255    
256                    return remove(ticket);
257            }
258    
259            /**
260             * Returns the number of tickets where key = &#63;.
261             *
262             * @param key the key
263             * @return the number of matching tickets
264             * @throws SystemException if a system exception occurred
265             */
266            public int countByKey(String key) throws SystemException {
267                    FinderPath finderPath = FINDER_PATH_COUNT_BY_KEY;
268    
269                    Object[] finderArgs = new Object[] { key };
270    
271                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
272                                    this);
273    
274                    if (count == null) {
275                            StringBundler query = new StringBundler(2);
276    
277                            query.append(_SQL_COUNT_TICKET_WHERE);
278    
279                            boolean bindKey = false;
280    
281                            if (key == null) {
282                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
283                            }
284                            else if (key.equals(StringPool.BLANK)) {
285                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
286                            }
287                            else {
288                                    bindKey = true;
289    
290                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
291                            }
292    
293                            String sql = query.toString();
294    
295                            Session session = null;
296    
297                            try {
298                                    session = openSession();
299    
300                                    Query q = session.createQuery(sql);
301    
302                                    QueryPos qPos = QueryPos.getInstance(q);
303    
304                                    if (bindKey) {
305                                            qPos.add(key);
306                                    }
307    
308                                    count = (Long)q.uniqueResult();
309    
310                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
311                            }
312                            catch (Exception e) {
313                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
314    
315                                    throw processException(e);
316                            }
317                            finally {
318                                    closeSession(session);
319                            }
320                    }
321    
322                    return count.intValue();
323            }
324    
325            private static final String _FINDER_COLUMN_KEY_KEY_1 = "ticket.key IS NULL";
326            private static final String _FINDER_COLUMN_KEY_KEY_2 = "ticket.key = ?";
327            private static final String _FINDER_COLUMN_KEY_KEY_3 = "(ticket.key IS NULL OR ticket.key = '')";
328    
329            /**
330             * Caches the ticket in the entity cache if it is enabled.
331             *
332             * @param ticket the ticket
333             */
334            public void cacheResult(Ticket ticket) {
335                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
336                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
337    
338                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
339                            new Object[] { ticket.getKey() }, ticket);
340    
341                    ticket.resetOriginalValues();
342            }
343    
344            /**
345             * Caches the tickets in the entity cache if it is enabled.
346             *
347             * @param tickets the tickets
348             */
349            public void cacheResult(List<Ticket> tickets) {
350                    for (Ticket ticket : tickets) {
351                            if (EntityCacheUtil.getResult(
352                                                    TicketModelImpl.ENTITY_CACHE_ENABLED, TicketImpl.class,
353                                                    ticket.getPrimaryKey()) == null) {
354                                    cacheResult(ticket);
355                            }
356                            else {
357                                    ticket.resetOriginalValues();
358                            }
359                    }
360            }
361    
362            /**
363             * Clears the cache for all tickets.
364             *
365             * <p>
366             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
367             * </p>
368             */
369            @Override
370            public void clearCache() {
371                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
372                            CacheRegistryUtil.clear(TicketImpl.class.getName());
373                    }
374    
375                    EntityCacheUtil.clearCache(TicketImpl.class.getName());
376    
377                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
378                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
379                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
380            }
381    
382            /**
383             * Clears the cache for the ticket.
384             *
385             * <p>
386             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
387             * </p>
388             */
389            @Override
390            public void clearCache(Ticket ticket) {
391                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
392                            TicketImpl.class, ticket.getPrimaryKey());
393    
394                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
395                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
396    
397                    clearUniqueFindersCache(ticket);
398            }
399    
400            @Override
401            public void clearCache(List<Ticket> tickets) {
402                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
403                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
404    
405                    for (Ticket ticket : tickets) {
406                            EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
407                                    TicketImpl.class, ticket.getPrimaryKey());
408    
409                            clearUniqueFindersCache(ticket);
410                    }
411            }
412    
413            protected void cacheUniqueFindersCache(Ticket ticket) {
414                    if (ticket.isNew()) {
415                            Object[] args = new Object[] { ticket.getKey() };
416    
417                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, args,
418                                    Long.valueOf(1));
419                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY, args, ticket);
420                    }
421                    else {
422                            TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
423    
424                            if ((ticketModelImpl.getColumnBitmask() &
425                                            FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
426                                    Object[] args = new Object[] { ticket.getKey() };
427    
428                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, args,
429                                            Long.valueOf(1));
430                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY, args, ticket);
431                            }
432                    }
433            }
434    
435            protected void clearUniqueFindersCache(Ticket ticket) {
436                    TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
437    
438                    Object[] args = new Object[] { ticket.getKey() };
439    
440                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
441                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
442    
443                    if ((ticketModelImpl.getColumnBitmask() &
444                                    FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
445                            args = new Object[] { ticketModelImpl.getOriginalKey() };
446    
447                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
448                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
449                    }
450            }
451    
452            /**
453             * Creates a new ticket with the primary key. Does not add the ticket to the database.
454             *
455             * @param ticketId the primary key for the new ticket
456             * @return the new ticket
457             */
458            public Ticket create(long ticketId) {
459                    Ticket ticket = new TicketImpl();
460    
461                    ticket.setNew(true);
462                    ticket.setPrimaryKey(ticketId);
463    
464                    return ticket;
465            }
466    
467            /**
468             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
469             *
470             * @param ticketId the primary key of the ticket
471             * @return the ticket that was removed
472             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
473             * @throws SystemException if a system exception occurred
474             */
475            public Ticket remove(long ticketId)
476                    throws NoSuchTicketException, SystemException {
477                    return remove((Serializable)ticketId);
478            }
479    
480            /**
481             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
482             *
483             * @param primaryKey the primary key of the ticket
484             * @return the ticket that was removed
485             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
486             * @throws SystemException if a system exception occurred
487             */
488            @Override
489            public Ticket remove(Serializable primaryKey)
490                    throws NoSuchTicketException, SystemException {
491                    Session session = null;
492    
493                    try {
494                            session = openSession();
495    
496                            Ticket ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
497    
498                            if (ticket == null) {
499                                    if (_log.isWarnEnabled()) {
500                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
501                                    }
502    
503                                    throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
504                                            primaryKey);
505                            }
506    
507                            return remove(ticket);
508                    }
509                    catch (NoSuchTicketException nsee) {
510                            throw nsee;
511                    }
512                    catch (Exception e) {
513                            throw processException(e);
514                    }
515                    finally {
516                            closeSession(session);
517                    }
518            }
519    
520            @Override
521            protected Ticket removeImpl(Ticket ticket) throws SystemException {
522                    ticket = toUnwrappedModel(ticket);
523    
524                    Session session = null;
525    
526                    try {
527                            session = openSession();
528    
529                            if (!session.contains(ticket)) {
530                                    ticket = (Ticket)session.get(TicketImpl.class,
531                                                    ticket.getPrimaryKeyObj());
532                            }
533    
534                            if (ticket != null) {
535                                    session.delete(ticket);
536                            }
537                    }
538                    catch (Exception e) {
539                            throw processException(e);
540                    }
541                    finally {
542                            closeSession(session);
543                    }
544    
545                    if (ticket != null) {
546                            clearCache(ticket);
547                    }
548    
549                    return ticket;
550            }
551    
552            @Override
553            public Ticket updateImpl(com.liferay.portal.model.Ticket ticket)
554                    throws SystemException {
555                    ticket = toUnwrappedModel(ticket);
556    
557                    boolean isNew = ticket.isNew();
558    
559                    Session session = null;
560    
561                    try {
562                            session = openSession();
563    
564                            if (ticket.isNew()) {
565                                    session.save(ticket);
566    
567                                    ticket.setNew(false);
568                            }
569                            else {
570                                    session.merge(ticket);
571                            }
572                    }
573                    catch (Exception e) {
574                            throw processException(e);
575                    }
576                    finally {
577                            closeSession(session);
578                    }
579    
580                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
581    
582                    if (isNew || !TicketModelImpl.COLUMN_BITMASK_ENABLED) {
583                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
584                    }
585    
586                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
587                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
588    
589                    clearUniqueFindersCache(ticket);
590                    cacheUniqueFindersCache(ticket);
591    
592                    return ticket;
593            }
594    
595            protected Ticket toUnwrappedModel(Ticket ticket) {
596                    if (ticket instanceof TicketImpl) {
597                            return ticket;
598                    }
599    
600                    TicketImpl ticketImpl = new TicketImpl();
601    
602                    ticketImpl.setNew(ticket.isNew());
603                    ticketImpl.setPrimaryKey(ticket.getPrimaryKey());
604    
605                    ticketImpl.setTicketId(ticket.getTicketId());
606                    ticketImpl.setCompanyId(ticket.getCompanyId());
607                    ticketImpl.setCreateDate(ticket.getCreateDate());
608                    ticketImpl.setClassNameId(ticket.getClassNameId());
609                    ticketImpl.setClassPK(ticket.getClassPK());
610                    ticketImpl.setKey(ticket.getKey());
611                    ticketImpl.setType(ticket.getType());
612                    ticketImpl.setExtraInfo(ticket.getExtraInfo());
613                    ticketImpl.setExpirationDate(ticket.getExpirationDate());
614    
615                    return ticketImpl;
616            }
617    
618            /**
619             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
620             *
621             * @param primaryKey the primary key of the ticket
622             * @return the ticket
623             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
624             * @throws SystemException if a system exception occurred
625             */
626            @Override
627            public Ticket findByPrimaryKey(Serializable primaryKey)
628                    throws NoSuchTicketException, SystemException {
629                    Ticket ticket = fetchByPrimaryKey(primaryKey);
630    
631                    if (ticket == null) {
632                            if (_log.isWarnEnabled()) {
633                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
634                            }
635    
636                            throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
637                                    primaryKey);
638                    }
639    
640                    return ticket;
641            }
642    
643            /**
644             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchTicketException} if it could not be found.
645             *
646             * @param ticketId the primary key of the ticket
647             * @return the ticket
648             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
649             * @throws SystemException if a system exception occurred
650             */
651            public Ticket findByPrimaryKey(long ticketId)
652                    throws NoSuchTicketException, SystemException {
653                    return findByPrimaryKey((Serializable)ticketId);
654            }
655    
656            /**
657             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
658             *
659             * @param primaryKey the primary key of the ticket
660             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
661             * @throws SystemException if a system exception occurred
662             */
663            @Override
664            public Ticket fetchByPrimaryKey(Serializable primaryKey)
665                    throws SystemException {
666                    Ticket ticket = (Ticket)EntityCacheUtil.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
667                                    TicketImpl.class, primaryKey);
668    
669                    if (ticket == _nullTicket) {
670                            return null;
671                    }
672    
673                    if (ticket == null) {
674                            Session session = null;
675    
676                            try {
677                                    session = openSession();
678    
679                                    ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
680    
681                                    if (ticket != null) {
682                                            cacheResult(ticket);
683                                    }
684                                    else {
685                                            EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
686                                                    TicketImpl.class, primaryKey, _nullTicket);
687                                    }
688                            }
689                            catch (Exception e) {
690                                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
691                                            TicketImpl.class, primaryKey);
692    
693                                    throw processException(e);
694                            }
695                            finally {
696                                    closeSession(session);
697                            }
698                    }
699    
700                    return ticket;
701            }
702    
703            /**
704             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
705             *
706             * @param ticketId the primary key of the ticket
707             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
708             * @throws SystemException if a system exception occurred
709             */
710            public Ticket fetchByPrimaryKey(long ticketId) throws SystemException {
711                    return fetchByPrimaryKey((Serializable)ticketId);
712            }
713    
714            /**
715             * Returns all the tickets.
716             *
717             * @return the tickets
718             * @throws SystemException if a system exception occurred
719             */
720            public List<Ticket> findAll() throws SystemException {
721                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
722            }
723    
724            /**
725             * Returns a range of all the tickets.
726             *
727             * <p>
728             * 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.
729             * </p>
730             *
731             * @param start the lower bound of the range of tickets
732             * @param end the upper bound of the range of tickets (not inclusive)
733             * @return the range of tickets
734             * @throws SystemException if a system exception occurred
735             */
736            public List<Ticket> findAll(int start, int end) throws SystemException {
737                    return findAll(start, end, null);
738            }
739    
740            /**
741             * Returns an ordered range of all the tickets.
742             *
743             * <p>
744             * 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.
745             * </p>
746             *
747             * @param start the lower bound of the range of tickets
748             * @param end the upper bound of the range of tickets (not inclusive)
749             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
750             * @return the ordered range of tickets
751             * @throws SystemException if a system exception occurred
752             */
753            public List<Ticket> findAll(int start, int end,
754                    OrderByComparator orderByComparator) throws SystemException {
755                    boolean pagination = true;
756                    FinderPath finderPath = null;
757                    Object[] finderArgs = null;
758    
759                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
760                                    (orderByComparator == null)) {
761                            pagination = false;
762                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
763                            finderArgs = FINDER_ARGS_EMPTY;
764                    }
765                    else {
766                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
767                            finderArgs = new Object[] { start, end, orderByComparator };
768                    }
769    
770                    List<Ticket> list = (List<Ticket>)FinderCacheUtil.getResult(finderPath,
771                                    finderArgs, this);
772    
773                    if (list == null) {
774                            StringBundler query = null;
775                            String sql = null;
776    
777                            if (orderByComparator != null) {
778                                    query = new StringBundler(2 +
779                                                    (orderByComparator.getOrderByFields().length * 3));
780    
781                                    query.append(_SQL_SELECT_TICKET);
782    
783                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
784                                            orderByComparator);
785    
786                                    sql = query.toString();
787                            }
788                            else {
789                                    sql = _SQL_SELECT_TICKET;
790    
791                                    if (pagination) {
792                                            sql = sql.concat(TicketModelImpl.ORDER_BY_JPQL);
793                                    }
794                            }
795    
796                            Session session = null;
797    
798                            try {
799                                    session = openSession();
800    
801                                    Query q = session.createQuery(sql);
802    
803                                    if (!pagination) {
804                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
805                                                            end, false);
806    
807                                            Collections.sort(list);
808    
809                                            list = new UnmodifiableList<Ticket>(list);
810                                    }
811                                    else {
812                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
813                                                            end);
814                                    }
815    
816                                    cacheResult(list);
817    
818                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
819                            }
820                            catch (Exception e) {
821                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
822    
823                                    throw processException(e);
824                            }
825                            finally {
826                                    closeSession(session);
827                            }
828                    }
829    
830                    return list;
831            }
832    
833            /**
834             * Removes all the tickets from the database.
835             *
836             * @throws SystemException if a system exception occurred
837             */
838            public void removeAll() throws SystemException {
839                    for (Ticket ticket : findAll()) {
840                            remove(ticket);
841                    }
842            }
843    
844            /**
845             * Returns the number of tickets.
846             *
847             * @return the number of tickets
848             * @throws SystemException if a system exception occurred
849             */
850            public int countAll() throws SystemException {
851                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
852                                    FINDER_ARGS_EMPTY, this);
853    
854                    if (count == null) {
855                            Session session = null;
856    
857                            try {
858                                    session = openSession();
859    
860                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
861    
862                                    count = (Long)q.uniqueResult();
863    
864                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
865                                            FINDER_ARGS_EMPTY, count);
866                            }
867                            catch (Exception e) {
868                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
869                                            FINDER_ARGS_EMPTY);
870    
871                                    throw processException(e);
872                            }
873                            finally {
874                                    closeSession(session);
875                            }
876                    }
877    
878                    return count.intValue();
879            }
880    
881            @Override
882            protected Set<String> getBadColumnNames() {
883                    return _badColumnNames;
884            }
885    
886            /**
887             * Initializes the ticket persistence.
888             */
889            public void afterPropertiesSet() {
890                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
891                                            com.liferay.portal.util.PropsUtil.get(
892                                                    "value.object.listener.com.liferay.portal.model.Ticket")));
893    
894                    if (listenerClassNames.length > 0) {
895                            try {
896                                    List<ModelListener<Ticket>> listenersList = new ArrayList<ModelListener<Ticket>>();
897    
898                                    for (String listenerClassName : listenerClassNames) {
899                                            listenersList.add((ModelListener<Ticket>)InstanceFactory.newInstance(
900                                                            getClassLoader(), listenerClassName));
901                                    }
902    
903                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
904                            }
905                            catch (Exception e) {
906                                    _log.error(e);
907                            }
908                    }
909            }
910    
911            public void destroy() {
912                    EntityCacheUtil.removeCache(TicketImpl.class.getName());
913                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
914                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
915                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
916            }
917    
918            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
919            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
920            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
921            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
922            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
923            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
924            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
925            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
926            private static Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
927            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
928                                    "key", "type"
929                            });
930            private static Ticket _nullTicket = new TicketImpl() {
931                            @Override
932                            public Object clone() {
933                                    return this;
934                            }
935    
936                            @Override
937                            public CacheModel<Ticket> toCacheModel() {
938                                    return _nullTicketCacheModel;
939                            }
940                    };
941    
942            private static CacheModel<Ticket> _nullTicketCacheModel = new CacheModel<Ticket>() {
943                            public Ticket toEntityModel() {
944                                    return _nullTicket;
945                            }
946                    };
947    }