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 * 2));
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.getOrderByConditionFields().length * 3) +
724                                            (orderByComparator.getOrderByFields().length * 3));
725                    }
726                    else {
727                            query = new StringBundler(5);
728                    }
729    
730                    query.append(_SQL_SELECT_TICKET_WHERE);
731    
732                    query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
733    
734                    query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
735    
736                    query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
737    
738                    if (orderByComparator != null) {
739                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
740    
741                            if (orderByConditionFields.length > 0) {
742                                    query.append(WHERE_AND);
743                            }
744    
745                            for (int i = 0; i < orderByConditionFields.length; i++) {
746                                    query.append(_ORDER_BY_ENTITY_ALIAS);
747                                    query.append(orderByConditionFields[i]);
748    
749                                    if ((i + 1) < orderByConditionFields.length) {
750                                            if (orderByComparator.isAscending() ^ previous) {
751                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
752                                            }
753                                            else {
754                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
755                                            }
756                                    }
757                                    else {
758                                            if (orderByComparator.isAscending() ^ previous) {
759                                                    query.append(WHERE_GREATER_THAN);
760                                            }
761                                            else {
762                                                    query.append(WHERE_LESSER_THAN);
763                                            }
764                                    }
765                            }
766    
767                            query.append(ORDER_BY_CLAUSE);
768    
769                            String[] orderByFields = orderByComparator.getOrderByFields();
770    
771                            for (int i = 0; i < orderByFields.length; i++) {
772                                    query.append(_ORDER_BY_ENTITY_ALIAS);
773                                    query.append(orderByFields[i]);
774    
775                                    if ((i + 1) < orderByFields.length) {
776                                            if (orderByComparator.isAscending() ^ previous) {
777                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
778                                            }
779                                            else {
780                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
781                                            }
782                                    }
783                                    else {
784                                            if (orderByComparator.isAscending() ^ previous) {
785                                                    query.append(ORDER_BY_ASC);
786                                            }
787                                            else {
788                                                    query.append(ORDER_BY_DESC);
789                                            }
790                                    }
791                            }
792                    }
793                    else {
794                            query.append(TicketModelImpl.ORDER_BY_JPQL);
795                    }
796    
797                    String sql = query.toString();
798    
799                    Query q = session.createQuery(sql);
800    
801                    q.setFirstResult(0);
802                    q.setMaxResults(2);
803    
804                    QueryPos qPos = QueryPos.getInstance(q);
805    
806                    qPos.add(classNameId);
807    
808                    qPos.add(classPK);
809    
810                    qPos.add(type);
811    
812                    if (orderByComparator != null) {
813                            Object[] values = orderByComparator.getOrderByConditionValues(ticket);
814    
815                            for (Object value : values) {
816                                    qPos.add(value);
817                            }
818                    }
819    
820                    List<Ticket> list = q.list();
821    
822                    if (list.size() == 2) {
823                            return list.get(1);
824                    }
825                    else {
826                            return null;
827                    }
828            }
829    
830            /**
831             * Removes all the tickets where classNameId = &#63; and classPK = &#63; and type = &#63; from the database.
832             *
833             * @param classNameId the class name ID
834             * @param classPK the class p k
835             * @param type the type
836             */
837            @Override
838            public void removeByC_C_T(long classNameId, long classPK, int type) {
839                    for (Ticket ticket : findByC_C_T(classNameId, classPK, type,
840                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
841                            remove(ticket);
842                    }
843            }
844    
845            /**
846             * Returns the number of tickets where classNameId = &#63; and classPK = &#63; and type = &#63;.
847             *
848             * @param classNameId the class name ID
849             * @param classPK the class p k
850             * @param type the type
851             * @return the number of matching tickets
852             */
853            @Override
854            public int countByC_C_T(long classNameId, long classPK, int type) {
855                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_T;
856    
857                    Object[] finderArgs = new Object[] { classNameId, classPK, type };
858    
859                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
860    
861                    if (count == null) {
862                            StringBundler query = new StringBundler(4);
863    
864                            query.append(_SQL_COUNT_TICKET_WHERE);
865    
866                            query.append(_FINDER_COLUMN_C_C_T_CLASSNAMEID_2);
867    
868                            query.append(_FINDER_COLUMN_C_C_T_CLASSPK_2);
869    
870                            query.append(_FINDER_COLUMN_C_C_T_TYPE_2);
871    
872                            String sql = query.toString();
873    
874                            Session session = null;
875    
876                            try {
877                                    session = openSession();
878    
879                                    Query q = session.createQuery(sql);
880    
881                                    QueryPos qPos = QueryPos.getInstance(q);
882    
883                                    qPos.add(classNameId);
884    
885                                    qPos.add(classPK);
886    
887                                    qPos.add(type);
888    
889                                    count = (Long)q.uniqueResult();
890    
891                                    finderCache.putResult(finderPath, finderArgs, count);
892                            }
893                            catch (Exception e) {
894                                    finderCache.removeResult(finderPath, finderArgs);
895    
896                                    throw processException(e);
897                            }
898                            finally {
899                                    closeSession(session);
900                            }
901                    }
902    
903                    return count.intValue();
904            }
905    
906            private static final String _FINDER_COLUMN_C_C_T_CLASSNAMEID_2 = "ticket.classNameId = ? AND ";
907            private static final String _FINDER_COLUMN_C_C_T_CLASSPK_2 = "ticket.classPK = ? AND ";
908            private static final String _FINDER_COLUMN_C_C_T_TYPE_2 = "ticket.type = ?";
909    
910            public TicketPersistenceImpl() {
911                    setModelClass(Ticket.class);
912            }
913    
914            /**
915             * Caches the ticket in the entity cache if it is enabled.
916             *
917             * @param ticket the ticket
918             */
919            @Override
920            public void cacheResult(Ticket ticket) {
921                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
922                            TicketImpl.class, ticket.getPrimaryKey(), ticket);
923    
924                    finderCache.putResult(FINDER_PATH_FETCH_BY_KEY,
925                            new Object[] { ticket.getKey() }, ticket);
926    
927                    ticket.resetOriginalValues();
928            }
929    
930            /**
931             * Caches the tickets in the entity cache if it is enabled.
932             *
933             * @param tickets the tickets
934             */
935            @Override
936            public void cacheResult(List<Ticket> tickets) {
937                    for (Ticket ticket : tickets) {
938                            if (entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
939                                                    TicketImpl.class, ticket.getPrimaryKey()) == null) {
940                                    cacheResult(ticket);
941                            }
942                            else {
943                                    ticket.resetOriginalValues();
944                            }
945                    }
946            }
947    
948            /**
949             * Clears the cache for all tickets.
950             *
951             * <p>
952             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
953             * </p>
954             */
955            @Override
956            public void clearCache() {
957                    entityCache.clearCache(TicketImpl.class);
958    
959                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
960                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
961                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
962            }
963    
964            /**
965             * Clears the cache for the ticket.
966             *
967             * <p>
968             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
969             * </p>
970             */
971            @Override
972            public void clearCache(Ticket ticket) {
973                    entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
974                            TicketImpl.class, ticket.getPrimaryKey());
975    
976                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
977                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
978    
979                    clearUniqueFindersCache((TicketModelImpl)ticket);
980            }
981    
982            @Override
983            public void clearCache(List<Ticket> tickets) {
984                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
985                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
986    
987                    for (Ticket ticket : tickets) {
988                            entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
989                                    TicketImpl.class, ticket.getPrimaryKey());
990    
991                            clearUniqueFindersCache((TicketModelImpl)ticket);
992                    }
993            }
994    
995            protected void cacheUniqueFindersCache(TicketModelImpl ticketModelImpl,
996                    boolean isNew) {
997                    if (isNew) {
998                            Object[] args = new Object[] { ticketModelImpl.getKey() };
999    
1000                            finderCache.putResult(FINDER_PATH_COUNT_BY_KEY, args,
1001                                    Long.valueOf(1));
1002                            finderCache.putResult(FINDER_PATH_FETCH_BY_KEY, args,
1003                                    ticketModelImpl);
1004                    }
1005                    else {
1006                            if ((ticketModelImpl.getColumnBitmask() &
1007                                            FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
1008                                    Object[] args = new Object[] { ticketModelImpl.getKey() };
1009    
1010                                    finderCache.putResult(FINDER_PATH_COUNT_BY_KEY, args,
1011                                            Long.valueOf(1));
1012                                    finderCache.putResult(FINDER_PATH_FETCH_BY_KEY, args,
1013                                            ticketModelImpl);
1014                            }
1015                    }
1016            }
1017    
1018            protected void clearUniqueFindersCache(TicketModelImpl ticketModelImpl) {
1019                    Object[] args = new Object[] { ticketModelImpl.getKey() };
1020    
1021                    finderCache.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
1022                    finderCache.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
1023    
1024                    if ((ticketModelImpl.getColumnBitmask() &
1025                                    FINDER_PATH_FETCH_BY_KEY.getColumnBitmask()) != 0) {
1026                            args = new Object[] { ticketModelImpl.getOriginalKey() };
1027    
1028                            finderCache.removeResult(FINDER_PATH_COUNT_BY_KEY, args);
1029                            finderCache.removeResult(FINDER_PATH_FETCH_BY_KEY, args);
1030                    }
1031            }
1032    
1033            /**
1034             * Creates a new ticket with the primary key. Does not add the ticket to the database.
1035             *
1036             * @param ticketId the primary key for the new ticket
1037             * @return the new ticket
1038             */
1039            @Override
1040            public Ticket create(long ticketId) {
1041                    Ticket ticket = new TicketImpl();
1042    
1043                    ticket.setNew(true);
1044                    ticket.setPrimaryKey(ticketId);
1045    
1046                    ticket.setCompanyId(companyProvider.getCompanyId());
1047    
1048                    return ticket;
1049            }
1050    
1051            /**
1052             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
1053             *
1054             * @param ticketId the primary key of the ticket
1055             * @return the ticket that was removed
1056             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1057             */
1058            @Override
1059            public Ticket remove(long ticketId) throws NoSuchTicketException {
1060                    return remove((Serializable)ticketId);
1061            }
1062    
1063            /**
1064             * Removes the ticket with the primary key from the database. Also notifies the appropriate model listeners.
1065             *
1066             * @param primaryKey the primary key of the ticket
1067             * @return the ticket that was removed
1068             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1069             */
1070            @Override
1071            public Ticket remove(Serializable primaryKey) throws NoSuchTicketException {
1072                    Session session = null;
1073    
1074                    try {
1075                            session = openSession();
1076    
1077                            Ticket ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
1078    
1079                            if (ticket == null) {
1080                                    if (_log.isWarnEnabled()) {
1081                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1082                                    }
1083    
1084                                    throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1085                                            primaryKey);
1086                            }
1087    
1088                            return remove(ticket);
1089                    }
1090                    catch (NoSuchTicketException nsee) {
1091                            throw nsee;
1092                    }
1093                    catch (Exception e) {
1094                            throw processException(e);
1095                    }
1096                    finally {
1097                            closeSession(session);
1098                    }
1099            }
1100    
1101            @Override
1102            protected Ticket removeImpl(Ticket ticket) {
1103                    ticket = toUnwrappedModel(ticket);
1104    
1105                    Session session = null;
1106    
1107                    try {
1108                            session = openSession();
1109    
1110                            if (!session.contains(ticket)) {
1111                                    ticket = (Ticket)session.get(TicketImpl.class,
1112                                                    ticket.getPrimaryKeyObj());
1113                            }
1114    
1115                            if (ticket != null) {
1116                                    session.delete(ticket);
1117                            }
1118                    }
1119                    catch (Exception e) {
1120                            throw processException(e);
1121                    }
1122                    finally {
1123                            closeSession(session);
1124                    }
1125    
1126                    if (ticket != null) {
1127                            clearCache(ticket);
1128                    }
1129    
1130                    return ticket;
1131            }
1132    
1133            @Override
1134            public Ticket updateImpl(Ticket ticket) {
1135                    ticket = toUnwrappedModel(ticket);
1136    
1137                    boolean isNew = ticket.isNew();
1138    
1139                    TicketModelImpl ticketModelImpl = (TicketModelImpl)ticket;
1140    
1141                    Session session = null;
1142    
1143                    try {
1144                            session = openSession();
1145    
1146                            if (ticket.isNew()) {
1147                                    session.save(ticket);
1148    
1149                                    ticket.setNew(false);
1150                            }
1151                            else {
1152                                    ticket = (Ticket)session.merge(ticket);
1153                            }
1154                    }
1155                    catch (Exception e) {
1156                            throw processException(e);
1157                    }
1158                    finally {
1159                            closeSession(session);
1160                    }
1161    
1162                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1163    
1164                    if (isNew || !TicketModelImpl.COLUMN_BITMASK_ENABLED) {
1165                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1166                    }
1167    
1168                    else {
1169                            if ((ticketModelImpl.getColumnBitmask() &
1170                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T.getColumnBitmask()) != 0) {
1171                                    Object[] args = new Object[] {
1172                                                    ticketModelImpl.getOriginalClassNameId(),
1173                                                    ticketModelImpl.getOriginalClassPK(),
1174                                                    ticketModelImpl.getOriginalType()
1175                                            };
1176    
1177                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
1178                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
1179                                            args);
1180    
1181                                    args = new Object[] {
1182                                                    ticketModelImpl.getClassNameId(),
1183                                                    ticketModelImpl.getClassPK(), ticketModelImpl.getType()
1184                                            };
1185    
1186                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_T, args);
1187                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_T,
1188                                            args);
1189                            }
1190                    }
1191    
1192                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1193                            TicketImpl.class, ticket.getPrimaryKey(), ticket, false);
1194    
1195                    clearUniqueFindersCache(ticketModelImpl);
1196                    cacheUniqueFindersCache(ticketModelImpl, isNew);
1197    
1198                    ticket.resetOriginalValues();
1199    
1200                    return ticket;
1201            }
1202    
1203            protected Ticket toUnwrappedModel(Ticket ticket) {
1204                    if (ticket instanceof TicketImpl) {
1205                            return ticket;
1206                    }
1207    
1208                    TicketImpl ticketImpl = new TicketImpl();
1209    
1210                    ticketImpl.setNew(ticket.isNew());
1211                    ticketImpl.setPrimaryKey(ticket.getPrimaryKey());
1212    
1213                    ticketImpl.setMvccVersion(ticket.getMvccVersion());
1214                    ticketImpl.setTicketId(ticket.getTicketId());
1215                    ticketImpl.setCompanyId(ticket.getCompanyId());
1216                    ticketImpl.setCreateDate(ticket.getCreateDate());
1217                    ticketImpl.setClassNameId(ticket.getClassNameId());
1218                    ticketImpl.setClassPK(ticket.getClassPK());
1219                    ticketImpl.setKey(ticket.getKey());
1220                    ticketImpl.setType(ticket.getType());
1221                    ticketImpl.setExtraInfo(ticket.getExtraInfo());
1222                    ticketImpl.setExpirationDate(ticket.getExpirationDate());
1223    
1224                    return ticketImpl;
1225            }
1226    
1227            /**
1228             * Returns the ticket with the primary key or throws a {@link com.liferay.portal.exception.NoSuchModelException} if it could not be found.
1229             *
1230             * @param primaryKey the primary key of the ticket
1231             * @return the ticket
1232             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1233             */
1234            @Override
1235            public Ticket findByPrimaryKey(Serializable primaryKey)
1236                    throws NoSuchTicketException {
1237                    Ticket ticket = fetchByPrimaryKey(primaryKey);
1238    
1239                    if (ticket == null) {
1240                            if (_log.isWarnEnabled()) {
1241                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1242                            }
1243    
1244                            throw new NoSuchTicketException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1245                                    primaryKey);
1246                    }
1247    
1248                    return ticket;
1249            }
1250    
1251            /**
1252             * Returns the ticket with the primary key or throws a {@link NoSuchTicketException} if it could not be found.
1253             *
1254             * @param ticketId the primary key of the ticket
1255             * @return the ticket
1256             * @throws NoSuchTicketException if a ticket with the primary key could not be found
1257             */
1258            @Override
1259            public Ticket findByPrimaryKey(long ticketId) throws NoSuchTicketException {
1260                    return findByPrimaryKey((Serializable)ticketId);
1261            }
1262    
1263            /**
1264             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
1265             *
1266             * @param primaryKey the primary key of the ticket
1267             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
1268             */
1269            @Override
1270            public Ticket fetchByPrimaryKey(Serializable primaryKey) {
1271                    Ticket ticket = (Ticket)entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1272                                    TicketImpl.class, primaryKey);
1273    
1274                    if (ticket == _nullTicket) {
1275                            return null;
1276                    }
1277    
1278                    if (ticket == null) {
1279                            Session session = null;
1280    
1281                            try {
1282                                    session = openSession();
1283    
1284                                    ticket = (Ticket)session.get(TicketImpl.class, primaryKey);
1285    
1286                                    if (ticket != null) {
1287                                            cacheResult(ticket);
1288                                    }
1289                                    else {
1290                                            entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1291                                                    TicketImpl.class, primaryKey, _nullTicket);
1292                                    }
1293                            }
1294                            catch (Exception e) {
1295                                    entityCache.removeResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1296                                            TicketImpl.class, primaryKey);
1297    
1298                                    throw processException(e);
1299                            }
1300                            finally {
1301                                    closeSession(session);
1302                            }
1303                    }
1304    
1305                    return ticket;
1306            }
1307    
1308            /**
1309             * Returns the ticket with the primary key or returns <code>null</code> if it could not be found.
1310             *
1311             * @param ticketId the primary key of the ticket
1312             * @return the ticket, or <code>null</code> if a ticket with the primary key could not be found
1313             */
1314            @Override
1315            public Ticket fetchByPrimaryKey(long ticketId) {
1316                    return fetchByPrimaryKey((Serializable)ticketId);
1317            }
1318    
1319            @Override
1320            public Map<Serializable, Ticket> fetchByPrimaryKeys(
1321                    Set<Serializable> primaryKeys) {
1322                    if (primaryKeys.isEmpty()) {
1323                            return Collections.emptyMap();
1324                    }
1325    
1326                    Map<Serializable, Ticket> map = new HashMap<Serializable, Ticket>();
1327    
1328                    if (primaryKeys.size() == 1) {
1329                            Iterator<Serializable> iterator = primaryKeys.iterator();
1330    
1331                            Serializable primaryKey = iterator.next();
1332    
1333                            Ticket ticket = fetchByPrimaryKey(primaryKey);
1334    
1335                            if (ticket != null) {
1336                                    map.put(primaryKey, ticket);
1337                            }
1338    
1339                            return map;
1340                    }
1341    
1342                    Set<Serializable> uncachedPrimaryKeys = null;
1343    
1344                    for (Serializable primaryKey : primaryKeys) {
1345                            Ticket ticket = (Ticket)entityCache.getResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1346                                            TicketImpl.class, primaryKey);
1347    
1348                            if (ticket == null) {
1349                                    if (uncachedPrimaryKeys == null) {
1350                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1351                                    }
1352    
1353                                    uncachedPrimaryKeys.add(primaryKey);
1354                            }
1355                            else {
1356                                    map.put(primaryKey, ticket);
1357                            }
1358                    }
1359    
1360                    if (uncachedPrimaryKeys == null) {
1361                            return map;
1362                    }
1363    
1364                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1365                                    1);
1366    
1367                    query.append(_SQL_SELECT_TICKET_WHERE_PKS_IN);
1368    
1369                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1370                            query.append(String.valueOf(primaryKey));
1371    
1372                            query.append(StringPool.COMMA);
1373                    }
1374    
1375                    query.setIndex(query.index() - 1);
1376    
1377                    query.append(StringPool.CLOSE_PARENTHESIS);
1378    
1379                    String sql = query.toString();
1380    
1381                    Session session = null;
1382    
1383                    try {
1384                            session = openSession();
1385    
1386                            Query q = session.createQuery(sql);
1387    
1388                            for (Ticket ticket : (List<Ticket>)q.list()) {
1389                                    map.put(ticket.getPrimaryKeyObj(), ticket);
1390    
1391                                    cacheResult(ticket);
1392    
1393                                    uncachedPrimaryKeys.remove(ticket.getPrimaryKeyObj());
1394                            }
1395    
1396                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1397                                    entityCache.putResult(TicketModelImpl.ENTITY_CACHE_ENABLED,
1398                                            TicketImpl.class, primaryKey, _nullTicket);
1399                            }
1400                    }
1401                    catch (Exception e) {
1402                            throw processException(e);
1403                    }
1404                    finally {
1405                            closeSession(session);
1406                    }
1407    
1408                    return map;
1409            }
1410    
1411            /**
1412             * Returns all the tickets.
1413             *
1414             * @return the tickets
1415             */
1416            @Override
1417            public List<Ticket> findAll() {
1418                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1419            }
1420    
1421            /**
1422             * Returns a range of all the tickets.
1423             *
1424             * <p>
1425             * 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.
1426             * </p>
1427             *
1428             * @param start the lower bound of the range of tickets
1429             * @param end the upper bound of the range of tickets (not inclusive)
1430             * @return the range of tickets
1431             */
1432            @Override
1433            public List<Ticket> findAll(int start, int end) {
1434                    return findAll(start, end, null);
1435            }
1436    
1437            /**
1438             * Returns an ordered range of all the tickets.
1439             *
1440             * <p>
1441             * 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.
1442             * </p>
1443             *
1444             * @param start the lower bound of the range of tickets
1445             * @param end the upper bound of the range of tickets (not inclusive)
1446             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1447             * @return the ordered range of tickets
1448             */
1449            @Override
1450            public List<Ticket> findAll(int start, int end,
1451                    OrderByComparator<Ticket> orderByComparator) {
1452                    return findAll(start, end, orderByComparator, true);
1453            }
1454    
1455            /**
1456             * Returns an ordered range of all the tickets.
1457             *
1458             * <p>
1459             * 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.
1460             * </p>
1461             *
1462             * @param start the lower bound of the range of tickets
1463             * @param end the upper bound of the range of tickets (not inclusive)
1464             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1465             * @param retrieveFromCache whether to retrieve from the finder cache
1466             * @return the ordered range of tickets
1467             */
1468            @Override
1469            public List<Ticket> findAll(int start, int end,
1470                    OrderByComparator<Ticket> orderByComparator, boolean retrieveFromCache) {
1471                    boolean pagination = true;
1472                    FinderPath finderPath = null;
1473                    Object[] finderArgs = null;
1474    
1475                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1476                                    (orderByComparator == null)) {
1477                            pagination = false;
1478                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1479                            finderArgs = FINDER_ARGS_EMPTY;
1480                    }
1481                    else {
1482                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1483                            finderArgs = new Object[] { start, end, orderByComparator };
1484                    }
1485    
1486                    List<Ticket> list = null;
1487    
1488                    if (retrieveFromCache) {
1489                            list = (List<Ticket>)finderCache.getResult(finderPath, finderArgs,
1490                                            this);
1491                    }
1492    
1493                    if (list == null) {
1494                            StringBundler query = null;
1495                            String sql = null;
1496    
1497                            if (orderByComparator != null) {
1498                                    query = new StringBundler(2 +
1499                                                    (orderByComparator.getOrderByFields().length * 2));
1500    
1501                                    query.append(_SQL_SELECT_TICKET);
1502    
1503                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1504                                            orderByComparator);
1505    
1506                                    sql = query.toString();
1507                            }
1508                            else {
1509                                    sql = _SQL_SELECT_TICKET;
1510    
1511                                    if (pagination) {
1512                                            sql = sql.concat(TicketModelImpl.ORDER_BY_JPQL);
1513                                    }
1514                            }
1515    
1516                            Session session = null;
1517    
1518                            try {
1519                                    session = openSession();
1520    
1521                                    Query q = session.createQuery(sql);
1522    
1523                                    if (!pagination) {
1524                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
1525                                                            end, false);
1526    
1527                                            Collections.sort(list);
1528    
1529                                            list = Collections.unmodifiableList(list);
1530                                    }
1531                                    else {
1532                                            list = (List<Ticket>)QueryUtil.list(q, getDialect(), start,
1533                                                            end);
1534                                    }
1535    
1536                                    cacheResult(list);
1537    
1538                                    finderCache.putResult(finderPath, finderArgs, list);
1539                            }
1540                            catch (Exception e) {
1541                                    finderCache.removeResult(finderPath, finderArgs);
1542    
1543                                    throw processException(e);
1544                            }
1545                            finally {
1546                                    closeSession(session);
1547                            }
1548                    }
1549    
1550                    return list;
1551            }
1552    
1553            /**
1554             * Removes all the tickets from the database.
1555             *
1556             */
1557            @Override
1558            public void removeAll() {
1559                    for (Ticket ticket : findAll()) {
1560                            remove(ticket);
1561                    }
1562            }
1563    
1564            /**
1565             * Returns the number of tickets.
1566             *
1567             * @return the number of tickets
1568             */
1569            @Override
1570            public int countAll() {
1571                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
1572                                    FINDER_ARGS_EMPTY, this);
1573    
1574                    if (count == null) {
1575                            Session session = null;
1576    
1577                            try {
1578                                    session = openSession();
1579    
1580                                    Query q = session.createQuery(_SQL_COUNT_TICKET);
1581    
1582                                    count = (Long)q.uniqueResult();
1583    
1584                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
1585                                            count);
1586                            }
1587                            catch (Exception e) {
1588                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
1589                                            FINDER_ARGS_EMPTY);
1590    
1591                                    throw processException(e);
1592                            }
1593                            finally {
1594                                    closeSession(session);
1595                            }
1596                    }
1597    
1598                    return count.intValue();
1599            }
1600    
1601            @Override
1602            public Set<String> getBadColumnNames() {
1603                    return _badColumnNames;
1604            }
1605    
1606            @Override
1607            protected Map<String, Integer> getTableColumnsMap() {
1608                    return TicketModelImpl.TABLE_COLUMNS_MAP;
1609            }
1610    
1611            /**
1612             * Initializes the ticket persistence.
1613             */
1614            public void afterPropertiesSet() {
1615            }
1616    
1617            public void destroy() {
1618                    entityCache.removeCache(TicketImpl.class.getName());
1619                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
1620                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1621                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1622            }
1623    
1624            @BeanReference(type = CompanyProviderWrapper.class)
1625            protected CompanyProvider companyProvider;
1626            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
1627            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
1628            private static final String _SQL_SELECT_TICKET = "SELECT ticket FROM Ticket ticket";
1629            private static final String _SQL_SELECT_TICKET_WHERE_PKS_IN = "SELECT ticket FROM Ticket ticket WHERE ticketId IN (";
1630            private static final String _SQL_SELECT_TICKET_WHERE = "SELECT ticket FROM Ticket ticket WHERE ";
1631            private static final String _SQL_COUNT_TICKET = "SELECT COUNT(ticket) FROM Ticket ticket";
1632            private static final String _SQL_COUNT_TICKET_WHERE = "SELECT COUNT(ticket) FROM Ticket ticket WHERE ";
1633            private static final String _ORDER_BY_ENTITY_ALIAS = "ticket.";
1634            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Ticket exists with the primary key ";
1635            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Ticket exists with the key {";
1636            private static final Log _log = LogFactoryUtil.getLog(TicketPersistenceImpl.class);
1637            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1638                                    "key", "type"
1639                            });
1640            private static final Ticket _nullTicket = new TicketImpl() {
1641                            @Override
1642                            public Object clone() {
1643                                    return this;
1644                            }
1645    
1646                            @Override
1647                            public CacheModel<Ticket> toCacheModel() {
1648                                    return _nullTicketCacheModel;
1649                            }
1650                    };
1651    
1652            private static final CacheModel<Ticket> _nullTicketCacheModel = new NullCacheModel();
1653    
1654            private static class NullCacheModel implements CacheModel<Ticket>,
1655                    MVCCModel {
1656                    @Override
1657                    public long getMvccVersion() {
1658                            return -1;
1659                    }
1660    
1661                    @Override
1662                    public void setMvccVersion(long mvccVersion) {
1663                    }
1664    
1665                    @Override
1666                    public Ticket toEntityModel() {
1667                            return _nullTicket;
1668                    }
1669            }
1670    }