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