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