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