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