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             if (BatchSessionUtil.isEnabled()) {
126                 Object staleObject = session.get(PortletImpl.class,
127                         portlet.getPrimaryKeyObj());
128 
129                 if (staleObject != null) {
130                     session.evict(staleObject);
131                 }
132             }
133 
134             session.delete(portlet);
135 
136             session.flush();
137 
138             return portlet;
139         }
140         catch (Exception e) {
141             throw processException(e);
142         }
143         finally {
144             closeSession(session);
145 
146             FinderCacheUtil.clearCache(Portlet.class.getName());
147         }
148     }
149 
150     /**
151      * @deprecated Use <code>update(Portlet portlet, boolean merge)</code>.
152      */
153     public Portlet update(Portlet portlet) throws SystemException {
154         if (_log.isWarnEnabled()) {
155             _log.warn(
156                 "Using the deprecated update(Portlet portlet) method. Use update(Portlet portlet, boolean merge) instead.");
157         }
158 
159         return update(portlet, false);
160     }
161 
162     /**
163      * Add, update, or merge, the entity. This method also calls the model
164      * listeners to trigger the proper events associated with adding, deleting,
165      * or updating an entity.
166      *
167      * @param        portlet the entity to add, update, or merge
168      * @param        merge boolean value for whether to merge the entity. The
169      *                default value is false. Setting merge to true is more
170      *                expensive and should only be true when portlet is
171      *                transient. See LEP-5473 for a detailed discussion of this
172      *                method.
173      * @return        true if the portlet can be displayed via Ajax
174      */
175     public Portlet update(Portlet portlet, boolean merge)
176         throws SystemException {
177         boolean isNew = portlet.isNew();
178 
179         if (_listeners.length > 0) {
180             for (ModelListener listener : _listeners) {
181                 if (isNew) {
182                     listener.onBeforeCreate(portlet);
183                 }
184                 else {
185                     listener.onBeforeUpdate(portlet);
186                 }
187             }
188         }
189 
190         portlet = updateImpl(portlet, merge);
191 
192         if (_listeners.length > 0) {
193             for (ModelListener listener : _listeners) {
194                 if (isNew) {
195                     listener.onAfterCreate(portlet);
196                 }
197                 else {
198                     listener.onAfterUpdate(portlet);
199                 }
200             }
201         }
202 
203         return portlet;
204     }
205 
206     public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
207         boolean merge) throws SystemException {
208         Session session = null;
209 
210         try {
211             session = openSession();
212 
213             BatchSessionUtil.update(session, portlet, merge);
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 = null;
675 
676                 if (obc == null) {
677                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
678                             start, end, false);
679 
680                     Collections.sort(list);
681                 }
682                 else {
683                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
684                             start, end);
685                 }
686 
687                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
688                     finderClassName, finderMethodName, finderParams,
689                     finderArgs, list);
690 
691                 return list;
692             }
693             catch (Exception e) {
694                 throw processException(e);
695             }
696             finally {
697                 closeSession(session);
698             }
699         }
700         else {
701             return (List<Portlet>)result;
702         }
703     }
704 
705     public void removeByCompanyId(long companyId) throws SystemException {
706         for (Portlet portlet : findByCompanyId(companyId)) {
707             remove(portlet);
708         }
709     }
710 
711     public void removeByC_P(long companyId, String portletId)
712         throws NoSuchPortletException, SystemException {
713         Portlet portlet = findByC_P(companyId, portletId);
714 
715         remove(portlet);
716     }
717 
718     public void removeAll() throws SystemException {
719         for (Portlet portlet : findAll()) {
720             remove(portlet);
721         }
722     }
723 
724     public int countByCompanyId(long companyId) throws SystemException {
725         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
726         String finderClassName = Portlet.class.getName();
727         String finderMethodName = "countByCompanyId";
728         String[] finderParams = new String[] { Long.class.getName() };
729         Object[] finderArgs = new Object[] { new Long(companyId) };
730 
731         Object result = null;
732 
733         if (finderClassNameCacheEnabled) {
734             result = FinderCacheUtil.getResult(finderClassName,
735                     finderMethodName, finderParams, finderArgs, this);
736         }
737 
738         if (result == null) {
739             Session session = null;
740 
741             try {
742                 session = openSession();
743 
744                 StringBuilder query = new StringBuilder();
745 
746                 query.append("SELECT COUNT(*) ");
747                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
748 
749                 query.append("companyId = ?");
750 
751                 query.append(" ");
752 
753                 Query q = session.createQuery(query.toString());
754 
755                 QueryPos qPos = QueryPos.getInstance(q);
756 
757                 qPos.add(companyId);
758 
759                 Long count = null;
760 
761                 Iterator<Long> itr = q.list().iterator();
762 
763                 if (itr.hasNext()) {
764                     count = itr.next();
765                 }
766 
767                 if (count == null) {
768                     count = new Long(0);
769                 }
770 
771                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
772                     finderClassName, finderMethodName, finderParams,
773                     finderArgs, count);
774 
775                 return count.intValue();
776             }
777             catch (Exception e) {
778                 throw processException(e);
779             }
780             finally {
781                 closeSession(session);
782             }
783         }
784         else {
785             return ((Long)result).intValue();
786         }
787     }
788 
789     public int countByC_P(long companyId, String portletId)
790         throws SystemException {
791         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
792         String finderClassName = Portlet.class.getName();
793         String finderMethodName = "countByC_P";
794         String[] finderParams = new String[] {
795                 Long.class.getName(), String.class.getName()
796             };
797         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
798 
799         Object result = null;
800 
801         if (finderClassNameCacheEnabled) {
802             result = FinderCacheUtil.getResult(finderClassName,
803                     finderMethodName, finderParams, finderArgs, this);
804         }
805 
806         if (result == null) {
807             Session session = null;
808 
809             try {
810                 session = openSession();
811 
812                 StringBuilder query = new StringBuilder();
813 
814                 query.append("SELECT COUNT(*) ");
815                 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
816 
817                 query.append("companyId = ?");
818 
819                 query.append(" AND ");
820 
821                 if (portletId == null) {
822                     query.append("portletId IS NULL");
823                 }
824                 else {
825                     query.append("portletId = ?");
826                 }
827 
828                 query.append(" ");
829 
830                 Query q = session.createQuery(query.toString());
831 
832                 QueryPos qPos = QueryPos.getInstance(q);
833 
834                 qPos.add(companyId);
835 
836                 if (portletId != null) {
837                     qPos.add(portletId);
838                 }
839 
840                 Long count = null;
841 
842                 Iterator<Long> itr = q.list().iterator();
843 
844                 if (itr.hasNext()) {
845                     count = itr.next();
846                 }
847 
848                 if (count == null) {
849                     count = new Long(0);
850                 }
851 
852                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
853                     finderClassName, finderMethodName, finderParams,
854                     finderArgs, count);
855 
856                 return count.intValue();
857             }
858             catch (Exception e) {
859                 throw processException(e);
860             }
861             finally {
862                 closeSession(session);
863             }
864         }
865         else {
866             return ((Long)result).intValue();
867         }
868     }
869 
870     public int countAll() throws SystemException {
871         boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
872         String finderClassName = Portlet.class.getName();
873         String finderMethodName = "countAll";
874         String[] finderParams = new String[] {  };
875         Object[] finderArgs = new Object[] {  };
876 
877         Object result = null;
878 
879         if (finderClassNameCacheEnabled) {
880             result = FinderCacheUtil.getResult(finderClassName,
881                     finderMethodName, finderParams, finderArgs, this);
882         }
883 
884         if (result == null) {
885             Session session = null;
886 
887             try {
888                 session = openSession();
889 
890                 Query q = session.createQuery(
891                         "SELECT COUNT(*) FROM com.liferay.portal.model.Portlet");
892 
893                 Long count = null;
894 
895                 Iterator<Long> itr = q.list().iterator();
896 
897                 if (itr.hasNext()) {
898                     count = itr.next();
899                 }
900 
901                 if (count == null) {
902                     count = new Long(0);
903                 }
904 
905                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
906                     finderClassName, finderMethodName, finderParams,
907                     finderArgs, count);
908 
909                 return count.intValue();
910             }
911             catch (Exception e) {
912                 throw processException(e);
913             }
914             finally {
915                 closeSession(session);
916             }
917         }
918         else {
919             return ((Long)result).intValue();
920         }
921     }
922 
923     public void registerListener(ModelListener listener) {
924         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
925 
926         listeners.add(listener);
927 
928         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
929     }
930 
931     public void unregisterListener(ModelListener listener) {
932         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
933 
934         listeners.remove(listener);
935 
936         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
937     }
938 
939     public void afterPropertiesSet() {
940         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
941                     com.liferay.portal.util.PropsUtil.get(
942                         "value.object.listener.com.liferay.portal.model.Portlet")));
943 
944         if (listenerClassNames.length > 0) {
945             try {
946                 List<ModelListener> listeners = new ArrayList<ModelListener>();
947 
948                 for (String listenerClassName : listenerClassNames) {
949                     listeners.add((ModelListener)Class.forName(
950                             listenerClassName).newInstance());
951                 }
952 
953                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
954             }
955             catch (Exception e) {
956                 _log.error(e);
957             }
958         }
959     }
960 
961     private static Log _log = LogFactory.getLog(PortletPersistenceImpl.class);
962     private ModelListener[] _listeners = new ModelListener[0];
963 }