001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.getOriginalKey() });
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                            Session session = null;
453    
454                            try {
455                                    session = openSession();
456    
457                                    StringBundler query = new StringBundler(3);
458    
459                                    query.append(_SQL_SELECT_TICKET_WHERE);
460    
461                                    if (key == null) {
462                                            query.append(_FINDER_COLUMN_KEY_KEY_1);
463                                    }
464                                    else {
465                                            if (key.equals(StringPool.BLANK)) {
466                                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
467                                            }
468                                            else {
469                                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
470                                            }
471                                    }
472    
473                                    query.append(TicketModelImpl.ORDER_BY_JPQL);
474    
475                                    String sql = query.toString();
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.putResult(FINDER_PATH_FETCH_BY_KEY,
515                                                    finderArgs, new ArrayList<Ticket>());
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                            Session session = null;
582    
583                            try {
584                                    session = openSession();
585    
586                                    StringBundler query = null;
587                                    String sql = null;
588    
589                                    if (orderByComparator != null) {
590                                            query = new StringBundler(2 +
591                                                            (orderByComparator.getOrderByFields().length * 3));
592    
593                                            query.append(_SQL_SELECT_TICKET);
594    
595                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
596                                                    orderByComparator);
597    
598                                            sql = query.toString();
599                                    }
600                                    else {
601                                            sql = _SQL_SELECT_TICKET.concat(TicketModelImpl.ORDER_BY_JPQL);
602                                    }
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                                            list = new ArrayList<Ticket>();
623                                    }
624    
625                                    cacheResult(list);
626    
627                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
628    
629                                    closeSession(session);
630                            }
631                    }
632    
633                    return list;
634            }
635    
636            /**
637             * Removes the ticket where key = &#63; from the database.
638             *
639             * @param key the key to search with
640             * @throws SystemException if a system exception occurred
641             */
642            public void removeByKey(String key)
643                    throws NoSuchTicketException, SystemException {
644                    Ticket ticket = findByKey(key);
645    
646                    remove(ticket);
647            }
648    
649            /**
650             * Removes all the tickets from the database.
651             *
652             * @throws SystemException if a system exception occurred
653             */
654            public void removeAll() throws SystemException {
655                    for (Ticket ticket : findAll()) {
656                            remove(ticket);
657                    }
658            }
659    
660            /**
661             * Counts all the tickets where key = &#63;.
662             *
663             * @param key the key to search with
664             * @return the number of matching tickets
665             * @throws SystemException if a system exception occurred
666             */
667            public int countByKey(String key) throws SystemException {
668                    Object[] finderArgs = new Object[] { key };
669    
670                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_KEY,
671                                    finderArgs, this);
672    
673                    if (count == null) {
674                            Session session = null;
675    
676                            try {
677                                    session = openSession();
678    
679                                    StringBundler query = new StringBundler(2);
680    
681                                    query.append(_SQL_COUNT_TICKET_WHERE);
682    
683                                    if (key == null) {
684                                            query.append(_FINDER_COLUMN_KEY_KEY_1);
685                                    }
686                                    else {
687                                            if (key.equals(StringPool.BLANK)) {
688                                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
689                                            }
690                                            else {
691                                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
692                                            }
693                                    }
694    
695                                    String sql = query.toString();
696    
697                                    Query q = session.createQuery(sql);
698    
699                                    QueryPos qPos = QueryPos.getInstance(q);
700    
701                                    if (key != null) {
702                                            qPos.add(key);
703                                    }
704    
705                                    count = (Long)q.uniqueResult();
706                            }
707                            catch (Exception e) {
708                                    throw processException(e);
709                            }
710                            finally {
711                                    if (count == null) {
712                                            count = Long.valueOf(0);
713                                    }
714    
715                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KEY, finderArgs,
716                                            count);
717    
718                                    closeSession(session);
719                            }
720                    }
721    
722                    return count.intValue();
723            }
724    
725            /**
726             * Counts all the tickets.
727             *
728             * @return the number of tickets
729             * @throws SystemException if a system exception occurred
730             */
731            public int countAll() throws SystemException {
732                    Object[] finderArgs = new Object[0];
733    
734                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
735                                    finderArgs, this);
736    
737                    if (count == null) {
738                            Session session = null;
739    
740                            try {
741                                    session = openSession();
742    
743                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
744    
745                                    count = (Long)q.uniqueResult();
746                            }
747                            catch (Exception e) {
748                                    throw processException(e);
749                            }
750                            finally {
751                                    if (count == null) {
752                                            count = Long.valueOf(0);
753                                    }
754    
755                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
756                                            count);
757    
758                                    closeSession(session);
759                            }
760                    }
761    
762                    return count.intValue();
763            }
764    
765            /**
766             * Initializes the ticket persistence.
767             */
768            public void afterPropertiesSet() {
769                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
770                                            com.liferay.portal.util.PropsUtil.get(
771                                                    "value.object.listener.com.liferay.portal.model.Ticket")));
772    
773                    if (listenerClassNames.length > 0) {
774                            try {
775                                    List<ModelListener<Ticket>> listenersList = new ArrayList<ModelListener<Ticket>>();
776    
777                                    for (String listenerClassName : listenerClassNames) {
778                                            listenersList.add((ModelListener<Ticket>)InstanceFactory.newInstance(
779                                                            listenerClassName));
780                                    }
781    
782                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
783                            }
784                            catch (Exception e) {
785                                    _log.error(e);
786                            }
787                    }
788            }
789    
790            public void destroy() {
791                    EntityCacheUtil.removeCache(TicketImpl.class.getName());
792                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
793                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
794            }
795    
796            @BeanReference(type = AccountPersistence.class)
797            protected AccountPersistence accountPersistence;
798            @BeanReference(type = AddressPersistence.class)
799            protected AddressPersistence addressPersistence;
800            @BeanReference(type = BrowserTrackerPersistence.class)
801            protected BrowserTrackerPersistence browserTrackerPersistence;
802            @BeanReference(type = ClassNamePersistence.class)
803            protected ClassNamePersistence classNamePersistence;
804            @BeanReference(type = ClusterGroupPersistence.class)
805            protected ClusterGroupPersistence clusterGroupPersistence;
806            @BeanReference(type = CompanyPersistence.class)
807            protected CompanyPersistence companyPersistence;
808            @BeanReference(type = ContactPersistence.class)
809            protected ContactPersistence contactPersistence;
810            @BeanReference(type = CountryPersistence.class)
811            protected CountryPersistence countryPersistence;
812            @BeanReference(type = EmailAddressPersistence.class)
813            protected EmailAddressPersistence emailAddressPersistence;
814            @BeanReference(type = GroupPersistence.class)
815            protected GroupPersistence groupPersistence;
816            @BeanReference(type = ImagePersistence.class)
817            protected ImagePersistence imagePersistence;
818            @BeanReference(type = LayoutPersistence.class)
819            protected LayoutPersistence layoutPersistence;
820            @BeanReference(type = LayoutPrototypePersistence.class)
821            protected LayoutPrototypePersistence layoutPrototypePersistence;
822            @BeanReference(type = LayoutSetPersistence.class)
823            protected LayoutSetPersistence layoutSetPersistence;
824            @BeanReference(type = LayoutSetPrototypePersistence.class)
825            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
826            @BeanReference(type = ListTypePersistence.class)
827            protected ListTypePersistence listTypePersistence;
828            @BeanReference(type = LockPersistence.class)
829            protected LockPersistence lockPersistence;
830            @BeanReference(type = MembershipRequestPersistence.class)
831            protected MembershipRequestPersistence membershipRequestPersistence;
832            @BeanReference(type = OrganizationPersistence.class)
833            protected OrganizationPersistence organizationPersistence;
834            @BeanReference(type = OrgGroupPermissionPersistence.class)
835            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
836            @BeanReference(type = OrgGroupRolePersistence.class)
837            protected OrgGroupRolePersistence orgGroupRolePersistence;
838            @BeanReference(type = OrgLaborPersistence.class)
839            protected OrgLaborPersistence orgLaborPersistence;
840            @BeanReference(type = PasswordPolicyPersistence.class)
841            protected PasswordPolicyPersistence passwordPolicyPersistence;
842            @BeanReference(type = PasswordPolicyRelPersistence.class)
843            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
844            @BeanReference(type = PasswordTrackerPersistence.class)
845            protected PasswordTrackerPersistence passwordTrackerPersistence;
846            @BeanReference(type = PermissionPersistence.class)
847            protected PermissionPersistence permissionPersistence;
848            @BeanReference(type = PhonePersistence.class)
849            protected PhonePersistence phonePersistence;
850            @BeanReference(type = PluginSettingPersistence.class)
851            protected PluginSettingPersistence pluginSettingPersistence;
852            @BeanReference(type = PortletPersistence.class)
853            protected PortletPersistence portletPersistence;
854            @BeanReference(type = PortletItemPersistence.class)
855            protected PortletItemPersistence portletItemPersistence;
856            @BeanReference(type = PortletPreferencesPersistence.class)
857            protected PortletPreferencesPersistence portletPreferencesPersistence;
858            @BeanReference(type = RegionPersistence.class)
859            protected RegionPersistence regionPersistence;
860            @BeanReference(type = ReleasePersistence.class)
861            protected ReleasePersistence releasePersistence;
862            @BeanReference(type = ResourcePersistence.class)
863            protected ResourcePersistence resourcePersistence;
864            @BeanReference(type = ResourceActionPersistence.class)
865            protected ResourceActionPersistence resourceActionPersistence;
866            @BeanReference(type = ResourceCodePersistence.class)
867            protected ResourceCodePersistence resourceCodePersistence;
868            @BeanReference(type = ResourcePermissionPersistence.class)
869            protected ResourcePermissionPersistence resourcePermissionPersistence;
870            @BeanReference(type = RolePersistence.class)
871            protected RolePersistence rolePersistence;
872            @BeanReference(type = ServiceComponentPersistence.class)
873            protected ServiceComponentPersistence serviceComponentPersistence;
874            @BeanReference(type = ShardPersistence.class)
875            protected ShardPersistence shardPersistence;
876            @BeanReference(type = SubscriptionPersistence.class)
877            protected SubscriptionPersistence subscriptionPersistence;
878            @BeanReference(type = TicketPersistence.class)
879            protected TicketPersistence ticketPersistence;
880            @BeanReference(type = TeamPersistence.class)
881            protected TeamPersistence teamPersistence;
882            @BeanReference(type = UserPersistence.class)
883            protected UserPersistence userPersistence;
884            @BeanReference(type = UserGroupPersistence.class)
885            protected UserGroupPersistence userGroupPersistence;
886            @BeanReference(type = UserGroupGroupRolePersistence.class)
887            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
888            @BeanReference(type = UserGroupRolePersistence.class)
889            protected UserGroupRolePersistence userGroupRolePersistence;
890            @BeanReference(type = UserIdMapperPersistence.class)
891            protected UserIdMapperPersistence userIdMapperPersistence;
892            @BeanReference(type = UserTrackerPersistence.class)
893            protected UserTrackerPersistence userTrackerPersistence;
894            @BeanReference(type = UserTrackerPathPersistence.class)
895            protected UserTrackerPathPersistence userTrackerPathPersistence;
896            @BeanReference(type = WebDAVPropsPersistence.class)
897            protected WebDAVPropsPersistence webDAVPropsPersistence;
898            @BeanReference(type = WebsitePersistence.class)
899            protected WebsitePersistence websitePersistence;
900            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
901            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
902            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
903            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
904            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
905            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
906            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
907            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
908            private static final String _FINDER_COLUMN_KEY_KEY_1 = "ticket.key IS NULL";
909            private static final String _FINDER_COLUMN_KEY_KEY_2 = "ticket.key = ?";
910            private static final String _FINDER_COLUMN_KEY_KEY_3 = "(ticket.key IS NULL OR ticket.key = ?)";
911            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
912            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
913            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
914            private static Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
915    }