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