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