001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.service.persistence;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
018    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
019    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderPath;
021    import com.liferay.portal.kernel.dao.orm.Query;
022    import com.liferay.portal.kernel.dao.orm.QueryPos;
023    import com.liferay.portal.kernel.dao.orm.QueryUtil;
024    import com.liferay.portal.kernel.dao.orm.Session;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.GetterUtil;
029    import com.liferay.portal.kernel.util.InstanceFactory;
030    import com.liferay.portal.kernel.util.OrderByComparator;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.StringUtil;
034    import com.liferay.portal.kernel.util.UnmodifiableList;
035    import com.liferay.portal.model.CacheModel;
036    import com.liferay.portal.model.ModelListener;
037    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
038    
039    import com.liferay.portlet.expando.NoSuchRowException;
040    import com.liferay.portlet.expando.model.ExpandoRow;
041    import com.liferay.portlet.expando.model.impl.ExpandoRowImpl;
042    import com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.Collections;
048    import java.util.List;
049    
050    /**
051     * The persistence implementation for the expando row service.
052     *
053     * <p>
054     * Caching information and settings can be found in <code>portal.properties</code>
055     * </p>
056     *
057     * @author Brian Wing Shun Chan
058     * @see ExpandoRowPersistence
059     * @see ExpandoRowUtil
060     * @generated
061     */
062    public class ExpandoRowPersistenceImpl extends BasePersistenceImpl<ExpandoRow>
063            implements ExpandoRowPersistence {
064            /*
065             * NOTE FOR DEVELOPERS:
066             *
067             * Never modify or reference this class directly. Always use {@link ExpandoRowUtil} to access the expando row persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
068             */
069            public static final String FINDER_CLASS_NAME_ENTITY = ExpandoRowImpl.class.getName();
070            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071                    ".List1";
072            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List2";
074            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
075                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
076                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
078                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
081                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
084                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTableId",
086                            new String[] {
087                                    Long.class.getName(),
088                                    
089                            Integer.class.getName(), Integer.class.getName(),
090                                    OrderByComparator.class.getName()
091                            });
092            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID =
093                    new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
094                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
095                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByTableId",
096                            new String[] { Long.class.getName() },
097                            ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK);
098            public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
099                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByTableId",
101                            new String[] { Long.class.getName() });
102    
103            /**
104             * Returns all the expando rows where tableId = &#63;.
105             *
106             * @param tableId the table ID
107             * @return the matching expando rows
108             * @throws SystemException if a system exception occurred
109             */
110            public List<ExpandoRow> findByTableId(long tableId)
111                    throws SystemException {
112                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
113            }
114    
115            /**
116             * Returns a range of all the expando rows where tableId = &#63;.
117             *
118             * <p>
119             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
120             * </p>
121             *
122             * @param tableId the table ID
123             * @param start the lower bound of the range of expando rows
124             * @param end the upper bound of the range of expando rows (not inclusive)
125             * @return the range of matching expando rows
126             * @throws SystemException if a system exception occurred
127             */
128            public List<ExpandoRow> findByTableId(long tableId, int start, int end)
129                    throws SystemException {
130                    return findByTableId(tableId, start, end, null);
131            }
132    
133            /**
134             * Returns an ordered range of all the expando rows where tableId = &#63;.
135             *
136             * <p>
137             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
138             * </p>
139             *
140             * @param tableId the table ID
141             * @param start the lower bound of the range of expando rows
142             * @param end the upper bound of the range of expando rows (not inclusive)
143             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
144             * @return the ordered range of matching expando rows
145             * @throws SystemException if a system exception occurred
146             */
147            public List<ExpandoRow> findByTableId(long tableId, int start, int end,
148                    OrderByComparator orderByComparator) throws SystemException {
149                    boolean pagination = true;
150                    FinderPath finderPath = null;
151                    Object[] finderArgs = null;
152    
153                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
154                                    (orderByComparator == null)) {
155                            pagination = false;
156                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID;
157                            finderArgs = new Object[] { tableId };
158                    }
159                    else {
160                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID;
161                            finderArgs = new Object[] { tableId, start, end, orderByComparator };
162                    }
163    
164                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
165                                    finderArgs, this);
166    
167                    if ((list != null) && !list.isEmpty()) {
168                            for (ExpandoRow expandoRow : list) {
169                                    if ((tableId != expandoRow.getTableId())) {
170                                            list = null;
171    
172                                            break;
173                                    }
174                            }
175                    }
176    
177                    if (list == null) {
178                            StringBundler query = null;
179    
180                            if (orderByComparator != null) {
181                                    query = new StringBundler(3 +
182                                                    (orderByComparator.getOrderByFields().length * 3));
183                            }
184                            else {
185                                    query = new StringBundler(3);
186                            }
187    
188                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
189    
190                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
191    
192                            if (orderByComparator != null) {
193                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
194                                            orderByComparator);
195                            }
196                            else
197                             if (pagination) {
198                                    query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
199                            }
200    
201                            String sql = query.toString();
202    
203                            Session session = null;
204    
205                            try {
206                                    session = openSession();
207    
208                                    Query q = session.createQuery(sql);
209    
210                                    QueryPos qPos = QueryPos.getInstance(q);
211    
212                                    qPos.add(tableId);
213    
214                                    if (!pagination) {
215                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
216                                                            start, end, false);
217    
218                                            Collections.sort(list);
219    
220                                            list = new UnmodifiableList<ExpandoRow>(list);
221                                    }
222                                    else {
223                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
224                                                            start, end);
225                                    }
226    
227                                    cacheResult(list);
228    
229                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
230                            }
231                            catch (Exception e) {
232                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
233    
234                                    throw processException(e);
235                            }
236                            finally {
237                                    closeSession(session);
238                            }
239                    }
240    
241                    return list;
242            }
243    
244            /**
245             * Returns the first expando row in the ordered set where tableId = &#63;.
246             *
247             * @param tableId the table ID
248             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
249             * @return the first matching expando row
250             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
251             * @throws SystemException if a system exception occurred
252             */
253            public ExpandoRow findByTableId_First(long tableId,
254                    OrderByComparator orderByComparator)
255                    throws NoSuchRowException, SystemException {
256                    ExpandoRow expandoRow = fetchByTableId_First(tableId, orderByComparator);
257    
258                    if (expandoRow != null) {
259                            return expandoRow;
260                    }
261    
262                    StringBundler msg = new StringBundler(4);
263    
264                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
265    
266                    msg.append("tableId=");
267                    msg.append(tableId);
268    
269                    msg.append(StringPool.CLOSE_CURLY_BRACE);
270    
271                    throw new NoSuchRowException(msg.toString());
272            }
273    
274            /**
275             * Returns the first expando row in the ordered set where tableId = &#63;.
276             *
277             * @param tableId the table ID
278             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
279             * @return the first matching expando row, or <code>null</code> if a matching expando row could not be found
280             * @throws SystemException if a system exception occurred
281             */
282            public ExpandoRow fetchByTableId_First(long tableId,
283                    OrderByComparator orderByComparator) throws SystemException {
284                    List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
285    
286                    if (!list.isEmpty()) {
287                            return list.get(0);
288                    }
289    
290                    return null;
291            }
292    
293            /**
294             * Returns the last expando row in the ordered set where tableId = &#63;.
295             *
296             * @param tableId the table ID
297             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
298             * @return the last matching expando row
299             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
300             * @throws SystemException if a system exception occurred
301             */
302            public ExpandoRow findByTableId_Last(long tableId,
303                    OrderByComparator orderByComparator)
304                    throws NoSuchRowException, SystemException {
305                    ExpandoRow expandoRow = fetchByTableId_Last(tableId, orderByComparator);
306    
307                    if (expandoRow != null) {
308                            return expandoRow;
309                    }
310    
311                    StringBundler msg = new StringBundler(4);
312    
313                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
314    
315                    msg.append("tableId=");
316                    msg.append(tableId);
317    
318                    msg.append(StringPool.CLOSE_CURLY_BRACE);
319    
320                    throw new NoSuchRowException(msg.toString());
321            }
322    
323            /**
324             * Returns the last expando row in the ordered set where tableId = &#63;.
325             *
326             * @param tableId the table ID
327             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
328             * @return the last matching expando row, or <code>null</code> if a matching expando row could not be found
329             * @throws SystemException if a system exception occurred
330             */
331            public ExpandoRow fetchByTableId_Last(long tableId,
332                    OrderByComparator orderByComparator) throws SystemException {
333                    int count = countByTableId(tableId);
334    
335                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
336                                    orderByComparator);
337    
338                    if (!list.isEmpty()) {
339                            return list.get(0);
340                    }
341    
342                    return null;
343            }
344    
345            /**
346             * Returns the expando rows before and after the current expando row in the ordered set where tableId = &#63;.
347             *
348             * @param rowId the primary key of the current expando row
349             * @param tableId the table ID
350             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
351             * @return the previous, current, and next expando row
352             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
353             * @throws SystemException if a system exception occurred
354             */
355            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
356                    OrderByComparator orderByComparator)
357                    throws NoSuchRowException, SystemException {
358                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
359    
360                    Session session = null;
361    
362                    try {
363                            session = openSession();
364    
365                            ExpandoRow[] array = new ExpandoRowImpl[3];
366    
367                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
368                                            orderByComparator, true);
369    
370                            array[1] = expandoRow;
371    
372                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
373                                            orderByComparator, false);
374    
375                            return array;
376                    }
377                    catch (Exception e) {
378                            throw processException(e);
379                    }
380                    finally {
381                            closeSession(session);
382                    }
383            }
384    
385            protected ExpandoRow getByTableId_PrevAndNext(Session session,
386                    ExpandoRow expandoRow, long tableId,
387                    OrderByComparator orderByComparator, boolean previous) {
388                    StringBundler query = null;
389    
390                    if (orderByComparator != null) {
391                            query = new StringBundler(6 +
392                                            (orderByComparator.getOrderByFields().length * 6));
393                    }
394                    else {
395                            query = new StringBundler(3);
396                    }
397    
398                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
399    
400                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
401    
402                    if (orderByComparator != null) {
403                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
404    
405                            if (orderByConditionFields.length > 0) {
406                                    query.append(WHERE_AND);
407                            }
408    
409                            for (int i = 0; i < orderByConditionFields.length; i++) {
410                                    query.append(_ORDER_BY_ENTITY_ALIAS);
411                                    query.append(orderByConditionFields[i]);
412    
413                                    if ((i + 1) < orderByConditionFields.length) {
414                                            if (orderByComparator.isAscending() ^ previous) {
415                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
416                                            }
417                                            else {
418                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
419                                            }
420                                    }
421                                    else {
422                                            if (orderByComparator.isAscending() ^ previous) {
423                                                    query.append(WHERE_GREATER_THAN);
424                                            }
425                                            else {
426                                                    query.append(WHERE_LESSER_THAN);
427                                            }
428                                    }
429                            }
430    
431                            query.append(ORDER_BY_CLAUSE);
432    
433                            String[] orderByFields = orderByComparator.getOrderByFields();
434    
435                            for (int i = 0; i < orderByFields.length; i++) {
436                                    query.append(_ORDER_BY_ENTITY_ALIAS);
437                                    query.append(orderByFields[i]);
438    
439                                    if ((i + 1) < orderByFields.length) {
440                                            if (orderByComparator.isAscending() ^ previous) {
441                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
442                                            }
443                                            else {
444                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
445                                            }
446                                    }
447                                    else {
448                                            if (orderByComparator.isAscending() ^ previous) {
449                                                    query.append(ORDER_BY_ASC);
450                                            }
451                                            else {
452                                                    query.append(ORDER_BY_DESC);
453                                            }
454                                    }
455                            }
456                    }
457                    else {
458                            query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
459                    }
460    
461                    String sql = query.toString();
462    
463                    Query q = session.createQuery(sql);
464    
465                    q.setFirstResult(0);
466                    q.setMaxResults(2);
467    
468                    QueryPos qPos = QueryPos.getInstance(q);
469    
470                    qPos.add(tableId);
471    
472                    if (orderByComparator != null) {
473                            Object[] values = orderByComparator.getOrderByConditionValues(expandoRow);
474    
475                            for (Object value : values) {
476                                    qPos.add(value);
477                            }
478                    }
479    
480                    List<ExpandoRow> list = q.list();
481    
482                    if (list.size() == 2) {
483                            return list.get(1);
484                    }
485                    else {
486                            return null;
487                    }
488            }
489    
490            /**
491             * Removes all the expando rows where tableId = &#63; from the database.
492             *
493             * @param tableId the table ID
494             * @throws SystemException if a system exception occurred
495             */
496            public void removeByTableId(long tableId) throws SystemException {
497                    for (ExpandoRow expandoRow : findByTableId(tableId, QueryUtil.ALL_POS,
498                                    QueryUtil.ALL_POS, null)) {
499                            remove(expandoRow);
500                    }
501            }
502    
503            /**
504             * Returns the number of expando rows where tableId = &#63;.
505             *
506             * @param tableId the table ID
507             * @return the number of matching expando rows
508             * @throws SystemException if a system exception occurred
509             */
510            public int countByTableId(long tableId) throws SystemException {
511                    FinderPath finderPath = FINDER_PATH_COUNT_BY_TABLEID;
512    
513                    Object[] finderArgs = new Object[] { tableId };
514    
515                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
516                                    this);
517    
518                    if (count == null) {
519                            StringBundler query = new StringBundler(2);
520    
521                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
522    
523                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
524    
525                            String sql = query.toString();
526    
527                            Session session = null;
528    
529                            try {
530                                    session = openSession();
531    
532                                    Query q = session.createQuery(sql);
533    
534                                    QueryPos qPos = QueryPos.getInstance(q);
535    
536                                    qPos.add(tableId);
537    
538                                    count = (Long)q.uniqueResult();
539    
540                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
541                            }
542                            catch (Exception e) {
543                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
544    
545                                    throw processException(e);
546                            }
547                            finally {
548                                    closeSession(session);
549                            }
550                    }
551    
552                    return count.intValue();
553            }
554    
555            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
556            public static final FinderPath FINDER_PATH_FETCH_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
557                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
558                            FINDER_CLASS_NAME_ENTITY, "fetchByT_C",
559                            new String[] { Long.class.getName(), Long.class.getName() },
560                            ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK |
561                            ExpandoRowModelImpl.CLASSPK_COLUMN_BITMASK);
562            public static final FinderPath FINDER_PATH_COUNT_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
563                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
564                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByT_C",
565                            new String[] { Long.class.getName(), Long.class.getName() });
566    
567            /**
568             * Returns the expando row where tableId = &#63; and classPK = &#63; or throws a {@link com.liferay.portlet.expando.NoSuchRowException} if it could not be found.
569             *
570             * @param tableId the table ID
571             * @param classPK the class p k
572             * @return the matching expando row
573             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
574             * @throws SystemException if a system exception occurred
575             */
576            public ExpandoRow findByT_C(long tableId, long classPK)
577                    throws NoSuchRowException, SystemException {
578                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
579    
580                    if (expandoRow == null) {
581                            StringBundler msg = new StringBundler(6);
582    
583                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
584    
585                            msg.append("tableId=");
586                            msg.append(tableId);
587    
588                            msg.append(", classPK=");
589                            msg.append(classPK);
590    
591                            msg.append(StringPool.CLOSE_CURLY_BRACE);
592    
593                            if (_log.isWarnEnabled()) {
594                                    _log.warn(msg.toString());
595                            }
596    
597                            throw new NoSuchRowException(msg.toString());
598                    }
599    
600                    return expandoRow;
601            }
602    
603            /**
604             * Returns the expando row where tableId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
605             *
606             * @param tableId the table ID
607             * @param classPK the class p k
608             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
609             * @throws SystemException if a system exception occurred
610             */
611            public ExpandoRow fetchByT_C(long tableId, long classPK)
612                    throws SystemException {
613                    return fetchByT_C(tableId, classPK, true);
614            }
615    
616            /**
617             * Returns the expando row where tableId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
618             *
619             * @param tableId the table ID
620             * @param classPK the class p k
621             * @param retrieveFromCache whether to use the finder cache
622             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
623             * @throws SystemException if a system exception occurred
624             */
625            public ExpandoRow fetchByT_C(long tableId, long classPK,
626                    boolean retrieveFromCache) throws SystemException {
627                    Object[] finderArgs = new Object[] { tableId, classPK };
628    
629                    Object result = null;
630    
631                    if (retrieveFromCache) {
632                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
633                                            finderArgs, this);
634                    }
635    
636                    if (result instanceof ExpandoRow) {
637                            ExpandoRow expandoRow = (ExpandoRow)result;
638    
639                            if ((tableId != expandoRow.getTableId()) ||
640                                            (classPK != expandoRow.getClassPK())) {
641                                    result = null;
642                            }
643                    }
644    
645                    if (result == null) {
646                            StringBundler query = new StringBundler(4);
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                            Session session = null;
657    
658                            try {
659                                    session = openSession();
660    
661                                    Query q = session.createQuery(sql);
662    
663                                    QueryPos qPos = QueryPos.getInstance(q);
664    
665                                    qPos.add(tableId);
666    
667                                    qPos.add(classPK);
668    
669                                    List<ExpandoRow> list = q.list();
670    
671                                    if (list.isEmpty()) {
672                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
673                                                    finderArgs, list);
674                                    }
675                                    else {
676                                            ExpandoRow expandoRow = list.get(0);
677    
678                                            result = expandoRow;
679    
680                                            cacheResult(expandoRow);
681    
682                                            if ((expandoRow.getTableId() != tableId) ||
683                                                            (expandoRow.getClassPK() != classPK)) {
684                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
685                                                            finderArgs, expandoRow);
686                                            }
687                                    }
688                            }
689                            catch (Exception e) {
690                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
691                                            finderArgs);
692    
693                                    throw processException(e);
694                            }
695                            finally {
696                                    closeSession(session);
697                            }
698                    }
699    
700                    if (result instanceof List<?>) {
701                            return null;
702                    }
703                    else {
704                            return (ExpandoRow)result;
705                    }
706            }
707    
708            /**
709             * Removes the expando row where tableId = &#63; and classPK = &#63; from the database.
710             *
711             * @param tableId the table ID
712             * @param classPK the class p k
713             * @return the expando row that was removed
714             * @throws SystemException if a system exception occurred
715             */
716            public ExpandoRow removeByT_C(long tableId, long classPK)
717                    throws NoSuchRowException, SystemException {
718                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
719    
720                    return remove(expandoRow);
721            }
722    
723            /**
724             * Returns the number of expando rows where tableId = &#63; and classPK = &#63;.
725             *
726             * @param tableId the table ID
727             * @param classPK the class p k
728             * @return the number of matching expando rows
729             * @throws SystemException if a system exception occurred
730             */
731            public int countByT_C(long tableId, long classPK) throws SystemException {
732                    FinderPath finderPath = FINDER_PATH_COUNT_BY_T_C;
733    
734                    Object[] finderArgs = new Object[] { tableId, classPK };
735    
736                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
737                                    this);
738    
739                    if (count == null) {
740                            StringBundler query = new StringBundler(3);
741    
742                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
743    
744                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
745    
746                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
747    
748                            String sql = query.toString();
749    
750                            Session session = null;
751    
752                            try {
753                                    session = openSession();
754    
755                                    Query q = session.createQuery(sql);
756    
757                                    QueryPos qPos = QueryPos.getInstance(q);
758    
759                                    qPos.add(tableId);
760    
761                                    qPos.add(classPK);
762    
763                                    count = (Long)q.uniqueResult();
764    
765                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
766                            }
767                            catch (Exception e) {
768                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
769    
770                                    throw processException(e);
771                            }
772                            finally {
773                                    closeSession(session);
774                            }
775                    }
776    
777                    return count.intValue();
778            }
779    
780            private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
781            private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
782    
783            /**
784             * Caches the expando row in the entity cache if it is enabled.
785             *
786             * @param expandoRow the expando row
787             */
788            public void cacheResult(ExpandoRow expandoRow) {
789                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
790                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
791    
792                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
793                            new Object[] { expandoRow.getTableId(), expandoRow.getClassPK() },
794                            expandoRow);
795    
796                    expandoRow.resetOriginalValues();
797            }
798    
799            /**
800             * Caches the expando rows in the entity cache if it is enabled.
801             *
802             * @param expandoRows the expando rows
803             */
804            public void cacheResult(List<ExpandoRow> expandoRows) {
805                    for (ExpandoRow expandoRow : expandoRows) {
806                            if (EntityCacheUtil.getResult(
807                                                    ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
808                                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey()) == null) {
809                                    cacheResult(expandoRow);
810                            }
811                            else {
812                                    expandoRow.resetOriginalValues();
813                            }
814                    }
815            }
816    
817            /**
818             * Clears the cache for all expando rows.
819             *
820             * <p>
821             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
822             * </p>
823             */
824            @Override
825            public void clearCache() {
826                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
827                            CacheRegistryUtil.clear(ExpandoRowImpl.class.getName());
828                    }
829    
830                    EntityCacheUtil.clearCache(ExpandoRowImpl.class.getName());
831    
832                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
833                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
834                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
835            }
836    
837            /**
838             * Clears the cache for the expando row.
839             *
840             * <p>
841             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
842             * </p>
843             */
844            @Override
845            public void clearCache(ExpandoRow expandoRow) {
846                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
847                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
848    
849                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
850                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
851    
852                    clearUniqueFindersCache(expandoRow);
853            }
854    
855            @Override
856            public void clearCache(List<ExpandoRow> expandoRows) {
857                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
858                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
859    
860                    for (ExpandoRow expandoRow : expandoRows) {
861                            EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
862                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey());
863    
864                            clearUniqueFindersCache(expandoRow);
865                    }
866            }
867    
868            protected void cacheUniqueFindersCache(ExpandoRow expandoRow) {
869                    if (expandoRow.isNew()) {
870                            Object[] args = new Object[] {
871                                            expandoRow.getTableId(), expandoRow.getClassPK()
872                                    };
873    
874                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
875                                    Long.valueOf(1));
876                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args, expandoRow);
877                    }
878                    else {
879                            ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
880    
881                            if ((expandoRowModelImpl.getColumnBitmask() &
882                                            FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
883                                    Object[] args = new Object[] {
884                                                    expandoRow.getTableId(), expandoRow.getClassPK()
885                                            };
886    
887                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
888                                            Long.valueOf(1));
889                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args,
890                                            expandoRow);
891                            }
892                    }
893            }
894    
895            protected void clearUniqueFindersCache(ExpandoRow expandoRow) {
896                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
897    
898                    Object[] args = new Object[] {
899                                    expandoRow.getTableId(), expandoRow.getClassPK()
900                            };
901    
902                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
903                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
904    
905                    if ((expandoRowModelImpl.getColumnBitmask() &
906                                    FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
907                            args = new Object[] {
908                                            expandoRowModelImpl.getOriginalTableId(),
909                                            expandoRowModelImpl.getOriginalClassPK()
910                                    };
911    
912                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
913                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
914                    }
915            }
916    
917            /**
918             * Creates a new expando row with the primary key. Does not add the expando row to the database.
919             *
920             * @param rowId the primary key for the new expando row
921             * @return the new expando row
922             */
923            public ExpandoRow create(long rowId) {
924                    ExpandoRow expandoRow = new ExpandoRowImpl();
925    
926                    expandoRow.setNew(true);
927                    expandoRow.setPrimaryKey(rowId);
928    
929                    return expandoRow;
930            }
931    
932            /**
933             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
934             *
935             * @param rowId the primary key of the expando row
936             * @return the expando row that was removed
937             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
938             * @throws SystemException if a system exception occurred
939             */
940            public ExpandoRow remove(long rowId)
941                    throws NoSuchRowException, SystemException {
942                    return remove((Serializable)rowId);
943            }
944    
945            /**
946             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
947             *
948             * @param primaryKey the primary key of the expando row
949             * @return the expando row that was removed
950             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
951             * @throws SystemException if a system exception occurred
952             */
953            @Override
954            public ExpandoRow remove(Serializable primaryKey)
955                    throws NoSuchRowException, SystemException {
956                    Session session = null;
957    
958                    try {
959                            session = openSession();
960    
961                            ExpandoRow expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
962                                            primaryKey);
963    
964                            if (expandoRow == null) {
965                                    if (_log.isWarnEnabled()) {
966                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
967                                    }
968    
969                                    throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
970                                            primaryKey);
971                            }
972    
973                            return remove(expandoRow);
974                    }
975                    catch (NoSuchRowException nsee) {
976                            throw nsee;
977                    }
978                    catch (Exception e) {
979                            throw processException(e);
980                    }
981                    finally {
982                            closeSession(session);
983                    }
984            }
985    
986            @Override
987            protected ExpandoRow removeImpl(ExpandoRow expandoRow)
988                    throws SystemException {
989                    expandoRow = toUnwrappedModel(expandoRow);
990    
991                    Session session = null;
992    
993                    try {
994                            session = openSession();
995    
996                            if (!session.contains(expandoRow)) {
997                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
998                                                    expandoRow.getPrimaryKeyObj());
999                            }
1000    
1001                            if (expandoRow != null) {
1002                                    session.delete(expandoRow);
1003                            }
1004                    }
1005                    catch (Exception e) {
1006                            throw processException(e);
1007                    }
1008                    finally {
1009                            closeSession(session);
1010                    }
1011    
1012                    if (expandoRow != null) {
1013                            clearCache(expandoRow);
1014                    }
1015    
1016                    return expandoRow;
1017            }
1018    
1019            @Override
1020            public ExpandoRow updateImpl(
1021                    com.liferay.portlet.expando.model.ExpandoRow expandoRow)
1022                    throws SystemException {
1023                    expandoRow = toUnwrappedModel(expandoRow);
1024    
1025                    boolean isNew = expandoRow.isNew();
1026    
1027                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
1028    
1029                    Session session = null;
1030    
1031                    try {
1032                            session = openSession();
1033    
1034                            if (expandoRow.isNew()) {
1035                                    session.save(expandoRow);
1036    
1037                                    expandoRow.setNew(false);
1038                            }
1039                            else {
1040                                    session.merge(expandoRow);
1041                            }
1042                    }
1043                    catch (Exception e) {
1044                            throw processException(e);
1045                    }
1046                    finally {
1047                            closeSession(session);
1048                    }
1049    
1050                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1051    
1052                    if (isNew || !ExpandoRowModelImpl.COLUMN_BITMASK_ENABLED) {
1053                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1054                    }
1055    
1056                    else {
1057                            if ((expandoRowModelImpl.getColumnBitmask() &
1058                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID.getColumnBitmask()) != 0) {
1059                                    Object[] args = new Object[] {
1060                                                    expandoRowModelImpl.getOriginalTableId()
1061                                            };
1062    
1063                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1064                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1065                                            args);
1066    
1067                                    args = new Object[] { expandoRowModelImpl.getTableId() };
1068    
1069                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1070                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1071                                            args);
1072                            }
1073                    }
1074    
1075                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1076                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
1077    
1078                    clearUniqueFindersCache(expandoRow);
1079                    cacheUniqueFindersCache(expandoRow);
1080    
1081                    return expandoRow;
1082            }
1083    
1084            protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
1085                    if (expandoRow instanceof ExpandoRowImpl) {
1086                            return expandoRow;
1087                    }
1088    
1089                    ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
1090    
1091                    expandoRowImpl.setNew(expandoRow.isNew());
1092                    expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
1093    
1094                    expandoRowImpl.setRowId(expandoRow.getRowId());
1095                    expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
1096                    expandoRowImpl.setModifiedDate(expandoRow.getModifiedDate());
1097                    expandoRowImpl.setTableId(expandoRow.getTableId());
1098                    expandoRowImpl.setClassPK(expandoRow.getClassPK());
1099    
1100                    return expandoRowImpl;
1101            }
1102    
1103            /**
1104             * Returns the expando row with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1105             *
1106             * @param primaryKey the primary key of the expando row
1107             * @return the expando row
1108             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
1109             * @throws SystemException if a system exception occurred
1110             */
1111            @Override
1112            public ExpandoRow findByPrimaryKey(Serializable primaryKey)
1113                    throws NoSuchRowException, SystemException {
1114                    ExpandoRow expandoRow = fetchByPrimaryKey(primaryKey);
1115    
1116                    if (expandoRow == null) {
1117                            if (_log.isWarnEnabled()) {
1118                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1119                            }
1120    
1121                            throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1122                                    primaryKey);
1123                    }
1124    
1125                    return expandoRow;
1126            }
1127    
1128            /**
1129             * Returns the expando row with the primary key or throws a {@link com.liferay.portlet.expando.NoSuchRowException} if it could not be found.
1130             *
1131             * @param rowId the primary key of the expando row
1132             * @return the expando row
1133             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
1134             * @throws SystemException if a system exception occurred
1135             */
1136            public ExpandoRow findByPrimaryKey(long rowId)
1137                    throws NoSuchRowException, SystemException {
1138                    return findByPrimaryKey((Serializable)rowId);
1139            }
1140    
1141            /**
1142             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
1143             *
1144             * @param primaryKey the primary key of the expando row
1145             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
1146             * @throws SystemException if a system exception occurred
1147             */
1148            @Override
1149            public ExpandoRow fetchByPrimaryKey(Serializable primaryKey)
1150                    throws SystemException {
1151                    ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1152                                    ExpandoRowImpl.class, primaryKey);
1153    
1154                    if (expandoRow == _nullExpandoRow) {
1155                            return null;
1156                    }
1157    
1158                    if (expandoRow == null) {
1159                            Session session = null;
1160    
1161                            try {
1162                                    session = openSession();
1163    
1164                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1165                                                    primaryKey);
1166    
1167                                    if (expandoRow != null) {
1168                                            cacheResult(expandoRow);
1169                                    }
1170                                    else {
1171                                            EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1172                                                    ExpandoRowImpl.class, primaryKey, _nullExpandoRow);
1173                                    }
1174                            }
1175                            catch (Exception e) {
1176                                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1177                                            ExpandoRowImpl.class, primaryKey);
1178    
1179                                    throw processException(e);
1180                            }
1181                            finally {
1182                                    closeSession(session);
1183                            }
1184                    }
1185    
1186                    return expandoRow;
1187            }
1188    
1189            /**
1190             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
1191             *
1192             * @param rowId the primary key of the expando row
1193             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
1194             * @throws SystemException if a system exception occurred
1195             */
1196            public ExpandoRow fetchByPrimaryKey(long rowId) throws SystemException {
1197                    return fetchByPrimaryKey((Serializable)rowId);
1198            }
1199    
1200            /**
1201             * Returns all the expando rows.
1202             *
1203             * @return the expando rows
1204             * @throws SystemException if a system exception occurred
1205             */
1206            public List<ExpandoRow> findAll() throws SystemException {
1207                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1208            }
1209    
1210            /**
1211             * Returns a range of all the expando rows.
1212             *
1213             * <p>
1214             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1215             * </p>
1216             *
1217             * @param start the lower bound of the range of expando rows
1218             * @param end the upper bound of the range of expando rows (not inclusive)
1219             * @return the range of expando rows
1220             * @throws SystemException if a system exception occurred
1221             */
1222            public List<ExpandoRow> findAll(int start, int end)
1223                    throws SystemException {
1224                    return findAll(start, end, null);
1225            }
1226    
1227            /**
1228             * Returns an ordered range of all the expando rows.
1229             *
1230             * <p>
1231             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1232             * </p>
1233             *
1234             * @param start the lower bound of the range of expando rows
1235             * @param end the upper bound of the range of expando rows (not inclusive)
1236             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1237             * @return the ordered range of expando rows
1238             * @throws SystemException if a system exception occurred
1239             */
1240            public List<ExpandoRow> findAll(int start, int end,
1241                    OrderByComparator orderByComparator) throws SystemException {
1242                    boolean pagination = true;
1243                    FinderPath finderPath = null;
1244                    Object[] finderArgs = null;
1245    
1246                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1247                                    (orderByComparator == null)) {
1248                            pagination = false;
1249                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1250                            finderArgs = FINDER_ARGS_EMPTY;
1251                    }
1252                    else {
1253                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1254                            finderArgs = new Object[] { start, end, orderByComparator };
1255                    }
1256    
1257                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
1258                                    finderArgs, this);
1259    
1260                    if (list == null) {
1261                            StringBundler query = null;
1262                            String sql = null;
1263    
1264                            if (orderByComparator != null) {
1265                                    query = new StringBundler(2 +
1266                                                    (orderByComparator.getOrderByFields().length * 3));
1267    
1268                                    query.append(_SQL_SELECT_EXPANDOROW);
1269    
1270                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1271                                            orderByComparator);
1272    
1273                                    sql = query.toString();
1274                            }
1275                            else {
1276                                    sql = _SQL_SELECT_EXPANDOROW;
1277    
1278                                    if (pagination) {
1279                                            sql = sql.concat(ExpandoRowModelImpl.ORDER_BY_JPQL);
1280                                    }
1281                            }
1282    
1283                            Session session = null;
1284    
1285                            try {
1286                                    session = openSession();
1287    
1288                                    Query q = session.createQuery(sql);
1289    
1290                                    if (!pagination) {
1291                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1292                                                            start, end, false);
1293    
1294                                            Collections.sort(list);
1295    
1296                                            list = new UnmodifiableList<ExpandoRow>(list);
1297                                    }
1298                                    else {
1299                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1300                                                            start, end);
1301                                    }
1302    
1303                                    cacheResult(list);
1304    
1305                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1306                            }
1307                            catch (Exception e) {
1308                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1309    
1310                                    throw processException(e);
1311                            }
1312                            finally {
1313                                    closeSession(session);
1314                            }
1315                    }
1316    
1317                    return list;
1318            }
1319    
1320            /**
1321             * Removes all the expando rows from the database.
1322             *
1323             * @throws SystemException if a system exception occurred
1324             */
1325            public void removeAll() throws SystemException {
1326                    for (ExpandoRow expandoRow : findAll()) {
1327                            remove(expandoRow);
1328                    }
1329            }
1330    
1331            /**
1332             * Returns the number of expando rows.
1333             *
1334             * @return the number of expando rows
1335             * @throws SystemException if a system exception occurred
1336             */
1337            public int countAll() throws SystemException {
1338                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1339                                    FINDER_ARGS_EMPTY, this);
1340    
1341                    if (count == null) {
1342                            Session session = null;
1343    
1344                            try {
1345                                    session = openSession();
1346    
1347                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1348    
1349                                    count = (Long)q.uniqueResult();
1350    
1351                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1352                                            FINDER_ARGS_EMPTY, count);
1353                            }
1354                            catch (Exception e) {
1355                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1356                                            FINDER_ARGS_EMPTY);
1357    
1358                                    throw processException(e);
1359                            }
1360                            finally {
1361                                    closeSession(session);
1362                            }
1363                    }
1364    
1365                    return count.intValue();
1366            }
1367    
1368            /**
1369             * Initializes the expando row persistence.
1370             */
1371            public void afterPropertiesSet() {
1372                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1373                                            com.liferay.portal.util.PropsUtil.get(
1374                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1375    
1376                    if (listenerClassNames.length > 0) {
1377                            try {
1378                                    List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1379    
1380                                    for (String listenerClassName : listenerClassNames) {
1381                                            listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
1382                                                            listenerClassName));
1383                                    }
1384    
1385                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1386                            }
1387                            catch (Exception e) {
1388                                    _log.error(e);
1389                            }
1390                    }
1391            }
1392    
1393            public void destroy() {
1394                    EntityCacheUtil.removeCache(ExpandoRowImpl.class.getName());
1395                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1396                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1397                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1398            }
1399    
1400            private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
1401            private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
1402            private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
1403            private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
1404            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
1405            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
1406            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
1407            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1408            private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1409            private static ExpandoRow _nullExpandoRow = new ExpandoRowImpl() {
1410                            @Override
1411                            public Object clone() {
1412                                    return this;
1413                            }
1414    
1415                            @Override
1416                            public CacheModel<ExpandoRow> toCacheModel() {
1417                                    return _nullExpandoRowCacheModel;
1418                            }
1419                    };
1420    
1421            private static CacheModel<ExpandoRow> _nullExpandoRowCacheModel = new CacheModel<ExpandoRow>() {
1422                            public ExpandoRow toEntityModel() {
1423                                    return _nullExpandoRow;
1424                            }
1425                    };
1426    }