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    
334            public TicketPersistenceImpl() {
335                    setModelClass(Ticket.class);
336            }
337    
338            /**
339             * Caches the ticket in the entity cache if it is enabled.
340             *
341             * @param ticket the ticket
342             */
343            @Override
344            public void cacheResult(Ticket ticket) {
345                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
346                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
347    
348                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
349                            new Object[] { ticket.getKey() }, ticket);
350    
351                    ticket.resetOriginalValues();
352            }
353    
354            /**
355             * Caches the tickets in the entity cache if it is enabled.
356             *
357             * @param tickets the tickets
358             */
359            @Override
360            public void cacheResult(List<Ticket> tickets) {
361                    for (Ticket ticket : tickets) {
362                            if (EntityCacheUtil.getResult(
363                                                    TicketModelImpl.ENTITY_CACHE_ENABLED, TicketImpl.class,
364                                                    ticket.getPrimaryKey()) == null) {
365                                    cacheResult(ticket);
366                            }
367                            else {
368                                    ticket.resetOriginalValues();
369                            }
370                    }
371            }
372    
373            /**
374             * Clears the cache for all tickets.
375             *
376             * <p>
377             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
378             * </p>
379             */
380            @Override
381            public void clearCache() {
382                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
383                            CacheRegistryUtil.clear(TicketImpl.class.getName());
384                    }
385    
386                    EntityCacheUtil.clearCache(TicketImpl.class.getName());
387    
388                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
389                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
390                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
391            }
392    
393            /**
394             * Clears the cache for the ticket.
395             *
396             * <p>
397             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
398             * </p>
399             */
400            @Override
401            public void clearCache(Ticket ticket) {
402                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
403                            TicketImpl.class, ticket.getPrimaryKey());
404    
405                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
406                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
407    
408                    clearUniqueFindersCache(ticket);
409            }
410    
411            @Override
412            public void clearCache(List<Ticket> tickets) {
413                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
414                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
415    
416                    for (Ticket ticket : tickets) {
417                            EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
418                                    TicketImpl.class, ticket.getPrimaryKey());
419    
420                            clearUniqueFindersCache(ticket);
421                    }
422            }
423    
424            protected void cacheUniqueFindersCache(Ticket ticket) {
425                    if (ticket.isNew()) {
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                    else {
433                            TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
434    
435                            if ((ticketModelImpl.getColumnBitmask() &
436                                            FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
437                                    Object[] args = new Object[] { ticket.getKey() };
438    
439                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, args,
440                                            Long.valueOf(1));
441                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY, args, ticket);
442                            }
443                    }
444            }
445    
446            protected void clearUniqueFindersCache(Ticket ticket) {
447                    TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
448    
449                    Object[] args = new Object[] { ticket.getKey() };
450    
451                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
452                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
453    
454                    if ((ticketModelImpl.getColumnBitmask() &
455                                    FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
456                            args = new Object[] { ticketModelImpl.getOriginalKey() };
457    
458                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
459                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
460                    }
461            }
462    
463            /**
464             * Creates a new ticket with the primary key. Does not add the ticket to the database.
465             *
466             * @param ticketId the primary key for the new ticket
467             * @return the new ticket
468             */
469            @Override
470            public Ticket create(long ticketId) {
471                    Ticket ticket = new TicketImpl();
472    
473                    ticket.setNew(true);
474                    ticket.setPrimaryKey(ticketId);
475    
476                    return ticket;
477            }
478    
479            /**
480             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
481             *
482             * @param ticketId the primary key of the ticket
483             * @return the ticket that was removed
484             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
485             * @throws SystemException if a system exception occurred
486             */
487            @Override
488            public Ticket remove(long ticketId)
489                    throws NoSuchTicketException, SystemException {
490                    return remove((Serializable)ticketId);
491            }
492    
493            /**
494             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
495             *
496             * @param primaryKey the primary key of the ticket
497             * @return the ticket that was removed
498             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
499             * @throws SystemException if a system exception occurred
500             */
501            @Override
502            public Ticket remove(Serializable primaryKey)
503                    throws NoSuchTicketException, SystemException {
504                    Session session = null;
505    
506                    try {
507                            session = openSession();
508    
509                            Ticket ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
510    
511                            if (ticket == null) {
512                                    if (_log.isWarnEnabled()) {
513                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
514                                    }
515    
516                                    throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
517                                            primaryKey);
518                            }
519    
520                            return remove(ticket);
521                    }
522                    catch (NoSuchTicketException nsee) {
523                            throw nsee;
524                    }
525                    catch (Exception e) {
526                            throw processException(e);
527                    }
528                    finally {
529                            closeSession(session);
530                    }
531            }
532    
533            @Override
534            protected Ticket removeImpl(Ticket ticket) throws SystemException {
535                    ticket = toUnwrappedModel(ticket);
536    
537                    Session session = null;
538    
539                    try {
540                            session = openSession();
541    
542                            if (!session.contains(ticket)) {
543                                    ticket = (Ticket)session.get(TicketImpl.class,
544                                                    ticket.getPrimaryKeyObj());
545                            }
546    
547                            if (ticket != null) {
548                                    session.delete(ticket);
549                            }
550                    }
551                    catch (Exception e) {
552                            throw processException(e);
553                    }
554                    finally {
555                            closeSession(session);
556                    }
557    
558                    if (ticket != null) {
559                            clearCache(ticket);
560                    }
561    
562                    return ticket;
563            }
564    
565            @Override
566            public Ticket updateImpl(com.liferay.portal.model.Ticket ticket)
567                    throws SystemException {
568                    ticket = toUnwrappedModel(ticket);
569    
570                    boolean isNew = ticket.isNew();
571    
572                    Session session = null;
573    
574                    try {
575                            session = openSession();
576    
577                            if (ticket.isNew()) {
578                                    session.save(ticket);
579    
580                                    ticket.setNew(false);
581                            }
582                            else {
583                                    session.merge(ticket);
584                            }
585                    }
586                    catch (Exception e) {
587                            throw processException(e);
588                    }
589                    finally {
590                            closeSession(session);
591                    }
592    
593                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
594    
595                    if (isNew || !TicketModelImpl.COLUMN_BITMASK_ENABLED) {
596                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
597                    }
598    
599                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
600                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
601    
602                    clearUniqueFindersCache(ticket);
603                    cacheUniqueFindersCache(ticket);
604    
605                    return ticket;
606            }
607    
608            protected Ticket toUnwrappedModel(Ticket ticket) {
609                    if (ticket instanceof TicketImpl) {
610                            return ticket;
611                    }
612    
613                    TicketImpl ticketImpl = new TicketImpl();
614    
615                    ticketImpl.setNew(ticket.isNew());
616                    ticketImpl.setPrimaryKey(ticket.getPrimaryKey());
617    
618                    ticketImpl.setTicketId(ticket.getTicketId());
619                    ticketImpl.setCompanyId(ticket.getCompanyId());
620                    ticketImpl.setCreateDate(ticket.getCreateDate());
621                    ticketImpl.setClassNameId(ticket.getClassNameId());
622                    ticketImpl.setClassPK(ticket.getClassPK());
623                    ticketImpl.setKey(ticket.getKey());
624                    ticketImpl.setType(ticket.getType());
625                    ticketImpl.setExtraInfo(ticket.getExtraInfo());
626                    ticketImpl.setExpirationDate(ticket.getExpirationDate());
627    
628                    return ticketImpl;
629            }
630    
631            /**
632             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
633             *
634             * @param primaryKey the primary key of the ticket
635             * @return the ticket
636             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
637             * @throws SystemException if a system exception occurred
638             */
639            @Override
640            public Ticket findByPrimaryKey(Serializable primaryKey)
641                    throws NoSuchTicketException, SystemException {
642                    Ticket ticket = fetchByPrimaryKey(primaryKey);
643    
644                    if (ticket == null) {
645                            if (_log.isWarnEnabled()) {
646                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
647                            }
648    
649                            throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
650                                    primaryKey);
651                    }
652    
653                    return ticket;
654            }
655    
656            /**
657             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchTicketException} if it could not be found.
658             *
659             * @param ticketId the primary key of the ticket
660             * @return the ticket
661             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
662             * @throws SystemException if a system exception occurred
663             */
664            @Override
665            public Ticket findByPrimaryKey(long ticketId)
666                    throws NoSuchTicketException, SystemException {
667                    return findByPrimaryKey((Serializable)ticketId);
668            }
669    
670            /**
671             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
672             *
673             * @param primaryKey the primary key of the ticket
674             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
675             * @throws SystemException if a system exception occurred
676             */
677            @Override
678            public Ticket fetchByPrimaryKey(Serializable primaryKey)
679                    throws SystemException {
680                    Ticket ticket = (Ticket)EntityCacheUtil.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
681                                    TicketImpl.class, primaryKey);
682    
683                    if (ticket == _nullTicket) {
684                            return null;
685                    }
686    
687                    if (ticket == null) {
688                            Session session = null;
689    
690                            try {
691                                    session = openSession();
692    
693                                    ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
694    
695                                    if (ticket != null) {
696                                            cacheResult(ticket);
697                                    }
698                                    else {
699                                            EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
700                                                    TicketImpl.class, primaryKey, _nullTicket);
701                                    }
702                            }
703                            catch (Exception e) {
704                                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
705                                            TicketImpl.class, primaryKey);
706    
707                                    throw processException(e);
708                            }
709                            finally {
710                                    closeSession(session);
711                            }
712                    }
713    
714                    return ticket;
715            }
716    
717            /**
718             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
719             *
720             * @param ticketId the primary key of the ticket
721             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
722             * @throws SystemException if a system exception occurred
723             */
724            @Override
725            public Ticket fetchByPrimaryKey(long ticketId) throws SystemException {
726                    return fetchByPrimaryKey((Serializable)ticketId);
727            }
728    
729            /**
730             * Returns all the tickets.
731             *
732             * @return the tickets
733             * @throws SystemException if a system exception occurred
734             */
735            @Override
736            public List<Ticket> findAll() throws SystemException {
737                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
738            }
739    
740            /**
741             * Returns a 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             * @return the range of tickets
750             * @throws SystemException if a system exception occurred
751             */
752            @Override
753            public List<Ticket> findAll(int start, int end) throws SystemException {
754                    return findAll(start, end, null);
755            }
756    
757            /**
758             * Returns an ordered range of all the tickets.
759             *
760             * <p>
761             * 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.
762             * </p>
763             *
764             * @param start the lower bound of the range of tickets
765             * @param end the upper bound of the range of tickets (not inclusive)
766             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
767             * @return the ordered range of tickets
768             * @throws SystemException if a system exception occurred
769             */
770            @Override
771            public List<Ticket> findAll(int start, int end,
772                    OrderByComparator orderByComparator) throws SystemException {
773                    boolean pagination = true;
774                    FinderPath finderPath = null;
775                    Object[] finderArgs = null;
776    
777                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
778                                    (orderByComparator == null)) {
779                            pagination = false;
780                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
781                            finderArgs = FINDER_ARGS_EMPTY;
782                    }
783                    else {
784                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
785                            finderArgs = new Object[] { start, end, orderByComparator };
786                    }
787    
788                    List<Ticket> list = (List<Ticket>)FinderCacheUtil.getResult(finderPath,
789                                    finderArgs, this);
790    
791                    if (list == null) {
792                            StringBundler query = null;
793                            String sql = null;
794    
795                            if (orderByComparator != null) {
796                                    query = new StringBundler(2 +
797                                                    (orderByComparator.getOrderByFields().length * 3));
798    
799                                    query.append(_SQL_SELECT_TICKET);
800    
801                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
802                                            orderByComparator);
803    
804                                    sql = query.toString();
805                            }
806                            else {
807                                    sql = _SQL_SELECT_TICKET;
808    
809                                    if (pagination) {
810                                            sql = sql.concat(TicketModelImpl.ORDER_BY_JPQL);
811                                    }
812                            }
813    
814                            Session session = null;
815    
816                            try {
817                                    session = openSession();
818    
819                                    Query q = session.createQuery(sql);
820    
821                                    if (!pagination) {
822                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
823                                                            end, false);
824    
825                                            Collections.sort(list);
826    
827                                            list = new UnmodifiableList<Ticket>(list);
828                                    }
829                                    else {
830                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
831                                                            end);
832                                    }
833    
834                                    cacheResult(list);
835    
836                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
837                            }
838                            catch (Exception e) {
839                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
840    
841                                    throw processException(e);
842                            }
843                            finally {
844                                    closeSession(session);
845                            }
846                    }
847    
848                    return list;
849            }
850    
851            /**
852             * Removes all the tickets from the database.
853             *
854             * @throws SystemException if a system exception occurred
855             */
856            @Override
857            public void removeAll() throws SystemException {
858                    for (Ticket ticket : findAll()) {
859                            remove(ticket);
860                    }
861            }
862    
863            /**
864             * Returns the number of tickets.
865             *
866             * @return the number of tickets
867             * @throws SystemException if a system exception occurred
868             */
869            @Override
870            public int countAll() throws SystemException {
871                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
872                                    FINDER_ARGS_EMPTY, this);
873    
874                    if (count == null) {
875                            Session session = null;
876    
877                            try {
878                                    session = openSession();
879    
880                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
881    
882                                    count = (Long)q.uniqueResult();
883    
884                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
885                                            FINDER_ARGS_EMPTY, count);
886                            }
887                            catch (Exception e) {
888                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
889                                            FINDER_ARGS_EMPTY);
890    
891                                    throw processException(e);
892                            }
893                            finally {
894                                    closeSession(session);
895                            }
896                    }
897    
898                    return count.intValue();
899            }
900    
901            @Override
902            protected Set<String> getBadColumnNames() {
903                    return _badColumnNames;
904            }
905    
906            /**
907             * Initializes the ticket persistence.
908             */
909            public void afterPropertiesSet() {
910                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
911                                            com.liferay.portal.util.PropsUtil.get(
912                                                    "value.object.listener.com.liferay.portal.model.Ticket")));
913    
914                    if (listenerClassNames.length > 0) {
915                            try {
916                                    List<ModelListener<Ticket>> listenersList = new ArrayList<ModelListener<Ticket>>();
917    
918                                    for (String listenerClassName : listenerClassNames) {
919                                            listenersList.add((ModelListener<Ticket>)InstanceFactory.newInstance(
920                                                            getClassLoader(), listenerClassName));
921                                    }
922    
923                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
924                            }
925                            catch (Exception e) {
926                                    _log.error(e);
927                            }
928                    }
929            }
930    
931            public void destroy() {
932                    EntityCacheUtil.removeCache(TicketImpl.class.getName());
933                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
934                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
935                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
936            }
937    
938            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
939            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
940            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
941            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
942            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
943            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
944            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
945            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
946            private static Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
947            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
948                                    "key", "type"
949                            });
950            private static Ticket _nullTicket = new TicketImpl() {
951                            @Override
952                            public Object clone() {
953                                    return this;
954                            }
955    
956                            @Override
957                            public CacheModel<Ticket> toCacheModel() {
958                                    return _nullTicketCacheModel;
959                            }
960                    };
961    
962            private static CacheModel<Ticket> _nullTicketCacheModel = new CacheModel<Ticket>() {
963                            @Override
964                            public Ticket toEntityModel() {
965                                    return _nullTicket;
966                            }
967                    };
968    }