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