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            public PortletPersistenceImpl() {
839                    setModelClass(Portlet.class);
840            }
841    
842            /**
843             * Caches the portlet in the entity cache if it is enabled.
844             *
845             * @param portlet the portlet
846             */
847            @Override
848            public void cacheResult(Portlet portlet) {
849                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
850                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
851    
852                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
853                            new Object[] { portlet.getCompanyId(), portlet.getPortletId() },
854                            portlet);
855    
856                    portlet.resetOriginalValues();
857            }
858    
859            /**
860             * Caches the portlets in the entity cache if it is enabled.
861             *
862             * @param portlets the portlets
863             */
864            @Override
865            public void cacheResult(List<Portlet> portlets) {
866                    for (Portlet portlet : portlets) {
867                            if (EntityCacheUtil.getResult(
868                                                    PortletModelImpl.ENTITY_CACHE_ENABLED,
869                                                    PortletImpl.class, portlet.getPrimaryKey()) == null) {
870                                    cacheResult(portlet);
871                            }
872                            else {
873                                    portlet.resetOriginalValues();
874                            }
875                    }
876            }
877    
878            /**
879             * Clears the cache for all portlets.
880             *
881             * <p>
882             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
883             * </p>
884             */
885            @Override
886            public void clearCache() {
887                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
888                            CacheRegistryUtil.clear(PortletImpl.class.getName());
889                    }
890    
891                    EntityCacheUtil.clearCache(PortletImpl.class.getName());
892    
893                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
894                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
895                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
896            }
897    
898            /**
899             * Clears the cache for the portlet.
900             *
901             * <p>
902             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
903             * </p>
904             */
905            @Override
906            public void clearCache(Portlet portlet) {
907                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
908                            PortletImpl.class, portlet.getPrimaryKey());
909    
910                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
911                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
912    
913                    clearUniqueFindersCache(portlet);
914            }
915    
916            @Override
917            public void clearCache(List<Portlet> portlets) {
918                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
919                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
920    
921                    for (Portlet portlet : portlets) {
922                            EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
923                                    PortletImpl.class, portlet.getPrimaryKey());
924    
925                            clearUniqueFindersCache(portlet);
926                    }
927            }
928    
929            protected void cacheUniqueFindersCache(Portlet portlet) {
930                    if (portlet.isNew()) {
931                            Object[] args = new Object[] {
932                                            portlet.getCompanyId(), portlet.getPortletId()
933                                    };
934    
935                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, args,
936                                    Long.valueOf(1));
937                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P, args, portlet);
938                    }
939                    else {
940                            PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
941    
942                            if ((portletModelImpl.getColumnBitmask() &
943                                            FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
944                                    Object[] args = new Object[] {
945                                                    portlet.getCompanyId(), portlet.getPortletId()
946                                            };
947    
948                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, args,
949                                            Long.valueOf(1));
950                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P, args,
951                                            portlet);
952                            }
953                    }
954            }
955    
956            protected void clearUniqueFindersCache(Portlet portlet) {
957                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
958    
959                    Object[] args = new Object[] {
960                                    portlet.getCompanyId(), portlet.getPortletId()
961                            };
962    
963                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
964                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
965    
966                    if ((portletModelImpl.getColumnBitmask() &
967                                    FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
968                            args = new Object[] {
969                                            portletModelImpl.getOriginalCompanyId(),
970                                            portletModelImpl.getOriginalPortletId()
971                                    };
972    
973                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
974                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
975                    }
976            }
977    
978            /**
979             * Creates a new portlet with the primary key. Does not add the portlet to the database.
980             *
981             * @param id the primary key for the new portlet
982             * @return the new portlet
983             */
984            @Override
985            public Portlet create(long id) {
986                    Portlet portlet = new PortletImpl();
987    
988                    portlet.setNew(true);
989                    portlet.setPrimaryKey(id);
990    
991                    return portlet;
992            }
993    
994            /**
995             * Removes the portlet with the primary key from the database. Also notifies the appropriate model listeners.
996             *
997             * @param id the primary key of the portlet
998             * @return the portlet that was removed
999             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1000             * @throws SystemException if a system exception occurred
1001             */
1002            @Override
1003            public Portlet remove(long id)
1004                    throws NoSuchPortletException, SystemException {
1005                    return remove((Serializable)id);
1006            }
1007    
1008            /**
1009             * Removes the portlet with the primary key from the database. Also notifies the appropriate model listeners.
1010             *
1011             * @param primaryKey the primary key of the portlet
1012             * @return the portlet that was removed
1013             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1014             * @throws SystemException if a system exception occurred
1015             */
1016            @Override
1017            public Portlet remove(Serializable primaryKey)
1018                    throws NoSuchPortletException, SystemException {
1019                    Session session = null;
1020    
1021                    try {
1022                            session = openSession();
1023    
1024                            Portlet portlet = (Portlet)session.get(PortletImpl.class, primaryKey);
1025    
1026                            if (portlet == null) {
1027                                    if (_log.isWarnEnabled()) {
1028                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1029                                    }
1030    
1031                                    throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1032                                            primaryKey);
1033                            }
1034    
1035                            return remove(portlet);
1036                    }
1037                    catch (NoSuchPortletException nsee) {
1038                            throw nsee;
1039                    }
1040                    catch (Exception e) {
1041                            throw processException(e);
1042                    }
1043                    finally {
1044                            closeSession(session);
1045                    }
1046            }
1047    
1048            @Override
1049            protected Portlet removeImpl(Portlet portlet) throws SystemException {
1050                    portlet = toUnwrappedModel(portlet);
1051    
1052                    Session session = null;
1053    
1054                    try {
1055                            session = openSession();
1056    
1057                            if (!session.contains(portlet)) {
1058                                    portlet = (Portlet)session.get(PortletImpl.class,
1059                                                    portlet.getPrimaryKeyObj());
1060                            }
1061    
1062                            if (portlet != null) {
1063                                    session.delete(portlet);
1064                            }
1065                    }
1066                    catch (Exception e) {
1067                            throw processException(e);
1068                    }
1069                    finally {
1070                            closeSession(session);
1071                    }
1072    
1073                    if (portlet != null) {
1074                            clearCache(portlet);
1075                    }
1076    
1077                    return portlet;
1078            }
1079    
1080            @Override
1081            public Portlet updateImpl(com.liferay.portal.model.Portlet portlet)
1082                    throws SystemException {
1083                    portlet = toUnwrappedModel(portlet);
1084    
1085                    boolean isNew = portlet.isNew();
1086    
1087                    PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
1088    
1089                    Session session = null;
1090    
1091                    try {
1092                            session = openSession();
1093    
1094                            if (portlet.isNew()) {
1095                                    session.save(portlet);
1096    
1097                                    portlet.setNew(false);
1098                            }
1099                            else {
1100                                    session.merge(portlet);
1101                            }
1102                    }
1103                    catch (Exception e) {
1104                            throw processException(e);
1105                    }
1106                    finally {
1107                            closeSession(session);
1108                    }
1109    
1110                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1111    
1112                    if (isNew || !PortletModelImpl.COLUMN_BITMASK_ENABLED) {
1113                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1114                    }
1115    
1116                    else {
1117                            if ((portletModelImpl.getColumnBitmask() &
1118                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
1119                                    Object[] args = new Object[] {
1120                                                    portletModelImpl.getOriginalCompanyId()
1121                                            };
1122    
1123                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1124                                            args);
1125                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1126                                            args);
1127    
1128                                    args = new Object[] { portletModelImpl.getCompanyId() };
1129    
1130                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
1131                                            args);
1132                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
1133                                            args);
1134                            }
1135                    }
1136    
1137                    EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1138                            PortletImpl.class, portlet.getPrimaryKey(), portlet);
1139    
1140                    clearUniqueFindersCache(portlet);
1141                    cacheUniqueFindersCache(portlet);
1142    
1143                    return portlet;
1144            }
1145    
1146            protected Portlet toUnwrappedModel(Portlet portlet) {
1147                    if (portlet instanceof PortletImpl) {
1148                            return portlet;
1149                    }
1150    
1151                    PortletImpl portletImpl = new PortletImpl();
1152    
1153                    portletImpl.setNew(portlet.isNew());
1154                    portletImpl.setPrimaryKey(portlet.getPrimaryKey());
1155    
1156                    portletImpl.setId(portlet.getId());
1157                    portletImpl.setCompanyId(portlet.getCompanyId());
1158                    portletImpl.setPortletId(portlet.getPortletId());
1159                    portletImpl.setRoles(portlet.getRoles());
1160                    portletImpl.setActive(portlet.isActive());
1161    
1162                    return portletImpl;
1163            }
1164    
1165            /**
1166             * Returns the portlet with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1167             *
1168             * @param primaryKey the primary key of the portlet
1169             * @return the portlet
1170             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1171             * @throws SystemException if a system exception occurred
1172             */
1173            @Override
1174            public Portlet findByPrimaryKey(Serializable primaryKey)
1175                    throws NoSuchPortletException, SystemException {
1176                    Portlet portlet = fetchByPrimaryKey(primaryKey);
1177    
1178                    if (portlet == null) {
1179                            if (_log.isWarnEnabled()) {
1180                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1181                            }
1182    
1183                            throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1184                                    primaryKey);
1185                    }
1186    
1187                    return portlet;
1188            }
1189    
1190            /**
1191             * Returns the portlet with the primary key or throws a {@link com.liferay.portal.NoSuchPortletException} if it could not be found.
1192             *
1193             * @param id the primary key of the portlet
1194             * @return the portlet
1195             * @throws com.liferay.portal.NoSuchPortletException if a portlet with the primary key could not be found
1196             * @throws SystemException if a system exception occurred
1197             */
1198            @Override
1199            public Portlet findByPrimaryKey(long id)
1200                    throws NoSuchPortletException, SystemException {
1201                    return findByPrimaryKey((Serializable)id);
1202            }
1203    
1204            /**
1205             * Returns the portlet with the primary key or returns <code>null</code> if it could not be found.
1206             *
1207             * @param primaryKey the primary key of the portlet
1208             * @return the portlet, or <code>null</code> if a portlet with the primary key could not be found
1209             * @throws SystemException if a system exception occurred
1210             */
1211            @Override
1212            public Portlet fetchByPrimaryKey(Serializable primaryKey)
1213                    throws SystemException {
1214                    Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1215                                    PortletImpl.class, primaryKey);
1216    
1217                    if (portlet == _nullPortlet) {
1218                            return null;
1219                    }
1220    
1221                    if (portlet == null) {
1222                            Session session = null;
1223    
1224                            try {
1225                                    session = openSession();
1226    
1227                                    portlet = (Portlet)session.get(PortletImpl.class, primaryKey);
1228    
1229                                    if (portlet != null) {
1230                                            cacheResult(portlet);
1231                                    }
1232                                    else {
1233                                            EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1234                                                    PortletImpl.class, primaryKey, _nullPortlet);
1235                                    }
1236                            }
1237                            catch (Exception e) {
1238                                    EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
1239                                            PortletImpl.class, primaryKey);
1240    
1241                                    throw processException(e);
1242                            }
1243                            finally {
1244                                    closeSession(session);
1245                            }
1246                    }
1247    
1248                    return portlet;
1249            }
1250    
1251            /**
1252             * Returns the portlet with the primary key or returns <code>null</code> if it could not be found.
1253             *
1254             * @param id the primary key of the portlet
1255             * @return the portlet, or <code>null</code> if a portlet with the primary key could not be found
1256             * @throws SystemException if a system exception occurred
1257             */
1258            @Override
1259            public Portlet fetchByPrimaryKey(long id) throws SystemException {
1260                    return fetchByPrimaryKey((Serializable)id);
1261            }
1262    
1263            /**
1264             * Returns all the portlets.
1265             *
1266             * @return the portlets
1267             * @throws SystemException if a system exception occurred
1268             */
1269            @Override
1270            public List<Portlet> findAll() throws SystemException {
1271                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1272            }
1273    
1274            /**
1275             * Returns a range of all the portlets.
1276             *
1277             * <p>
1278             * 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.
1279             * </p>
1280             *
1281             * @param start the lower bound of the range of portlets
1282             * @param end the upper bound of the range of portlets (not inclusive)
1283             * @return the range of portlets
1284             * @throws SystemException if a system exception occurred
1285             */
1286            @Override
1287            public List<Portlet> findAll(int start, int end) throws SystemException {
1288                    return findAll(start, end, null);
1289            }
1290    
1291            /**
1292             * Returns an ordered range of all the portlets.
1293             *
1294             * <p>
1295             * 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.
1296             * </p>
1297             *
1298             * @param start the lower bound of the range of portlets
1299             * @param end the upper bound of the range of portlets (not inclusive)
1300             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1301             * @return the ordered range of portlets
1302             * @throws SystemException if a system exception occurred
1303             */
1304            @Override
1305            public List<Portlet> findAll(int start, int end,
1306                    OrderByComparator orderByComparator) throws SystemException {
1307                    boolean pagination = true;
1308                    FinderPath finderPath = null;
1309                    Object[] finderArgs = null;
1310    
1311                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1312                                    (orderByComparator == null)) {
1313                            pagination = false;
1314                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1315                            finderArgs = FINDER_ARGS_EMPTY;
1316                    }
1317                    else {
1318                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1319                            finderArgs = new Object[] { start, end, orderByComparator };
1320                    }
1321    
1322                    List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(finderPath,
1323                                    finderArgs, this);
1324    
1325                    if (list == null) {
1326                            StringBundler query = null;
1327                            String sql = null;
1328    
1329                            if (orderByComparator != null) {
1330                                    query = new StringBundler(2 +
1331                                                    (orderByComparator.getOrderByFields().length * 3));
1332    
1333                                    query.append(_SQL_SELECT_PORTLET);
1334    
1335                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1336                                            orderByComparator);
1337    
1338                                    sql = query.toString();
1339                            }
1340                            else {
1341                                    sql = _SQL_SELECT_PORTLET;
1342    
1343                                    if (pagination) {
1344                                            sql = sql.concat(PortletModelImpl.ORDER_BY_JPQL);
1345                                    }
1346                            }
1347    
1348                            Session session = null;
1349    
1350                            try {
1351                                    session = openSession();
1352    
1353                                    Query q = session.createQuery(sql);
1354    
1355                                    if (!pagination) {
1356                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1357                                                            start, end, false);
1358    
1359                                            Collections.sort(list);
1360    
1361                                            list = new UnmodifiableList<Portlet>(list);
1362                                    }
1363                                    else {
1364                                            list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1365                                                            start, end);
1366                                    }
1367    
1368                                    cacheResult(list);
1369    
1370                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1371                            }
1372                            catch (Exception e) {
1373                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1374    
1375                                    throw processException(e);
1376                            }
1377                            finally {
1378                                    closeSession(session);
1379                            }
1380                    }
1381    
1382                    return list;
1383            }
1384    
1385            /**
1386             * Removes all the portlets from the database.
1387             *
1388             * @throws SystemException if a system exception occurred
1389             */
1390            @Override
1391            public void removeAll() throws SystemException {
1392                    for (Portlet portlet : findAll()) {
1393                            remove(portlet);
1394                    }
1395            }
1396    
1397            /**
1398             * Returns the number of portlets.
1399             *
1400             * @return the number of portlets
1401             * @throws SystemException if a system exception occurred
1402             */
1403            @Override
1404            public int countAll() throws SystemException {
1405                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1406                                    FINDER_ARGS_EMPTY, this);
1407    
1408                    if (count == null) {
1409                            Session session = null;
1410    
1411                            try {
1412                                    session = openSession();
1413    
1414                                    Query q = session.createQuery(_SQL_COUNT_PORTLET);
1415    
1416                                    count = (Long)q.uniqueResult();
1417    
1418                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1419                                            FINDER_ARGS_EMPTY, count);
1420                            }
1421                            catch (Exception e) {
1422                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1423                                            FINDER_ARGS_EMPTY);
1424    
1425                                    throw processException(e);
1426                            }
1427                            finally {
1428                                    closeSession(session);
1429                            }
1430                    }
1431    
1432                    return count.intValue();
1433            }
1434    
1435            @Override
1436            protected Set<String> getBadColumnNames() {
1437                    return _badColumnNames;
1438            }
1439    
1440            /**
1441             * Initializes the portlet persistence.
1442             */
1443            public void afterPropertiesSet() {
1444                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1445                                            com.liferay.portal.util.PropsUtil.get(
1446                                                    "value.object.listener.com.liferay.portal.model.Portlet")));
1447    
1448                    if (listenerClassNames.length > 0) {
1449                            try {
1450                                    List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
1451    
1452                                    for (String listenerClassName : listenerClassNames) {
1453                                            listenersList.add((ModelListener<Portlet>)InstanceFactory.newInstance(
1454                                                            getClassLoader(), listenerClassName));
1455                                    }
1456    
1457                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1458                            }
1459                            catch (Exception e) {
1460                                    _log.error(e);
1461                            }
1462                    }
1463            }
1464    
1465            public void destroy() {
1466                    EntityCacheUtil.removeCache(PortletImpl.class.getName());
1467                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1468                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1469                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1470            }
1471    
1472            private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1473            private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1474            private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1475            private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1476            private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1477            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1478            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1479            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1480            private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1481            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1482                                    "id", "active"
1483                            });
1484            private static Portlet _nullPortlet = new PortletImpl() {
1485                            @Override
1486                            public Object clone() {
1487                                    return this;
1488                            }
1489    
1490                            @Override
1491                            public CacheModel<Portlet> toCacheModel() {
1492                                    return _nullPortletCacheModel;
1493                            }
1494                    };
1495    
1496            private static CacheModel<Portlet> _nullPortletCacheModel = new CacheModel<Portlet>() {
1497                            @Override
1498                            public Portlet toEntityModel() {
1499                                    return _nullPortlet;
1500                            }
1501                    };
1502    }