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