001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchTicketException;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.SetUtil;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.Validator;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.MVCCModel;
038    import com.liferay.portal.model.Ticket;
039    import com.liferay.portal.model.impl.TicketImpl;
040    import com.liferay.portal.model.impl.TicketModelImpl;
041    import com.liferay.portal.service.persistence.TicketPersistence;
042    
043    import java.io.Serializable;
044    
045    import java.util.Collections;
046    import java.util.HashMap;
047    import java.util.HashSet;
048    import java.util.Iterator;
049    import java.util.List;
050    import java.util.Map;
051    import java.util.Set;
052    
053    /**
054     * The persistence implementation for the ticket service.
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see TicketPersistence
062     * @see TicketUtil
063     * @generated
064     */
065    @ProviderType
066    public class TicketPersistenceImpl extends BasePersistenceImpl<Ticket>
067            implements TicketPersistence {
068            /*
069             * NOTE FOR DEVELOPERS:
070             *
071             * 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.
072             */
073            public static final String FINDER_CLASS_NAME_ENTITY = TicketImpl.class.getName();
074            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List1";
076            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
077                    ".List2";
078            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
079                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
080                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
082                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
085                            TicketModelImpl.FINDER_CACHE_ENABLED, Long.class,
086                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
087            public static final FinderPath FINDER_PATH_FETCH_BY_KEY = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
088                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
089                            FINDER_CLASS_NAME_ENTITY, "fetchByKey",
090                            new String[] { String.class.getName() },
091                            TicketModelImpl.KEY_COLUMN_BITMASK);
092            public static final FinderPath FINDER_PATH_COUNT_BY_KEY = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
093                            TicketModelImpl.FINDER_CACHE_ENABLED, Long.class,
094                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByKey",
095                            new String[] { String.class.getName() });
096    
097            /**
098             * Returns the ticket where key = &#63; or throws a {@link com.liferay.portal.NoSuchTicketException} if it could not be found.
099             *
100             * @param key the key
101             * @return the matching ticket
102             * @throws com.liferay.portal.NoSuchTicketException if a matching ticket could not be found
103             */
104            @Override
105            public Ticket findByKey(String key) throws NoSuchTicketException {
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             */
134            @Override
135            public Ticket fetchByKey(String key) {
136                    return fetchByKey(key, true);
137            }
138    
139            /**
140             * Returns the ticket where key = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
141             *
142             * @param key the key
143             * @param retrieveFromCache whether to use the finder cache
144             * @return the matching ticket, or <code>null</code> if a matching ticket could not be found
145             */
146            @Override
147            public Ticket fetchByKey(String key, boolean retrieveFromCache) {
148                    Object[] finderArgs = new Object[] { key };
149    
150                    Object result = null;
151    
152                    if (retrieveFromCache) {
153                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_KEY,
154                                            finderArgs, this);
155                    }
156    
157                    if (result instanceof Ticket) {
158                            Ticket ticket = (Ticket)result;
159    
160                            if (!Validator.equals(key, ticket.getKey())) {
161                                    result = null;
162                            }
163                    }
164    
165                    if (result == null) {
166                            StringBundler query = new StringBundler(3);
167    
168                            query.append(_SQL_SELECT_TICKET_WHERE);
169    
170                            boolean bindKey = false;
171    
172                            if (key == null) {
173                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
174                            }
175                            else if (key.equals(StringPool.BLANK)) {
176                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
177                            }
178                            else {
179                                    bindKey = true;
180    
181                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
182                            }
183    
184                            String sql = query.toString();
185    
186                            Session session = null;
187    
188                            try {
189                                    session = openSession();
190    
191                                    Query q = session.createQuery(sql);
192    
193                                    QueryPos qPos = QueryPos.getInstance(q);
194    
195                                    if (bindKey) {
196                                            qPos.add(key);
197                                    }
198    
199                                    List<Ticket> list = q.list();
200    
201                                    if (list.isEmpty()) {
202                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
203                                                    finderArgs, list);
204                                    }
205                                    else {
206                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
207                                                    _log.warn(
208                                                            "TicketPersistenceImpl.fetchByKey(String, boolean) with parameters (" +
209                                                            StringUtil.merge(finderArgs) +
210                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
211                                            }
212    
213                                            Ticket ticket = list.get(0);
214    
215                                            result = ticket;
216    
217                                            cacheResult(ticket);
218    
219                                            if ((ticket.getKey() == null) ||
220                                                            !ticket.getKey().equals(key)) {
221                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
222                                                            finderArgs, ticket);
223                                            }
224                                    }
225                            }
226                            catch (Exception e) {
227                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY,
228                                            finderArgs);
229    
230                                    throw processException(e);
231                            }
232                            finally {
233                                    closeSession(session);
234                            }
235                    }
236    
237                    if (result instanceof List<?>) {
238                            return null;
239                    }
240                    else {
241                            return (Ticket)result;
242                    }
243            }
244    
245            /**
246             * Removes the ticket where key = &#63; from the database.
247             *
248             * @param key the key
249             * @return the ticket that was removed
250             */
251            @Override
252            public Ticket removeByKey(String key) throws NoSuchTicketException {
253                    Ticket ticket = findByKey(key);
254    
255                    return remove(ticket);
256            }
257    
258            /**
259             * Returns the number of tickets where key = &#63;.
260             *
261             * @param key the key
262             * @return the number of matching tickets
263             */
264            @Override
265            public int countByKey(String key) {
266                    FinderPath finderPath = FINDER_PATH_COUNT_BY_KEY;
267    
268                    Object[] finderArgs = new Object[] { key };
269    
270                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
271                                    this);
272    
273                    if (count == null) {
274                            StringBundler query = new StringBundler(2);
275    
276                            query.append(_SQL_COUNT_TICKET_WHERE);
277    
278                            boolean bindKey = false;
279    
280                            if (key == null) {
281                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
282                            }
283                            else if (key.equals(StringPool.BLANK)) {
284                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
285                            }
286                            else {
287                                    bindKey = true;
288    
289                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
290                            }
291    
292                            String sql = query.toString();
293    
294                            Session session = null;
295    
296                            try {
297                                    session = openSession();
298    
299                                    Query q = session.createQuery(sql);
300    
301                                    QueryPos qPos = QueryPos.getInstance(q);
302    
303                                    if (bindKey) {
304                                            qPos.add(key);
305                                    }
306    
307                                    count = (Long)q.uniqueResult();
308    
309                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
310                            }
311                            catch (Exception e) {
312                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
313    
314                                    throw processException(e);
315                            }
316                            finally {
317                                    closeSession(session);
318                            }
319                    }
320    
321                    return count.intValue();
322            }
323    
324            private static final String _FINDER_COLUMN_KEY_KEY_1 = "ticket.key IS NULL";
325            private static final String _FINDER_COLUMN_KEY_KEY_2 = "ticket.key = ?";
326            private static final String _FINDER_COLUMN_KEY_KEY_3 = "(ticket.key IS NULL OR ticket.key = '')";
327    
328            public TicketPersistenceImpl() {
329                    setModelClass(Ticket.class);
330            }
331    
332            /**
333             * Caches the ticket in the entity cache if it is enabled.
334             *
335             * @param ticket the ticket
336             */
337            @Override
338            public void cacheResult(Ticket ticket) {
339                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
340                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
341    
342                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY,
343                            new Object[] { ticket.getKey() }, ticket);
344    
345                    ticket.resetOriginalValues();
346            }
347    
348            /**
349             * Caches the tickets in the entity cache if it is enabled.
350             *
351             * @param tickets the tickets
352             */
353            @Override
354            public void cacheResult(List<Ticket> tickets) {
355                    for (Ticket ticket : tickets) {
356                            if (EntityCacheUtil.getResult(
357                                                    TicketModelImpl.ENTITY_CACHE_ENABLED, TicketImpl.class,
358                                                    ticket.getPrimaryKey()) == null) {
359                                    cacheResult(ticket);
360                            }
361                            else {
362                                    ticket.resetOriginalValues();
363                            }
364                    }
365            }
366    
367            /**
368             * Clears the cache for all tickets.
369             *
370             * <p>
371             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
372             * </p>
373             */
374            @Override
375            public void clearCache() {
376                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
377                            CacheRegistryUtil.clear(TicketImpl.class.getName());
378                    }
379    
380                    EntityCacheUtil.clearCache(TicketImpl.class);
381    
382                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
383                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
384                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
385            }
386    
387            /**
388             * Clears the cache for the ticket.
389             *
390             * <p>
391             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
392             * </p>
393             */
394            @Override
395            public void clearCache(Ticket ticket) {
396                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
397                            TicketImpl.class, ticket.getPrimaryKey());
398    
399                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
400                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
401    
402                    clearUniqueFindersCache(ticket);
403            }
404    
405            @Override
406            public void clearCache(List<Ticket> tickets) {
407                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
408                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
409    
410                    for (Ticket ticket : tickets) {
411                            EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
412                                    TicketImpl.class, ticket.getPrimaryKey());
413    
414                            clearUniqueFindersCache(ticket);
415                    }
416            }
417    
418            protected void cacheUniqueFindersCache(Ticket ticket) {
419                    if (ticket.isNew()) {
420                            Object[] args = new Object[] { ticket.getKey() };
421    
422                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, args,
423                                    Long.valueOf(1));
424                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY, args, ticket);
425                    }
426                    else {
427                            TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
428    
429                            if ((ticketModelImpl.getColumnBitmask() &
430                                            FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
431                                    Object[] args = new Object[] { ticket.getKey() };
432    
433                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, args,
434                                            Long.valueOf(1));
435                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_KEY, args, ticket);
436                            }
437                    }
438            }
439    
440            protected void clearUniqueFindersCache(Ticket ticket) {
441                    TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
442    
443                    Object[] args = new Object[] { ticket.getKey() };
444    
445                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
446                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
447    
448                    if ((ticketModelImpl.getColumnBitmask() &
449                                    FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
450                            args = new Object[] { ticketModelImpl.getOriginalKey() };
451    
452                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
453                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
454                    }
455            }
456    
457            /**
458             * Creates a new ticket with the primary key. Does not add the ticket to the database.
459             *
460             * @param ticketId the primary key for the new ticket
461             * @return the new ticket
462             */
463            @Override
464            public Ticket create(long ticketId) {
465                    Ticket ticket = new TicketImpl();
466    
467                    ticket.setNew(true);
468                    ticket.setPrimaryKey(ticketId);
469    
470                    return ticket;
471            }
472    
473            /**
474             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
475             *
476             * @param ticketId the primary key of the ticket
477             * @return the ticket that was removed
478             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
479             */
480            @Override
481            public Ticket remove(long ticketId) throws NoSuchTicketException {
482                    return remove((Serializable)ticketId);
483            }
484    
485            /**
486             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
487             *
488             * @param primaryKey the primary key of the ticket
489             * @return the ticket that was removed
490             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
491             */
492            @Override
493            public Ticket remove(Serializable primaryKey) throws NoSuchTicketException {
494                    Session session = null;
495    
496                    try {
497                            session = openSession();
498    
499                            Ticket ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
500    
501                            if (ticket == null) {
502                                    if (_log.isWarnEnabled()) {
503                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
504                                    }
505    
506                                    throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
507                                            primaryKey);
508                            }
509    
510                            return remove(ticket);
511                    }
512                    catch (NoSuchTicketException nsee) {
513                            throw nsee;
514                    }
515                    catch (Exception e) {
516                            throw processException(e);
517                    }
518                    finally {
519                            closeSession(session);
520                    }
521            }
522    
523            @Override
524            protected Ticket removeImpl(Ticket ticket) {
525                    ticket = toUnwrappedModel(ticket);
526    
527                    Session session = null;
528    
529                    try {
530                            session = openSession();
531    
532                            if (!session.contains(ticket)) {
533                                    ticket = (Ticket)session.get(TicketImpl.class,
534                                                    ticket.getPrimaryKeyObj());
535                            }
536    
537                            if (ticket != null) {
538                                    session.delete(ticket);
539                            }
540                    }
541                    catch (Exception e) {
542                            throw processException(e);
543                    }
544                    finally {
545                            closeSession(session);
546                    }
547    
548                    if (ticket != null) {
549                            clearCache(ticket);
550                    }
551    
552                    return ticket;
553            }
554    
555            @Override
556            public Ticket updateImpl(com.liferay.portal.model.Ticket ticket) {
557                    ticket = toUnwrappedModel(ticket);
558    
559                    boolean isNew = ticket.isNew();
560    
561                    Session session = null;
562    
563                    try {
564                            session = openSession();
565    
566                            if (ticket.isNew()) {
567                                    session.save(ticket);
568    
569                                    ticket.setNew(false);
570                            }
571                            else {
572                                    session.merge(ticket);
573                            }
574                    }
575                    catch (Exception e) {
576                            throw processException(e);
577                    }
578                    finally {
579                            closeSession(session);
580                    }
581    
582                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
583    
584                    if (isNew || !TicketModelImpl.COLUMN_BITMASK_ENABLED) {
585                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
586                    }
587    
588                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
589                            TicketImpl.class, ticket.getPrimaryKey(), ticket, false);
590    
591                    clearUniqueFindersCache(ticket);
592                    cacheUniqueFindersCache(ticket);
593    
594                    ticket.resetOriginalValues();
595    
596                    return ticket;
597            }
598    
599            protected Ticket toUnwrappedModel(Ticket ticket) {
600                    if (ticket instanceof TicketImpl) {
601                            return ticket;
602                    }
603    
604                    TicketImpl ticketImpl = new TicketImpl();
605    
606                    ticketImpl.setNew(ticket.isNew());
607                    ticketImpl.setPrimaryKey(ticket.getPrimaryKey());
608    
609                    ticketImpl.setMvccVersion(ticket.getMvccVersion());
610                    ticketImpl.setTicketId(ticket.getTicketId());
611                    ticketImpl.setCompanyId(ticket.getCompanyId());
612                    ticketImpl.setCreateDate(ticket.getCreateDate());
613                    ticketImpl.setClassNameId(ticket.getClassNameId());
614                    ticketImpl.setClassPK(ticket.getClassPK());
615                    ticketImpl.setKey(ticket.getKey());
616                    ticketImpl.setType(ticket.getType());
617                    ticketImpl.setExtraInfo(ticket.getExtraInfo());
618                    ticketImpl.setExpirationDate(ticket.getExpirationDate());
619    
620                    return ticketImpl;
621            }
622    
623            /**
624             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
625             *
626             * @param primaryKey the primary key of the ticket
627             * @return the ticket
628             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
629             */
630            @Override
631            public Ticket findByPrimaryKey(Serializable primaryKey)
632                    throws NoSuchTicketException {
633                    Ticket ticket = fetchByPrimaryKey(primaryKey);
634    
635                    if (ticket == null) {
636                            if (_log.isWarnEnabled()) {
637                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
638                            }
639    
640                            throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
641                                    primaryKey);
642                    }
643    
644                    return ticket;
645            }
646    
647            /**
648             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.NoSuchTicketException} if it could not be found.
649             *
650             * @param ticketId the primary key of the ticket
651             * @return the ticket
652             * @throws com.liferay.portal.NoSuchTicketException if a ticket with the primary key could not be found
653             */
654            @Override
655            public Ticket findByPrimaryKey(long ticketId) throws NoSuchTicketException {
656                    return findByPrimaryKey((Serializable)ticketId);
657            }
658    
659            /**
660             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
661             *
662             * @param primaryKey the primary key of the ticket
663             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
664             */
665            @Override
666            public Ticket fetchByPrimaryKey(Serializable primaryKey) {
667                    Ticket ticket = (Ticket)EntityCacheUtil.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
668                                    TicketImpl.class, primaryKey);
669    
670                    if (ticket == _nullTicket) {
671                            return null;
672                    }
673    
674                    if (ticket == null) {
675                            Session session = null;
676    
677                            try {
678                                    session = openSession();
679    
680                                    ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
681    
682                                    if (ticket != null) {
683                                            cacheResult(ticket);
684                                    }
685                                    else {
686                                            EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
687                                                    TicketImpl.class, primaryKey, _nullTicket);
688                                    }
689                            }
690                            catch (Exception e) {
691                                    EntityCacheUtil.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
692                                            TicketImpl.class, primaryKey);
693    
694                                    throw processException(e);
695                            }
696                            finally {
697                                    closeSession(session);
698                            }
699                    }
700    
701                    return ticket;
702            }
703    
704            /**
705             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
706             *
707             * @param ticketId the primary key of the ticket
708             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
709             */
710            @Override
711            public Ticket fetchByPrimaryKey(long ticketId) {
712                    return fetchByPrimaryKey((Serializable)ticketId);
713            }
714    
715            @Override
716            public Map<Serializable, Ticket> fetchByPrimaryKeys(
717                    Set<Serializable> primaryKeys) {
718                    if (primaryKeys.isEmpty()) {
719                            return Collections.emptyMap();
720                    }
721    
722                    Map<Serializable, Ticket> map = new HashMap<Serializable, Ticket>();
723    
724                    if (primaryKeys.size() == 1) {
725                            Iterator<Serializable> iterator = primaryKeys.iterator();
726    
727                            Serializable primaryKey = iterator.next();
728    
729                            Ticket ticket = fetchByPrimaryKey(primaryKey);
730    
731                            if (ticket != null) {
732                                    map.put(primaryKey, ticket);
733                            }
734    
735                            return map;
736                    }
737    
738                    Set<Serializable> uncachedPrimaryKeys = null;
739    
740                    for (Serializable primaryKey : primaryKeys) {
741                            Ticket ticket = (Ticket)EntityCacheUtil.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
742                                            TicketImpl.class, primaryKey);
743    
744                            if (ticket == null) {
745                                    if (uncachedPrimaryKeys == null) {
746                                            uncachedPrimaryKeys = new HashSet<Serializable>();
747                                    }
748    
749                                    uncachedPrimaryKeys.add(primaryKey);
750                            }
751                            else {
752                                    map.put(primaryKey, ticket);
753                            }
754                    }
755    
756                    if (uncachedPrimaryKeys == null) {
757                            return map;
758                    }
759    
760                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
761                                    1);
762    
763                    query.append(_SQL_SELECT_TICKET_WHERE_PKS_IN);
764    
765                    for (Serializable primaryKey : uncachedPrimaryKeys) {
766                            query.append(String.valueOf(primaryKey));
767    
768                            query.append(StringPool.COMMA);
769                    }
770    
771                    query.setIndex(query.index() - 1);
772    
773                    query.append(StringPool.CLOSE_PARENTHESIS);
774    
775                    String sql = query.toString();
776    
777                    Session session = null;
778    
779                    try {
780                            session = openSession();
781    
782                            Query q = session.createQuery(sql);
783    
784                            for (Ticket ticket : (List<Ticket>)q.list()) {
785                                    map.put(ticket.getPrimaryKeyObj(), ticket);
786    
787                                    cacheResult(ticket);
788    
789                                    uncachedPrimaryKeys.remove(ticket.getPrimaryKeyObj());
790                            }
791    
792                            for (Serializable primaryKey : uncachedPrimaryKeys) {
793                                    EntityCacheUtil.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
794                                            TicketImpl.class, primaryKey, _nullTicket);
795                            }
796                    }
797                    catch (Exception e) {
798                            throw processException(e);
799                    }
800                    finally {
801                            closeSession(session);
802                    }
803    
804                    return map;
805            }
806    
807            /**
808             * Returns all the tickets.
809             *
810             * @return the tickets
811             */
812            @Override
813            public List<Ticket> findAll() {
814                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
815            }
816    
817            /**
818             * Returns a range of all the tickets.
819             *
820             * <p>
821             * 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.
822             * </p>
823             *
824             * @param start the lower bound of the range of tickets
825             * @param end the upper bound of the range of tickets (not inclusive)
826             * @return the range of tickets
827             */
828            @Override
829            public List<Ticket> findAll(int start, int end) {
830                    return findAll(start, end, null);
831            }
832    
833            /**
834             * Returns an ordered range of all the tickets.
835             *
836             * <p>
837             * 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.
838             * </p>
839             *
840             * @param start the lower bound of the range of tickets
841             * @param end the upper bound of the range of tickets (not inclusive)
842             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
843             * @return the ordered range of tickets
844             */
845            @Override
846            public List<Ticket> findAll(int start, int end,
847                    OrderByComparator<Ticket> orderByComparator) {
848                    boolean pagination = true;
849                    FinderPath finderPath = null;
850                    Object[] finderArgs = null;
851    
852                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
853                                    (orderByComparator == null)) {
854                            pagination = false;
855                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
856                            finderArgs = FINDER_ARGS_EMPTY;
857                    }
858                    else {
859                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
860                            finderArgs = new Object[] { start, end, orderByComparator };
861                    }
862    
863                    List<Ticket> list = (List<Ticket>)FinderCacheUtil.getResult(finderPath,
864                                    finderArgs, this);
865    
866                    if (list == null) {
867                            StringBundler query = null;
868                            String sql = null;
869    
870                            if (orderByComparator != null) {
871                                    query = new StringBundler(2 +
872                                                    (orderByComparator.getOrderByFields().length * 3));
873    
874                                    query.append(_SQL_SELECT_TICKET);
875    
876                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
877                                            orderByComparator);
878    
879                                    sql = query.toString();
880                            }
881                            else {
882                                    sql = _SQL_SELECT_TICKET;
883    
884                                    if (pagination) {
885                                            sql = sql.concat(TicketModelImpl.ORDER_BY_JPQL);
886                                    }
887                            }
888    
889                            Session session = null;
890    
891                            try {
892                                    session = openSession();
893    
894                                    Query q = session.createQuery(sql);
895    
896                                    if (!pagination) {
897                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
898                                                            end, false);
899    
900                                            Collections.sort(list);
901    
902                                            list = Collections.unmodifiableList(list);
903                                    }
904                                    else {
905                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
906                                                            end);
907                                    }
908    
909                                    cacheResult(list);
910    
911                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
912                            }
913                            catch (Exception e) {
914                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
915    
916                                    throw processException(e);
917                            }
918                            finally {
919                                    closeSession(session);
920                            }
921                    }
922    
923                    return list;
924            }
925    
926            /**
927             * Removes all the tickets from the database.
928             *
929             */
930            @Override
931            public void removeAll() {
932                    for (Ticket ticket : findAll()) {
933                            remove(ticket);
934                    }
935            }
936    
937            /**
938             * Returns the number of tickets.
939             *
940             * @return the number of tickets
941             */
942            @Override
943            public int countAll() {
944                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
945                                    FINDER_ARGS_EMPTY, this);
946    
947                    if (count == null) {
948                            Session session = null;
949    
950                            try {
951                                    session = openSession();
952    
953                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
954    
955                                    count = (Long)q.uniqueResult();
956    
957                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
958                                            FINDER_ARGS_EMPTY, count);
959                            }
960                            catch (Exception e) {
961                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
962                                            FINDER_ARGS_EMPTY);
963    
964                                    throw processException(e);
965                            }
966                            finally {
967                                    closeSession(session);
968                            }
969                    }
970    
971                    return count.intValue();
972            }
973    
974            @Override
975            protected Set<String> getBadColumnNames() {
976                    return _badColumnNames;
977            }
978    
979            /**
980             * Initializes the ticket persistence.
981             */
982            public void afterPropertiesSet() {
983            }
984    
985            public void destroy() {
986                    EntityCacheUtil.removeCache(TicketImpl.class.getName());
987                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
988                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
989                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
990            }
991    
992            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
993            private static final String _SQL_SELECT_TICKET_WHERE_PKS_IN = "SELECT ticket FROM Ticket ticket WHERE ticketId IN (";
994            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
995            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
996            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
997            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
998            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
999            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
1000            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1001            private static final Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
1002            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1003                                    "key", "type"
1004                            });
1005            private static final Ticket _nullTicket = new TicketImpl() {
1006                            @Override
1007                            public Object clone() {
1008                                    return this;
1009                            }
1010    
1011                            @Override
1012                            public CacheModel<Ticket> toCacheModel() {
1013                                    return _nullTicketCacheModel;
1014                            }
1015                    };
1016    
1017            private static final CacheModel<Ticket> _nullTicketCacheModel = new NullCacheModel();
1018    
1019            private static class NullCacheModel implements CacheModel<Ticket>,
1020                    MVCCModel {
1021                    @Override
1022                    public long getMvccVersion() {
1023                            return -1;
1024                    }
1025    
1026                    @Override
1027                    public void setMvccVersion(long mvccVersion) {
1028                    }
1029    
1030                    @Override
1031                    public Ticket toEntityModel() {
1032                            return _nullTicket;
1033                    }
1034            }
1035    }