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