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