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