001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
419    
420                            try {
421                                    session = openSession();
422    
423                                    StringBundler query = null;
424    
425                                    if (orderByComparator != null) {
426                                            query = new StringBundler(3 +
427                                                            (orderByComparator.getOrderByFields().length * 3));
428                                    }
429                                    else {
430                                            query = new StringBundler(3);
431                                    }
432    
433                                    query.append(_SQL_SELECT_LISTTYPE_WHERE);
434    
435                                    if (type == null) {
436                                            query.append(_FINDER_COLUMN_TYPE_TYPE_1);
437                                    }
438                                    else {
439                                            if (type.equals(StringPool.BLANK)) {
440                                                    query.append(_FINDER_COLUMN_TYPE_TYPE_3);
441                                            }
442                                            else {
443                                                    query.append(_FINDER_COLUMN_TYPE_TYPE_2);
444                                            }
445                                    }
446    
447                                    if (orderByComparator != null) {
448                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
449                                                    orderByComparator);
450                                    }
451    
452                                    else {
453                                            query.append(ListTypeModelImpl.ORDER_BY_JPQL);
454                                    }
455    
456                                    String sql = query.toString();
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                                            list = new ArrayList<ListType>();
475                                    }
476    
477                                    cacheResult(list);
478    
479                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TYPE, finderArgs,
480                                            list);
481    
482                                    closeSession(session);
483                            }
484                    }
485    
486                    return list;
487            }
488    
489            /**
490             * Finds the first list type in the ordered set where type = &#63;.
491             *
492             * <p>
493             * 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.
494             * </p>
495             *
496             * @param type the type to search with
497             * @param orderByComparator the comparator to order the set by
498             * @return the first matching list type
499             * @throws com.liferay.portal.NoSuchListTypeException if a matching list type could not be found
500             * @throws SystemException if a system exception occurred
501             */
502            public ListType findByType_First(String type,
503                    OrderByComparator orderByComparator)
504                    throws NoSuchListTypeException, SystemException {
505                    List<ListType> list = findByType(type, 0, 1, orderByComparator);
506    
507                    if (list.isEmpty()) {
508                            StringBundler msg = new StringBundler(4);
509    
510                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
511    
512                            msg.append("type=");
513                            msg.append(type);
514    
515                            msg.append(StringPool.CLOSE_CURLY_BRACE);
516    
517                            throw new NoSuchListTypeException(msg.toString());
518                    }
519                    else {
520                            return list.get(0);
521                    }
522            }
523    
524            /**
525             * Finds the last list type in the ordered set where type = &#63;.
526             *
527             * <p>
528             * 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.
529             * </p>
530             *
531             * @param type the type to search with
532             * @param orderByComparator the comparator to order the set by
533             * @return the last matching list type
534             * @throws com.liferay.portal.NoSuchListTypeException if a matching list type could not be found
535             * @throws SystemException if a system exception occurred
536             */
537            public ListType findByType_Last(String type,
538                    OrderByComparator orderByComparator)
539                    throws NoSuchListTypeException, SystemException {
540                    int count = countByType(type);
541    
542                    List<ListType> list = findByType(type, count - 1, count,
543                                    orderByComparator);
544    
545                    if (list.isEmpty()) {
546                            StringBundler msg = new StringBundler(4);
547    
548                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
549    
550                            msg.append("type=");
551                            msg.append(type);
552    
553                            msg.append(StringPool.CLOSE_CURLY_BRACE);
554    
555                            throw new NoSuchListTypeException(msg.toString());
556                    }
557                    else {
558                            return list.get(0);
559                    }
560            }
561    
562            /**
563             * Finds the list types before and after the current list type in the ordered set where type = &#63;.
564             *
565             * <p>
566             * 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.
567             * </p>
568             *
569             * @param listTypeId the primary key of the current list type
570             * @param type the type to search with
571             * @param orderByComparator the comparator to order the set by
572             * @return the previous, current, and next list type
573             * @throws com.liferay.portal.NoSuchListTypeException if a list type with the primary key could not be found
574             * @throws SystemException if a system exception occurred
575             */
576            public ListType[] findByType_PrevAndNext(int listTypeId, String type,
577                    OrderByComparator orderByComparator)
578                    throws NoSuchListTypeException, SystemException {
579                    ListType listType = findByPrimaryKey(listTypeId);
580    
581                    Session session = null;
582    
583                    try {
584                            session = openSession();
585    
586                            ListType[] array = new ListTypeImpl[3];
587    
588                            array[0] = getByType_PrevAndNext(session, listType, type,
589                                            orderByComparator, true);
590    
591                            array[1] = listType;
592    
593                            array[2] = getByType_PrevAndNext(session, listType, type,
594                                            orderByComparator, false);
595    
596                            return array;
597                    }
598                    catch (Exception e) {
599                            throw processException(e);
600                    }
601                    finally {
602                            closeSession(session);
603                    }
604            }
605    
606            protected ListType getByType_PrevAndNext(Session session,
607                    ListType listType, String type, OrderByComparator orderByComparator,
608                    boolean previous) {
609                    StringBundler query = null;
610    
611                    if (orderByComparator != null) {
612                            query = new StringBundler(6 +
613                                            (orderByComparator.getOrderByFields().length * 6));
614                    }
615                    else {
616                            query = new StringBundler(3);
617                    }
618    
619                    query.append(_SQL_SELECT_LISTTYPE_WHERE);
620    
621                    if (type == null) {
622                            query.append(_FINDER_COLUMN_TYPE_TYPE_1);
623                    }
624                    else {
625                            if (type.equals(StringPool.BLANK)) {
626                                    query.append(_FINDER_COLUMN_TYPE_TYPE_3);
627                            }
628                            else {
629                                    query.append(_FINDER_COLUMN_TYPE_TYPE_2);
630                            }
631                    }
632    
633                    if (orderByComparator != null) {
634                            String[] orderByFields = orderByComparator.getOrderByFields();
635    
636                            if (orderByFields.length > 0) {
637                                    query.append(WHERE_AND);
638                            }
639    
640                            for (int i = 0; i < orderByFields.length; i++) {
641                                    query.append(_ORDER_BY_ENTITY_ALIAS);
642                                    query.append(orderByFields[i]);
643    
644                                    if ((i + 1) < orderByFields.length) {
645                                            if (orderByComparator.isAscending() ^ previous) {
646                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
647                                            }
648                                            else {
649                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
650                                            }
651                                    }
652                                    else {
653                                            if (orderByComparator.isAscending() ^ previous) {
654                                                    query.append(WHERE_GREATER_THAN);
655                                            }
656                                            else {
657                                                    query.append(WHERE_LESSER_THAN);
658                                            }
659                                    }
660                            }
661    
662                            query.append(ORDER_BY_CLAUSE);
663    
664                            for (int i = 0; i < orderByFields.length; i++) {
665                                    query.append(_ORDER_BY_ENTITY_ALIAS);
666                                    query.append(orderByFields[i]);
667    
668                                    if ((i + 1) < orderByFields.length) {
669                                            if (orderByComparator.isAscending() ^ previous) {
670                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
671                                            }
672                                            else {
673                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
674                                            }
675                                    }
676                                    else {
677                                            if (orderByComparator.isAscending() ^ previous) {
678                                                    query.append(ORDER_BY_ASC);
679                                            }
680                                            else {
681                                                    query.append(ORDER_BY_DESC);
682                                            }
683                                    }
684                            }
685                    }
686    
687                    else {
688                            query.append(ListTypeModelImpl.ORDER_BY_JPQL);
689                    }
690    
691                    String sql = query.toString();
692    
693                    Query q = session.createQuery(sql);
694    
695                    q.setFirstResult(0);
696                    q.setMaxResults(2);
697    
698                    QueryPos qPos = QueryPos.getInstance(q);
699    
700                    if (type != null) {
701                            qPos.add(type);
702                    }
703    
704                    if (orderByComparator != null) {
705                            Object[] values = orderByComparator.getOrderByValues(listType);
706    
707                            for (Object value : values) {
708                                    qPos.add(value);
709                            }
710                    }
711    
712                    List<ListType> list = q.list();
713    
714                    if (list.size() == 2) {
715                            return list.get(1);
716                    }
717                    else {
718                            return null;
719                    }
720            }
721    
722            /**
723             * Finds all the list types.
724             *
725             * @return the list types
726             * @throws SystemException if a system exception occurred
727             */
728            public List<ListType> findAll() throws SystemException {
729                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
730            }
731    
732            /**
733             * Finds a range of all the list types.
734             *
735             * <p>
736             * 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.
737             * </p>
738             *
739             * @param start the lower bound of the range of list types to return
740             * @param end the upper bound of the range of list types to return (not inclusive)
741             * @return the range of list types
742             * @throws SystemException if a system exception occurred
743             */
744            public List<ListType> findAll(int start, int end) throws SystemException {
745                    return findAll(start, end, null);
746            }
747    
748            /**
749             * Finds an ordered range of all the list types.
750             *
751             * <p>
752             * 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.
753             * </p>
754             *
755             * @param start the lower bound of the range of list types to return
756             * @param end the upper bound of the range of list types to return (not inclusive)
757             * @param orderByComparator the comparator to order the results by
758             * @return the ordered range of list types
759             * @throws SystemException if a system exception occurred
760             */
761            public List<ListType> findAll(int start, int end,
762                    OrderByComparator orderByComparator) throws SystemException {
763                    Object[] finderArgs = new Object[] {
764                                    String.valueOf(start), String.valueOf(end),
765                                    String.valueOf(orderByComparator)
766                            };
767    
768                    List<ListType> list = (List<ListType>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
769                                    finderArgs, this);
770    
771                    if (list == null) {
772                            Session session = null;
773    
774                            try {
775                                    session = openSession();
776    
777                                    StringBundler query = null;
778                                    String sql = null;
779    
780                                    if (orderByComparator != null) {
781                                            query = new StringBundler(2 +
782                                                            (orderByComparator.getOrderByFields().length * 3));
783    
784                                            query.append(_SQL_SELECT_LISTTYPE);
785    
786                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
787                                                    orderByComparator);
788    
789                                            sql = query.toString();
790                                    }
791                                    else {
792                                            sql = _SQL_SELECT_LISTTYPE.concat(ListTypeModelImpl.ORDER_BY_JPQL);
793                                    }
794    
795                                    Query q = session.createQuery(sql);
796    
797                                    if (orderByComparator == null) {
798                                            list = (List<ListType>)QueryUtil.list(q, getDialect(),
799                                                            start, end, false);
800    
801                                            Collections.sort(list);
802                                    }
803                                    else {
804                                            list = (List<ListType>)QueryUtil.list(q, getDialect(),
805                                                            start, end);
806                                    }
807                            }
808                            catch (Exception e) {
809                                    throw processException(e);
810                            }
811                            finally {
812                                    if (list == null) {
813                                            list = new ArrayList<ListType>();
814                                    }
815    
816                                    cacheResult(list);
817    
818                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
819    
820                                    closeSession(session);
821                            }
822                    }
823    
824                    return list;
825            }
826    
827            /**
828             * Removes all the list types where type = &#63; from the database.
829             *
830             * @param type the type to search with
831             * @throws SystemException if a system exception occurred
832             */
833            public void removeByType(String type) throws SystemException {
834                    for (ListType listType : findByType(type)) {
835                            remove(listType);
836                    }
837            }
838    
839            /**
840             * Removes all the list types from the database.
841             *
842             * @throws SystemException if a system exception occurred
843             */
844            public void removeAll() throws SystemException {
845                    for (ListType listType : findAll()) {
846                            remove(listType);
847                    }
848            }
849    
850            /**
851             * Counts all the list types where type = &#63;.
852             *
853             * @param type the type to search with
854             * @return the number of matching list types
855             * @throws SystemException if a system exception occurred
856             */
857            public int countByType(String type) throws SystemException {
858                    Object[] finderArgs = new Object[] { type };
859    
860                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TYPE,
861                                    finderArgs, this);
862    
863                    if (count == null) {
864                            Session session = null;
865    
866                            try {
867                                    session = openSession();
868    
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                                    Query q = session.createQuery(sql);
888    
889                                    QueryPos qPos = QueryPos.getInstance(q);
890    
891                                    if (type != null) {
892                                            qPos.add(type);
893                                    }
894    
895                                    count = (Long)q.uniqueResult();
896                            }
897                            catch (Exception e) {
898                                    throw processException(e);
899                            }
900                            finally {
901                                    if (count == null) {
902                                            count = Long.valueOf(0);
903                                    }
904    
905                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TYPE,
906                                            finderArgs, count);
907    
908                                    closeSession(session);
909                            }
910                    }
911    
912                    return count.intValue();
913            }
914    
915            /**
916             * Counts all the list types.
917             *
918             * @return the number of list types
919             * @throws SystemException if a system exception occurred
920             */
921            public int countAll() throws SystemException {
922                    Object[] finderArgs = new Object[0];
923    
924                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
925                                    finderArgs, this);
926    
927                    if (count == null) {
928                            Session session = null;
929    
930                            try {
931                                    session = openSession();
932    
933                                    Query q = session.createQuery(_SQL_COUNT_LISTTYPE);
934    
935                                    count = (Long)q.uniqueResult();
936                            }
937                            catch (Exception e) {
938                                    throw processException(e);
939                            }
940                            finally {
941                                    if (count == null) {
942                                            count = Long.valueOf(0);
943                                    }
944    
945                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
946                                            count);
947    
948                                    closeSession(session);
949                            }
950                    }
951    
952                    return count.intValue();
953            }
954    
955            /**
956             * Initializes the list type persistence.
957             */
958            public void afterPropertiesSet() {
959                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
960                                            com.liferay.portal.util.PropsUtil.get(
961                                                    "value.object.listener.com.liferay.portal.model.ListType")));
962    
963                    if (listenerClassNames.length > 0) {
964                            try {
965                                    List<ModelListener<ListType>> listenersList = new ArrayList<ModelListener<ListType>>();
966    
967                                    for (String listenerClassName : listenerClassNames) {
968                                            listenersList.add((ModelListener<ListType>)InstanceFactory.newInstance(
969                                                            listenerClassName));
970                                    }
971    
972                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
973                            }
974                            catch (Exception e) {
975                                    _log.error(e);
976                            }
977                    }
978            }
979    
980            public void destroy() {
981                    EntityCacheUtil.removeCache(ListTypeImpl.class.getName());
982                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
983                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
984            }
985    
986            @BeanReference(type = AccountPersistence.class)
987            protected AccountPersistence accountPersistence;
988            @BeanReference(type = AddressPersistence.class)
989            protected AddressPersistence addressPersistence;
990            @BeanReference(type = BrowserTrackerPersistence.class)
991            protected BrowserTrackerPersistence browserTrackerPersistence;
992            @BeanReference(type = ClassNamePersistence.class)
993            protected ClassNamePersistence classNamePersistence;
994            @BeanReference(type = ClusterGroupPersistence.class)
995            protected ClusterGroupPersistence clusterGroupPersistence;
996            @BeanReference(type = CompanyPersistence.class)
997            protected CompanyPersistence companyPersistence;
998            @BeanReference(type = ContactPersistence.class)
999            protected ContactPersistence contactPersistence;
1000            @BeanReference(type = CountryPersistence.class)
1001            protected CountryPersistence countryPersistence;
1002            @BeanReference(type = EmailAddressPersistence.class)
1003            protected EmailAddressPersistence emailAddressPersistence;
1004            @BeanReference(type = GroupPersistence.class)
1005            protected GroupPersistence groupPersistence;
1006            @BeanReference(type = ImagePersistence.class)
1007            protected ImagePersistence imagePersistence;
1008            @BeanReference(type = LayoutPersistence.class)
1009            protected LayoutPersistence layoutPersistence;
1010            @BeanReference(type = LayoutPrototypePersistence.class)
1011            protected LayoutPrototypePersistence layoutPrototypePersistence;
1012            @BeanReference(type = LayoutSetPersistence.class)
1013            protected LayoutSetPersistence layoutSetPersistence;
1014            @BeanReference(type = LayoutSetPrototypePersistence.class)
1015            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1016            @BeanReference(type = ListTypePersistence.class)
1017            protected ListTypePersistence listTypePersistence;
1018            @BeanReference(type = LockPersistence.class)
1019            protected LockPersistence lockPersistence;
1020            @BeanReference(type = MembershipRequestPersistence.class)
1021            protected MembershipRequestPersistence membershipRequestPersistence;
1022            @BeanReference(type = OrganizationPersistence.class)
1023            protected OrganizationPersistence organizationPersistence;
1024            @BeanReference(type = OrgGroupPermissionPersistence.class)
1025            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1026            @BeanReference(type = OrgGroupRolePersistence.class)
1027            protected OrgGroupRolePersistence orgGroupRolePersistence;
1028            @BeanReference(type = OrgLaborPersistence.class)
1029            protected OrgLaborPersistence orgLaborPersistence;
1030            @BeanReference(type = PasswordPolicyPersistence.class)
1031            protected PasswordPolicyPersistence passwordPolicyPersistence;
1032            @BeanReference(type = PasswordPolicyRelPersistence.class)
1033            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1034            @BeanReference(type = PasswordTrackerPersistence.class)
1035            protected PasswordTrackerPersistence passwordTrackerPersistence;
1036            @BeanReference(type = PermissionPersistence.class)
1037            protected PermissionPersistence permissionPersistence;
1038            @BeanReference(type = PhonePersistence.class)
1039            protected PhonePersistence phonePersistence;
1040            @BeanReference(type = PluginSettingPersistence.class)
1041            protected PluginSettingPersistence pluginSettingPersistence;
1042            @BeanReference(type = PortletPersistence.class)
1043            protected PortletPersistence portletPersistence;
1044            @BeanReference(type = PortletItemPersistence.class)
1045            protected PortletItemPersistence portletItemPersistence;
1046            @BeanReference(type = PortletPreferencesPersistence.class)
1047            protected PortletPreferencesPersistence portletPreferencesPersistence;
1048            @BeanReference(type = RegionPersistence.class)
1049            protected RegionPersistence regionPersistence;
1050            @BeanReference(type = ReleasePersistence.class)
1051            protected ReleasePersistence releasePersistence;
1052            @BeanReference(type = ResourcePersistence.class)
1053            protected ResourcePersistence resourcePersistence;
1054            @BeanReference(type = ResourceActionPersistence.class)
1055            protected ResourceActionPersistence resourceActionPersistence;
1056            @BeanReference(type = ResourceCodePersistence.class)
1057            protected ResourceCodePersistence resourceCodePersistence;
1058            @BeanReference(type = ResourcePermissionPersistence.class)
1059            protected ResourcePermissionPersistence resourcePermissionPersistence;
1060            @BeanReference(type = RolePersistence.class)
1061            protected RolePersistence rolePersistence;
1062            @BeanReference(type = ServiceComponentPersistence.class)
1063            protected ServiceComponentPersistence serviceComponentPersistence;
1064            @BeanReference(type = ShardPersistence.class)
1065            protected ShardPersistence shardPersistence;
1066            @BeanReference(type = SubscriptionPersistence.class)
1067            protected SubscriptionPersistence subscriptionPersistence;
1068            @BeanReference(type = TicketPersistence.class)
1069            protected TicketPersistence ticketPersistence;
1070            @BeanReference(type = TeamPersistence.class)
1071            protected TeamPersistence teamPersistence;
1072            @BeanReference(type = UserPersistence.class)
1073            protected UserPersistence userPersistence;
1074            @BeanReference(type = UserGroupPersistence.class)
1075            protected UserGroupPersistence userGroupPersistence;
1076            @BeanReference(type = UserGroupGroupRolePersistence.class)
1077            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1078            @BeanReference(type = UserGroupRolePersistence.class)
1079            protected UserGroupRolePersistence userGroupRolePersistence;
1080            @BeanReference(type = UserIdMapperPersistence.class)
1081            protected UserIdMapperPersistence userIdMapperPersistence;
1082            @BeanReference(type = UserTrackerPersistence.class)
1083            protected UserTrackerPersistence userTrackerPersistence;
1084            @BeanReference(type = UserTrackerPathPersistence.class)
1085            protected UserTrackerPathPersistence userTrackerPathPersistence;
1086            @BeanReference(type = WebDAVPropsPersistence.class)
1087            protected WebDAVPropsPersistence webDAVPropsPersistence;
1088            @BeanReference(type = WebsitePersistence.class)
1089            protected WebsitePersistence websitePersistence;
1090            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1091            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1092            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1093            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1094            private static final String _SQL_SELECT_LISTTYPE = "SELECT listType FROM ListType listType";
1095            private static final String _SQL_SELECT_LISTTYPE_WHERE = "SELECT listType FROM ListType listType WHERE ";
1096            private static final String _SQL_COUNT_LISTTYPE = "SELECT COUNT(listType) FROM ListType listType";
1097            private static final String _SQL_COUNT_LISTTYPE_WHERE = "SELECT COUNT(listType) FROM ListType listType WHERE ";
1098            private static final String _FINDER_COLUMN_TYPE_TYPE_1 = "listType.type IS NULL";
1099            private static final String _FINDER_COLUMN_TYPE_TYPE_2 = "listType.type = ?";
1100            private static final String _FINDER_COLUMN_TYPE_TYPE_3 = "(listType.type IS NULL OR listType.type = ?)";
1101            private static final String _ORDER_BY_ENTITY_ALIAS = "listType.";
1102            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ListType exists with the primary key ";
1103            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ListType exists with the key {";
1104            private static Log _log = LogFactoryUtil.getLog(ListTypePersistenceImpl.class);
1105    }