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