1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.NoSuchPortletException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.annotation.BeanReference;
21  import com.liferay.portal.kernel.cache.CacheRegistry;
22  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
24  import com.liferay.portal.kernel.dao.orm.FinderPath;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.model.Portlet;
39  import com.liferay.portal.model.impl.PortletImpl;
40  import com.liferay.portal.model.impl.PortletModelImpl;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import java.io.Serializable;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.List;
48  
49  /**
50   * <a href="PortletPersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * <p>
53   * ServiceBuilder generated this class. Modifications in this class will be
54   * overwritten the next time is generated.
55   * </p>
56   *
57   * @author    Brian Wing Shun Chan
58   * @see       PortletPersistence
59   * @see       PortletUtil
60   * @generated
61   */
62  public class PortletPersistenceImpl extends BasePersistenceImpl<Portlet>
63      implements PortletPersistence {
64      public static final String FINDER_CLASS_NAME_ENTITY = PortletImpl.class.getName();
65      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
66          ".List";
67      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
68              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
69              "findByCompanyId", new String[] { Long.class.getName() });
70      public static final FinderPath FINDER_PATH_FIND_BY_OBC_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
71              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
72              "findByCompanyId",
73              new String[] {
74                  Long.class.getName(),
75                  
76              "java.lang.Integer", "java.lang.Integer",
77                  "com.liferay.portal.kernel.util.OrderByComparator"
78              });
79      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
80              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81              "countByCompanyId", new String[] { Long.class.getName() });
82      public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
83              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
84              "fetchByC_P",
85              new String[] { Long.class.getName(), String.class.getName() });
86      public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
87              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "countByC_P",
89              new String[] { Long.class.getName(), String.class.getName() });
90      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
91              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
92              "findAll", new String[0]);
93      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
94              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
95              "countAll", new String[0]);
96  
97      public void cacheResult(Portlet portlet) {
98          EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
99              PortletImpl.class, portlet.getPrimaryKey(), portlet);
100 
101         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
102             new Object[] {
103                 new Long(portlet.getCompanyId()),
104                 
105             portlet.getPortletId()
106             }, portlet);
107     }
108 
109     public void cacheResult(List<Portlet> portlets) {
110         for (Portlet portlet : portlets) {
111             if (EntityCacheUtil.getResult(
112                         PortletModelImpl.ENTITY_CACHE_ENABLED,
113                         PortletImpl.class, portlet.getPrimaryKey(), this) == null) {
114                 cacheResult(portlet);
115             }
116         }
117     }
118 
119     public void clearCache() {
120         CacheRegistry.clear(PortletImpl.class.getName());
121         EntityCacheUtil.clearCache(PortletImpl.class.getName());
122         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
123         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
124     }
125 
126     public Portlet create(long id) {
127         Portlet portlet = new PortletImpl();
128 
129         portlet.setNew(true);
130         portlet.setPrimaryKey(id);
131 
132         return portlet;
133     }
134 
135     public Portlet remove(Serializable primaryKey)
136         throws NoSuchModelException, SystemException {
137         return remove(((Long)primaryKey).longValue());
138     }
139 
140     public Portlet remove(long id)
141         throws NoSuchPortletException, SystemException {
142         Session session = null;
143 
144         try {
145             session = openSession();
146 
147             Portlet portlet = (Portlet)session.get(PortletImpl.class,
148                     new Long(id));
149 
150             if (portlet == null) {
151                 if (_log.isWarnEnabled()) {
152                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
153                 }
154 
155                 throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
156                     id);
157             }
158 
159             return remove(portlet);
160         }
161         catch (NoSuchPortletException nsee) {
162             throw nsee;
163         }
164         catch (Exception e) {
165             throw processException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172     public Portlet remove(Portlet portlet) throws SystemException {
173         for (ModelListener<Portlet> listener : listeners) {
174             listener.onBeforeRemove(portlet);
175         }
176 
177         portlet = removeImpl(portlet);
178 
179         for (ModelListener<Portlet> listener : listeners) {
180             listener.onAfterRemove(portlet);
181         }
182 
183         return portlet;
184     }
185 
186     protected Portlet removeImpl(Portlet portlet) throws SystemException {
187         portlet = toUnwrappedModel(portlet);
188 
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             if (portlet.isCachedModel() || BatchSessionUtil.isEnabled()) {
195                 Object staleObject = session.get(PortletImpl.class,
196                         portlet.getPrimaryKeyObj());
197 
198                 if (staleObject != null) {
199                     session.evict(staleObject);
200                 }
201             }
202 
203             session.delete(portlet);
204 
205             session.flush();
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213 
214         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
215 
216         PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
217 
218         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
219             new Object[] {
220                 new Long(portletModelImpl.getOriginalCompanyId()),
221                 
222             portletModelImpl.getOriginalPortletId()
223             });
224 
225         EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
226             PortletImpl.class, portlet.getPrimaryKey());
227 
228         return portlet;
229     }
230 
231     /**
232      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
233      */
234     public Portlet update(Portlet portlet) throws SystemException {
235         if (_log.isWarnEnabled()) {
236             _log.warn(
237                 "Using the deprecated update(Portlet portlet) method. Use update(Portlet portlet, boolean merge) instead.");
238         }
239 
240         return update(portlet, false);
241     }
242 
243     public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
244         boolean merge) throws SystemException {
245         portlet = toUnwrappedModel(portlet);
246 
247         boolean isNew = portlet.isNew();
248 
249         PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
250 
251         Session session = null;
252 
253         try {
254             session = openSession();
255 
256             BatchSessionUtil.update(session, portlet, merge);
257 
258             portlet.setNew(false);
259         }
260         catch (Exception e) {
261             throw processException(e);
262         }
263         finally {
264             closeSession(session);
265         }
266 
267         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
268 
269         EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
270             PortletImpl.class, portlet.getPrimaryKey(), portlet);
271 
272         if (!isNew &&
273                 ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
274                 !Validator.equals(portlet.getPortletId(),
275                     portletModelImpl.getOriginalPortletId()))) {
276             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
277                 new Object[] {
278                     new Long(portletModelImpl.getOriginalCompanyId()),
279                     
280                 portletModelImpl.getOriginalPortletId()
281                 });
282         }
283 
284         if (isNew ||
285                 ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
286                 !Validator.equals(portlet.getPortletId(),
287                     portletModelImpl.getOriginalPortletId()))) {
288             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
289                 new Object[] {
290                     new Long(portlet.getCompanyId()),
291                     
292                 portlet.getPortletId()
293                 }, portlet);
294         }
295 
296         return portlet;
297     }
298 
299     protected Portlet toUnwrappedModel(Portlet portlet) {
300         if (portlet instanceof PortletImpl) {
301             return portlet;
302         }
303 
304         PortletImpl portletImpl = new PortletImpl();
305 
306         portletImpl.setNew(portlet.isNew());
307         portletImpl.setPrimaryKey(portlet.getPrimaryKey());
308 
309         portletImpl.setId(portlet.getId());
310         portletImpl.setCompanyId(portlet.getCompanyId());
311         portletImpl.setPortletId(portlet.getPortletId());
312         portletImpl.setRoles(portlet.getRoles());
313         portletImpl.setActive(portlet.isActive());
314 
315         return portletImpl;
316     }
317 
318     public Portlet findByPrimaryKey(Serializable primaryKey)
319         throws NoSuchModelException, SystemException {
320         return findByPrimaryKey(((Long)primaryKey).longValue());
321     }
322 
323     public Portlet findByPrimaryKey(long id)
324         throws NoSuchPortletException, SystemException {
325         Portlet portlet = fetchByPrimaryKey(id);
326 
327         if (portlet == null) {
328             if (_log.isWarnEnabled()) {
329                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
330             }
331 
332             throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
333                 id);
334         }
335 
336         return portlet;
337     }
338 
339     public Portlet fetchByPrimaryKey(Serializable primaryKey)
340         throws SystemException {
341         return fetchByPrimaryKey(((Long)primaryKey).longValue());
342     }
343 
344     public Portlet fetchByPrimaryKey(long id) throws SystemException {
345         Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
346                 PortletImpl.class, id, this);
347 
348         if (portlet == null) {
349             Session session = null;
350 
351             try {
352                 session = openSession();
353 
354                 portlet = (Portlet)session.get(PortletImpl.class, new Long(id));
355             }
356             catch (Exception e) {
357                 throw processException(e);
358             }
359             finally {
360                 if (portlet != null) {
361                     cacheResult(portlet);
362                 }
363 
364                 closeSession(session);
365             }
366         }
367 
368         return portlet;
369     }
370 
371     public List<Portlet> findByCompanyId(long companyId)
372         throws SystemException {
373         Object[] finderArgs = new Object[] { new Long(companyId) };
374 
375         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
376                 finderArgs, this);
377 
378         if (list == null) {
379             Session session = null;
380 
381             try {
382                 session = openSession();
383 
384                 StringBundler query = new StringBundler(2);
385 
386                 query.append(_SQL_SELECT_PORTLET_WHERE);
387 
388                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
389 
390                 String sql = query.toString();
391 
392                 Query q = session.createQuery(sql);
393 
394                 QueryPos qPos = QueryPos.getInstance(q);
395 
396                 qPos.add(companyId);
397 
398                 list = q.list();
399             }
400             catch (Exception e) {
401                 throw processException(e);
402             }
403             finally {
404                 if (list == null) {
405                     list = new ArrayList<Portlet>();
406                 }
407 
408                 cacheResult(list);
409 
410                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
411                     finderArgs, list);
412 
413                 closeSession(session);
414             }
415         }
416 
417         return list;
418     }
419 
420     public List<Portlet> findByCompanyId(long companyId, int start, int end)
421         throws SystemException {
422         return findByCompanyId(companyId, start, end, null);
423     }
424 
425     public List<Portlet> findByCompanyId(long companyId, int start, int end,
426         OrderByComparator orderByComparator) throws SystemException {
427         Object[] finderArgs = new Object[] {
428                 new Long(companyId),
429                 
430                 String.valueOf(start), String.valueOf(end),
431                 String.valueOf(orderByComparator)
432             };
433 
434         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
435                 finderArgs, this);
436 
437         if (list == null) {
438             Session session = null;
439 
440             try {
441                 session = openSession();
442 
443                 StringBundler query = null;
444 
445                 if (orderByComparator != null) {
446                     query = new StringBundler(3 +
447                             (orderByComparator.getOrderByFields().length * 3));
448                 }
449                 else {
450                     query = new StringBundler(2);
451                 }
452 
453                 query.append(_SQL_SELECT_PORTLET_WHERE);
454 
455                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
456 
457                 if (orderByComparator != null) {
458                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
459                         orderByComparator);
460                 }
461 
462                 String sql = query.toString();
463 
464                 Query q = session.createQuery(sql);
465 
466                 QueryPos qPos = QueryPos.getInstance(q);
467 
468                 qPos.add(companyId);
469 
470                 list = (List<Portlet>)QueryUtil.list(q, getDialect(), start, end);
471             }
472             catch (Exception e) {
473                 throw processException(e);
474             }
475             finally {
476                 if (list == null) {
477                     list = new ArrayList<Portlet>();
478                 }
479 
480                 cacheResult(list);
481 
482                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
483                     finderArgs, list);
484 
485                 closeSession(session);
486             }
487         }
488 
489         return list;
490     }
491 
492     public Portlet findByCompanyId_First(long companyId,
493         OrderByComparator orderByComparator)
494         throws NoSuchPortletException, SystemException {
495         List<Portlet> list = findByCompanyId(companyId, 0, 1, orderByComparator);
496 
497         if (list.isEmpty()) {
498             StringBundler msg = new StringBundler(4);
499 
500             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
501 
502             msg.append("companyId=");
503             msg.append(companyId);
504 
505             msg.append(StringPool.CLOSE_CURLY_BRACE);
506 
507             throw new NoSuchPortletException(msg.toString());
508         }
509         else {
510             return list.get(0);
511         }
512     }
513 
514     public Portlet findByCompanyId_Last(long companyId,
515         OrderByComparator orderByComparator)
516         throws NoSuchPortletException, SystemException {
517         int count = countByCompanyId(companyId);
518 
519         List<Portlet> list = findByCompanyId(companyId, count - 1, count,
520                 orderByComparator);
521 
522         if (list.isEmpty()) {
523             StringBundler msg = new StringBundler(4);
524 
525             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
526 
527             msg.append("companyId=");
528             msg.append(companyId);
529 
530             msg.append(StringPool.CLOSE_CURLY_BRACE);
531 
532             throw new NoSuchPortletException(msg.toString());
533         }
534         else {
535             return list.get(0);
536         }
537     }
538 
539     public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
540         OrderByComparator orderByComparator)
541         throws NoSuchPortletException, SystemException {
542         Portlet portlet = findByPrimaryKey(id);
543 
544         int count = countByCompanyId(companyId);
545 
546         Session session = null;
547 
548         try {
549             session = openSession();
550 
551             StringBundler query = null;
552 
553             if (orderByComparator != null) {
554                 query = new StringBundler(3 +
555                         (orderByComparator.getOrderByFields().length * 3));
556             }
557             else {
558                 query = new StringBundler(2);
559             }
560 
561             query.append(_SQL_SELECT_PORTLET_WHERE);
562 
563             query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
564 
565             if (orderByComparator != null) {
566                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
567                     orderByComparator);
568             }
569 
570             String sql = query.toString();
571 
572             Query q = session.createQuery(sql);
573 
574             QueryPos qPos = QueryPos.getInstance(q);
575 
576             qPos.add(companyId);
577 
578             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
579                     orderByComparator, portlet);
580 
581             Portlet[] array = new PortletImpl[3];
582 
583             array[0] = (Portlet)objArray[0];
584             array[1] = (Portlet)objArray[1];
585             array[2] = (Portlet)objArray[2];
586 
587             return array;
588         }
589         catch (Exception e) {
590             throw processException(e);
591         }
592         finally {
593             closeSession(session);
594         }
595     }
596 
597     public Portlet findByC_P(long companyId, String portletId)
598         throws NoSuchPortletException, SystemException {
599         Portlet portlet = fetchByC_P(companyId, portletId);
600 
601         if (portlet == null) {
602             StringBundler msg = new StringBundler(6);
603 
604             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
605 
606             msg.append("companyId=");
607             msg.append(companyId);
608 
609             msg.append(", portletId=");
610             msg.append(portletId);
611 
612             msg.append(StringPool.CLOSE_CURLY_BRACE);
613 
614             if (_log.isWarnEnabled()) {
615                 _log.warn(msg.toString());
616             }
617 
618             throw new NoSuchPortletException(msg.toString());
619         }
620 
621         return portlet;
622     }
623 
624     public Portlet fetchByC_P(long companyId, String portletId)
625         throws SystemException {
626         return fetchByC_P(companyId, portletId, true);
627     }
628 
629     public Portlet fetchByC_P(long companyId, String portletId,
630         boolean retrieveFromCache) throws SystemException {
631         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
632 
633         Object result = null;
634 
635         if (retrieveFromCache) {
636             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
637                     finderArgs, this);
638         }
639 
640         if (result == null) {
641             Session session = null;
642 
643             try {
644                 session = openSession();
645 
646                 StringBundler query = new StringBundler(3);
647 
648                 query.append(_SQL_SELECT_PORTLET_WHERE);
649 
650                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
651 
652                 if (portletId == null) {
653                     query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
654                 }
655                 else {
656                     if (portletId.equals(StringPool.BLANK)) {
657                         query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
658                     }
659                     else {
660                         query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
661                     }
662                 }
663 
664                 String sql = query.toString();
665 
666                 Query q = session.createQuery(sql);
667 
668                 QueryPos qPos = QueryPos.getInstance(q);
669 
670                 qPos.add(companyId);
671 
672                 if (portletId != null) {
673                     qPos.add(portletId);
674                 }
675 
676                 List<Portlet> list = q.list();
677 
678                 result = list;
679 
680                 Portlet portlet = null;
681 
682                 if (list.isEmpty()) {
683                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
684                         finderArgs, list);
685                 }
686                 else {
687                     portlet = list.get(0);
688 
689                     cacheResult(portlet);
690 
691                     if ((portlet.getCompanyId() != companyId) ||
692                             (portlet.getPortletId() == null) ||
693                             !portlet.getPortletId().equals(portletId)) {
694                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
695                             finderArgs, portlet);
696                     }
697                 }
698 
699                 return portlet;
700             }
701             catch (Exception e) {
702                 throw processException(e);
703             }
704             finally {
705                 if (result == null) {
706                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
707                         finderArgs, new ArrayList<Portlet>());
708                 }
709 
710                 closeSession(session);
711             }
712         }
713         else {
714             if (result instanceof List<?>) {
715                 return null;
716             }
717             else {
718                 return (Portlet)result;
719             }
720         }
721     }
722 
723     public List<Portlet> findAll() throws SystemException {
724         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
725     }
726 
727     public List<Portlet> findAll(int start, int end) throws SystemException {
728         return findAll(start, end, null);
729     }
730 
731     public List<Portlet> findAll(int start, int end,
732         OrderByComparator orderByComparator) throws SystemException {
733         Object[] finderArgs = new Object[] {
734                 String.valueOf(start), String.valueOf(end),
735                 String.valueOf(orderByComparator)
736             };
737 
738         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
739                 finderArgs, this);
740 
741         if (list == null) {
742             Session session = null;
743 
744             try {
745                 session = openSession();
746 
747                 StringBundler query = null;
748                 String sql = null;
749 
750                 if (orderByComparator != null) {
751                     query = new StringBundler(2 +
752                             (orderByComparator.getOrderByFields().length * 3));
753 
754                     query.append(_SQL_SELECT_PORTLET);
755 
756                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
757                         orderByComparator);
758 
759                     sql = query.toString();
760                 }
761 
762                 sql = _SQL_SELECT_PORTLET;
763 
764                 Query q = session.createQuery(sql);
765 
766                 if (orderByComparator == null) {
767                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
768                             start, end, false);
769 
770                     Collections.sort(list);
771                 }
772                 else {
773                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
774                             start, end);
775                 }
776             }
777             catch (Exception e) {
778                 throw processException(e);
779             }
780             finally {
781                 if (list == null) {
782                     list = new ArrayList<Portlet>();
783                 }
784 
785                 cacheResult(list);
786 
787                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
788 
789                 closeSession(session);
790             }
791         }
792 
793         return list;
794     }
795 
796     public void removeByCompanyId(long companyId) throws SystemException {
797         for (Portlet portlet : findByCompanyId(companyId)) {
798             remove(portlet);
799         }
800     }
801 
802     public void removeByC_P(long companyId, String portletId)
803         throws NoSuchPortletException, SystemException {
804         Portlet portlet = findByC_P(companyId, portletId);
805 
806         remove(portlet);
807     }
808 
809     public void removeAll() throws SystemException {
810         for (Portlet portlet : findAll()) {
811             remove(portlet);
812         }
813     }
814 
815     public int countByCompanyId(long companyId) throws SystemException {
816         Object[] finderArgs = new Object[] { new Long(companyId) };
817 
818         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
819                 finderArgs, this);
820 
821         if (count == null) {
822             Session session = null;
823 
824             try {
825                 session = openSession();
826 
827                 StringBundler query = new StringBundler(2);
828 
829                 query.append(_SQL_COUNT_PORTLET_WHERE);
830 
831                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
832 
833                 String sql = query.toString();
834 
835                 Query q = session.createQuery(sql);
836 
837                 QueryPos qPos = QueryPos.getInstance(q);
838 
839                 qPos.add(companyId);
840 
841                 count = (Long)q.uniqueResult();
842             }
843             catch (Exception e) {
844                 throw processException(e);
845             }
846             finally {
847                 if (count == null) {
848                     count = Long.valueOf(0);
849                 }
850 
851                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
852                     finderArgs, count);
853 
854                 closeSession(session);
855             }
856         }
857 
858         return count.intValue();
859     }
860 
861     public int countByC_P(long companyId, String portletId)
862         throws SystemException {
863         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
864 
865         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
866                 finderArgs, this);
867 
868         if (count == null) {
869             Session session = null;
870 
871             try {
872                 session = openSession();
873 
874                 StringBundler query = new StringBundler(3);
875 
876                 query.append(_SQL_COUNT_PORTLET_WHERE);
877 
878                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
879 
880                 if (portletId == null) {
881                     query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
882                 }
883                 else {
884                     if (portletId.equals(StringPool.BLANK)) {
885                         query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
886                     }
887                     else {
888                         query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
889                     }
890                 }
891 
892                 String sql = query.toString();
893 
894                 Query q = session.createQuery(sql);
895 
896                 QueryPos qPos = QueryPos.getInstance(q);
897 
898                 qPos.add(companyId);
899 
900                 if (portletId != null) {
901                     qPos.add(portletId);
902                 }
903 
904                 count = (Long)q.uniqueResult();
905             }
906             catch (Exception e) {
907                 throw processException(e);
908             }
909             finally {
910                 if (count == null) {
911                     count = Long.valueOf(0);
912                 }
913 
914                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
915                     count);
916 
917                 closeSession(session);
918             }
919         }
920 
921         return count.intValue();
922     }
923 
924     public int countAll() throws SystemException {
925         Object[] finderArgs = new Object[0];
926 
927         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
928                 finderArgs, this);
929 
930         if (count == null) {
931             Session session = null;
932 
933             try {
934                 session = openSession();
935 
936                 Query q = session.createQuery(_SQL_COUNT_PORTLET);
937 
938                 count = (Long)q.uniqueResult();
939             }
940             catch (Exception e) {
941                 throw processException(e);
942             }
943             finally {
944                 if (count == null) {
945                     count = Long.valueOf(0);
946                 }
947 
948                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
949                     count);
950 
951                 closeSession(session);
952             }
953         }
954 
955         return count.intValue();
956     }
957 
958     public void afterPropertiesSet() {
959         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
960                     com.liferay.portal.util.PropsUtil.get(
961                         "value.object.listener.com.liferay.portal.model.Portlet")));
962 
963         if (listenerClassNames.length > 0) {
964             try {
965                 List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
966 
967                 for (String listenerClassName : listenerClassNames) {
968                     listenersList.add((ModelListener<Portlet>)Class.forName(
969                             listenerClassName).newInstance());
970                 }
971 
972                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
973             }
974             catch (Exception e) {
975                 _log.error(e);
976             }
977         }
978     }
979 
980     @BeanReference(type = AccountPersistence.class)
981     protected AccountPersistence accountPersistence;
982     @BeanReference(type = AddressPersistence.class)
983     protected AddressPersistence addressPersistence;
984     @BeanReference(type = BrowserTrackerPersistence.class)
985     protected BrowserTrackerPersistence browserTrackerPersistence;
986     @BeanReference(type = ClassNamePersistence.class)
987     protected ClassNamePersistence classNamePersistence;
988     @BeanReference(type = CompanyPersistence.class)
989     protected CompanyPersistence companyPersistence;
990     @BeanReference(type = ContactPersistence.class)
991     protected ContactPersistence contactPersistence;
992     @BeanReference(type = CountryPersistence.class)
993     protected CountryPersistence countryPersistence;
994     @BeanReference(type = EmailAddressPersistence.class)
995     protected EmailAddressPersistence emailAddressPersistence;
996     @BeanReference(type = GroupPersistence.class)
997     protected GroupPersistence groupPersistence;
998     @BeanReference(type = ImagePersistence.class)
999     protected ImagePersistence imagePersistence;
1000    @BeanReference(type = LayoutPersistence.class)
1001    protected LayoutPersistence layoutPersistence;
1002    @BeanReference(type = LayoutSetPersistence.class)
1003    protected LayoutSetPersistence layoutSetPersistence;
1004    @BeanReference(type = ListTypePersistence.class)
1005    protected ListTypePersistence listTypePersistence;
1006    @BeanReference(type = LockPersistence.class)
1007    protected LockPersistence lockPersistence;
1008    @BeanReference(type = MembershipRequestPersistence.class)
1009    protected MembershipRequestPersistence membershipRequestPersistence;
1010    @BeanReference(type = OrganizationPersistence.class)
1011    protected OrganizationPersistence organizationPersistence;
1012    @BeanReference(type = OrgGroupPermissionPersistence.class)
1013    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1014    @BeanReference(type = OrgGroupRolePersistence.class)
1015    protected OrgGroupRolePersistence orgGroupRolePersistence;
1016    @BeanReference(type = OrgLaborPersistence.class)
1017    protected OrgLaborPersistence orgLaborPersistence;
1018    @BeanReference(type = PasswordPolicyPersistence.class)
1019    protected PasswordPolicyPersistence passwordPolicyPersistence;
1020    @BeanReference(type = PasswordPolicyRelPersistence.class)
1021    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1022    @BeanReference(type = PasswordTrackerPersistence.class)
1023    protected PasswordTrackerPersistence passwordTrackerPersistence;
1024    @BeanReference(type = PermissionPersistence.class)
1025    protected PermissionPersistence permissionPersistence;
1026    @BeanReference(type = PhonePersistence.class)
1027    protected PhonePersistence phonePersistence;
1028    @BeanReference(type = PluginSettingPersistence.class)
1029    protected PluginSettingPersistence pluginSettingPersistence;
1030    @BeanReference(type = PortletPersistence.class)
1031    protected PortletPersistence portletPersistence;
1032    @BeanReference(type = PortletItemPersistence.class)
1033    protected PortletItemPersistence portletItemPersistence;
1034    @BeanReference(type = PortletPreferencesPersistence.class)
1035    protected PortletPreferencesPersistence portletPreferencesPersistence;
1036    @BeanReference(type = RegionPersistence.class)
1037    protected RegionPersistence regionPersistence;
1038    @BeanReference(type = ReleasePersistence.class)
1039    protected ReleasePersistence releasePersistence;
1040    @BeanReference(type = ResourcePersistence.class)
1041    protected ResourcePersistence resourcePersistence;
1042    @BeanReference(type = ResourceActionPersistence.class)
1043    protected ResourceActionPersistence resourceActionPersistence;
1044    @BeanReference(type = ResourceCodePersistence.class)
1045    protected ResourceCodePersistence resourceCodePersistence;
1046    @BeanReference(type = ResourcePermissionPersistence.class)
1047    protected ResourcePermissionPersistence resourcePermissionPersistence;
1048    @BeanReference(type = RolePersistence.class)
1049    protected RolePersistence rolePersistence;
1050    @BeanReference(type = ServiceComponentPersistence.class)
1051    protected ServiceComponentPersistence serviceComponentPersistence;
1052    @BeanReference(type = ShardPersistence.class)
1053    protected ShardPersistence shardPersistence;
1054    @BeanReference(type = SubscriptionPersistence.class)
1055    protected SubscriptionPersistence subscriptionPersistence;
1056    @BeanReference(type = UserPersistence.class)
1057    protected UserPersistence userPersistence;
1058    @BeanReference(type = UserGroupPersistence.class)
1059    protected UserGroupPersistence userGroupPersistence;
1060    @BeanReference(type = UserGroupGroupRolePersistence.class)
1061    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1062    @BeanReference(type = UserGroupRolePersistence.class)
1063    protected UserGroupRolePersistence userGroupRolePersistence;
1064    @BeanReference(type = UserIdMapperPersistence.class)
1065    protected UserIdMapperPersistence userIdMapperPersistence;
1066    @BeanReference(type = UserTrackerPersistence.class)
1067    protected UserTrackerPersistence userTrackerPersistence;
1068    @BeanReference(type = UserTrackerPathPersistence.class)
1069    protected UserTrackerPathPersistence userTrackerPathPersistence;
1070    @BeanReference(type = WebDAVPropsPersistence.class)
1071    protected WebDAVPropsPersistence webDAVPropsPersistence;
1072    @BeanReference(type = WebsitePersistence.class)
1073    protected WebsitePersistence websitePersistence;
1074    private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1075    private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1076    private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1077    private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1078    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "portlet.companyId = ?";
1079    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "portlet.companyId = ? AND ";
1080    private static final String _FINDER_COLUMN_C_P_PORTLETID_1 = "portlet.portletId IS NULL";
1081    private static final String _FINDER_COLUMN_C_P_PORTLETID_2 = "portlet.portletId = ?";
1082    private static final String _FINDER_COLUMN_C_P_PORTLETID_3 = "(portlet.portletId IS NULL OR portlet.portletId = ?)";
1083    private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1084    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1085    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1086    private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1087}