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