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