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