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.NoSuchPortletException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.InstanceFactory;
031    import com.liferay.portal.kernel.util.OrderByComparator;
032    import com.liferay.portal.kernel.util.StringBundler;
033    import com.liferay.portal.kernel.util.StringPool;
034    import com.liferay.portal.kernel.util.StringUtil;
035    import com.liferay.portal.kernel.util.UnmodifiableList;
036    import com.liferay.portal.kernel.util.Validator;
037    import com.liferay.portal.model.CacheModel;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.model.Portlet;
040    import com.liferay.portal.model.impl.PortletImpl;
041    import com.liferay.portal.model.impl.PortletModelImpl;
042    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * The persistence implementation for the portlet service.
052     *
053     * <p>
054     * Caching information and settings can be found in <code>portal.properties</code>
055     * </p>
056     *
057     * @author Brian Wing Shun Chan
058     * @see PortletPersistence
059     * @see PortletUtil
060     * @generated
061     */
062    public class PortletPersistenceImpl extends BasePersistenceImpl<Portlet>
063            implements PortletPersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * Never modify or reference this class directly. Always use {@link PortletUtil} to access the portlet persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = PortletImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
075                            PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
076                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
078                            PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
081                            PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
084                    new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
085                            PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
086                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
087                            new String[] {
088                                    Long.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_COMPANYID =
094                    new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
095                            PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
096                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
097                            new String[] { Long.class.getName() },
098                            PortletModelImpl.COMPANYID_COLUMN_BITMASK);
099            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
100                            PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
101                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
102                            new String[] { Long.class.getName() });
103    
104            /**
105             * Returns all the portlets where companyId = &#63;.
106             *
107             * @param companyId the company ID
108             * @return the matching portlets
109             * @throws SystemException if a system exception occurred
110             */
111            public List<Portlet> findByCompanyId(long companyId)
112                    throws SystemException {
113                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
114                            null);
115            }
116    
117            /**
118             * Returns a range of all the portlets where companyId = &#63;.
119             *
120             * <p>
121             * 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.PortletModelImpl}. 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.
122             * </p>
123             *
124             * @param companyId the company ID
125             * @param start the lower bound of the range of portlets
126             * @param end the upper bound of the range of portlets (not inclusive)
127             * @return the range of matching portlets
128             * @throws SystemException if a system exception occurred
129             */
130            public List<Portlet> findByCompanyId(long companyId, int start, int end)
131                    throws SystemException {
132                    return findByCompanyId(companyId, start, end, null);
133            }
134    
135            /**
136             * Returns an ordered range of all the portlets where companyId = &#63;.
137             *
138             * <p>
139             * 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.PortletModelImpl}. 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.
140             * </p>
141             *
142             * @param companyId the company ID
143             * @param start the lower bound of the range of portlets
144             * @param end the upper bound of the range of portlets (not inclusive)
145             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
146             * @return the ordered range of matching portlets
147             * @throws SystemException if a system exception occurred
148             */
149            public List<Portlet> findByCompanyId(long companyId, int start, int end,
150                    OrderByComparator orderByComparator) throws SystemException {
151                    boolean pagination = true;
152                    FinderPath finderPath = null;
153                    Object[] finderArgs = null;
154    
155                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
156                                    (orderByComparator == null)) {
157                            pagination = false;
158                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
159                            finderArgs = new Object[] { companyId };
160                    }
161                    else {
162                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
163                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
164                    }
165    
166                    List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(finderPath,
167                                    finderArgs, this);
168    
169                    if ((list != null) && !list.isEmpty()) {
170                            for (Portlet portlet : list) {
171                                    if ((companyId != portlet.getCompanyId())) {
172                                            list = null;
173    
174                                            break;
175                                    }
176                            }
177                    }
178    
179                    if (list == null) {
180                            StringBundler query = null;
181    
182                            if (orderByComparator != null) {
183                                    query = new StringBundler(3 +
184                                                    (orderByComparator.getOrderByFields().length * 3));
185                            }
186                            else {
187                                    query = new StringBundler(3);
188                            }
189    
190                            query.append(_SQL_SELECT_PORTLET_WHERE);
191    
192                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
193    
194                            if (orderByComparator != null) {
195                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
196                                            orderByComparator);
197                            }
198                            else
199                             if (pagination) {
200                                    query.append(PortletModelImpl.ORDER_BY_JPQL);
201                            }
202    
203                            String sql = query.toString();
204    
205                            Session session = null;
206    
207                            try {
208                                    session = openSession();
209    
210                                    Query q = session.createQuery(sql);
211    
212                                    QueryPos qPos = QueryPos.getInstance(q);
213    
214                                    qPos.add(companyId);
215    
216                                    if (!pagination) {
217                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
218                                                            start, end, false);
219    
220                                            Collections.sort(list);
221    
222                                            list = new UnmodifiableList<Portlet>(list);
223                                    }
224                                    else {
225                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
226                                                            start, end);
227                                    }
228    
229                                    cacheResult(list);
230    
231                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
232                            }
233                            catch (Exception e) {
234                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
235    
236                                    throw processException(e);
237                            }
238                            finally {
239                                    closeSession(session);
240                            }
241                    }
242    
243                    return list;
244            }
245    
246            /**
247             * Returns the first portlet in the ordered set where companyId = &#63;.
248             *
249             * @param companyId the company ID
250             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
251             * @return the first matching portlet
252             * @throws com.liferay.portal.NoSuchPortletException if a matching portlet could not be found
253             * @throws SystemException if a system exception occurred
254             */
255            public Portlet findByCompanyId_First(long companyId,
256                    OrderByComparator orderByComparator)
257                    throws NoSuchPortletException, SystemException {
258                    Portlet portlet = fetchByCompanyId_First(companyId, orderByComparator);
259    
260                    if (portlet != null) {
261                            return portlet;
262                    }
263    
264                    StringBundler msg = new StringBundler(4);
265    
266                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
267    
268                    msg.append("companyId=");
269                    msg.append(companyId);
270    
271                    msg.append(StringPool.CLOSE_CURLY_BRACE);
272    
273                    throw new NoSuchPortletException(msg.toString());
274            }
275    
276            /**
277             * Returns the first portlet in the ordered set where companyId = &#63;.
278             *
279             * @param companyId the company ID
280             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
281             * @return the first matching portlet, or <code>null</code> if a matching portlet could not be found
282             * @throws SystemException if a system exception occurred
283             */
284            public Portlet fetchByCompanyId_First(long companyId,
285                    OrderByComparator orderByComparator) throws SystemException {
286                    List<Portlet> list = findByCompanyId(companyId, 0, 1, orderByComparator);
287    
288                    if (!list.isEmpty()) {
289                            return list.get(0);
290                    }
291    
292                    return null;
293            }
294    
295            /**
296             * Returns the last portlet in the ordered set where companyId = &#63;.
297             *
298             * @param companyId the company ID
299             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
300             * @return the last matching portlet
301             * @throws com.liferay.portal.NoSuchPortletException if a matching portlet could not be found
302             * @throws SystemException if a system exception occurred
303             */
304            public Portlet findByCompanyId_Last(long companyId,
305                    OrderByComparator orderByComparator)
306                    throws NoSuchPortletException, SystemException {
307                    Portlet portlet = fetchByCompanyId_Last(companyId, orderByComparator);
308    
309                    if (portlet != null) {
310                            return portlet;
311                    }
312    
313                    StringBundler msg = new StringBundler(4);
314    
315                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
316    
317                    msg.append("companyId=");
318                    msg.append(companyId);
319    
320                    msg.append(StringPool.CLOSE_CURLY_BRACE);
321    
322                    throw new NoSuchPortletException(msg.toString());
323            }
324    
325            /**
326             * Returns the last portlet in the ordered set where companyId = &#63;.
327             *
328             * @param companyId the company ID
329             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
330             * @return the last matching portlet, or <code>null</code> if a matching portlet could not be found
331             * @throws SystemException if a system exception occurred
332             */
333            public Portlet fetchByCompanyId_Last(long companyId,
334                    OrderByComparator orderByComparator) throws SystemException {
335                    int count = countByCompanyId(companyId);
336    
337                    List<Portlet> list = findByCompanyId(companyId, count - 1, count,
338                                    orderByComparator);
339    
340                    if (!list.isEmpty()) {
341                            return list.get(0);
342                    }
343    
344                    return null;
345            }
346    
347            /**
348             * Returns the portlets before and after the current portlet in the ordered set where companyId = &#63;.
349             *
350             * @param id the primary key of the current portlet
351             * @param companyId the company ID
352             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
353             * @return the previous, current, and next portlet
354             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
355             * @throws SystemException if a system exception occurred
356             */
357            public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
358                    OrderByComparator orderByComparator)
359                    throws NoSuchPortletException, SystemException {
360                    Portlet portlet = findByPrimaryKey(id);
361    
362                    Session session = null;
363    
364                    try {
365                            session = openSession();
366    
367                            Portlet[] array = new PortletImpl[3];
368    
369                            array[0] = getByCompanyId_PrevAndNext(session, portlet, companyId,
370                                            orderByComparator, true);
371    
372                            array[1] = portlet;
373    
374                            array[2] = getByCompanyId_PrevAndNext(session, portlet, companyId,
375                                            orderByComparator, false);
376    
377                            return array;
378                    }
379                    catch (Exception e) {
380                            throw processException(e);
381                    }
382                    finally {
383                            closeSession(session);
384                    }
385            }
386    
387            protected Portlet getByCompanyId_PrevAndNext(Session session,
388                    Portlet portlet, long companyId, OrderByComparator orderByComparator,
389                    boolean previous) {
390                    StringBundler query = null;
391    
392                    if (orderByComparator != null) {
393                            query = new StringBundler(6 +
394                                            (orderByComparator.getOrderByFields().length * 6));
395                    }
396                    else {
397                            query = new StringBundler(3);
398                    }
399    
400                    query.append(_SQL_SELECT_PORTLET_WHERE);
401    
402                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
403    
404                    if (orderByComparator != null) {
405                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
406    
407                            if (orderByConditionFields.length > 0) {
408                                    query.append(WHERE_AND);
409                            }
410    
411                            for (int i = 0; i < orderByConditionFields.length; i++) {
412                                    query.append(_ORDER_BY_ENTITY_ALIAS);
413                                    query.append(orderByConditionFields[i]);
414    
415                                    if ((i + 1) < orderByConditionFields.length) {
416                                            if (orderByComparator.isAscending() ^ previous) {
417                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
418                                            }
419                                            else {
420                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
421                                            }
422                                    }
423                                    else {
424                                            if (orderByComparator.isAscending() ^ previous) {
425                                                    query.append(WHERE_GREATER_THAN);
426                                            }
427                                            else {
428                                                    query.append(WHERE_LESSER_THAN);
429                                            }
430                                    }
431                            }
432    
433                            query.append(ORDER_BY_CLAUSE);
434    
435                            String[] orderByFields = orderByComparator.getOrderByFields();
436    
437                            for (int i = 0; i < orderByFields.length; i++) {
438                                    query.append(_ORDER_BY_ENTITY_ALIAS);
439                                    query.append(orderByFields[i]);
440    
441                                    if ((i + 1) < orderByFields.length) {
442                                            if (orderByComparator.isAscending() ^ previous) {
443                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
444                                            }
445                                            else {
446                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
447                                            }
448                                    }
449                                    else {
450                                            if (orderByComparator.isAscending() ^ previous) {
451                                                    query.append(ORDER_BY_ASC);
452                                            }
453                                            else {
454                                                    query.append(ORDER_BY_DESC);
455                                            }
456                                    }
457                            }
458                    }
459                    else {
460                            query.append(PortletModelImpl.ORDER_BY_JPQL);
461                    }
462    
463                    String sql = query.toString();
464    
465                    Query q = session.createQuery(sql);
466    
467                    q.setFirstResult(0);
468                    q.setMaxResults(2);
469    
470                    QueryPos qPos = QueryPos.getInstance(q);
471    
472                    qPos.add(companyId);
473    
474                    if (orderByComparator != null) {
475                            Object[] values = orderByComparator.getOrderByConditionValues(portlet);
476    
477                            for (Object value : values) {
478                                    qPos.add(value);
479                            }
480                    }
481    
482                    List<Portlet> list = q.list();
483    
484                    if (list.size() == 2) {
485                            return list.get(1);
486                    }
487                    else {
488                            return null;
489                    }
490            }
491    
492            /**
493             * Removes all the portlets where companyId = &#63; from the database.
494             *
495             * @param companyId the company ID
496             * @throws SystemException if a system exception occurred
497             */
498            public void removeByCompanyId(long companyId) throws SystemException {
499                    for (Portlet portlet : findByCompanyId(companyId, QueryUtil.ALL_POS,
500                                    QueryUtil.ALL_POS, null)) {
501                            remove(portlet);
502                    }
503            }
504    
505            /**
506             * Returns the number of portlets where companyId = &#63;.
507             *
508             * @param companyId the company ID
509             * @return the number of matching portlets
510             * @throws SystemException if a system exception occurred
511             */
512            public int countByCompanyId(long companyId) throws SystemException {
513                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
514    
515                    Object[] finderArgs = new Object[] { companyId };
516    
517                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
518                                    this);
519    
520                    if (count == null) {
521                            StringBundler query = new StringBundler(2);
522    
523                            query.append(_SQL_COUNT_PORTLET_WHERE);
524    
525                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
526    
527                            String sql = query.toString();
528    
529                            Session session = null;
530    
531                            try {
532                                    session = openSession();
533    
534                                    Query q = session.createQuery(sql);
535    
536                                    QueryPos qPos = QueryPos.getInstance(q);
537    
538                                    qPos.add(companyId);
539    
540                                    count = (Long)q.uniqueResult();
541    
542                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
543                            }
544                            catch (Exception e) {
545                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
546    
547                                    throw processException(e);
548                            }
549                            finally {
550                                    closeSession(session);
551                            }
552                    }
553    
554                    return count.intValue();
555            }
556    
557            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "portlet.companyId = ?";
558            public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
559                            PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
560                            FINDER_CLASS_NAME_ENTITY, "fetchByC_P",
561                            new String[] { Long.class.getName(), String.class.getName() },
562                            PortletModelImpl.COMPANYID_COLUMN_BITMASK |
563                            PortletModelImpl.PORTLETID_COLUMN_BITMASK);
564            public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
565                            PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
566                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_P",
567                            new String[] { Long.class.getName(), String.class.getName() });
568    
569            /**
570             * Returns the portlet where companyId = &#63; and portletId = &#63; or throws a {@link com.liferay.portal.NoSuchPortletException} if it could not be found.
571             *
572             * @param companyId the company ID
573             * @param portletId the portlet ID
574             * @return the matching portlet
575             * @throws com.liferay.portal.NoSuchPortletException if a matching portlet could not be found
576             * @throws SystemException if a system exception occurred
577             */
578            public Portlet findByC_P(long companyId, String portletId)
579                    throws NoSuchPortletException, SystemException {
580                    Portlet portlet = fetchByC_P(companyId, portletId);
581    
582                    if (portlet == null) {
583                            StringBundler msg = new StringBundler(6);
584    
585                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
586    
587                            msg.append("companyId=");
588                            msg.append(companyId);
589    
590                            msg.append(", portletId=");
591                            msg.append(portletId);
592    
593                            msg.append(StringPool.CLOSE_CURLY_BRACE);
594    
595                            if (_log.isWarnEnabled()) {
596                                    _log.warn(msg.toString());
597                            }
598    
599                            throw new NoSuchPortletException(msg.toString());
600                    }
601    
602                    return portlet;
603            }
604    
605            /**
606             * Returns the portlet where companyId = &#63; and portletId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
607             *
608             * @param companyId the company ID
609             * @param portletId the portlet ID
610             * @return the matching portlet, or <code>null</code> if a matching portlet could not be found
611             * @throws SystemException if a system exception occurred
612             */
613            public Portlet fetchByC_P(long companyId, String portletId)
614                    throws SystemException {
615                    return fetchByC_P(companyId, portletId, true);
616            }
617    
618            /**
619             * Returns the portlet where companyId = &#63; and portletId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
620             *
621             * @param companyId the company ID
622             * @param portletId the portlet ID
623             * @param retrieveFromCache whether to use the finder cache
624             * @return the matching portlet, or <code>null</code> if a matching portlet could not be found
625             * @throws SystemException if a system exception occurred
626             */
627            public Portlet fetchByC_P(long companyId, String portletId,
628                    boolean retrieveFromCache) throws SystemException {
629                    Object[] finderArgs = new Object[] { companyId, portletId };
630    
631                    Object result = null;
632    
633                    if (retrieveFromCache) {
634                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
635                                            finderArgs, this);
636                    }
637    
638                    if (result instanceof Portlet) {
639                            Portlet portlet = (Portlet)result;
640    
641                            if ((companyId != portlet.getCompanyId()) ||
642                                            !Validator.equals(portletId, portlet.getPortletId())) {
643                                    result = null;
644                            }
645                    }
646    
647                    if (result == null) {
648                            StringBundler query = new StringBundler(4);
649    
650                            query.append(_SQL_SELECT_PORTLET_WHERE);
651    
652                            query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
653    
654                            boolean bindPortletId = false;
655    
656                            if (portletId == null) {
657                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
658                            }
659                            else if (portletId.equals(StringPool.BLANK)) {
660                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
661                            }
662                            else {
663                                    bindPortletId = true;
664    
665                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
666                            }
667    
668                            String sql = query.toString();
669    
670                            Session session = null;
671    
672                            try {
673                                    session = openSession();
674    
675                                    Query q = session.createQuery(sql);
676    
677                                    QueryPos qPos = QueryPos.getInstance(q);
678    
679                                    qPos.add(companyId);
680    
681                                    if (bindPortletId) {
682                                            qPos.add(portletId);
683                                    }
684    
685                                    List<Portlet> list = q.list();
686    
687                                    if (list.isEmpty()) {
688                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
689                                                    finderArgs, list);
690                                    }
691                                    else {
692                                            Portlet portlet = list.get(0);
693    
694                                            result = portlet;
695    
696                                            cacheResult(portlet);
697    
698                                            if ((portlet.getCompanyId() != companyId) ||
699                                                            (portlet.getPortletId() == null) ||
700                                                            !portlet.getPortletId().equals(portletId)) {
701                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
702                                                            finderArgs, portlet);
703                                            }
704                                    }
705                            }
706                            catch (Exception e) {
707                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
708                                            finderArgs);
709    
710                                    throw processException(e);
711                            }
712                            finally {
713                                    closeSession(session);
714                            }
715                    }
716    
717                    if (result instanceof List<?>) {
718                            return null;
719                    }
720                    else {
721                            return (Portlet)result;
722                    }
723            }
724    
725            /**
726             * Removes the portlet where companyId = &#63; and portletId = &#63; from the database.
727             *
728             * @param companyId the company ID
729             * @param portletId the portlet ID
730             * @return the portlet that was removed
731             * @throws SystemException if a system exception occurred
732             */
733            public Portlet removeByC_P(long companyId, String portletId)
734                    throws NoSuchPortletException, SystemException {
735                    Portlet portlet = findByC_P(companyId, portletId);
736    
737                    return remove(portlet);
738            }
739    
740            /**
741             * Returns the number of portlets where companyId = &#63; and portletId = &#63;.
742             *
743             * @param companyId the company ID
744             * @param portletId the portlet ID
745             * @return the number of matching portlets
746             * @throws SystemException if a system exception occurred
747             */
748            public int countByC_P(long companyId, String portletId)
749                    throws SystemException {
750                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_P;
751    
752                    Object[] finderArgs = new Object[] { companyId, portletId };
753    
754                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
755                                    this);
756    
757                    if (count == null) {
758                            StringBundler query = new StringBundler(3);
759    
760                            query.append(_SQL_COUNT_PORTLET_WHERE);
761    
762                            query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
763    
764                            boolean bindPortletId = false;
765    
766                            if (portletId == null) {
767                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
768                            }
769                            else if (portletId.equals(StringPool.BLANK)) {
770                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
771                            }
772                            else {
773                                    bindPortletId = true;
774    
775                                    query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
776                            }
777    
778                            String sql = query.toString();
779    
780                            Session session = null;
781    
782                            try {
783                                    session = openSession();
784    
785                                    Query q = session.createQuery(sql);
786    
787                                    QueryPos qPos = QueryPos.getInstance(q);
788    
789                                    qPos.add(companyId);
790    
791                                    if (bindPortletId) {
792                                            qPos.add(portletId);
793                                    }
794    
795                                    count = (Long)q.uniqueResult();
796    
797                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
798                            }
799                            catch (Exception e) {
800                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
801    
802                                    throw processException(e);
803                            }
804                            finally {
805                                    closeSession(session);
806                            }
807                    }
808    
809                    return count.intValue();
810            }
811    
812            private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "portlet.companyId = ? AND ";
813            private static final String _FINDER_COLUMN_C_P_PORTLETID_1 = "portlet.portletId IS NULL";
814            private static final String _FINDER_COLUMN_C_P_PORTLETID_2 = "portlet.portletId = ?";
815            private static final String _FINDER_COLUMN_C_P_PORTLETID_3 = "(portlet.portletId IS NULL OR portlet.portletId = '')";
816    
817            /**
818             * Caches the portlet in the entity cache if it is enabled.
819             *
820             * @param portlet the portlet
821             */
822            public void cacheResult(Portlet portlet) {
823                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
824                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
825    
826                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
827                            new Object[] { portlet.getCompanyId(), portlet.getPortletId() },
828                            portlet);
829    
830                    portlet.resetOriginalValues();
831            }
832    
833            /**
834             * Caches the portlets in the entity cache if it is enabled.
835             *
836             * @param portlets the portlets
837             */
838            public void cacheResult(List<Portlet> portlets) {
839                    for (Portlet portlet : portlets) {
840                            if (EntityCacheUtil.getResult(
841                                                    PortletModelImpl.ENTITY_CACHE_ENABLED,
842                                                    PortletImpl.class, portlet.getPrimaryKey()) == null) {
843                                    cacheResult(portlet);
844                            }
845                            else {
846                                    portlet.resetOriginalValues();
847                            }
848                    }
849            }
850    
851            /**
852             * Clears the cache for all portlets.
853             *
854             * <p>
855             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
856             * </p>
857             */
858            @Override
859            public void clearCache() {
860                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
861                            CacheRegistryUtil.clear(PortletImpl.class.getName());
862                    }
863    
864                    EntityCacheUtil.clearCache(PortletImpl.class.getName());
865    
866                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
867                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
868                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
869            }
870    
871            /**
872             * Clears the cache for the portlet.
873             *
874             * <p>
875             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
876             * </p>
877             */
878            @Override
879            public void clearCache(Portlet portlet) {
880                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
881                            PortletImpl.class, portlet.getPrimaryKey());
882    
883                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
884                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
885    
886                    clearUniqueFindersCache(portlet);
887            }
888    
889            @Override
890            public void clearCache(List<Portlet> portlets) {
891                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
892                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
893    
894                    for (Portlet portlet : portlets) {
895                            EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
896                                    PortletImpl.class, portlet.getPrimaryKey());
897    
898                            clearUniqueFindersCache(portlet);
899                    }
900            }
901    
902            protected void cacheUniqueFindersCache(Portlet portlet) {
903                    if (portlet.isNew()) {
904                            Object[] args = new Object[] {
905                                            portlet.getCompanyId(), portlet.getPortletId()
906                                    };
907    
908                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, args,
909                                    Long.valueOf(1));
910                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P, args, portlet);
911                    }
912                    else {
913                            PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
914    
915                            if ((portletModelImpl.getColumnBitmask() &
916                                            FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
917                                    Object[] args = new Object[] {
918                                                    portlet.getCompanyId(), portlet.getPortletId()
919                                            };
920    
921                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, args,
922                                            Long.valueOf(1));
923                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P, args,
924                                            portlet);
925                            }
926                    }
927            }
928    
929            protected void clearUniqueFindersCache(Portlet portlet) {
930                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
931    
932                    Object[] args = new Object[] {
933                                    portlet.getCompanyId(), portlet.getPortletId()
934                            };
935    
936                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
937                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
938    
939                    if ((portletModelImpl.getColumnBitmask() &
940                                    FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
941                            args = new Object[] {
942                                            portletModelImpl.getOriginalCompanyId(),
943                                            portletModelImpl.getOriginalPortletId()
944                                    };
945    
946                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
947                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
948                    }
949            }
950    
951            /**
952             * Creates a new portlet with the primary key. Does not add the portlet to the database.
953             *
954             * @param id the primary key for the new portlet
955             * @return the new portlet
956             */
957            public Portlet create(long id) {
958                    Portlet portlet = new PortletImpl();
959    
960                    portlet.setNew(true);
961                    portlet.setPrimaryKey(id);
962    
963                    return portlet;
964            }
965    
966            /**
967             * Removes the portlet with the primary key from the database. Also notifies the appropriate model listeners.
968             *
969             * @param id the primary key of the portlet
970             * @return the portlet that was removed
971             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
972             * @throws SystemException if a system exception occurred
973             */
974            public Portlet remove(long id)
975                    throws NoSuchPortletException, SystemException {
976                    return remove((Serializable)id);
977            }
978    
979            /**
980             * Removes the portlet with the primary key from the database. Also notifies the appropriate model listeners.
981             *
982             * @param primaryKey the primary key of the portlet
983             * @return the portlet that was removed
984             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
985             * @throws SystemException if a system exception occurred
986             */
987            @Override
988            public Portlet remove(Serializable primaryKey)
989                    throws NoSuchPortletException, SystemException {
990                    Session session = null;
991    
992                    try {
993                            session = openSession();
994    
995                            Portlet portlet = (Portlet)session.get(PortletImpl.class, primaryKey);
996    
997                            if (portlet == null) {
998                                    if (_log.isWarnEnabled()) {
999                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1000                                    }
1001    
1002                                    throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1003                                            primaryKey);
1004                            }
1005    
1006                            return remove(portlet);
1007                    }
1008                    catch (NoSuchPortletException nsee) {
1009                            throw nsee;
1010                    }
1011                    catch (Exception e) {
1012                            throw processException(e);
1013                    }
1014                    finally {
1015                            closeSession(session);
1016                    }
1017            }
1018    
1019            @Override
1020            protected Portlet removeImpl(Portlet portlet) throws SystemException {
1021                    portlet = toUnwrappedModel(portlet);
1022    
1023                    Session session = null;
1024    
1025                    try {
1026                            session = openSession();
1027    
1028                            if (!session.contains(portlet)) {
1029                                    portlet = (Portlet)session.get(PortletImpl.class,
1030                                                    portlet.getPrimaryKeyObj());
1031                            }
1032    
1033                            if (portlet != null) {
1034                                    session.delete(portlet);
1035                            }
1036                    }
1037                    catch (Exception e) {
1038                            throw processException(e);
1039                    }
1040                    finally {
1041                            closeSession(session);
1042                    }
1043    
1044                    if (portlet != null) {
1045                            clearCache(portlet);
1046                    }
1047    
1048                    return portlet;
1049            }
1050    
1051            @Override
1052            public Portlet updateImpl(com.liferay.portal.model.Portlet portlet)
1053                    throws SystemException {
1054                    portlet = toUnwrappedModel(portlet);
1055    
1056                    boolean isNew = portlet.isNew();
1057    
1058                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
1059    
1060                    Session session = null;
1061    
1062                    try {
1063                            session = openSession();
1064    
1065                            if (portlet.isNew()) {
1066                                    session.save(portlet);
1067    
1068                                    portlet.setNew(false);
1069                            }
1070                            else {
1071                                    session.merge(portlet);
1072                            }
1073                    }
1074                    catch (Exception e) {
1075                            throw processException(e);
1076                    }
1077                    finally {
1078                            closeSession(session);
1079                    }
1080    
1081                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1082    
1083                    if (isNew || !PortletModelImpl.COLUMN_BITMASK_ENABLED) {
1084                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1085                    }
1086    
1087                    else {
1088                            if ((portletModelImpl.getColumnBitmask() &
1089                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
1090                                    Object[] args = new Object[] {
1091                                                    portletModelImpl.getOriginalCompanyId()
1092                                            };
1093    
1094                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1095                                            args);
1096                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1097                                            args);
1098    
1099                                    args = new Object[] { portletModelImpl.getCompanyId() };
1100    
1101                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1102                                            args);
1103                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1104                                            args);
1105                            }
1106                    }
1107    
1108                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1109                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
1110    
1111                    clearUniqueFindersCache(portlet);
1112                    cacheUniqueFindersCache(portlet);
1113    
1114                    return portlet;
1115            }
1116    
1117            protected Portlet toUnwrappedModel(Portlet portlet) {
1118                    if (portlet instanceof PortletImpl) {
1119                            return portlet;
1120                    }
1121    
1122                    PortletImpl portletImpl = new PortletImpl();
1123    
1124                    portletImpl.setNew(portlet.isNew());
1125                    portletImpl.setPrimaryKey(portlet.getPrimaryKey());
1126    
1127                    portletImpl.setId(portlet.getId());
1128                    portletImpl.setCompanyId(portlet.getCompanyId());
1129                    portletImpl.setPortletId(portlet.getPortletId());
1130                    portletImpl.setRoles(portlet.getRoles());
1131                    portletImpl.setActive(portlet.isActive());
1132    
1133                    return portletImpl;
1134            }
1135    
1136            /**
1137             * Returns the portlet with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1138             *
1139             * @param primaryKey the primary key of the portlet
1140             * @return the portlet
1141             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1142             * @throws SystemException if a system exception occurred
1143             */
1144            @Override
1145            public Portlet findByPrimaryKey(Serializable primaryKey)
1146                    throws NoSuchPortletException, SystemException {
1147                    Portlet portlet = fetchByPrimaryKey(primaryKey);
1148    
1149                    if (portlet == null) {
1150                            if (_log.isWarnEnabled()) {
1151                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1152                            }
1153    
1154                            throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1155                                    primaryKey);
1156                    }
1157    
1158                    return portlet;
1159            }
1160    
1161            /**
1162             * Returns the portlet with the primary key or throws a {@link com.liferay.portal.NoSuchPortletException} if it could not be found.
1163             *
1164             * @param id the primary key of the portlet
1165             * @return the portlet
1166             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1167             * @throws SystemException if a system exception occurred
1168             */
1169            public Portlet findByPrimaryKey(long id)
1170                    throws NoSuchPortletException, SystemException {
1171                    return findByPrimaryKey((Serializable)id);
1172            }
1173    
1174            /**
1175             * Returns the portlet with the primary key or returns <code>null</code> if it could not be found.
1176             *
1177             * @param primaryKey the primary key of the portlet
1178             * @return the portlet, or <code>null</code> if a portlet with the primary key could not be found
1179             * @throws SystemException if a system exception occurred
1180             */
1181            @Override
1182            public Portlet fetchByPrimaryKey(Serializable primaryKey)
1183                    throws SystemException {
1184                    Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1185                                    PortletImpl.class, primaryKey);
1186    
1187                    if (portlet == _nullPortlet) {
1188                            return null;
1189                    }
1190    
1191                    if (portlet == null) {
1192                            Session session = null;
1193    
1194                            try {
1195                                    session = openSession();
1196    
1197                                    portlet = (Portlet)session.get(PortletImpl.class, primaryKey);
1198    
1199                                    if (portlet != null) {
1200                                            cacheResult(portlet);
1201                                    }
1202                                    else {
1203                                            EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1204                                                    PortletImpl.class, primaryKey, _nullPortlet);
1205                                    }
1206                            }
1207                            catch (Exception e) {
1208                                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1209                                            PortletImpl.class, primaryKey);
1210    
1211                                    throw processException(e);
1212                            }
1213                            finally {
1214                                    closeSession(session);
1215                            }
1216                    }
1217    
1218                    return portlet;
1219            }
1220    
1221            /**
1222             * Returns the portlet with the primary key or returns <code>null</code> if it could not be found.
1223             *
1224             * @param id the primary key of the portlet
1225             * @return the portlet, or <code>null</code> if a portlet with the primary key could not be found
1226             * @throws SystemException if a system exception occurred
1227             */
1228            public Portlet fetchByPrimaryKey(long id) throws SystemException {
1229                    return fetchByPrimaryKey((Serializable)id);
1230            }
1231    
1232            /**
1233             * Returns all the portlets.
1234             *
1235             * @return the portlets
1236             * @throws SystemException if a system exception occurred
1237             */
1238            public List<Portlet> findAll() throws SystemException {
1239                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1240            }
1241    
1242            /**
1243             * Returns a range of all the portlets.
1244             *
1245             * <p>
1246             * 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.PortletModelImpl}. 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.
1247             * </p>
1248             *
1249             * @param start the lower bound of the range of portlets
1250             * @param end the upper bound of the range of portlets (not inclusive)
1251             * @return the range of portlets
1252             * @throws SystemException if a system exception occurred
1253             */
1254            public List<Portlet> findAll(int start, int end) throws SystemException {
1255                    return findAll(start, end, null);
1256            }
1257    
1258            /**
1259             * Returns an ordered range of all the portlets.
1260             *
1261             * <p>
1262             * 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.PortletModelImpl}. 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.
1263             * </p>
1264             *
1265             * @param start the lower bound of the range of portlets
1266             * @param end the upper bound of the range of portlets (not inclusive)
1267             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1268             * @return the ordered range of portlets
1269             * @throws SystemException if a system exception occurred
1270             */
1271            public List<Portlet> findAll(int start, int end,
1272                    OrderByComparator orderByComparator) throws SystemException {
1273                    boolean pagination = true;
1274                    FinderPath finderPath = null;
1275                    Object[] finderArgs = null;
1276    
1277                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1278                                    (orderByComparator == null)) {
1279                            pagination = false;
1280                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1281                            finderArgs = FINDER_ARGS_EMPTY;
1282                    }
1283                    else {
1284                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1285                            finderArgs = new Object[] { start, end, orderByComparator };
1286                    }
1287    
1288                    List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(finderPath,
1289                                    finderArgs, this);
1290    
1291                    if (list == null) {
1292                            StringBundler query = null;
1293                            String sql = null;
1294    
1295                            if (orderByComparator != null) {
1296                                    query = new StringBundler(2 +
1297                                                    (orderByComparator.getOrderByFields().length * 3));
1298    
1299                                    query.append(_SQL_SELECT_PORTLET);
1300    
1301                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1302                                            orderByComparator);
1303    
1304                                    sql = query.toString();
1305                            }
1306                            else {
1307                                    sql = _SQL_SELECT_PORTLET;
1308    
1309                                    if (pagination) {
1310                                            sql = sql.concat(PortletModelImpl.ORDER_BY_JPQL);
1311                                    }
1312                            }
1313    
1314                            Session session = null;
1315    
1316                            try {
1317                                    session = openSession();
1318    
1319                                    Query q = session.createQuery(sql);
1320    
1321                                    if (!pagination) {
1322                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1323                                                            start, end, false);
1324    
1325                                            Collections.sort(list);
1326    
1327                                            list = new UnmodifiableList<Portlet>(list);
1328                                    }
1329                                    else {
1330                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1331                                                            start, end);
1332                                    }
1333    
1334                                    cacheResult(list);
1335    
1336                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1337                            }
1338                            catch (Exception e) {
1339                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1340    
1341                                    throw processException(e);
1342                            }
1343                            finally {
1344                                    closeSession(session);
1345                            }
1346                    }
1347    
1348                    return list;
1349            }
1350    
1351            /**
1352             * Removes all the portlets from the database.
1353             *
1354             * @throws SystemException if a system exception occurred
1355             */
1356            public void removeAll() throws SystemException {
1357                    for (Portlet portlet : findAll()) {
1358                            remove(portlet);
1359                    }
1360            }
1361    
1362            /**
1363             * Returns the number of portlets.
1364             *
1365             * @return the number of portlets
1366             * @throws SystemException if a system exception occurred
1367             */
1368            public int countAll() throws SystemException {
1369                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1370                                    FINDER_ARGS_EMPTY, this);
1371    
1372                    if (count == null) {
1373                            Session session = null;
1374    
1375                            try {
1376                                    session = openSession();
1377    
1378                                    Query q = session.createQuery(_SQL_COUNT_PORTLET);
1379    
1380                                    count = (Long)q.uniqueResult();
1381    
1382                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1383                                            FINDER_ARGS_EMPTY, count);
1384                            }
1385                            catch (Exception e) {
1386                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1387                                            FINDER_ARGS_EMPTY);
1388    
1389                                    throw processException(e);
1390                            }
1391                            finally {
1392                                    closeSession(session);
1393                            }
1394                    }
1395    
1396                    return count.intValue();
1397            }
1398    
1399            /**
1400             * Initializes the portlet persistence.
1401             */
1402            public void afterPropertiesSet() {
1403                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1404                                            com.liferay.portal.util.PropsUtil.get(
1405                                                    "value.object.listener.com.liferay.portal.model.Portlet")));
1406    
1407                    if (listenerClassNames.length > 0) {
1408                            try {
1409                                    List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
1410    
1411                                    for (String listenerClassName : listenerClassNames) {
1412                                            listenersList.add((ModelListener<Portlet>)InstanceFactory.newInstance(
1413                                                            listenerClassName));
1414                                    }
1415    
1416                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1417                            }
1418                            catch (Exception e) {
1419                                    _log.error(e);
1420                            }
1421                    }
1422            }
1423    
1424            public void destroy() {
1425                    EntityCacheUtil.removeCache(PortletImpl.class.getName());
1426                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1427                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1428                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1429            }
1430    
1431            private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1432            private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1433            private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1434            private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1435            private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1436            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1437            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1438            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1439            private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1440            private static Portlet _nullPortlet = new PortletImpl() {
1441                            @Override
1442                            public Object clone() {
1443                                    return this;
1444                            }
1445    
1446                            @Override
1447                            public CacheModel<Portlet> toCacheModel() {
1448                                    return _nullPortletCacheModel;
1449                            }
1450                    };
1451    
1452            private static CacheModel<Portlet> _nullPortletCacheModel = new CacheModel<Portlet>() {
1453                            public Portlet toEntityModel() {
1454                                    return _nullPortlet;
1455                            }
1456                    };
1457    }