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