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