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