001    /**
002     * Copyright (c) 2000-2011 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;
016    
017    import com.liferay.portal.NoSuchListTypeException;
018    import com.liferay.portal.NoSuchModelException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.ListType;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.impl.ListTypeImpl;
040    import com.liferay.portal.model.impl.ListTypeModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import java.io.Serializable;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.List;
048    
049    /**
050     * The persistence implementation for the list type service.
051     *
052     * <p>
053     * Never modify or reference this class directly. Always use {@link ListTypeUtil} to access the list type persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
054     * </p>
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see ListTypePersistence
062     * @see ListTypeUtil
063     * @generated
064     */
065    public class ListTypePersistenceImpl extends BasePersistenceImpl<ListType>
066            implements ListTypePersistence {
067            public static final String FINDER_CLASS_NAME_ENTITY = ListTypeImpl.class.getName();
068            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
069                    ".List";
070            public static final FinderPath FINDER_PATH_FIND_BY_TYPE = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
071                            ListTypeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
072                            "findByType",
073                            new String[] {
074                                    String.class.getName(),
075                                    
076                            "java.lang.Integer", "java.lang.Integer",
077                                    "com.liferay.portal.kernel.util.OrderByComparator"
078                            });
079            public static final FinderPath FINDER_PATH_COUNT_BY_TYPE = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
080                            ListTypeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
081                            "countByType", new String[] { String.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
083                            ListTypeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
086                            ListTypeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
087                            "countAll", new String[0]);
088    
089            /**
090             * Caches the list type in the entity cache if it is enabled.
091             *
092             * @param listType the list type to cache
093             */
094            public void cacheResult(ListType listType) {
095                    EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
096                            ListTypeImpl.class, listType.getPrimaryKey(), listType);
097            }
098    
099            /**
100             * Caches the list types in the entity cache if it is enabled.
101             *
102             * @param listTypes the list types to cache
103             */
104            public void cacheResult(List<ListType> listTypes) {
105                    for (ListType listType : listTypes) {
106                            if (EntityCacheUtil.getResult(
107                                                    ListTypeModelImpl.ENTITY_CACHE_ENABLED,
108                                                    ListTypeImpl.class, listType.getPrimaryKey(), this) == null) {
109                                    cacheResult(listType);
110                            }
111                    }
112            }
113    
114            /**
115             * Clears the cache for all list types.
116             *
117             * <p>
118             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
119             * </p>
120             */
121            public void clearCache() {
122                    CacheRegistryUtil.clear(ListTypeImpl.class.getName());
123                    EntityCacheUtil.clearCache(ListTypeImpl.class.getName());
124                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
125                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
126            }
127    
128            /**
129             * Clears the cache for the list type.
130             *
131             * <p>
132             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
133             * </p>
134             */
135            public void clearCache(ListType listType) {
136                    EntityCacheUtil.removeResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
137                            ListTypeImpl.class, listType.getPrimaryKey());
138            }
139    
140            /**
141             * Creates a new list type with the primary key. Does not add the list type to the database.
142             *
143             * @param listTypeId the primary key for the new list type
144             * @return the new list type
145             */
146            public ListType create(int listTypeId) {
147                    ListType listType = new ListTypeImpl();
148    
149                    listType.setNew(true);
150                    listType.setPrimaryKey(listTypeId);
151    
152                    return listType;
153            }
154    
155            /**
156             * Removes the list type with the primary key from the database. Also notifies the appropriate model listeners.
157             *
158             * @param primaryKey the primary key of the list type to remove
159             * @return the list type that was removed
160             * @throws com.liferay.portal.NoSuchModelException if a list type with the primary key could not be found
161             * @throws SystemException if a system exception occurred
162             */
163            public ListType remove(Serializable primaryKey)
164                    throws NoSuchModelException, SystemException {
165                    return remove(((Integer)primaryKey).intValue());
166            }
167    
168            /**
169             * Removes the list type with the primary key from the database. Also notifies the appropriate model listeners.
170             *
171             * @param listTypeId the primary key of the list type to remove
172             * @return the list type that was removed
173             * @throws com.liferay.portal.NoSuchListTypeException if a list type with the primary key could not be found
174             * @throws SystemException if a system exception occurred
175             */
176            public ListType remove(int listTypeId)
177                    throws NoSuchListTypeException, SystemException {
178                    Session session = null;
179    
180                    try {
181                            session = openSession();
182    
183                            ListType listType = (ListType)session.get(ListTypeImpl.class,
184                                            new Integer(listTypeId));
185    
186                            if (listType == null) {
187                                    if (_log.isWarnEnabled()) {
188                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + listTypeId);
189                                    }
190    
191                                    throw new NoSuchListTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
192                                            listTypeId);
193                            }
194    
195                            return remove(listType);
196                    }
197                    catch (NoSuchListTypeException nsee) {
198                            throw nsee;
199                    }
200                    catch (Exception e) {
201                            throw processException(e);
202                    }
203                    finally {
204                            closeSession(session);
205                    }
206            }
207    
208            protected ListType removeImpl(ListType listType) throws SystemException {
209                    listType = toUnwrappedModel(listType);
210    
211                    Session session = null;
212    
213                    try {
214                            session = openSession();
215    
216                            BatchSessionUtil.delete(session, listType);
217                    }
218                    catch (Exception e) {
219                            throw processException(e);
220                    }
221                    finally {
222                            closeSession(session);
223                    }
224    
225                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
226    
227                    EntityCacheUtil.removeResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
228                            ListTypeImpl.class, listType.getPrimaryKey());
229    
230                    return listType;
231            }
232    
233            public ListType updateImpl(com.liferay.portal.model.ListType listType,
234                    boolean merge) throws SystemException {
235                    listType = toUnwrappedModel(listType);
236    
237                    Session session = null;
238    
239                    try {
240                            session = openSession();
241    
242                            BatchSessionUtil.update(session, listType, merge);
243    
244                            listType.setNew(false);
245                    }
246                    catch (Exception e) {
247                            throw processException(e);
248                    }
249                    finally {
250                            closeSession(session);
251                    }
252    
253                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
254    
255                    EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
256                            ListTypeImpl.class, listType.getPrimaryKey(), listType);
257    
258                    return listType;
259            }
260    
261            protected ListType toUnwrappedModel(ListType listType) {
262                    if (listType instanceof ListTypeImpl) {
263                            return listType;
264                    }
265    
266                    ListTypeImpl listTypeImpl = new ListTypeImpl();
267    
268                    listTypeImpl.setNew(listType.isNew());
269                    listTypeImpl.setPrimaryKey(listType.getPrimaryKey());
270    
271                    listTypeImpl.setListTypeId(listType.getListTypeId());
272                    listTypeImpl.setName(listType.getName());
273                    listTypeImpl.setType(listType.getType());
274    
275                    return listTypeImpl;
276            }
277    
278            /**
279             * Finds the list type with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
280             *
281             * @param primaryKey the primary key of the list type to find
282             * @return the list type
283             * @throws com.liferay.portal.NoSuchModelException if a list type with the primary key could not be found
284             * @throws SystemException if a system exception occurred
285             */
286            public ListType findByPrimaryKey(Serializable primaryKey)
287                    throws NoSuchModelException, SystemException {
288                    return findByPrimaryKey(((Integer)primaryKey).intValue());
289            }
290    
291            /**
292             * Finds the list type with the primary key or throws a {@link com.liferay.portal.NoSuchListTypeException} if it could not be found.
293             *
294             * @param listTypeId the primary key of the list type to find
295             * @return the list type
296             * @throws com.liferay.portal.NoSuchListTypeException if a list type with the primary key could not be found
297             * @throws SystemException if a system exception occurred
298             */
299            public ListType findByPrimaryKey(int listTypeId)
300                    throws NoSuchListTypeException, SystemException {
301                    ListType listType = fetchByPrimaryKey(listTypeId);
302    
303                    if (listType == null) {
304                            if (_log.isWarnEnabled()) {
305                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + listTypeId);
306                            }
307    
308                            throw new NoSuchListTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
309                                    listTypeId);
310                    }
311    
312                    return listType;
313            }
314    
315            /**
316             * Finds the list type with the primary key or returns <code>null</code> if it could not be found.
317             *
318             * @param primaryKey the primary key of the list type to find
319             * @return the list type, or <code>null</code> if a list type with the primary key could not be found
320             * @throws SystemException if a system exception occurred
321             */
322            public ListType fetchByPrimaryKey(Serializable primaryKey)
323                    throws SystemException {
324                    return fetchByPrimaryKey(((Integer)primaryKey).intValue());
325            }
326    
327            /**
328             * Finds the list type with the primary key or returns <code>null</code> if it could not be found.
329             *
330             * @param listTypeId the primary key of the list type to find
331             * @return the list type, or <code>null</code> if a list type with the primary key could not be found
332             * @throws SystemException if a system exception occurred
333             */
334            public ListType fetchByPrimaryKey(int listTypeId) throws SystemException {
335                    ListType listType = (ListType)EntityCacheUtil.getResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
336                                    ListTypeImpl.class, listTypeId, this);
337    
338                    if (listType == null) {
339                            Session session = null;
340    
341                            try {
342                                    session = openSession();
343    
344                                    listType = (ListType)session.get(ListTypeImpl.class,
345                                                    new Integer(listTypeId));
346                            }
347                            catch (Exception e) {
348                                    throw processException(e);
349                            }
350                            finally {
351                                    if (listType != null) {
352                                            cacheResult(listType);
353                                    }
354    
355                                    closeSession(session);
356                            }
357                    }
358    
359                    return listType;
360            }
361    
362            /**
363             * Finds all the list types where type = &#63;.
364             *
365             * @param type the type to search with
366             * @return the matching list types
367             * @throws SystemException if a system exception occurred
368             */
369            public List<ListType> findByType(String type) throws SystemException {
370                    return findByType(type, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
371            }
372    
373            /**
374             * Finds a range of all the list types where type = &#63;.
375             *
376             * <p>
377             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
378             * </p>
379             *
380             * @param type the type to search with
381             * @param start the lower bound of the range of list types to return
382             * @param end the upper bound of the range of list types to return (not inclusive)
383             * @return the range of matching list types
384             * @throws SystemException if a system exception occurred
385             */
386            public List<ListType> findByType(String type, int start, int end)
387                    throws SystemException {
388                    return findByType(type, start, end, null);
389            }
390    
391            /**
392             * Finds an ordered range of all the list types where 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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
396             * </p>
397             *
398             * @param type the type to search with
399             * @param start the lower bound of the range of list types to return
400             * @param end the upper bound of the range of list types to return (not inclusive)
401             * @param orderByComparator the comparator to order the results by
402             * @return the ordered range of matching list types
403             * @throws SystemException if a system exception occurred
404             */
405            public List<ListType> findByType(String type, int start, int end,
406                    OrderByComparator orderByComparator) throws SystemException {
407                    Object[] finderArgs = new Object[] {
408                                    type,
409                                    
410                                    String.valueOf(start), String.valueOf(end),
411                                    String.valueOf(orderByComparator)
412                            };
413    
414                    List<ListType> list = (List<ListType>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_TYPE,
415                                    finderArgs, this);
416    
417                    if (list == null) {
418                            StringBundler query = null;
419    
420                            if (orderByComparator != null) {
421                                    query = new StringBundler(3 +
422                                                    (orderByComparator.getOrderByFields().length * 3));
423                            }
424                            else {
425                                    query = new StringBundler(3);
426                            }
427    
428                            query.append(_SQL_SELECT_LISTTYPE_WHERE);
429    
430                            if (type == null) {
431                                    query.append(_FINDER_COLUMN_TYPE_TYPE_1);
432                            }
433                            else {
434                                    if (type.equals(StringPool.BLANK)) {
435                                            query.append(_FINDER_COLUMN_TYPE_TYPE_3);
436                                    }
437                                    else {
438                                            query.append(_FINDER_COLUMN_TYPE_TYPE_2);
439                                    }
440                            }
441    
442                            if (orderByComparator != null) {
443                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
444                                            orderByComparator);
445                            }
446    
447                            else {
448                                    query.append(ListTypeModelImpl.ORDER_BY_JPQL);
449                            }
450    
451                            String sql = query.toString();
452    
453                            Session session = null;
454    
455                            try {
456                                    session = openSession();
457    
458                                    Query q = session.createQuery(sql);
459    
460                                    QueryPos qPos = QueryPos.getInstance(q);
461    
462                                    if (type != null) {
463                                            qPos.add(type);
464                                    }
465    
466                                    list = (List<ListType>)QueryUtil.list(q, getDialect(), start,
467                                                    end);
468                            }
469                            catch (Exception e) {
470                                    throw processException(e);
471                            }
472                            finally {
473                                    if (list == null) {
474                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_TYPE,
475                                                    finderArgs);
476                                    }
477                                    else {
478                                            cacheResult(list);
479    
480                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TYPE,
481                                                    finderArgs, list);
482                                    }
483    
484                                    closeSession(session);
485                            }
486                    }
487    
488                    return list;
489            }
490    
491            /**
492             * Finds the first list type in the ordered set where type = &#63;.
493             *
494             * <p>
495             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
496             * </p>
497             *
498             * @param type the type to search with
499             * @param orderByComparator the comparator to order the set by
500             * @return the first matching list type
501             * @throws com.liferay.portal.NoSuchListTypeException if a matching list type could not be found
502             * @throws SystemException if a system exception occurred
503             */
504            public ListType findByType_First(String type,
505                    OrderByComparator orderByComparator)
506                    throws NoSuchListTypeException, SystemException {
507                    List<ListType> list = findByType(type, 0, 1, orderByComparator);
508    
509                    if (list.isEmpty()) {
510                            StringBundler msg = new StringBundler(4);
511    
512                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
513    
514                            msg.append("type=");
515                            msg.append(type);
516    
517                            msg.append(StringPool.CLOSE_CURLY_BRACE);
518    
519                            throw new NoSuchListTypeException(msg.toString());
520                    }
521                    else {
522                            return list.get(0);
523                    }
524            }
525    
526            /**
527             * Finds the last list type in the ordered set where type = &#63;.
528             *
529             * <p>
530             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
531             * </p>
532             *
533             * @param type the type to search with
534             * @param orderByComparator the comparator to order the set by
535             * @return the last matching list type
536             * @throws com.liferay.portal.NoSuchListTypeException if a matching list type could not be found
537             * @throws SystemException if a system exception occurred
538             */
539            public ListType findByType_Last(String type,
540                    OrderByComparator orderByComparator)
541                    throws NoSuchListTypeException, SystemException {
542                    int count = countByType(type);
543    
544                    List<ListType> list = findByType(type, count - 1, count,
545                                    orderByComparator);
546    
547                    if (list.isEmpty()) {
548                            StringBundler msg = new StringBundler(4);
549    
550                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
551    
552                            msg.append("type=");
553                            msg.append(type);
554    
555                            msg.append(StringPool.CLOSE_CURLY_BRACE);
556    
557                            throw new NoSuchListTypeException(msg.toString());
558                    }
559                    else {
560                            return list.get(0);
561                    }
562            }
563    
564            /**
565             * Finds the list types before and after the current list type in the ordered set where type = &#63;.
566             *
567             * <p>
568             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
569             * </p>
570             *
571             * @param listTypeId the primary key of the current list type
572             * @param type the type to search with
573             * @param orderByComparator the comparator to order the set by
574             * @return the previous, current, and next list type
575             * @throws com.liferay.portal.NoSuchListTypeException if a list type with the primary key could not be found
576             * @throws SystemException if a system exception occurred
577             */
578            public ListType[] findByType_PrevAndNext(int listTypeId, String type,
579                    OrderByComparator orderByComparator)
580                    throws NoSuchListTypeException, SystemException {
581                    ListType listType = findByPrimaryKey(listTypeId);
582    
583                    Session session = null;
584    
585                    try {
586                            session = openSession();
587    
588                            ListType[] array = new ListTypeImpl[3];
589    
590                            array[0] = getByType_PrevAndNext(session, listType, type,
591                                            orderByComparator, true);
592    
593                            array[1] = listType;
594    
595                            array[2] = getByType_PrevAndNext(session, listType, type,
596                                            orderByComparator, false);
597    
598                            return array;
599                    }
600                    catch (Exception e) {
601                            throw processException(e);
602                    }
603                    finally {
604                            closeSession(session);
605                    }
606            }
607    
608            protected ListType getByType_PrevAndNext(Session session,
609                    ListType listType, String type, OrderByComparator orderByComparator,
610                    boolean previous) {
611                    StringBundler query = null;
612    
613                    if (orderByComparator != null) {
614                            query = new StringBundler(6 +
615                                            (orderByComparator.getOrderByFields().length * 6));
616                    }
617                    else {
618                            query = new StringBundler(3);
619                    }
620    
621                    query.append(_SQL_SELECT_LISTTYPE_WHERE);
622    
623                    if (type == null) {
624                            query.append(_FINDER_COLUMN_TYPE_TYPE_1);
625                    }
626                    else {
627                            if (type.equals(StringPool.BLANK)) {
628                                    query.append(_FINDER_COLUMN_TYPE_TYPE_3);
629                            }
630                            else {
631                                    query.append(_FINDER_COLUMN_TYPE_TYPE_2);
632                            }
633                    }
634    
635                    if (orderByComparator != null) {
636                            String[] orderByFields = orderByComparator.getOrderByFields();
637    
638                            if (orderByFields.length > 0) {
639                                    query.append(WHERE_AND);
640                            }
641    
642                            for (int i = 0; i < orderByFields.length; i++) {
643                                    query.append(_ORDER_BY_ENTITY_ALIAS);
644                                    query.append(orderByFields[i]);
645    
646                                    if ((i + 1) < orderByFields.length) {
647                                            if (orderByComparator.isAscending() ^ previous) {
648                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
649                                            }
650                                            else {
651                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
652                                            }
653                                    }
654                                    else {
655                                            if (orderByComparator.isAscending() ^ previous) {
656                                                    query.append(WHERE_GREATER_THAN);
657                                            }
658                                            else {
659                                                    query.append(WHERE_LESSER_THAN);
660                                            }
661                                    }
662                            }
663    
664                            query.append(ORDER_BY_CLAUSE);
665    
666                            for (int i = 0; i < orderByFields.length; i++) {
667                                    query.append(_ORDER_BY_ENTITY_ALIAS);
668                                    query.append(orderByFields[i]);
669    
670                                    if ((i + 1) < orderByFields.length) {
671                                            if (orderByComparator.isAscending() ^ previous) {
672                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
673                                            }
674                                            else {
675                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
676                                            }
677                                    }
678                                    else {
679                                            if (orderByComparator.isAscending() ^ previous) {
680                                                    query.append(ORDER_BY_ASC);
681                                            }
682                                            else {
683                                                    query.append(ORDER_BY_DESC);
684                                            }
685                                    }
686                            }
687                    }
688    
689                    else {
690                            query.append(ListTypeModelImpl.ORDER_BY_JPQL);
691                    }
692    
693                    String sql = query.toString();
694    
695                    Query q = session.createQuery(sql);
696    
697                    q.setFirstResult(0);
698                    q.setMaxResults(2);
699    
700                    QueryPos qPos = QueryPos.getInstance(q);
701    
702                    if (type != null) {
703                            qPos.add(type);
704                    }
705    
706                    if (orderByComparator != null) {
707                            Object[] values = orderByComparator.getOrderByValues(listType);
708    
709                            for (Object value : values) {
710                                    qPos.add(value);
711                            }
712                    }
713    
714                    List<ListType> list = q.list();
715    
716                    if (list.size() == 2) {
717                            return list.get(1);
718                    }
719                    else {
720                            return null;
721                    }
722            }
723    
724            /**
725             * Finds all the list types.
726             *
727             * @return the list types
728             * @throws SystemException if a system exception occurred
729             */
730            public List<ListType> findAll() throws SystemException {
731                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
732            }
733    
734            /**
735             * Finds a range of all the list types.
736             *
737             * <p>
738             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
739             * </p>
740             *
741             * @param start the lower bound of the range of list types to return
742             * @param end the upper bound of the range of list types to return (not inclusive)
743             * @return the range of list types
744             * @throws SystemException if a system exception occurred
745             */
746            public List<ListType> findAll(int start, int end) throws SystemException {
747                    return findAll(start, end, null);
748            }
749    
750            /**
751             * Finds an ordered range of all the list types.
752             *
753             * <p>
754             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
755             * </p>
756             *
757             * @param start the lower bound of the range of list types to return
758             * @param end the upper bound of the range of list types to return (not inclusive)
759             * @param orderByComparator the comparator to order the results by
760             * @return the ordered range of list types
761             * @throws SystemException if a system exception occurred
762             */
763            public List<ListType> findAll(int start, int end,
764                    OrderByComparator orderByComparator) throws SystemException {
765                    Object[] finderArgs = new Object[] {
766                                    String.valueOf(start), String.valueOf(end),
767                                    String.valueOf(orderByComparator)
768                            };
769    
770                    List<ListType> list = (List<ListType>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
771                                    finderArgs, this);
772    
773                    if (list == null) {
774                            StringBundler query = null;
775                            String sql = null;
776    
777                            if (orderByComparator != null) {
778                                    query = new StringBundler(2 +
779                                                    (orderByComparator.getOrderByFields().length * 3));
780    
781                                    query.append(_SQL_SELECT_LISTTYPE);
782    
783                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
784                                            orderByComparator);
785    
786                                    sql = query.toString();
787                            }
788                            else {
789                                    sql = _SQL_SELECT_LISTTYPE.concat(ListTypeModelImpl.ORDER_BY_JPQL);
790                            }
791    
792                            Session session = null;
793    
794                            try {
795                                    session = openSession();
796    
797                                    Query q = session.createQuery(sql);
798    
799                                    if (orderByComparator == null) {
800                                            list = (List<ListType>)QueryUtil.list(q, getDialect(),
801                                                            start, end, false);
802    
803                                            Collections.sort(list);
804                                    }
805                                    else {
806                                            list = (List<ListType>)QueryUtil.list(q, getDialect(),
807                                                            start, end);
808                                    }
809                            }
810                            catch (Exception e) {
811                                    throw processException(e);
812                            }
813                            finally {
814                                    if (list == null) {
815                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
816                                                    finderArgs);
817                                    }
818                                    else {
819                                            cacheResult(list);
820    
821                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
822                                                    list);
823                                    }
824    
825                                    closeSession(session);
826                            }
827                    }
828    
829                    return list;
830            }
831    
832            /**
833             * Removes all the list types where type = &#63; from the database.
834             *
835             * @param type the type to search with
836             * @throws SystemException if a system exception occurred
837             */
838            public void removeByType(String type) throws SystemException {
839                    for (ListType listType : findByType(type)) {
840                            remove(listType);
841                    }
842            }
843    
844            /**
845             * Removes all the list types from the database.
846             *
847             * @throws SystemException if a system exception occurred
848             */
849            public void removeAll() throws SystemException {
850                    for (ListType listType : findAll()) {
851                            remove(listType);
852                    }
853            }
854    
855            /**
856             * Counts all the list types where type = &#63;.
857             *
858             * @param type the type to search with
859             * @return the number of matching list types
860             * @throws SystemException if a system exception occurred
861             */
862            public int countByType(String type) throws SystemException {
863                    Object[] finderArgs = new Object[] { type };
864    
865                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TYPE,
866                                    finderArgs, this);
867    
868                    if (count == null) {
869                            StringBundler query = new StringBundler(2);
870    
871                            query.append(_SQL_COUNT_LISTTYPE_WHERE);
872    
873                            if (type == null) {
874                                    query.append(_FINDER_COLUMN_TYPE_TYPE_1);
875                            }
876                            else {
877                                    if (type.equals(StringPool.BLANK)) {
878                                            query.append(_FINDER_COLUMN_TYPE_TYPE_3);
879                                    }
880                                    else {
881                                            query.append(_FINDER_COLUMN_TYPE_TYPE_2);
882                                    }
883                            }
884    
885                            String sql = query.toString();
886    
887                            Session session = null;
888    
889                            try {
890                                    session = openSession();
891    
892                                    Query q = session.createQuery(sql);
893    
894                                    QueryPos qPos = QueryPos.getInstance(q);
895    
896                                    if (type != null) {
897                                            qPos.add(type);
898                                    }
899    
900                                    count = (Long)q.uniqueResult();
901                            }
902                            catch (Exception e) {
903                                    throw processException(e);
904                            }
905                            finally {
906                                    if (count == null) {
907                                            count = Long.valueOf(0);
908                                    }
909    
910                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TYPE,
911                                            finderArgs, count);
912    
913                                    closeSession(session);
914                            }
915                    }
916    
917                    return count.intValue();
918            }
919    
920            /**
921             * Counts all the list types.
922             *
923             * @return the number of list types
924             * @throws SystemException if a system exception occurred
925             */
926            public int countAll() throws SystemException {
927                    Object[] finderArgs = new Object[0];
928    
929                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
930                                    finderArgs, this);
931    
932                    if (count == null) {
933                            Session session = null;
934    
935                            try {
936                                    session = openSession();
937    
938                                    Query q = session.createQuery(_SQL_COUNT_LISTTYPE);
939    
940                                    count = (Long)q.uniqueResult();
941                            }
942                            catch (Exception e) {
943                                    throw processException(e);
944                            }
945                            finally {
946                                    if (count == null) {
947                                            count = Long.valueOf(0);
948                                    }
949    
950                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
951                                            count);
952    
953                                    closeSession(session);
954                            }
955                    }
956    
957                    return count.intValue();
958            }
959    
960            /**
961             * Initializes the list type persistence.
962             */
963            public void afterPropertiesSet() {
964                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
965                                            com.liferay.portal.util.PropsUtil.get(
966                                                    "value.object.listener.com.liferay.portal.model.ListType")));
967    
968                    if (listenerClassNames.length > 0) {
969                            try {
970                                    List<ModelListener<ListType>> listenersList = new ArrayList<ModelListener<ListType>>();
971    
972                                    for (String listenerClassName : listenerClassNames) {
973                                            listenersList.add((ModelListener<ListType>)InstanceFactory.newInstance(
974                                                            listenerClassName));
975                                    }
976    
977                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
978                            }
979                            catch (Exception e) {
980                                    _log.error(e);
981                            }
982                    }
983            }
984    
985            public void destroy() {
986                    EntityCacheUtil.removeCache(ListTypeImpl.class.getName());
987                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
988                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
989            }
990    
991            @BeanReference(type = AccountPersistence.class)
992            protected AccountPersistence accountPersistence;
993            @BeanReference(type = AddressPersistence.class)
994            protected AddressPersistence addressPersistence;
995            @BeanReference(type = BrowserTrackerPersistence.class)
996            protected BrowserTrackerPersistence browserTrackerPersistence;
997            @BeanReference(type = ClassNamePersistence.class)
998            protected ClassNamePersistence classNamePersistence;
999            @BeanReference(type = ClusterGroupPersistence.class)
1000            protected ClusterGroupPersistence clusterGroupPersistence;
1001            @BeanReference(type = CompanyPersistence.class)
1002            protected CompanyPersistence companyPersistence;
1003            @BeanReference(type = ContactPersistence.class)
1004            protected ContactPersistence contactPersistence;
1005            @BeanReference(type = CountryPersistence.class)
1006            protected CountryPersistence countryPersistence;
1007            @BeanReference(type = EmailAddressPersistence.class)
1008            protected EmailAddressPersistence emailAddressPersistence;
1009            @BeanReference(type = GroupPersistence.class)
1010            protected GroupPersistence groupPersistence;
1011            @BeanReference(type = ImagePersistence.class)
1012            protected ImagePersistence imagePersistence;
1013            @BeanReference(type = LayoutPersistence.class)
1014            protected LayoutPersistence layoutPersistence;
1015            @BeanReference(type = LayoutPrototypePersistence.class)
1016            protected LayoutPrototypePersistence layoutPrototypePersistence;
1017            @BeanReference(type = LayoutSetPersistence.class)
1018            protected LayoutSetPersistence layoutSetPersistence;
1019            @BeanReference(type = LayoutSetPrototypePersistence.class)
1020            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1021            @BeanReference(type = ListTypePersistence.class)
1022            protected ListTypePersistence listTypePersistence;
1023            @BeanReference(type = LockPersistence.class)
1024            protected LockPersistence lockPersistence;
1025            @BeanReference(type = MembershipRequestPersistence.class)
1026            protected MembershipRequestPersistence membershipRequestPersistence;
1027            @BeanReference(type = OrganizationPersistence.class)
1028            protected OrganizationPersistence organizationPersistence;
1029            @BeanReference(type = OrgGroupPermissionPersistence.class)
1030            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1031            @BeanReference(type = OrgGroupRolePersistence.class)
1032            protected OrgGroupRolePersistence orgGroupRolePersistence;
1033            @BeanReference(type = OrgLaborPersistence.class)
1034            protected OrgLaborPersistence orgLaborPersistence;
1035            @BeanReference(type = PasswordPolicyPersistence.class)
1036            protected PasswordPolicyPersistence passwordPolicyPersistence;
1037            @BeanReference(type = PasswordPolicyRelPersistence.class)
1038            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1039            @BeanReference(type = PasswordTrackerPersistence.class)
1040            protected PasswordTrackerPersistence passwordTrackerPersistence;
1041            @BeanReference(type = PermissionPersistence.class)
1042            protected PermissionPersistence permissionPersistence;
1043            @BeanReference(type = PhonePersistence.class)
1044            protected PhonePersistence phonePersistence;
1045            @BeanReference(type = PluginSettingPersistence.class)
1046            protected PluginSettingPersistence pluginSettingPersistence;
1047            @BeanReference(type = PortletPersistence.class)
1048            protected PortletPersistence portletPersistence;
1049            @BeanReference(type = PortletItemPersistence.class)
1050            protected PortletItemPersistence portletItemPersistence;
1051            @BeanReference(type = PortletPreferencesPersistence.class)
1052            protected PortletPreferencesPersistence portletPreferencesPersistence;
1053            @BeanReference(type = RegionPersistence.class)
1054            protected RegionPersistence regionPersistence;
1055            @BeanReference(type = ReleasePersistence.class)
1056            protected ReleasePersistence releasePersistence;
1057            @BeanReference(type = ResourcePersistence.class)
1058            protected ResourcePersistence resourcePersistence;
1059            @BeanReference(type = ResourceActionPersistence.class)
1060            protected ResourceActionPersistence resourceActionPersistence;
1061            @BeanReference(type = ResourceCodePersistence.class)
1062            protected ResourceCodePersistence resourceCodePersistence;
1063            @BeanReference(type = ResourcePermissionPersistence.class)
1064            protected ResourcePermissionPersistence resourcePermissionPersistence;
1065            @BeanReference(type = RolePersistence.class)
1066            protected RolePersistence rolePersistence;
1067            @BeanReference(type = ServiceComponentPersistence.class)
1068            protected ServiceComponentPersistence serviceComponentPersistence;
1069            @BeanReference(type = ShardPersistence.class)
1070            protected ShardPersistence shardPersistence;
1071            @BeanReference(type = SubscriptionPersistence.class)
1072            protected SubscriptionPersistence subscriptionPersistence;
1073            @BeanReference(type = TicketPersistence.class)
1074            protected TicketPersistence ticketPersistence;
1075            @BeanReference(type = TeamPersistence.class)
1076            protected TeamPersistence teamPersistence;
1077            @BeanReference(type = UserPersistence.class)
1078            protected UserPersistence userPersistence;
1079            @BeanReference(type = UserGroupPersistence.class)
1080            protected UserGroupPersistence userGroupPersistence;
1081            @BeanReference(type = UserGroupGroupRolePersistence.class)
1082            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1083            @BeanReference(type = UserGroupRolePersistence.class)
1084            protected UserGroupRolePersistence userGroupRolePersistence;
1085            @BeanReference(type = UserIdMapperPersistence.class)
1086            protected UserIdMapperPersistence userIdMapperPersistence;
1087            @BeanReference(type = UserTrackerPersistence.class)
1088            protected UserTrackerPersistence userTrackerPersistence;
1089            @BeanReference(type = UserTrackerPathPersistence.class)
1090            protected UserTrackerPathPersistence userTrackerPathPersistence;
1091            @BeanReference(type = WebDAVPropsPersistence.class)
1092            protected WebDAVPropsPersistence webDAVPropsPersistence;
1093            @BeanReference(type = WebsitePersistence.class)
1094            protected WebsitePersistence websitePersistence;
1095            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1096            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1097            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1098            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1099            private static final String _SQL_SELECT_LISTTYPE = "SELECT listType FROM ListType listType";
1100            private static final String _SQL_SELECT_LISTTYPE_WHERE = "SELECT listType FROM ListType listType WHERE ";
1101            private static final String _SQL_COUNT_LISTTYPE = "SELECT COUNT(listType) FROM ListType listType";
1102            private static final String _SQL_COUNT_LISTTYPE_WHERE = "SELECT COUNT(listType) FROM ListType listType WHERE ";
1103            private static final String _FINDER_COLUMN_TYPE_TYPE_1 = "listType.type IS NULL";
1104            private static final String _FINDER_COLUMN_TYPE_TYPE_2 = "listType.type = ?";
1105            private static final String _FINDER_COLUMN_TYPE_TYPE_3 = "(listType.type IS NULL OR listType.type = ?)";
1106            private static final String _ORDER_BY_ENTITY_ALIAS = "listType.";
1107            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ListType exists with the primary key ";
1108            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ListType exists with the key {";
1109            private static Log _log = LogFactoryUtil.getLog(ListTypePersistenceImpl.class);
1110    }