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