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