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