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