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