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