1   /**
2    * Copyright (c) 2000-2008 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.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.model.Portlet;
40  import com.liferay.portal.model.impl.PortletImpl;
41  import com.liferay.portal.model.impl.PortletModelImpl;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  /**
53   * <a href="PortletPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class PortletPersistenceImpl extends BasePersistenceImpl
59      implements PortletPersistence {
60      public Portlet create(long id) {
61          Portlet portlet = new PortletImpl();
62  
63          portlet.setNew(true);
64          portlet.setPrimaryKey(id);
65  
66          return portlet;
67      }
68  
69      public Portlet remove(long id)
70          throws NoSuchPortletException, SystemException {
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              Portlet portlet = (Portlet)session.get(PortletImpl.class,
77                      new Long(id));
78  
79              if (portlet == null) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn("No Portlet exists with the primary key " + id);
82                  }
83  
84                  throw new NoSuchPortletException(
85                      "No Portlet exists with the primary key " + id);
86              }
87  
88              return remove(portlet);
89          }
90          catch (NoSuchPortletException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public Portlet remove(Portlet portlet) throws SystemException {
102         if (_listeners.length > 0) {
103             for (ModelListener listener : _listeners) {
104                 listener.onBeforeRemove(portlet);
105             }
106         }
107 
108         portlet = removeImpl(portlet);
109 
110         if (_listeners.length > 0) {
111             for (ModelListener listener : _listeners) {
112                 listener.onAfterRemove(portlet);
113             }
114         }
115 
116         return portlet;
117     }
118 
119     protected Portlet removeImpl(Portlet portlet) throws SystemException {
120         Session session = null;
121 
122         try {
123             session = openSession();
124 
125             session.delete(portlet);
126 
127             session.flush();
128 
129             return portlet;
130         }
131         catch (Exception e) {
132             throw processException(e);
133         }
134         finally {
135             closeSession(session);
136 
137             FinderCacheUtil.clearCache(Portlet.class.getName());
138         }
139     }
140 
141     /**
142      * @deprecated Use <code>update(Portlet portlet, boolean merge)</code>.
143      */
144     public Portlet update(Portlet portlet) throws SystemException {
145         if (_log.isWarnEnabled()) {
146             _log.warn(
147                 "Using the deprecated update(Portlet portlet) method. Use update(Portlet portlet, boolean merge) instead.");
148         }
149 
150         return update(portlet, false);
151     }
152 
153     /**
154      * Add, update, or merge, the entity. This method also calls the model
155      * listeners to trigger the proper events associated with adding, deleting,
156      * or updating an entity.
157      *
158      * @param        portlet the entity to add, update, or merge
159      * @param        merge boolean value for whether to merge the entity. The
160      *                default value is false. Setting merge to true is more
161      *                expensive and should only be true when portlet is
162      *                transient. See LEP-5473 for a detailed discussion of this
163      *                method.
164      * @return        true if the portlet can be displayed via Ajax
165      */
166     public Portlet update(Portlet portlet, boolean merge)
167         throws SystemException {
168         boolean isNew = portlet.isNew();
169 
170         if (_listeners.length > 0) {
171             for (ModelListener listener : _listeners) {
172                 if (isNew) {
173                     listener.onBeforeCreate(portlet);
174                 }
175                 else {
176                     listener.onBeforeUpdate(portlet);
177                 }
178             }
179         }
180 
181         portlet = updateImpl(portlet, merge);
182 
183         if (_listeners.length > 0) {
184             for (ModelListener listener : _listeners) {
185                 if (isNew) {
186                     listener.onAfterCreate(portlet);
187                 }
188                 else {
189                     listener.onAfterUpdate(portlet);
190                 }
191             }
192         }
193 
194         return portlet;
195     }
196 
197     public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
198         boolean merge) throws SystemException {
199         Session session = null;
200 
201         try {
202             session = openSession();
203 
204             if (merge) {
205                 session.merge(portlet);
206             }
207             else {
208                 if (portlet.isNew()) {
209                     session.save(portlet);
210                 }
211             }
212 
213             session.flush();
214 
215             portlet.setNew(false);
216 
217             return portlet;
218         }
219         catch (Exception e) {
220             throw processException(e);
221         }
222         finally {
223             closeSession(session);
224 
225             FinderCacheUtil.clearCache(Portlet.class.getName());
226         }
227     }
228 
229     public Portlet findByPrimaryKey(long id)
230         throws NoSuchPortletException, SystemException {
231         Portlet portlet = fetchByPrimaryKey(id);
232 
233         if (portlet == null) {
234             if (_log.isWarnEnabled()) {
235                 _log.warn("No Portlet exists with the primary key " + id);
236             }
237 
238             throw new NoSuchPortletException(
239                 "No Portlet exists with the primary key " + id);
240         }
241 
242         return portlet;
243     }
244 
245     public Portlet fetchByPrimaryKey(long id) throws SystemException {
246         Session session = null;
247 
248         try {
249             session = openSession();
250 
251             return (Portlet)session.get(PortletImpl.class, new Long(id));
252         }
253         catch (Exception e) {
254             throw processException(e);
255         }
256         finally {
257             closeSession(session);
258         }
259     }
260 
261     public List<Portlet> findByCompanyId(long companyId)
262         throws SystemException {
263         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
264         String finderClassName = Portlet.class.getName();
265         String finderMethodName = "findByCompanyId";
266         String[] finderParams = new String[] { Long.class.getName() };
267         Object[] finderArgs = new Object[] { new Long(companyId) };
268 
269         Object result = null;
270 
271         if (finderClassNameCacheEnabled) {
272             result = FinderCacheUtil.getResult(finderClassName,
273                     finderMethodName, finderParams, finderArgs, this);
274         }
275 
276         if (result == null) {
277             Session session = null;
278 
279             try {
280                 session = openSession();
281 
282                 StringBuilder query = new StringBuilder();
283 
284                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
285 
286                 query.append("companyId = ?");
287 
288                 query.append(" ");
289 
290                 Query q = session.createQuery(query.toString());
291 
292                 QueryPos qPos = QueryPos.getInstance(q);
293 
294                 qPos.add(companyId);
295 
296                 List<Portlet> list = q.list();
297 
298                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
299                     finderClassName, finderMethodName, finderParams,
300                     finderArgs, list);
301 
302                 return list;
303             }
304             catch (Exception e) {
305                 throw processException(e);
306             }
307             finally {
308                 closeSession(session);
309             }
310         }
311         else {
312             return (List<Portlet>)result;
313         }
314     }
315 
316     public List<Portlet> findByCompanyId(long companyId, int start, int end)
317         throws SystemException {
318         return findByCompanyId(companyId, start, end, null);
319     }
320 
321     public List<Portlet> findByCompanyId(long companyId, int start, int end,
322         OrderByComparator obc) throws SystemException {
323         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
324         String finderClassName = Portlet.class.getName();
325         String finderMethodName = "findByCompanyId";
326         String[] finderParams = new String[] {
327                 Long.class.getName(),
328                 
329                 "java.lang.Integer", "java.lang.Integer",
330                 "com.liferay.portal.kernel.util.OrderByComparator"
331             };
332         Object[] finderArgs = new Object[] {
333                 new Long(companyId),
334                 
335                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
336             };
337 
338         Object result = null;
339 
340         if (finderClassNameCacheEnabled) {
341             result = FinderCacheUtil.getResult(finderClassName,
342                     finderMethodName, finderParams, finderArgs, this);
343         }
344 
345         if (result == null) {
346             Session session = null;
347 
348             try {
349                 session = openSession();
350 
351                 StringBuilder query = new StringBuilder();
352 
353                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
354 
355                 query.append("companyId = ?");
356 
357                 query.append(" ");
358 
359                 if (obc != null) {
360                     query.append("ORDER BY ");
361                     query.append(obc.getOrderBy());
362                 }
363 
364                 Query q = session.createQuery(query.toString());
365 
366                 QueryPos qPos = QueryPos.getInstance(q);
367 
368                 qPos.add(companyId);
369 
370                 List<Portlet> list = (List<Portlet>)QueryUtil.list(q,
371                         getDialect(), start, end);
372 
373                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
374                     finderClassName, finderMethodName, finderParams,
375                     finderArgs, list);
376 
377                 return list;
378             }
379             catch (Exception e) {
380                 throw processException(e);
381             }
382             finally {
383                 closeSession(session);
384             }
385         }
386         else {
387             return (List<Portlet>)result;
388         }
389     }
390 
391     public Portlet findByCompanyId_First(long companyId, OrderByComparator obc)
392         throws NoSuchPortletException, SystemException {
393         List<Portlet> list = findByCompanyId(companyId, 0, 1, obc);
394 
395         if (list.size() == 0) {
396             StringBuilder msg = new StringBuilder();
397 
398             msg.append("No Portlet exists with the key {");
399 
400             msg.append("companyId=" + companyId);
401 
402             msg.append(StringPool.CLOSE_CURLY_BRACE);
403 
404             throw new NoSuchPortletException(msg.toString());
405         }
406         else {
407             return list.get(0);
408         }
409     }
410 
411     public Portlet findByCompanyId_Last(long companyId, OrderByComparator obc)
412         throws NoSuchPortletException, SystemException {
413         int count = countByCompanyId(companyId);
414 
415         List<Portlet> list = findByCompanyId(companyId, count - 1, count, obc);
416 
417         if (list.size() == 0) {
418             StringBuilder msg = new StringBuilder();
419 
420             msg.append("No Portlet exists with the key {");
421 
422             msg.append("companyId=" + companyId);
423 
424             msg.append(StringPool.CLOSE_CURLY_BRACE);
425 
426             throw new NoSuchPortletException(msg.toString());
427         }
428         else {
429             return list.get(0);
430         }
431     }
432 
433     public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
434         OrderByComparator obc) throws NoSuchPortletException, SystemException {
435         Portlet portlet = findByPrimaryKey(id);
436 
437         int count = countByCompanyId(companyId);
438 
439         Session session = null;
440 
441         try {
442             session = openSession();
443 
444             StringBuilder query = new StringBuilder();
445 
446             query.append("FROM com.liferay.portal.model.Portlet WHERE ");
447 
448             query.append("companyId = ?");
449 
450             query.append(" ");
451 
452             if (obc != null) {
453                 query.append("ORDER BY ");
454                 query.append(obc.getOrderBy());
455             }
456 
457             Query q = session.createQuery(query.toString());
458 
459             QueryPos qPos = QueryPos.getInstance(q);
460 
461             qPos.add(companyId);
462 
463             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, portlet);
464 
465             Portlet[] array = new PortletImpl[3];
466 
467             array[0] = (Portlet)objArray[0];
468             array[1] = (Portlet)objArray[1];
469             array[2] = (Portlet)objArray[2];
470 
471             return array;
472         }
473         catch (Exception e) {
474             throw processException(e);
475         }
476         finally {
477             closeSession(session);
478         }
479     }
480 
481     public Portlet findByC_P(long companyId, String portletId)
482         throws NoSuchPortletException, SystemException {
483         Portlet portlet = fetchByC_P(companyId, portletId);
484 
485         if (portlet == null) {
486             StringBuilder msg = new StringBuilder();
487 
488             msg.append("No Portlet exists with the key {");
489 
490             msg.append("companyId=" + companyId);
491 
492             msg.append(", ");
493             msg.append("portletId=" + portletId);
494 
495             msg.append(StringPool.CLOSE_CURLY_BRACE);
496 
497             if (_log.isWarnEnabled()) {
498                 _log.warn(msg.toString());
499             }
500 
501             throw new NoSuchPortletException(msg.toString());
502         }
503 
504         return portlet;
505     }
506 
507     public Portlet fetchByC_P(long companyId, String portletId)
508         throws SystemException {
509         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
510         String finderClassName = Portlet.class.getName();
511         String finderMethodName = "fetchByC_P";
512         String[] finderParams = new String[] {
513                 Long.class.getName(), String.class.getName()
514             };
515         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
516 
517         Object result = null;
518 
519         if (finderClassNameCacheEnabled) {
520             result = FinderCacheUtil.getResult(finderClassName,
521                     finderMethodName, finderParams, finderArgs, this);
522         }
523 
524         if (result == null) {
525             Session session = null;
526 
527             try {
528                 session = openSession();
529 
530                 StringBuilder query = new StringBuilder();
531 
532                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
533 
534                 query.append("companyId = ?");
535 
536                 query.append(" AND ");
537 
538                 if (portletId == null) {
539                     query.append("portletId IS NULL");
540                 }
541                 else {
542                     query.append("portletId = ?");
543                 }
544 
545                 query.append(" ");
546 
547                 Query q = session.createQuery(query.toString());
548 
549                 QueryPos qPos = QueryPos.getInstance(q);
550 
551                 qPos.add(companyId);
552 
553                 if (portletId != null) {
554                     qPos.add(portletId);
555                 }
556 
557                 List<Portlet> list = q.list();
558 
559                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
560                     finderClassName, finderMethodName, finderParams,
561                     finderArgs, list);
562 
563                 if (list.size() == 0) {
564                     return null;
565                 }
566                 else {
567                     return list.get(0);
568                 }
569             }
570             catch (Exception e) {
571                 throw processException(e);
572             }
573             finally {
574                 closeSession(session);
575             }
576         }
577         else {
578             List<Portlet> list = (List<Portlet>)result;
579 
580             if (list.size() == 0) {
581                 return null;
582             }
583             else {
584                 return list.get(0);
585             }
586         }
587     }
588 
589     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
590         throws SystemException {
591         Session session = null;
592 
593         try {
594             session = openSession();
595 
596             dynamicQuery.compile(session);
597 
598             return dynamicQuery.list();
599         }
600         catch (Exception e) {
601             throw processException(e);
602         }
603         finally {
604             closeSession(session);
605         }
606     }
607 
608     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
609         int start, int end) throws SystemException {
610         Session session = null;
611 
612         try {
613             session = openSession();
614 
615             dynamicQuery.setLimit(start, end);
616 
617             dynamicQuery.compile(session);
618 
619             return dynamicQuery.list();
620         }
621         catch (Exception e) {
622             throw processException(e);
623         }
624         finally {
625             closeSession(session);
626         }
627     }
628 
629     public List<Portlet> findAll() throws SystemException {
630         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
631     }
632 
633     public List<Portlet> findAll(int start, int end) throws SystemException {
634         return findAll(start, end, null);
635     }
636 
637     public List<Portlet> findAll(int start, int end, OrderByComparator obc)
638         throws SystemException {
639         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
640         String finderClassName = Portlet.class.getName();
641         String finderMethodName = "findAll";
642         String[] finderParams = new String[] {
643                 "java.lang.Integer", "java.lang.Integer",
644                 "com.liferay.portal.kernel.util.OrderByComparator"
645             };
646         Object[] finderArgs = new Object[] {
647                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
648             };
649 
650         Object result = null;
651 
652         if (finderClassNameCacheEnabled) {
653             result = FinderCacheUtil.getResult(finderClassName,
654                     finderMethodName, finderParams, finderArgs, this);
655         }
656 
657         if (result == null) {
658             Session session = null;
659 
660             try {
661                 session = openSession();
662 
663                 StringBuilder query = new StringBuilder();
664 
665                 query.append("FROM com.liferay.portal.model.Portlet ");
666 
667                 if (obc != null) {
668                     query.append("ORDER BY ");
669                     query.append(obc.getOrderBy());
670                 }
671 
672                 Query q = session.createQuery(query.toString());
673 
674                 List<Portlet> list = (List<Portlet>)QueryUtil.list(q,
675                         getDialect(), start, end);
676 
677                 if (obc == null) {
678                     Collections.sort(list);
679                 }
680 
681                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
682                     finderClassName, finderMethodName, finderParams,
683                     finderArgs, list);
684 
685                 return list;
686             }
687             catch (Exception e) {
688                 throw processException(e);
689             }
690             finally {
691                 closeSession(session);
692             }
693         }
694         else {
695             return (List<Portlet>)result;
696         }
697     }
698 
699     public void removeByCompanyId(long companyId) throws SystemException {
700         for (Portlet portlet : findByCompanyId(companyId)) {
701             remove(portlet);
702         }
703     }
704 
705     public void removeByC_P(long companyId, String portletId)
706         throws NoSuchPortletException, SystemException {
707         Portlet portlet = findByC_P(companyId, portletId);
708 
709         remove(portlet);
710     }
711 
712     public void removeAll() throws SystemException {
713         for (Portlet portlet : findAll()) {
714             remove(portlet);
715         }
716     }
717 
718     public int countByCompanyId(long companyId) throws SystemException {
719         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
720         String finderClassName = Portlet.class.getName();
721         String finderMethodName = "countByCompanyId";
722         String[] finderParams = new String[] { Long.class.getName() };
723         Object[] finderArgs = new Object[] { new Long(companyId) };
724 
725         Object result = null;
726 
727         if (finderClassNameCacheEnabled) {
728             result = FinderCacheUtil.getResult(finderClassName,
729                     finderMethodName, finderParams, finderArgs, this);
730         }
731 
732         if (result == null) {
733             Session session = null;
734 
735             try {
736                 session = openSession();
737 
738                 StringBuilder query = new StringBuilder();
739 
740                 query.append("SELECT COUNT(*) ");
741                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
742 
743                 query.append("companyId = ?");
744 
745                 query.append(" ");
746 
747                 Query q = session.createQuery(query.toString());
748 
749                 QueryPos qPos = QueryPos.getInstance(q);
750 
751                 qPos.add(companyId);
752 
753                 Long count = null;
754 
755                 Iterator<Long> itr = q.list().iterator();
756 
757                 if (itr.hasNext()) {
758                     count = itr.next();
759                 }
760 
761                 if (count == null) {
762                     count = new Long(0);
763                 }
764 
765                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
766                     finderClassName, finderMethodName, finderParams,
767                     finderArgs, count);
768 
769                 return count.intValue();
770             }
771             catch (Exception e) {
772                 throw processException(e);
773             }
774             finally {
775                 closeSession(session);
776             }
777         }
778         else {
779             return ((Long)result).intValue();
780         }
781     }
782 
783     public int countByC_P(long companyId, String portletId)
784         throws SystemException {
785         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
786         String finderClassName = Portlet.class.getName();
787         String finderMethodName = "countByC_P";
788         String[] finderParams = new String[] {
789                 Long.class.getName(), String.class.getName()
790             };
791         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
792 
793         Object result = null;
794 
795         if (finderClassNameCacheEnabled) {
796             result = FinderCacheUtil.getResult(finderClassName,
797                     finderMethodName, finderParams, finderArgs, this);
798         }
799 
800         if (result == null) {
801             Session session = null;
802 
803             try {
804                 session = openSession();
805 
806                 StringBuilder query = new StringBuilder();
807 
808                 query.append("SELECT COUNT(*) ");
809                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
810 
811                 query.append("companyId = ?");
812 
813                 query.append(" AND ");
814 
815                 if (portletId == null) {
816                     query.append("portletId IS NULL");
817                 }
818                 else {
819                     query.append("portletId = ?");
820                 }
821 
822                 query.append(" ");
823 
824                 Query q = session.createQuery(query.toString());
825 
826                 QueryPos qPos = QueryPos.getInstance(q);
827 
828                 qPos.add(companyId);
829 
830                 if (portletId != null) {
831                     qPos.add(portletId);
832                 }
833 
834                 Long count = null;
835 
836                 Iterator<Long> itr = q.list().iterator();
837 
838                 if (itr.hasNext()) {
839                     count = itr.next();
840                 }
841 
842                 if (count == null) {
843                     count = new Long(0);
844                 }
845 
846                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
847                     finderClassName, finderMethodName, finderParams,
848                     finderArgs, count);
849 
850                 return count.intValue();
851             }
852             catch (Exception e) {
853                 throw processException(e);
854             }
855             finally {
856                 closeSession(session);
857             }
858         }
859         else {
860             return ((Long)result).intValue();
861         }
862     }
863 
864     public int countAll() throws SystemException {
865         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
866         String finderClassName = Portlet.class.getName();
867         String finderMethodName = "countAll";
868         String[] finderParams = new String[] {  };
869         Object[] finderArgs = new Object[] {  };
870 
871         Object result = null;
872 
873         if (finderClassNameCacheEnabled) {
874             result = FinderCacheUtil.getResult(finderClassName,
875                     finderMethodName, finderParams, finderArgs, this);
876         }
877 
878         if (result == null) {
879             Session session = null;
880 
881             try {
882                 session = openSession();
883 
884                 Query q = session.createQuery(
885                         "SELECT COUNT(*) FROM com.liferay.portal.model.Portlet");
886 
887                 Long count = null;
888 
889                 Iterator<Long> itr = q.list().iterator();
890 
891                 if (itr.hasNext()) {
892                     count = itr.next();
893                 }
894 
895                 if (count == null) {
896                     count = new Long(0);
897                 }
898 
899                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
900                     finderClassName, finderMethodName, finderParams,
901                     finderArgs, count);
902 
903                 return count.intValue();
904             }
905             catch (Exception e) {
906                 throw processException(e);
907             }
908             finally {
909                 closeSession(session);
910             }
911         }
912         else {
913             return ((Long)result).intValue();
914         }
915     }
916 
917     public void registerListener(ModelListener listener) {
918         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
919 
920         listeners.add(listener);
921 
922         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
923     }
924 
925     public void unregisterListener(ModelListener listener) {
926         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
927 
928         listeners.remove(listener);
929 
930         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
931     }
932 
933     public void afterPropertiesSet() {
934         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
935                     com.liferay.portal.util.PropsUtil.get(
936                         "value.object.listener.com.liferay.portal.model.Portlet")));
937 
938         if (listenerClassNames.length > 0) {
939             try {
940                 List<ModelListener> listeners = new ArrayList<ModelListener>();
941 
942                 for (String listenerClassName : listenerClassNames) {
943                     listeners.add((ModelListener)Class.forName(
944                             listenerClassName).newInstance());
945                 }
946 
947                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
948             }
949             catch (Exception e) {
950                 _log.error(e);
951             }
952         }
953     }
954 
955     private static Log _log = LogFactory.getLog(PortletPersistenceImpl.class);
956     private ModelListener[] _listeners = new ModelListener[0];
957 }