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