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