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