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