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                     "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ");
387 
388                 query.append("expandoRow.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                     "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ");
446 
447                 query.append("expandoRow.tableId = ?");
448 
449                 query.append(" ");
450 
451                 if (obc != null) {
452                     query.append("ORDER BY ");
453 
454                     String[] orderByFields = obc.getOrderByFields();
455 
456                     for (int i = 0; i < orderByFields.length; i++) {
457                         query.append("expandoRow.");
458                         query.append(orderByFields[i]);
459 
460                         if (obc.isAscending()) {
461                             query.append(" ASC");
462                         }
463                         else {
464                             query.append(" DESC");
465                         }
466 
467                         if ((i + 1) < orderByFields.length) {
468                             query.append(", ");
469                         }
470                     }
471                 }
472 
473                 Query q = session.createQuery(query.toString());
474 
475                 QueryPos qPos = QueryPos.getInstance(q);
476 
477                 qPos.add(tableId);
478 
479                 list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(), start,
480                         end);
481             }
482             catch (Exception e) {
483                 throw processException(e);
484             }
485             finally {
486                 if (list == null) {
487                     list = new ArrayList<ExpandoRow>();
488                 }
489 
490                 cacheResult(list);
491 
492                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
493                     finderArgs, list);
494 
495                 closeSession(session);
496             }
497         }
498 
499         return list;
500     }
501 
502     public ExpandoRow findByTableId_First(long tableId, OrderByComparator obc)
503         throws NoSuchRowException, SystemException {
504         List<ExpandoRow> list = findByTableId(tableId, 0, 1, obc);
505 
506         if (list.isEmpty()) {
507             StringBuilder msg = new StringBuilder();
508 
509             msg.append("No ExpandoRow exists with the key {");
510 
511             msg.append("tableId=" + tableId);
512 
513             msg.append(StringPool.CLOSE_CURLY_BRACE);
514 
515             throw new NoSuchRowException(msg.toString());
516         }
517         else {
518             return list.get(0);
519         }
520     }
521 
522     public ExpandoRow findByTableId_Last(long tableId, OrderByComparator obc)
523         throws NoSuchRowException, SystemException {
524         int count = countByTableId(tableId);
525 
526         List<ExpandoRow> list = findByTableId(tableId, count - 1, count, obc);
527 
528         if (list.isEmpty()) {
529             StringBuilder msg = new StringBuilder();
530 
531             msg.append("No ExpandoRow exists with the key {");
532 
533             msg.append("tableId=" + tableId);
534 
535             msg.append(StringPool.CLOSE_CURLY_BRACE);
536 
537             throw new NoSuchRowException(msg.toString());
538         }
539         else {
540             return list.get(0);
541         }
542     }
543 
544     public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
545         OrderByComparator obc) throws NoSuchRowException, SystemException {
546         ExpandoRow expandoRow = findByPrimaryKey(rowId);
547 
548         int count = countByTableId(tableId);
549 
550         Session session = null;
551 
552         try {
553             session = openSession();
554 
555             StringBuilder query = new StringBuilder();
556 
557             query.append("SELECT expandoRow FROM ExpandoRow expandoRow WHERE ");
558 
559             query.append("expandoRow.tableId = ?");
560 
561             query.append(" ");
562 
563             if (obc != null) {
564                 query.append("ORDER BY ");
565 
566                 String[] orderByFields = obc.getOrderByFields();
567 
568                 for (int i = 0; i < orderByFields.length; i++) {
569                     query.append("expandoRow.");
570                     query.append(orderByFields[i]);
571 
572                     if (obc.isAscending()) {
573                         query.append(" ASC");
574                     }
575                     else {
576                         query.append(" DESC");
577                     }
578 
579                     if ((i + 1) < orderByFields.length) {
580                         query.append(", ");
581                     }
582                 }
583             }
584 
585             Query q = session.createQuery(query.toString());
586 
587             QueryPos qPos = QueryPos.getInstance(q);
588 
589             qPos.add(tableId);
590 
591             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
592                     expandoRow);
593 
594             ExpandoRow[] array = new ExpandoRowImpl[3];
595 
596             array[0] = (ExpandoRow)objArray[0];
597             array[1] = (ExpandoRow)objArray[1];
598             array[2] = (ExpandoRow)objArray[2];
599 
600             return array;
601         }
602         catch (Exception e) {
603             throw processException(e);
604         }
605         finally {
606             closeSession(session);
607         }
608     }
609 
610     public ExpandoRow findByT_C(long tableId, long classPK)
611         throws NoSuchRowException, SystemException {
612         ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
613 
614         if (expandoRow == null) {
615             StringBuilder msg = new StringBuilder();
616 
617             msg.append("No ExpandoRow exists with the key {");
618 
619             msg.append("tableId=" + tableId);
620 
621             msg.append(", ");
622             msg.append("classPK=" + classPK);
623 
624             msg.append(StringPool.CLOSE_CURLY_BRACE);
625 
626             if (_log.isWarnEnabled()) {
627                 _log.warn(msg.toString());
628             }
629 
630             throw new NoSuchRowException(msg.toString());
631         }
632 
633         return expandoRow;
634     }
635 
636     public ExpandoRow fetchByT_C(long tableId, long classPK)
637         throws SystemException {
638         return fetchByT_C(tableId, classPK, true);
639     }
640 
641     public ExpandoRow fetchByT_C(long tableId, long classPK,
642         boolean retrieveFromCache) throws SystemException {
643         Object[] finderArgs = new Object[] { new Long(tableId), new Long(classPK) };
644 
645         Object result = null;
646 
647         if (retrieveFromCache) {
648             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
649                     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                     "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ");
662 
663                 query.append("expandoRow.tableId = ?");
664 
665                 query.append(" AND ");
666 
667                 query.append("expandoRow.classPK = ?");
668 
669                 query.append(" ");
670 
671                 Query q = session.createQuery(query.toString());
672 
673                 QueryPos qPos = QueryPos.getInstance(q);
674 
675                 qPos.add(tableId);
676 
677                 qPos.add(classPK);
678 
679                 List<ExpandoRow> list = q.list();
680 
681                 result = list;
682 
683                 ExpandoRow expandoRow = null;
684 
685                 if (list.isEmpty()) {
686                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
687                         finderArgs, list);
688                 }
689                 else {
690                     expandoRow = list.get(0);
691 
692                     cacheResult(expandoRow);
693 
694                     if ((expandoRow.getTableId() != tableId) ||
695                             (expandoRow.getClassPK() != classPK)) {
696                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
697                             finderArgs, expandoRow);
698                     }
699                 }
700 
701                 return expandoRow;
702             }
703             catch (Exception e) {
704                 throw processException(e);
705             }
706             finally {
707                 if (result == null) {
708                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
709                         finderArgs, new ArrayList<ExpandoRow>());
710                 }
711 
712                 closeSession(session);
713             }
714         }
715         else {
716             if (result instanceof List) {
717                 return null;
718             }
719             else {
720                 return (ExpandoRow)result;
721             }
722         }
723     }
724 
725     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
726         throws SystemException {
727         Session session = null;
728 
729         try {
730             session = openSession();
731 
732             dynamicQuery.compile(session);
733 
734             return dynamicQuery.list();
735         }
736         catch (Exception e) {
737             throw processException(e);
738         }
739         finally {
740             closeSession(session);
741         }
742     }
743 
744     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
745         int start, int end) throws SystemException {
746         Session session = null;
747 
748         try {
749             session = openSession();
750 
751             dynamicQuery.setLimit(start, end);
752 
753             dynamicQuery.compile(session);
754 
755             return dynamicQuery.list();
756         }
757         catch (Exception e) {
758             throw processException(e);
759         }
760         finally {
761             closeSession(session);
762         }
763     }
764 
765     public List<ExpandoRow> findAll() throws SystemException {
766         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
767     }
768 
769     public List<ExpandoRow> findAll(int start, int end)
770         throws SystemException {
771         return findAll(start, end, null);
772     }
773 
774     public List<ExpandoRow> findAll(int start, int end, OrderByComparator obc)
775         throws SystemException {
776         Object[] finderArgs = new Object[] {
777                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
778             };
779 
780         List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
781                 finderArgs, this);
782 
783         if (list == null) {
784             Session session = null;
785 
786             try {
787                 session = openSession();
788 
789                 StringBuilder query = new StringBuilder();
790 
791                 query.append("SELECT expandoRow FROM ExpandoRow expandoRow ");
792 
793                 if (obc != null) {
794                     query.append("ORDER BY ");
795 
796                     String[] orderByFields = obc.getOrderByFields();
797 
798                     for (int i = 0; i < orderByFields.length; i++) {
799                         query.append("expandoRow.");
800                         query.append(orderByFields[i]);
801 
802                         if (obc.isAscending()) {
803                             query.append(" ASC");
804                         }
805                         else {
806                             query.append(" DESC");
807                         }
808 
809                         if ((i + 1) < orderByFields.length) {
810                             query.append(", ");
811                         }
812                     }
813                 }
814 
815                 Query q = session.createQuery(query.toString());
816 
817                 if (obc == null) {
818                     list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
819                             start, end, false);
820 
821                     Collections.sort(list);
822                 }
823                 else {
824                     list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
825                             start, end);
826                 }
827             }
828             catch (Exception e) {
829                 throw processException(e);
830             }
831             finally {
832                 if (list == null) {
833                     list = new ArrayList<ExpandoRow>();
834                 }
835 
836                 cacheResult(list);
837 
838                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
839 
840                 closeSession(session);
841             }
842         }
843 
844         return list;
845     }
846 
847     public void removeByTableId(long tableId) throws SystemException {
848         for (ExpandoRow expandoRow : findByTableId(tableId)) {
849             remove(expandoRow);
850         }
851     }
852 
853     public void removeByT_C(long tableId, long classPK)
854         throws NoSuchRowException, SystemException {
855         ExpandoRow expandoRow = findByT_C(tableId, classPK);
856 
857         remove(expandoRow);
858     }
859 
860     public void removeAll() throws SystemException {
861         for (ExpandoRow expandoRow : findAll()) {
862             remove(expandoRow);
863         }
864     }
865 
866     public int countByTableId(long tableId) throws SystemException {
867         Object[] finderArgs = new Object[] { new Long(tableId) };
868 
869         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
870                 finderArgs, this);
871 
872         if (count == null) {
873             Session session = null;
874 
875             try {
876                 session = openSession();
877 
878                 StringBuilder query = new StringBuilder();
879 
880                 query.append("SELECT COUNT(expandoRow) ");
881                 query.append("FROM ExpandoRow expandoRow WHERE ");
882 
883                 query.append("expandoRow.tableId = ?");
884 
885                 query.append(" ");
886 
887                 Query q = session.createQuery(query.toString());
888 
889                 QueryPos qPos = QueryPos.getInstance(q);
890 
891                 qPos.add(tableId);
892 
893                 count = (Long)q.uniqueResult();
894             }
895             catch (Exception e) {
896                 throw processException(e);
897             }
898             finally {
899                 if (count == null) {
900                     count = Long.valueOf(0);
901                 }
902 
903                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
904                     finderArgs, count);
905 
906                 closeSession(session);
907             }
908         }
909 
910         return count.intValue();
911     }
912 
913     public int countByT_C(long tableId, long classPK) throws SystemException {
914         Object[] finderArgs = new Object[] { new Long(tableId), new Long(classPK) };
915 
916         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_C,
917                 finderArgs, this);
918 
919         if (count == null) {
920             Session session = null;
921 
922             try {
923                 session = openSession();
924 
925                 StringBuilder query = new StringBuilder();
926 
927                 query.append("SELECT COUNT(expandoRow) ");
928                 query.append("FROM ExpandoRow expandoRow WHERE ");
929 
930                 query.append("expandoRow.tableId = ?");
931 
932                 query.append(" AND ");
933 
934                 query.append("expandoRow.classPK = ?");
935 
936                 query.append(" ");
937 
938                 Query q = session.createQuery(query.toString());
939 
940                 QueryPos qPos = QueryPos.getInstance(q);
941 
942                 qPos.add(tableId);
943 
944                 qPos.add(classPK);
945 
946                 count = (Long)q.uniqueResult();
947             }
948             catch (Exception e) {
949                 throw processException(e);
950             }
951             finally {
952                 if (count == null) {
953                     count = Long.valueOf(0);
954                 }
955 
956                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, finderArgs,
957                     count);
958 
959                 closeSession(session);
960             }
961         }
962 
963         return count.intValue();
964     }
965 
966     public int countAll() throws SystemException {
967         Object[] finderArgs = new Object[0];
968 
969         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
970                 finderArgs, this);
971 
972         if (count == null) {
973             Session session = null;
974 
975             try {
976                 session = openSession();
977 
978                 Query q = session.createQuery(
979                         "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow");
980 
981                 count = (Long)q.uniqueResult();
982             }
983             catch (Exception e) {
984                 throw processException(e);
985             }
986             finally {
987                 if (count == null) {
988                     count = Long.valueOf(0);
989                 }
990 
991                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
992                     count);
993 
994                 closeSession(session);
995             }
996         }
997 
998         return count.intValue();
999     }
1000
1001    public void afterPropertiesSet() {
1002        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1003                    com.liferay.portal.util.PropsUtil.get(
1004                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1005
1006        if (listenerClassNames.length > 0) {
1007            try {
1008                List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1009
1010                for (String listenerClassName : listenerClassNames) {
1011                    listenersList.add((ModelListener<ExpandoRow>)Class.forName(
1012                            listenerClassName).newInstance());
1013                }
1014
1015                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1016            }
1017            catch (Exception e) {
1018                _log.error(e);
1019            }
1020        }
1021    }
1022
1023    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence.impl")
1024    protected com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence expandoColumnPersistence;
1025    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence.impl")
1026    protected com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence expandoRowPersistence;
1027    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence.impl")
1028    protected com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence expandoTablePersistence;
1029    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence.impl")
1030    protected com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence expandoValuePersistence;
1031    private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1032}