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