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