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