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