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