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