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