001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.exception.NoSuchTicketException;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.model.CacheModel;
033    import com.liferay.portal.kernel.model.MVCCModel;
034    import com.liferay.portal.kernel.model.Ticket;
035    import com.liferay.portal.kernel.service.persistence.CompanyProvider;
036    import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
037    import com.liferay.portal.kernel.service.persistence.TicketPersistence;
038    import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
039    import com.liferay.portal.kernel.util.OrderByComparator;
040    import com.liferay.portal.kernel.util.SetUtil;
041    import com.liferay.portal.kernel.util.StringBundler;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.StringUtil;
044    import com.liferay.portal.model.impl.TicketImpl;
045    import com.liferay.portal.model.impl.TicketModelImpl;
046    
047    import java.io.Serializable;
048    
049    import java.util.Collections;
050    import java.util.HashMap;
051    import java.util.HashSet;
052    import java.util.Iterator;
053    import java.util.List;
054    import java.util.Map;
055    import java.util.Objects;
056    import java.util.Set;
057    
058    /**
059     * The persistence implementation for the ticket service.
060     *
061     * <p>
062     * Caching information and settings can be found in <code>portal.properties</code>
063     * </p>
064     *
065     * @author Brian Wing Shun Chan
066     * @see TicketPersistence
067     * @see com.liferay.portal.kernel.service.persistence.TicketUtil
068     * @generated
069     */
070    @ProviderType
071    public class TicketPersistenceImpl extends BasePersistenceImpl<Ticket>
072            implements TicketPersistence {
073            /*
074             * NOTE FOR DEVELOPERS:
075             *
076             * 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.
077             */
078            public static final String FINDER_CLASS_NAME_ENTITY = TicketImpl.class.getName();
079            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
080                    ".List1";
081            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
082                    ".List2";
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
084                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
087                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
089            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
090                            TicketModelImpl.FINDER_CACHE_ENABLED, Long.class,
091                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
092            public static final FinderPath FINDER_PATH_FETCH_BY_KEY = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
093                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
094                            FINDER_CLASS_NAME_ENTITY, "fetchByKey",
095                            new String[] { String.class.getName() },
096                            TicketModelImpl.KEY_COLUMN_BITMASK);
097            public static final FinderPath FINDER_PATH_COUNT_BY_KEY = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
098                            TicketModelImpl.FINDER_CACHE_ENABLED, Long.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByKey",
100                            new String[] { String.class.getName() });
101    
102            /**
103             * Returns the ticket where key = &#63; or throws a {@link NoSuchTicketException} if it could not be found.
104             *
105             * @param key the key
106             * @return the matching ticket
107             * @throws NoSuchTicketException if a matching ticket could not be found
108             */
109            @Override
110            public Ticket findByKey(String key) throws NoSuchTicketException {
111                    Ticket ticket = fetchByKey(key);
112    
113                    if (ticket == null) {
114                            StringBundler msg = new StringBundler(4);
115    
116                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
117    
118                            msg.append("key=");
119                            msg.append(key);
120    
121                            msg.append(StringPool.CLOSE_CURLY_BRACE);
122    
123                            if (_log.isDebugEnabled()) {
124                                    _log.debug(msg.toString());
125                            }
126    
127                            throw new NoSuchTicketException(msg.toString());
128                    }
129    
130                    return ticket;
131            }
132    
133            /**
134             * Returns the ticket where key = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
135             *
136             * @param key the key
137             * @return the matching ticket, or <code>null</code> if a matching ticket could not be found
138             */
139            @Override
140            public Ticket fetchByKey(String key) {
141                    return fetchByKey(key, true);
142            }
143    
144            /**
145             * Returns the ticket where key = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
146             *
147             * @param key the key
148             * @param retrieveFromCache whether to retrieve from the finder cache
149             * @return the matching ticket, or <code>null</code> if a matching ticket could not be found
150             */
151            @Override
152            public Ticket fetchByKey(String key, boolean retrieveFromCache) {
153                    Object[] finderArgs = new Object[] { key };
154    
155                    Object result = null;
156    
157                    if (retrieveFromCache) {
158                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_KEY,
159                                            finderArgs, this);
160                    }
161    
162                    if (result instanceof Ticket) {
163                            Ticket ticket = (Ticket)result;
164    
165                            if (!Objects.equals(key, ticket.getKey())) {
166                                    result = null;
167                            }
168                    }
169    
170                    if (result == null) {
171                            StringBundler query = new StringBundler(3);
172    
173                            query.append(_SQL_SELECT_TICKET_WHERE);
174    
175                            boolean bindKey = false;
176    
177                            if (key == null) {
178                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
179                            }
180                            else if (key.equals(StringPool.BLANK)) {
181                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
182                            }
183                            else {
184                                    bindKey = true;
185    
186                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
187                            }
188    
189                            String sql = query.toString();
190    
191                            Session session = null;
192    
193                            try {
194                                    session = openSession();
195    
196                                    Query q = session.createQuery(sql);
197    
198                                    QueryPos qPos = QueryPos.getInstance(q);
199    
200                                    if (bindKey) {
201                                            qPos.add(key);
202                                    }
203    
204                                    List<Ticket> list = q.list();
205    
206                                    if (list.isEmpty()) {
207                                            finderCache.putResult(FINDER_PATH_FETCH_BY_KEY, finderArgs,
208                                                    list);
209                                    }
210                                    else {
211                                            if ((list.size() > 1) && _log.isWarnEnabled()) {
212                                                    _log.warn(
213                                                            "TicketPersistenceImpl.fetchByKey(String, boolean) with parameters (" +
214                                                            StringUtil.merge(finderArgs) +
215                                                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
216                                            }
217    
218                                            Ticket ticket = list.get(0);
219    
220                                            result = ticket;
221    
222                                            cacheResult(ticket);
223    
224                                            if ((ticket.getKey() == null) ||
225                                                            !ticket.getKey().equals(key)) {
226                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_KEY,
227                                                            finderArgs, ticket);
228                                            }
229                                    }
230                            }
231                            catch (Exception e) {
232                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_KEY, finderArgs);
233    
234                                    throw processException(e);
235                            }
236                            finally {
237                                    closeSession(session);
238                            }
239                    }
240    
241                    if (result instanceof List<?>) {
242                            return null;
243                    }
244                    else {
245                            return (Ticket)result;
246                    }
247            }
248    
249            /**
250             * Removes the ticket where key = &#63; from the database.
251             *
252             * @param key the key
253             * @return the ticket that was removed
254             */
255            @Override
256            public Ticket removeByKey(String key) throws NoSuchTicketException {
257                    Ticket ticket = findByKey(key);
258    
259                    return remove(ticket);
260            }
261    
262            /**
263             * Returns the number of tickets where key = &#63;.
264             *
265             * @param key the key
266             * @return the number of matching tickets
267             */
268            @Override
269            public int countByKey(String key) {
270                    FinderPath finderPath = FINDER_PATH_COUNT_BY_KEY;
271    
272                    Object[] finderArgs = new Object[] { key };
273    
274                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
275    
276                    if (count == null) {
277                            StringBundler query = new StringBundler(2);
278    
279                            query.append(_SQL_COUNT_TICKET_WHERE);
280    
281                            boolean bindKey = false;
282    
283                            if (key == null) {
284                                    query.append(_FINDER_COLUMN_KEY_KEY_1);
285                            }
286                            else if (key.equals(StringPool.BLANK)) {
287                                    query.append(_FINDER_COLUMN_KEY_KEY_3);
288                            }
289                            else {
290                                    bindKey = true;
291    
292                                    query.append(_FINDER_COLUMN_KEY_KEY_2);
293                            }
294    
295                            String sql = query.toString();
296    
297                            Session session = null;
298    
299                            try {
300                                    session = openSession();
301    
302                                    Query q = session.createQuery(sql);
303    
304                                    QueryPos qPos = QueryPos.getInstance(q);
305    
306                                    if (bindKey) {
307                                            qPos.add(key);
308                                    }
309    
310                                    count = (Long)q.uniqueResult();
311    
312                                    finderCache.putResult(finderPath, finderArgs, count);
313                            }
314                            catch (Exception e) {
315                                    finderCache.removeResult(finderPath, finderArgs);
316    
317                                    throw processException(e);
318                            }
319                            finally {
320                                    closeSession(session);
321                            }
322                    }
323    
324                    return count.intValue();
325            }
326    
327            private static final String _FINDER_COLUMN_KEY_KEY_1 = "ticket.key IS NULL";
328            private static final String _FINDER_COLUMN_KEY_KEY_2 = "ticket.key = ?";
329            private static final String _FINDER_COLUMN_KEY_KEY_3 = "(ticket.key IS NULL OR ticket.key = '')";
330            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
331                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
332                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_T",
333                            new String[] {
334                                    Long.class.getName(), Long.class.getName(),
335                                    Integer.class.getName(),
336                                    
337                            Integer.class.getName(), Integer.class.getName(),
338                                    OrderByComparator.class.getName()
339                            });
340            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
341                            TicketModelImpl.FINDER_CACHE_ENABLED, TicketImpl.class,
342                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_T",
343                            new String[] {
344                                    Long.class.getName(), Long.class.getName(),
345                                    Integer.class.getName()
346                            },
347                            TicketModelImpl.CLASSNAMEID_COLUMN_BITMASK |
348                            TicketModelImpl.CLASSPK_COLUMN_BITMASK |
349                            TicketModelImpl.TYPE_COLUMN_BITMASK);
350            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_T = new FinderPath(TicketModelImpl.ENTITY_CACHE_ENABLED,
351                            TicketModelImpl.FINDER_CACHE_ENABLED, Long.class,
352                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_T",
353                            new String[] {
354                                    Long.class.getName(), Long.class.getName(),
355                                    Integer.class.getName()
356                            });
357    
358            /**
359             * Returns all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
360             *
361             * @param classNameId the class name ID
362             * @param classPK the class p k
363             * @param type the type
364             * @return the matching tickets
365             */
366            @Override
367            public List<Ticket> findByC_C_T(long classNameId, long classPK, int type) {
368                    return findByC_C_T(classNameId, classPK, type, QueryUtil.ALL_POS,
369                            QueryUtil.ALL_POS, null);
370            }
371    
372            /**
373             * Returns a range of all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
374             *
375             * <p>
376             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
377             * </p>
378             *
379             * @param classNameId the class name ID
380             * @param classPK the class p k
381             * @param type the type
382             * @param start the lower bound of the range of tickets
383             * @param end the upper bound of the range of tickets (not inclusive)
384             * @return the range of matching tickets
385             */
386            @Override
387            public List<Ticket> findByC_C_T(long classNameId, long classPK, int type,
388                    int start, int end) {
389                    return findByC_C_T(classNameId, classPK, type, start, end, null);
390            }
391    
392            /**
393             * Returns an ordered range of all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
394             *
395             * <p>
396             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
397             * </p>
398             *
399             * @param classNameId the class name ID
400             * @param classPK the class p k
401             * @param type the type
402             * @param start the lower bound of the range of tickets
403             * @param end the upper bound of the range of tickets (not inclusive)
404             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
405             * @return the ordered range of matching tickets
406             */
407            @Override
408            public List<Ticket> findByC_C_T(long classNameId, long classPK, int type,
409                    int start, int end, OrderByComparator<Ticket> orderByComparator) {
410                    return findByC_C_T(classNameId, classPK, type, start, end,
411                            orderByComparator, true);
412            }
413    
414            /**
415             * Returns an ordered range of all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
416             *
417             * <p>
418             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
419             * </p>
420             *
421             * @param classNameId the class name ID
422             * @param classPK the class p k
423             * @param type the type
424             * @param start the lower bound of the range of tickets
425             * @param end the upper bound of the range of tickets (not inclusive)
426             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
427             * @param retrieveFromCache whether to retrieve from the finder cache
428             * @return the ordered range of matching tickets
429             */
430            @Override
431            public List<Ticket> findByC_C_T(long classNameId, long classPK, int type,
432                    int start, int end, OrderByComparator<Ticket> orderByComparator,
433                    boolean retrieveFromCache) {
434                    boolean pagination = true;
435                    FinderPath finderPath = null;
436                    Object[] finderArgs = null;
437    
438                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
439                                    (orderByComparator == null)) {
440                            pagination = false;
441                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T;
442                            finderArgs = new Object[] { classNameId, classPK, type };
443                    }
444                    else {
445                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_T;
446                            finderArgs = new Object[] {
447                                            classNameId, classPK, type,
448                                            
449                                            start, end, orderByComparator
450                                    };
451                    }
452    
453                    List<Ticket> list = null;
454    
455                    if (retrieveFromCache) {
456                            list = (List<Ticket>)finderCache.getResult(finderPath, finderArgs,
457                                            this);
458    
459                            if ((list != null) && !list.isEmpty()) {
460                                    for (Ticket ticket : list) {
461                                            if ((classNameId != ticket.getClassNameId()) ||
462                                                            (classPK != ticket.getClassPK()) ||
463                                                            (type != ticket.getType())) {
464                                                    list = null;
465    
466                                                    break;
467                                            }
468                                    }
469                            }
470                    }
471    
472                    if (list == null) {
473                            StringBundler query = null;
474    
475                            if (orderByComparator != null) {
476                                    query = new StringBundler(5 +
477                                                    (orderByComparator.getOrderByFields().length * 2));
478                            }
479                            else {
480                                    query = new StringBundler(5);
481                            }
482    
483                            query.append(_SQL_SELECT_TICKET_WHERE);
484    
485                            query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
486    
487                            query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
488    
489                            query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
490    
491                            if (orderByComparator != null) {
492                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
493                                            orderByComparator);
494                            }
495                            else
496                             if (pagination) {
497                                    query.append(TicketModelImpl.ORDER_BY_JPQL);
498                            }
499    
500                            String sql = query.toString();
501    
502                            Session session = null;
503    
504                            try {
505                                    session = openSession();
506    
507                                    Query q = session.createQuery(sql);
508    
509                                    QueryPos qPos = QueryPos.getInstance(q);
510    
511                                    qPos.add(classNameId);
512    
513                                    qPos.add(classPK);
514    
515                                    qPos.add(type);
516    
517                                    if (!pagination) {
518                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
519                                                            end, false);
520    
521                                            Collections.sort(list);
522    
523                                            list = Collections.unmodifiableList(list);
524                                    }
525                                    else {
526                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
527                                                            end);
528                                    }
529    
530                                    cacheResult(list);
531    
532                                    finderCache.putResult(finderPath, finderArgs, list);
533                            }
534                            catch (Exception e) {
535                                    finderCache.removeResult(finderPath, finderArgs);
536    
537                                    throw processException(e);
538                            }
539                            finally {
540                                    closeSession(session);
541                            }
542                    }
543    
544                    return list;
545            }
546    
547            /**
548             * Returns the first ticket in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
549             *
550             * @param classNameId the class name ID
551             * @param classPK the class p k
552             * @param type the type
553             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
554             * @return the first matching ticket
555             * @throws NoSuchTicketException if a matching ticket could not be found
556             */
557            @Override
558            public Ticket findByC_C_T_First(long classNameId, long classPK, int type,
559                    OrderByComparator<Ticket> orderByComparator)
560                    throws NoSuchTicketException {
561                    Ticket ticket = fetchByC_C_T_First(classNameId, classPK, type,
562                                    orderByComparator);
563    
564                    if (ticket != null) {
565                            return ticket;
566                    }
567    
568                    StringBundler msg = new StringBundler(8);
569    
570                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
571    
572                    msg.append("classNameId=");
573                    msg.append(classNameId);
574    
575                    msg.append(", classPK=");
576                    msg.append(classPK);
577    
578                    msg.append(", type=");
579                    msg.append(type);
580    
581                    msg.append(StringPool.CLOSE_CURLY_BRACE);
582    
583                    throw new NoSuchTicketException(msg.toString());
584            }
585    
586            /**
587             * Returns the first ticket in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
588             *
589             * @param classNameId the class name ID
590             * @param classPK the class p k
591             * @param type the type
592             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
593             * @return the first matching ticket, or <code>null</code> if a matching ticket could not be found
594             */
595            @Override
596            public Ticket fetchByC_C_T_First(long classNameId, long classPK, int type,
597                    OrderByComparator<Ticket> orderByComparator) {
598                    List<Ticket> list = findByC_C_T(classNameId, classPK, type, 0, 1,
599                                    orderByComparator);
600    
601                    if (!list.isEmpty()) {
602                            return list.get(0);
603                    }
604    
605                    return null;
606            }
607    
608            /**
609             * Returns the last ticket in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
610             *
611             * @param classNameId the class name ID
612             * @param classPK the class p k
613             * @param type the type
614             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
615             * @return the last matching ticket
616             * @throws NoSuchTicketException if a matching ticket could not be found
617             */
618            @Override
619            public Ticket findByC_C_T_Last(long classNameId, long classPK, int type,
620                    OrderByComparator<Ticket> orderByComparator)
621                    throws NoSuchTicketException {
622                    Ticket ticket = fetchByC_C_T_Last(classNameId, classPK, type,
623                                    orderByComparator);
624    
625                    if (ticket != null) {
626                            return ticket;
627                    }
628    
629                    StringBundler msg = new StringBundler(8);
630    
631                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
632    
633                    msg.append("classNameId=");
634                    msg.append(classNameId);
635    
636                    msg.append(", classPK=");
637                    msg.append(classPK);
638    
639                    msg.append(", type=");
640                    msg.append(type);
641    
642                    msg.append(StringPool.CLOSE_CURLY_BRACE);
643    
644                    throw new NoSuchTicketException(msg.toString());
645            }
646    
647            /**
648             * Returns the last ticket in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
649             *
650             * @param classNameId the class name ID
651             * @param classPK the class p k
652             * @param type the type
653             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
654             * @return the last matching ticket, or <code>null</code> if a matching ticket could not be found
655             */
656            @Override
657            public Ticket fetchByC_C_T_Last(long classNameId, long classPK, int type,
658                    OrderByComparator<Ticket> orderByComparator) {
659                    int count = countByC_C_T(classNameId, classPK, type);
660    
661                    if (count == 0) {
662                            return null;
663                    }
664    
665                    List<Ticket> list = findByC_C_T(classNameId, classPK, type, count - 1,
666                                    count, orderByComparator);
667    
668                    if (!list.isEmpty()) {
669                            return list.get(0);
670                    }
671    
672                    return null;
673            }
674    
675            /**
676             * Returns the tickets before and after the current ticket in the ordered set where classNameId = &#63; and classPK = &#63; and type = &#63;.
677             *
678             * @param ticketId the primary key of the current ticket
679             * @param classNameId the class name ID
680             * @param classPK the class p k
681             * @param type the type
682             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
683             * @return the previous, current, and next ticket
684             * @throws NoSuchTicketException if a ticket with the primary key could not be found
685             */
686            @Override
687            public Ticket[] findByC_C_T_PrevAndNext(long ticketId, long classNameId,
688                    long classPK, int type, OrderByComparator<Ticket> orderByComparator)
689                    throws NoSuchTicketException {
690                    Ticket ticket = findByPrimaryKey(ticketId);
691    
692                    Session session = null;
693    
694                    try {
695                            session = openSession();
696    
697                            Ticket[] array = new TicketImpl[3];
698    
699                            array[0] = getByC_C_T_PrevAndNext(session, ticket, classNameId,
700                                            classPK, type, orderByComparator, true);
701    
702                            array[1] = ticket;
703    
704                            array[2] = getByC_C_T_PrevAndNext(session, ticket, classNameId,
705                                            classPK, type, orderByComparator, false);
706    
707                            return array;
708                    }
709                    catch (Exception e) {
710                            throw processException(e);
711                    }
712                    finally {
713                            closeSession(session);
714                    }
715            }
716    
717            protected Ticket getByC_C_T_PrevAndNext(Session session, Ticket ticket,
718                    long classNameId, long classPK, int type,
719                    OrderByComparator<Ticket> orderByComparator, boolean previous) {
720                    StringBundler query = null;
721    
722                    if (orderByComparator != null) {
723                            query = new StringBundler(6 +
724                                            (orderByComparator.getOrderByConditionFields().length * 3) +
725                                            (orderByComparator.getOrderByFields().length * 3));
726                    }
727                    else {
728                            query = new StringBundler(5);
729                    }
730    
731                    query.append(_SQL_SELECT_TICKET_WHERE);
732    
733                    query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
734    
735                    query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
736    
737                    query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
738    
739                    if (orderByComparator != null) {
740                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
741    
742                            if (orderByConditionFields.length > 0) {
743                                    query.append(WHERE_AND);
744                            }
745    
746                            for (int i = 0; i < orderByConditionFields.length; i++) {
747                                    query.append(_ORDER_BY_ENTITY_ALIAS);
748                                    query.append(orderByConditionFields[i]);
749    
750                                    if ((i + 1) < orderByConditionFields.length) {
751                                            if (orderByComparator.isAscending() ^ previous) {
752                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
753                                            }
754                                            else {
755                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
756                                            }
757                                    }
758                                    else {
759                                            if (orderByComparator.isAscending() ^ previous) {
760                                                    query.append(WHERE_GREATER_THAN);
761                                            }
762                                            else {
763                                                    query.append(WHERE_LESSER_THAN);
764                                            }
765                                    }
766                            }
767    
768                            query.append(ORDER_BY_CLAUSE);
769    
770                            String[] orderByFields = orderByComparator.getOrderByFields();
771    
772                            for (int i = 0; i < orderByFields.length; i++) {
773                                    query.append(_ORDER_BY_ENTITY_ALIAS);
774                                    query.append(orderByFields[i]);
775    
776                                    if ((i + 1) < orderByFields.length) {
777                                            if (orderByComparator.isAscending() ^ previous) {
778                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
779                                            }
780                                            else {
781                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
782                                            }
783                                    }
784                                    else {
785                                            if (orderByComparator.isAscending() ^ previous) {
786                                                    query.append(ORDER_BY_ASC);
787                                            }
788                                            else {
789                                                    query.append(ORDER_BY_DESC);
790                                            }
791                                    }
792                            }
793                    }
794                    else {
795                            query.append(TicketModelImpl.ORDER_BY_JPQL);
796                    }
797    
798                    String sql = query.toString();
799    
800                    Query q = session.createQuery(sql);
801    
802                    q.setFirstResult(0);
803                    q.setMaxResults(2);
804    
805                    QueryPos qPos = QueryPos.getInstance(q);
806    
807                    qPos.add(classNameId);
808    
809                    qPos.add(classPK);
810    
811                    qPos.add(type);
812    
813                    if (orderByComparator != null) {
814                            Object[] values = orderByComparator.getOrderByConditionValues(ticket);
815    
816                            for (Object value : values) {
817                                    qPos.add(value);
818                            }
819                    }
820    
821                    List<Ticket> list = q.list();
822    
823                    if (list.size() == 2) {
824                            return list.get(1);
825                    }
826                    else {
827                            return null;
828                    }
829            }
830    
831            /**
832             * Removes all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63; from the database.
833             *
834             * @param classNameId the class name ID
835             * @param classPK the class p k
836             * @param type the type
837             */
838            @Override
839            public void removeByC_C_T(long classNameId, long classPK, int type) {
840                    for (Ticket ticket : findByC_C_T(classNameId, classPK, type,
841                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
842                            remove(ticket);
843                    }
844            }
845    
846            /**
847             * Returns the number of tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
848             *
849             * @param classNameId the class name ID
850             * @param classPK the class p k
851             * @param type the type
852             * @return the number of matching tickets
853             */
854            @Override
855            public int countByC_C_T(long classNameId, long classPK, int type) {
856                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_T;
857    
858                    Object[] finderArgs = new Object[] { classNameId, classPK, type };
859    
860                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
861    
862                    if (count == null) {
863                            StringBundler query = new StringBundler(4);
864    
865                            query.append(_SQL_COUNT_TICKET_WHERE);
866    
867                            query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
868    
869                            query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
870    
871                            query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
872    
873                            String sql = query.toString();
874    
875                            Session session = null;
876    
877                            try {
878                                    session = openSession();
879    
880                                    Query q = session.createQuery(sql);
881    
882                                    QueryPos qPos = QueryPos.getInstance(q);
883    
884                                    qPos.add(classNameId);
885    
886                                    qPos.add(classPK);
887    
888                                    qPos.add(type);
889    
890                                    count = (Long)q.uniqueResult();
891    
892                                    finderCache.putResult(finderPath, finderArgs, count);
893                            }
894                            catch (Exception e) {
895                                    finderCache.removeResult(finderPath, finderArgs);
896    
897                                    throw processException(e);
898                            }
899                            finally {
900                                    closeSession(session);
901                            }
902                    }
903    
904                    return count.intValue();
905            }
906    
907            private static final String _FINDER_COLUMN_C_C_T_CLASSNAMEID_2 = "ticket.classNameId = ? AND ";
908            private static final String _FINDER_COLUMN_C_C_T_CLASSPK_2 = "ticket.classPK = ? AND ";
909            private static final String _FINDER_COLUMN_C_C_T_TYPE_2 = "ticket.type = ?";
910    
911            public TicketPersistenceImpl() {
912                    setModelClass(Ticket.class);
913            }
914    
915            /**
916             * Caches the ticket in the entity cache if it is enabled.
917             *
918             * @param ticket the ticket
919             */
920            @Override
921            public void cacheResult(Ticket ticket) {
922                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
923                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
924    
925                    finderCache.putResult(FINDER_PATH_FETCH_BY_KEY,
926                            new Object[] { ticket.getKey() }, ticket);
927    
928                    ticket.resetOriginalValues();
929            }
930    
931            /**
932             * Caches the tickets in the entity cache if it is enabled.
933             *
934             * @param tickets the tickets
935             */
936            @Override
937            public void cacheResult(List<Ticket> tickets) {
938                    for (Ticket ticket : tickets) {
939                            if (entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
940                                                    TicketImpl.class, ticket.getPrimaryKey()) == null) {
941                                    cacheResult(ticket);
942                            }
943                            else {
944                                    ticket.resetOriginalValues();
945                            }
946                    }
947            }
948    
949            /**
950             * Clears the cache for all tickets.
951             *
952             * <p>
953             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
954             * </p>
955             */
956            @Override
957            public void clearCache() {
958                    entityCache.clearCache(TicketImpl.class);
959    
960                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
961                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
962                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
963            }
964    
965            /**
966             * Clears the cache for the ticket.
967             *
968             * <p>
969             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
970             * </p>
971             */
972            @Override
973            public void clearCache(Ticket ticket) {
974                    entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
975                            TicketImpl.class, ticket.getPrimaryKey());
976    
977                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
978                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
979    
980                    clearUniqueFindersCache((TicketModelImpl)ticket);
981            }
982    
983            @Override
984            public void clearCache(List<Ticket> tickets) {
985                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
986                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
987    
988                    for (Ticket ticket : tickets) {
989                            entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
990                                    TicketImpl.class, ticket.getPrimaryKey());
991    
992                            clearUniqueFindersCache((TicketModelImpl)ticket);
993                    }
994            }
995    
996            protected void cacheUniqueFindersCache(TicketModelImpl ticketModelImpl,
997                    boolean isNew) {
998                    if (isNew) {
999                            Object[] args = new Object[] { ticketModelImpl.getKey() };
1000    
1001                            finderCache.putResult(FINDER_PATH_COUNT_BY_KEY, args,
1002                                    Long.valueOf(1));
1003                            finderCache.putResult(FINDER_PATH_FETCH_BY_KEY, args,
1004                                    ticketModelImpl);
1005                    }
1006                    else {
1007                            if ((ticketModelImpl.getColumnBitmask() &
1008                                            FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
1009                                    Object[] args = new Object[] { ticketModelImpl.getKey() };
1010    
1011                                    finderCache.putResult(FINDER_PATH_COUNT_BY_KEY, args,
1012                                            Long.valueOf(1));
1013                                    finderCache.putResult(FINDER_PATH_FETCH_BY_KEY, args,
1014                                            ticketModelImpl);
1015                            }
1016                    }
1017            }
1018    
1019            protected void clearUniqueFindersCache(TicketModelImpl ticketModelImpl) {
1020                    Object[] args = new Object[] { ticketModelImpl.getKey() };
1021    
1022                    finderCache.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
1023                    finderCache.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
1024    
1025                    if ((ticketModelImpl.getColumnBitmask() &
1026                                    FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
1027                            args = new Object[] { ticketModelImpl.getOriginalKey() };
1028    
1029                            finderCache.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
1030                            finderCache.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
1031                    }
1032            }
1033    
1034            /**
1035             * Creates a new ticket with the primary key. Does not add the ticket to the database.
1036             *
1037             * @param ticketId the primary key for the new ticket
1038             * @return the new ticket
1039             */
1040            @Override
1041            public Ticket create(long ticketId) {
1042                    Ticket ticket = new TicketImpl();
1043    
1044                    ticket.setNew(true);
1045                    ticket.setPrimaryKey(ticketId);
1046    
1047                    ticket.setCompanyId(companyProvider.getCompanyId());
1048    
1049                    return ticket;
1050            }
1051    
1052            /**
1053             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
1054             *
1055             * @param ticketId the primary key of the ticket
1056             * @return the ticket that was removed
1057             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1058             */
1059            @Override
1060            public Ticket remove(long ticketId) throws NoSuchTicketException {
1061                    return remove((Serializable)ticketId);
1062            }
1063    
1064            /**
1065             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
1066             *
1067             * @param primaryKey the primary key of the ticket
1068             * @return the ticket that was removed
1069             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1070             */
1071            @Override
1072            public Ticket remove(Serializable primaryKey) throws NoSuchTicketException {
1073                    Session session = null;
1074    
1075                    try {
1076                            session = openSession();
1077    
1078                            Ticket ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
1079    
1080                            if (ticket == null) {
1081                                    if (_log.isDebugEnabled()) {
1082                                            _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1083                                    }
1084    
1085                                    throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1086                                            primaryKey);
1087                            }
1088    
1089                            return remove(ticket);
1090                    }
1091                    catch (NoSuchTicketException nsee) {
1092                            throw nsee;
1093                    }
1094                    catch (Exception e) {
1095                            throw processException(e);
1096                    }
1097                    finally {
1098                            closeSession(session);
1099                    }
1100            }
1101    
1102            @Override
1103            protected Ticket removeImpl(Ticket ticket) {
1104                    ticket = toUnwrappedModel(ticket);
1105    
1106                    Session session = null;
1107    
1108                    try {
1109                            session = openSession();
1110    
1111                            if (!session.contains(ticket)) {
1112                                    ticket = (Ticket)session.get(TicketImpl.class,
1113                                                    ticket.getPrimaryKeyObj());
1114                            }
1115    
1116                            if (ticket != null) {
1117                                    session.delete(ticket);
1118                            }
1119                    }
1120                    catch (Exception e) {
1121                            throw processException(e);
1122                    }
1123                    finally {
1124                            closeSession(session);
1125                    }
1126    
1127                    if (ticket != null) {
1128                            clearCache(ticket);
1129                    }
1130    
1131                    return ticket;
1132            }
1133    
1134            @Override
1135            public Ticket updateImpl(Ticket ticket) {
1136                    ticket = toUnwrappedModel(ticket);
1137    
1138                    boolean isNew = ticket.isNew();
1139    
1140                    TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
1141    
1142                    Session session = null;
1143    
1144                    try {
1145                            session = openSession();
1146    
1147                            if (ticket.isNew()) {
1148                                    session.save(ticket);
1149    
1150                                    ticket.setNew(false);
1151                            }
1152                            else {
1153                                    ticket = (Ticket)session.merge(ticket);
1154                            }
1155                    }
1156                    catch (Exception e) {
1157                            throw processException(e);
1158                    }
1159                    finally {
1160                            closeSession(session);
1161                    }
1162    
1163                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1164    
1165                    if (isNew || !TicketModelImpl.COLUMN_BITMASK_ENABLED) {
1166                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1167                    }
1168    
1169                    else {
1170                            if ((ticketModelImpl.getColumnBitmask() &
1171                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T.getColumnBitmask()) != 0) {
1172                                    Object[] args = new Object[] {
1173                                                    ticketModelImpl.getOriginalClassNameId(),
1174                                                    ticketModelImpl.getOriginalClassPK(),
1175                                                    ticketModelImpl.getOriginalType()
1176                                            };
1177    
1178                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
1179                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
1180                                            args);
1181    
1182                                    args = new Object[] {
1183                                                    ticketModelImpl.getClassNameId(),
1184                                                    ticketModelImpl.getClassPK(), ticketModelImpl.getType()
1185                                            };
1186    
1187                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
1188                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
1189                                            args);
1190                            }
1191                    }
1192    
1193                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1194                            TicketImpl.class, ticket.getPrimaryKey(), ticket, false);
1195    
1196                    clearUniqueFindersCache(ticketModelImpl);
1197                    cacheUniqueFindersCache(ticketModelImpl, isNew);
1198    
1199                    ticket.resetOriginalValues();
1200    
1201                    return ticket;
1202            }
1203    
1204            protected Ticket toUnwrappedModel(Ticket ticket) {
1205                    if (ticket instanceof TicketImpl) {
1206                            return ticket;
1207                    }
1208    
1209                    TicketImpl ticketImpl = new TicketImpl();
1210    
1211                    ticketImpl.setNew(ticket.isNew());
1212                    ticketImpl.setPrimaryKey(ticket.getPrimaryKey());
1213    
1214                    ticketImpl.setMvccVersion(ticket.getMvccVersion());
1215                    ticketImpl.setTicketId(ticket.getTicketId());
1216                    ticketImpl.setCompanyId(ticket.getCompanyId());
1217                    ticketImpl.setCreateDate(ticket.getCreateDate());
1218                    ticketImpl.setClassNameId(ticket.getClassNameId());
1219                    ticketImpl.setClassPK(ticket.getClassPK());
1220                    ticketImpl.setKey(ticket.getKey());
1221                    ticketImpl.setType(ticket.getType());
1222                    ticketImpl.setExtraInfo(ticket.getExtraInfo());
1223                    ticketImpl.setExpirationDate(ticket.getExpirationDate());
1224    
1225                    return ticketImpl;
1226            }
1227    
1228            /**
1229             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
1230             *
1231             * @param primaryKey the primary key of the ticket
1232             * @return the ticket
1233             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1234             */
1235            @Override
1236            public Ticket findByPrimaryKey(Serializable primaryKey)
1237                    throws NoSuchTicketException {
1238                    Ticket ticket = fetchByPrimaryKey(primaryKey);
1239    
1240                    if (ticket == null) {
1241                            if (_log.isDebugEnabled()) {
1242                                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1243                            }
1244    
1245                            throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1246                                    primaryKey);
1247                    }
1248    
1249                    return ticket;
1250            }
1251    
1252            /**
1253             * Returns the ticket with the primary key or throws a {@link NoSuchTicketException} if it could not be found.
1254             *
1255             * @param ticketId the primary key of the ticket
1256             * @return the ticket
1257             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1258             */
1259            @Override
1260            public Ticket findByPrimaryKey(long ticketId) throws NoSuchTicketException {
1261                    return findByPrimaryKey((Serializable)ticketId);
1262            }
1263    
1264            /**
1265             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
1266             *
1267             * @param primaryKey the primary key of the ticket
1268             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
1269             */
1270            @Override
1271            public Ticket fetchByPrimaryKey(Serializable primaryKey) {
1272                    Ticket ticket = (Ticket)entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1273                                    TicketImpl.class, primaryKey);
1274    
1275                    if (ticket == _nullTicket) {
1276                            return null;
1277                    }
1278    
1279                    if (ticket == null) {
1280                            Session session = null;
1281    
1282                            try {
1283                                    session = openSession();
1284    
1285                                    ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
1286    
1287                                    if (ticket != null) {
1288                                            cacheResult(ticket);
1289                                    }
1290                                    else {
1291                                            entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1292                                                    TicketImpl.class, primaryKey, _nullTicket);
1293                                    }
1294                            }
1295                            catch (Exception e) {
1296                                    entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1297                                            TicketImpl.class, primaryKey);
1298    
1299                                    throw processException(e);
1300                            }
1301                            finally {
1302                                    closeSession(session);
1303                            }
1304                    }
1305    
1306                    return ticket;
1307            }
1308    
1309            /**
1310             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
1311             *
1312             * @param ticketId the primary key of the ticket
1313             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
1314             */
1315            @Override
1316            public Ticket fetchByPrimaryKey(long ticketId) {
1317                    return fetchByPrimaryKey((Serializable)ticketId);
1318            }
1319    
1320            @Override
1321            public Map<Serializable, Ticket> fetchByPrimaryKeys(
1322                    Set<Serializable> primaryKeys) {
1323                    if (primaryKeys.isEmpty()) {
1324                            return Collections.emptyMap();
1325                    }
1326    
1327                    Map<Serializable, Ticket> map = new HashMap<Serializable, Ticket>();
1328    
1329                    if (primaryKeys.size() == 1) {
1330                            Iterator<Serializable> iterator = primaryKeys.iterator();
1331    
1332                            Serializable primaryKey = iterator.next();
1333    
1334                            Ticket ticket = fetchByPrimaryKey(primaryKey);
1335    
1336                            if (ticket != null) {
1337                                    map.put(primaryKey, ticket);
1338                            }
1339    
1340                            return map;
1341                    }
1342    
1343                    Set<Serializable> uncachedPrimaryKeys = null;
1344    
1345                    for (Serializable primaryKey : primaryKeys) {
1346                            Ticket ticket = (Ticket)entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1347                                            TicketImpl.class, primaryKey);
1348    
1349                            if (ticket == null) {
1350                                    if (uncachedPrimaryKeys == null) {
1351                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1352                                    }
1353    
1354                                    uncachedPrimaryKeys.add(primaryKey);
1355                            }
1356                            else {
1357                                    map.put(primaryKey, ticket);
1358                            }
1359                    }
1360    
1361                    if (uncachedPrimaryKeys == null) {
1362                            return map;
1363                    }
1364    
1365                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1366                                    1);
1367    
1368                    query.append(_SQL_SELECT_TICKET_WHERE_PKS_IN);
1369    
1370                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1371                            query.append(String.valueOf(primaryKey));
1372    
1373                            query.append(StringPool.COMMA);
1374                    }
1375    
1376                    query.setIndex(query.index() - 1);
1377    
1378                    query.append(StringPool.CLOSE_PARENTHESIS);
1379    
1380                    String sql = query.toString();
1381    
1382                    Session session = null;
1383    
1384                    try {
1385                            session = openSession();
1386    
1387                            Query q = session.createQuery(sql);
1388    
1389                            for (Ticket ticket : (List<Ticket>)q.list()) {
1390                                    map.put(ticket.getPrimaryKeyObj(), ticket);
1391    
1392                                    cacheResult(ticket);
1393    
1394                                    uncachedPrimaryKeys.remove(ticket.getPrimaryKeyObj());
1395                            }
1396    
1397                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1398                                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1399                                            TicketImpl.class, primaryKey, _nullTicket);
1400                            }
1401                    }
1402                    catch (Exception e) {
1403                            throw processException(e);
1404                    }
1405                    finally {
1406                            closeSession(session);
1407                    }
1408    
1409                    return map;
1410            }
1411    
1412            /**
1413             * Returns all the tickets.
1414             *
1415             * @return the tickets
1416             */
1417            @Override
1418            public List<Ticket> findAll() {
1419                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1420            }
1421    
1422            /**
1423             * Returns a range of all the tickets.
1424             *
1425             * <p>
1426             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1427             * </p>
1428             *
1429             * @param start the lower bound of the range of tickets
1430             * @param end the upper bound of the range of tickets (not inclusive)
1431             * @return the range of tickets
1432             */
1433            @Override
1434            public List<Ticket> findAll(int start, int end) {
1435                    return findAll(start, end, null);
1436            }
1437    
1438            /**
1439             * Returns an ordered range of all the tickets.
1440             *
1441             * <p>
1442             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1443             * </p>
1444             *
1445             * @param start the lower bound of the range of tickets
1446             * @param end the upper bound of the range of tickets (not inclusive)
1447             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1448             * @return the ordered range of tickets
1449             */
1450            @Override
1451            public List<Ticket> findAll(int start, int end,
1452                    OrderByComparator<Ticket> orderByComparator) {
1453                    return findAll(start, end, orderByComparator, true);
1454            }
1455    
1456            /**
1457             * Returns an ordered range of all the tickets.
1458             *
1459             * <p>
1460             * 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 QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link TicketModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1461             * </p>
1462             *
1463             * @param start the lower bound of the range of tickets
1464             * @param end the upper bound of the range of tickets (not inclusive)
1465             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1466             * @param retrieveFromCache whether to retrieve from the finder cache
1467             * @return the ordered range of tickets
1468             */
1469            @Override
1470            public List<Ticket> findAll(int start, int end,
1471                    OrderByComparator<Ticket> orderByComparator, boolean retrieveFromCache) {
1472                    boolean pagination = true;
1473                    FinderPath finderPath = null;
1474                    Object[] finderArgs = null;
1475    
1476                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1477                                    (orderByComparator == null)) {
1478                            pagination = false;
1479                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1480                            finderArgs = FINDER_ARGS_EMPTY;
1481                    }
1482                    else {
1483                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1484                            finderArgs = new Object[] { start, end, orderByComparator };
1485                    }
1486    
1487                    List<Ticket> list = null;
1488    
1489                    if (retrieveFromCache) {
1490                            list = (List<Ticket>)finderCache.getResult(finderPath, finderArgs,
1491                                            this);
1492                    }
1493    
1494                    if (list == null) {
1495                            StringBundler query = null;
1496                            String sql = null;
1497    
1498                            if (orderByComparator != null) {
1499                                    query = new StringBundler(2 +
1500                                                    (orderByComparator.getOrderByFields().length * 2));
1501    
1502                                    query.append(_SQL_SELECT_TICKET);
1503    
1504                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1505                                            orderByComparator);
1506    
1507                                    sql = query.toString();
1508                            }
1509                            else {
1510                                    sql = _SQL_SELECT_TICKET;
1511    
1512                                    if (pagination) {
1513                                            sql = sql.concat(TicketModelImpl.ORDER_BY_JPQL);
1514                                    }
1515                            }
1516    
1517                            Session session = null;
1518    
1519                            try {
1520                                    session = openSession();
1521    
1522                                    Query q = session.createQuery(sql);
1523    
1524                                    if (!pagination) {
1525                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
1526                                                            end, false);
1527    
1528                                            Collections.sort(list);
1529    
1530                                            list = Collections.unmodifiableList(list);
1531                                    }
1532                                    else {
1533                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
1534                                                            end);
1535                                    }
1536    
1537                                    cacheResult(list);
1538    
1539                                    finderCache.putResult(finderPath, finderArgs, list);
1540                            }
1541                            catch (Exception e) {
1542                                    finderCache.removeResult(finderPath, finderArgs);
1543    
1544                                    throw processException(e);
1545                            }
1546                            finally {
1547                                    closeSession(session);
1548                            }
1549                    }
1550    
1551                    return list;
1552            }
1553    
1554            /**
1555             * Removes all the tickets from the database.
1556             *
1557             */
1558            @Override
1559            public void removeAll() {
1560                    for (Ticket ticket : findAll()) {
1561                            remove(ticket);
1562                    }
1563            }
1564    
1565            /**
1566             * Returns the number of tickets.
1567             *
1568             * @return the number of tickets
1569             */
1570            @Override
1571            public int countAll() {
1572                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
1573                                    FINDER_ARGS_EMPTY, this);
1574    
1575                    if (count == null) {
1576                            Session session = null;
1577    
1578                            try {
1579                                    session = openSession();
1580    
1581                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
1582    
1583                                    count = (Long)q.uniqueResult();
1584    
1585                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
1586                                            count);
1587                            }
1588                            catch (Exception e) {
1589                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
1590                                            FINDER_ARGS_EMPTY);
1591    
1592                                    throw processException(e);
1593                            }
1594                            finally {
1595                                    closeSession(session);
1596                            }
1597                    }
1598    
1599                    return count.intValue();
1600            }
1601    
1602            @Override
1603            public Set<String> getBadColumnNames() {
1604                    return _badColumnNames;
1605            }
1606    
1607            @Override
1608            protected Map<String, Integer> getTableColumnsMap() {
1609                    return TicketModelImpl.TABLE_COLUMNS_MAP;
1610            }
1611    
1612            /**
1613             * Initializes the ticket persistence.
1614             */
1615            public void afterPropertiesSet() {
1616            }
1617    
1618            public void destroy() {
1619                    entityCache.removeCache(TicketImpl.class.getName());
1620                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1621                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1622                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1623            }
1624    
1625            @BeanReference(type = CompanyProviderWrapper.class)
1626            protected CompanyProvider companyProvider;
1627            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1628            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1629            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
1630            private static final String _SQL_SELECT_TICKET_WHERE_PKS_IN = "SELECT ticket FROM Ticket ticket WHERE ticketId IN (";
1631            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
1632            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
1633            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
1634            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
1635            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
1636            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
1637            private static final Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
1638            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1639                                    "key", "type"
1640                            });
1641            private static final Ticket _nullTicket = new TicketImpl() {
1642                            @Override
1643                            public Object clone() {
1644                                    return this;
1645                            }
1646    
1647                            @Override
1648                            public CacheModel<Ticket> toCacheModel() {
1649                                    return _nullTicketCacheModel;
1650                            }
1651                    };
1652    
1653            private static final CacheModel<Ticket> _nullTicketCacheModel = new NullCacheModel();
1654    
1655            private static class NullCacheModel implements CacheModel<Ticket>,
1656                    MVCCModel {
1657                    @Override
1658                    public long getMvccVersion() {
1659                            return -1;
1660                    }
1661    
1662                    @Override
1663                    public void setMvccVersion(long mvccVersion) {
1664                    }
1665    
1666                    @Override
1667                    public Ticket toEntityModel() {
1668                            return _nullTicket;
1669                    }
1670            }
1671    }