001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.log.Log;
027    import com.liferay.portal.kernel.log.LogFactoryUtil;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.SetUtil;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.model.CacheModel;
033    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
034    
035    import com.liferay.portlet.expando.NoSuchRowException;
036    import com.liferay.portlet.expando.model.ExpandoRow;
037    import com.liferay.portlet.expando.model.impl.ExpandoRowImpl;
038    import com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl;
039    import com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence;
040    
041    import java.io.Serializable;
042    
043    import java.util.Collections;
044    import java.util.HashMap;
045    import java.util.HashSet;
046    import java.util.Iterator;
047    import java.util.List;
048    import java.util.Map;
049    import java.util.Set;
050    
051    /**
052     * The persistence implementation for the expando row service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see ExpandoRowPersistence
060     * @see com.liferay.portlet.expando.service.persistence.ExpandoRowUtil
061     * @generated
062     */
063    @ProviderType
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             */
111            @Override
112            public List<ExpandoRow> findByTableId(long tableId) {
113                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
114            }
115    
116            /**
117             * Returns a range of all the expando rows where tableId = &#63;.
118             *
119             * <p>
120             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
121             * </p>
122             *
123             * @param tableId the table ID
124             * @param start the lower bound of the range of expando rows
125             * @param end the upper bound of the range of expando rows (not inclusive)
126             * @return the range of matching expando rows
127             */
128            @Override
129            public List<ExpandoRow> findByTableId(long tableId, int start, int end) {
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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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             */
146            @Override
147            public List<ExpandoRow> findByTableId(long tableId, int start, int end,
148                    OrderByComparator<ExpandoRow> orderByComparator) {
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 = Collections.unmodifiableList(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 NoSuchRowException if a matching expando row could not be found
251             */
252            @Override
253            public ExpandoRow findByTableId_First(long tableId,
254                    OrderByComparator<ExpandoRow> orderByComparator)
255                    throws NoSuchRowException {
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             */
281            @Override
282            public ExpandoRow fetchByTableId_First(long tableId,
283                    OrderByComparator<ExpandoRow> orderByComparator) {
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 NoSuchRowException if a matching expando row could not be found
300             */
301            @Override
302            public ExpandoRow findByTableId_Last(long tableId,
303                    OrderByComparator<ExpandoRow> orderByComparator)
304                    throws NoSuchRowException {
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             */
330            @Override
331            public ExpandoRow fetchByTableId_Last(long tableId,
332                    OrderByComparator<ExpandoRow> orderByComparator) {
333                    int count = countByTableId(tableId);
334    
335                    if (count == 0) {
336                            return null;
337                    }
338    
339                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
340                                    orderByComparator);
341    
342                    if (!list.isEmpty()) {
343                            return list.get(0);
344                    }
345    
346                    return null;
347            }
348    
349            /**
350             * Returns the expando rows before and after the current expando row in the ordered set where tableId = &#63;.
351             *
352             * @param rowId the primary key of the current expando row
353             * @param tableId the table ID
354             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
355             * @return the previous, current, and next expando row
356             * @throws NoSuchRowException if a expando row with the primary key could not be found
357             */
358            @Override
359            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
360                    OrderByComparator<ExpandoRow> orderByComparator)
361                    throws NoSuchRowException {
362                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
363    
364                    Session session = null;
365    
366                    try {
367                            session = openSession();
368    
369                            ExpandoRow[] array = new ExpandoRowImpl[3];
370    
371                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
372                                            orderByComparator, true);
373    
374                            array[1] = expandoRow;
375    
376                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
377                                            orderByComparator, false);
378    
379                            return array;
380                    }
381                    catch (Exception e) {
382                            throw processException(e);
383                    }
384                    finally {
385                            closeSession(session);
386                    }
387            }
388    
389            protected ExpandoRow getByTableId_PrevAndNext(Session session,
390                    ExpandoRow expandoRow, long tableId,
391                    OrderByComparator<ExpandoRow> orderByComparator, boolean previous) {
392                    StringBundler query = null;
393    
394                    if (orderByComparator != null) {
395                            query = new StringBundler(6 +
396                                            (orderByComparator.getOrderByFields().length * 6));
397                    }
398                    else {
399                            query = new StringBundler(3);
400                    }
401    
402                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
403    
404                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
405    
406                    if (orderByComparator != null) {
407                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
408    
409                            if (orderByConditionFields.length > 0) {
410                                    query.append(WHERE_AND);
411                            }
412    
413                            for (int i = 0; i < orderByConditionFields.length; i++) {
414                                    query.append(_ORDER_BY_ENTITY_ALIAS);
415                                    query.append(orderByConditionFields[i]);
416    
417                                    if ((i + 1) < orderByConditionFields.length) {
418                                            if (orderByComparator.isAscending() ^ previous) {
419                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
420                                            }
421                                            else {
422                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
423                                            }
424                                    }
425                                    else {
426                                            if (orderByComparator.isAscending() ^ previous) {
427                                                    query.append(WHERE_GREATER_THAN);
428                                            }
429                                            else {
430                                                    query.append(WHERE_LESSER_THAN);
431                                            }
432                                    }
433                            }
434    
435                            query.append(ORDER_BY_CLAUSE);
436    
437                            String[] orderByFields = orderByComparator.getOrderByFields();
438    
439                            for (int i = 0; i < orderByFields.length; i++) {
440                                    query.append(_ORDER_BY_ENTITY_ALIAS);
441                                    query.append(orderByFields[i]);
442    
443                                    if ((i + 1) < orderByFields.length) {
444                                            if (orderByComparator.isAscending() ^ previous) {
445                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
446                                            }
447                                            else {
448                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
449                                            }
450                                    }
451                                    else {
452                                            if (orderByComparator.isAscending() ^ previous) {
453                                                    query.append(ORDER_BY_ASC);
454                                            }
455                                            else {
456                                                    query.append(ORDER_BY_DESC);
457                                            }
458                                    }
459                            }
460                    }
461                    else {
462                            query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
463                    }
464    
465                    String sql = query.toString();
466    
467                    Query q = session.createQuery(sql);
468    
469                    q.setFirstResult(0);
470                    q.setMaxResults(2);
471    
472                    QueryPos qPos = QueryPos.getInstance(q);
473    
474                    qPos.add(tableId);
475    
476                    if (orderByComparator != null) {
477                            Object[] values = orderByComparator.getOrderByConditionValues(expandoRow);
478    
479                            for (Object value : values) {
480                                    qPos.add(value);
481                            }
482                    }
483    
484                    List<ExpandoRow> list = q.list();
485    
486                    if (list.size() == 2) {
487                            return list.get(1);
488                    }
489                    else {
490                            return null;
491                    }
492            }
493    
494            /**
495             * Removes all the expando rows where tableId = &#63; from the database.
496             *
497             * @param tableId the table ID
498             */
499            @Override
500            public void removeByTableId(long tableId) {
501                    for (ExpandoRow expandoRow : findByTableId(tableId, QueryUtil.ALL_POS,
502                                    QueryUtil.ALL_POS, null)) {
503                            remove(expandoRow);
504                    }
505            }
506    
507            /**
508             * Returns the number of expando rows where tableId = &#63;.
509             *
510             * @param tableId the table ID
511             * @return the number of matching expando rows
512             */
513            @Override
514            public int countByTableId(long tableId) {
515                    FinderPath finderPath = FINDER_PATH_COUNT_BY_TABLEID;
516    
517                    Object[] finderArgs = new Object[] { tableId };
518    
519                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
520                                    this);
521    
522                    if (count == null) {
523                            StringBundler query = new StringBundler(2);
524    
525                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
526    
527                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
528    
529                            String sql = query.toString();
530    
531                            Session session = null;
532    
533                            try {
534                                    session = openSession();
535    
536                                    Query q = session.createQuery(sql);
537    
538                                    QueryPos qPos = QueryPos.getInstance(q);
539    
540                                    qPos.add(tableId);
541    
542                                    count = (Long)q.uniqueResult();
543    
544                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
545                            }
546                            catch (Exception e) {
547                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
548    
549                                    throw processException(e);
550                            }
551                            finally {
552                                    closeSession(session);
553                            }
554                    }
555    
556                    return count.intValue();
557            }
558    
559            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
560            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSPK = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
561                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
562                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByClassPK",
563                            new String[] {
564                                    Long.class.getName(),
565                                    
566                            Integer.class.getName(), Integer.class.getName(),
567                                    OrderByComparator.class.getName()
568                            });
569            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK =
570                    new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
571                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
572                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByClassPK",
573                            new String[] { Long.class.getName() },
574                            ExpandoRowModelImpl.CLASSPK_COLUMN_BITMASK);
575            public static final FinderPath FINDER_PATH_COUNT_BY_CLASSPK = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
576                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
577                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByClassPK",
578                            new String[] { Long.class.getName() });
579    
580            /**
581             * Returns all the expando rows where classPK = &#63;.
582             *
583             * @param classPK the class p k
584             * @return the matching expando rows
585             */
586            @Override
587            public List<ExpandoRow> findByClassPK(long classPK) {
588                    return findByClassPK(classPK, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
589            }
590    
591            /**
592             * Returns a range of all the expando rows where classPK = &#63;.
593             *
594             * <p>
595             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
596             * </p>
597             *
598             * @param classPK the class p k
599             * @param start the lower bound of the range of expando rows
600             * @param end the upper bound of the range of expando rows (not inclusive)
601             * @return the range of matching expando rows
602             */
603            @Override
604            public List<ExpandoRow> findByClassPK(long classPK, int start, int end) {
605                    return findByClassPK(classPK, start, end, null);
606            }
607    
608            /**
609             * Returns an ordered range of all the expando rows where classPK = &#63;.
610             *
611             * <p>
612             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
613             * </p>
614             *
615             * @param classPK the class p k
616             * @param start the lower bound of the range of expando rows
617             * @param end the upper bound of the range of expando rows (not inclusive)
618             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
619             * @return the ordered range of matching expando rows
620             */
621            @Override
622            public List<ExpandoRow> findByClassPK(long classPK, int start, int end,
623                    OrderByComparator<ExpandoRow> orderByComparator) {
624                    boolean pagination = true;
625                    FinderPath finderPath = null;
626                    Object[] finderArgs = null;
627    
628                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
629                                    (orderByComparator == null)) {
630                            pagination = false;
631                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK;
632                            finderArgs = new Object[] { classPK };
633                    }
634                    else {
635                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CLASSPK;
636                            finderArgs = new Object[] { classPK, start, end, orderByComparator };
637                    }
638    
639                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
640                                    finderArgs, this);
641    
642                    if ((list != null) && !list.isEmpty()) {
643                            for (ExpandoRow expandoRow : list) {
644                                    if ((classPK != expandoRow.getClassPK())) {
645                                            list = null;
646    
647                                            break;
648                                    }
649                            }
650                    }
651    
652                    if (list == null) {
653                            StringBundler query = null;
654    
655                            if (orderByComparator != null) {
656                                    query = new StringBundler(3 +
657                                                    (orderByComparator.getOrderByFields().length * 3));
658                            }
659                            else {
660                                    query = new StringBundler(3);
661                            }
662    
663                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
664    
665                            query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
666    
667                            if (orderByComparator != null) {
668                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
669                                            orderByComparator);
670                            }
671                            else
672                             if (pagination) {
673                                    query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
674                            }
675    
676                            String sql = query.toString();
677    
678                            Session session = null;
679    
680                            try {
681                                    session = openSession();
682    
683                                    Query q = session.createQuery(sql);
684    
685                                    QueryPos qPos = QueryPos.getInstance(q);
686    
687                                    qPos.add(classPK);
688    
689                                    if (!pagination) {
690                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
691                                                            start, end, false);
692    
693                                            Collections.sort(list);
694    
695                                            list = Collections.unmodifiableList(list);
696                                    }
697                                    else {
698                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
699                                                            start, end);
700                                    }
701    
702                                    cacheResult(list);
703    
704                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
705                            }
706                            catch (Exception e) {
707                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
708    
709                                    throw processException(e);
710                            }
711                            finally {
712                                    closeSession(session);
713                            }
714                    }
715    
716                    return list;
717            }
718    
719            /**
720             * Returns the first expando row in the ordered set where classPK = &#63;.
721             *
722             * @param classPK the class p k
723             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
724             * @return the first matching expando row
725             * @throws NoSuchRowException if a matching expando row could not be found
726             */
727            @Override
728            public ExpandoRow findByClassPK_First(long classPK,
729                    OrderByComparator<ExpandoRow> orderByComparator)
730                    throws NoSuchRowException {
731                    ExpandoRow expandoRow = fetchByClassPK_First(classPK, orderByComparator);
732    
733                    if (expandoRow != null) {
734                            return expandoRow;
735                    }
736    
737                    StringBundler msg = new StringBundler(4);
738    
739                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
740    
741                    msg.append("classPK=");
742                    msg.append(classPK);
743    
744                    msg.append(StringPool.CLOSE_CURLY_BRACE);
745    
746                    throw new NoSuchRowException(msg.toString());
747            }
748    
749            /**
750             * Returns the first expando row in the ordered set where classPK = &#63;.
751             *
752             * @param classPK the class p k
753             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
754             * @return the first matching expando row, or <code>null</code> if a matching expando row could not be found
755             */
756            @Override
757            public ExpandoRow fetchByClassPK_First(long classPK,
758                    OrderByComparator<ExpandoRow> orderByComparator) {
759                    List<ExpandoRow> list = findByClassPK(classPK, 0, 1, orderByComparator);
760    
761                    if (!list.isEmpty()) {
762                            return list.get(0);
763                    }
764    
765                    return null;
766            }
767    
768            /**
769             * Returns the last expando row in the ordered set where classPK = &#63;.
770             *
771             * @param classPK the class p k
772             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
773             * @return the last matching expando row
774             * @throws NoSuchRowException if a matching expando row could not be found
775             */
776            @Override
777            public ExpandoRow findByClassPK_Last(long classPK,
778                    OrderByComparator<ExpandoRow> orderByComparator)
779                    throws NoSuchRowException {
780                    ExpandoRow expandoRow = fetchByClassPK_Last(classPK, orderByComparator);
781    
782                    if (expandoRow != null) {
783                            return expandoRow;
784                    }
785    
786                    StringBundler msg = new StringBundler(4);
787    
788                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
789    
790                    msg.append("classPK=");
791                    msg.append(classPK);
792    
793                    msg.append(StringPool.CLOSE_CURLY_BRACE);
794    
795                    throw new NoSuchRowException(msg.toString());
796            }
797    
798            /**
799             * Returns the last expando row in the ordered set where classPK = &#63;.
800             *
801             * @param classPK the class p k
802             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
803             * @return the last matching expando row, or <code>null</code> if a matching expando row could not be found
804             */
805            @Override
806            public ExpandoRow fetchByClassPK_Last(long classPK,
807                    OrderByComparator<ExpandoRow> orderByComparator) {
808                    int count = countByClassPK(classPK);
809    
810                    if (count == 0) {
811                            return null;
812                    }
813    
814                    List<ExpandoRow> list = findByClassPK(classPK, count - 1, count,
815                                    orderByComparator);
816    
817                    if (!list.isEmpty()) {
818                            return list.get(0);
819                    }
820    
821                    return null;
822            }
823    
824            /**
825             * Returns the expando rows before and after the current expando row in the ordered set where classPK = &#63;.
826             *
827             * @param rowId the primary key of the current expando row
828             * @param classPK the class p k
829             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
830             * @return the previous, current, and next expando row
831             * @throws NoSuchRowException if a expando row with the primary key could not be found
832             */
833            @Override
834            public ExpandoRow[] findByClassPK_PrevAndNext(long rowId, long classPK,
835                    OrderByComparator<ExpandoRow> orderByComparator)
836                    throws NoSuchRowException {
837                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
838    
839                    Session session = null;
840    
841                    try {
842                            session = openSession();
843    
844                            ExpandoRow[] array = new ExpandoRowImpl[3];
845    
846                            array[0] = getByClassPK_PrevAndNext(session, expandoRow, classPK,
847                                            orderByComparator, true);
848    
849                            array[1] = expandoRow;
850    
851                            array[2] = getByClassPK_PrevAndNext(session, expandoRow, classPK,
852                                            orderByComparator, false);
853    
854                            return array;
855                    }
856                    catch (Exception e) {
857                            throw processException(e);
858                    }
859                    finally {
860                            closeSession(session);
861                    }
862            }
863    
864            protected ExpandoRow getByClassPK_PrevAndNext(Session session,
865                    ExpandoRow expandoRow, long classPK,
866                    OrderByComparator<ExpandoRow> orderByComparator, boolean previous) {
867                    StringBundler query = null;
868    
869                    if (orderByComparator != null) {
870                            query = new StringBundler(6 +
871                                            (orderByComparator.getOrderByFields().length * 6));
872                    }
873                    else {
874                            query = new StringBundler(3);
875                    }
876    
877                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
878    
879                    query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
880    
881                    if (orderByComparator != null) {
882                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
883    
884                            if (orderByConditionFields.length > 0) {
885                                    query.append(WHERE_AND);
886                            }
887    
888                            for (int i = 0; i < orderByConditionFields.length; i++) {
889                                    query.append(_ORDER_BY_ENTITY_ALIAS);
890                                    query.append(orderByConditionFields[i]);
891    
892                                    if ((i + 1) < orderByConditionFields.length) {
893                                            if (orderByComparator.isAscending() ^ previous) {
894                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
895                                            }
896                                            else {
897                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
898                                            }
899                                    }
900                                    else {
901                                            if (orderByComparator.isAscending() ^ previous) {
902                                                    query.append(WHERE_GREATER_THAN);
903                                            }
904                                            else {
905                                                    query.append(WHERE_LESSER_THAN);
906                                            }
907                                    }
908                            }
909    
910                            query.append(ORDER_BY_CLAUSE);
911    
912                            String[] orderByFields = orderByComparator.getOrderByFields();
913    
914                            for (int i = 0; i < orderByFields.length; i++) {
915                                    query.append(_ORDER_BY_ENTITY_ALIAS);
916                                    query.append(orderByFields[i]);
917    
918                                    if ((i + 1) < orderByFields.length) {
919                                            if (orderByComparator.isAscending() ^ previous) {
920                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
921                                            }
922                                            else {
923                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
924                                            }
925                                    }
926                                    else {
927                                            if (orderByComparator.isAscending() ^ previous) {
928                                                    query.append(ORDER_BY_ASC);
929                                            }
930                                            else {
931                                                    query.append(ORDER_BY_DESC);
932                                            }
933                                    }
934                            }
935                    }
936                    else {
937                            query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
938                    }
939    
940                    String sql = query.toString();
941    
942                    Query q = session.createQuery(sql);
943    
944                    q.setFirstResult(0);
945                    q.setMaxResults(2);
946    
947                    QueryPos qPos = QueryPos.getInstance(q);
948    
949                    qPos.add(classPK);
950    
951                    if (orderByComparator != null) {
952                            Object[] values = orderByComparator.getOrderByConditionValues(expandoRow);
953    
954                            for (Object value : values) {
955                                    qPos.add(value);
956                            }
957                    }
958    
959                    List<ExpandoRow> list = q.list();
960    
961                    if (list.size() == 2) {
962                            return list.get(1);
963                    }
964                    else {
965                            return null;
966                    }
967            }
968    
969            /**
970             * Removes all the expando rows where classPK = &#63; from the database.
971             *
972             * @param classPK the class p k
973             */
974            @Override
975            public void removeByClassPK(long classPK) {
976                    for (ExpandoRow expandoRow : findByClassPK(classPK, QueryUtil.ALL_POS,
977                                    QueryUtil.ALL_POS, null)) {
978                            remove(expandoRow);
979                    }
980            }
981    
982            /**
983             * Returns the number of expando rows where classPK = &#63;.
984             *
985             * @param classPK the class p k
986             * @return the number of matching expando rows
987             */
988            @Override
989            public int countByClassPK(long classPK) {
990                    FinderPath finderPath = FINDER_PATH_COUNT_BY_CLASSPK;
991    
992                    Object[] finderArgs = new Object[] { classPK };
993    
994                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
995                                    this);
996    
997                    if (count == null) {
998                            StringBundler query = new StringBundler(2);
999    
1000                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1001    
1002                            query.append(_FINDER_COLUMN_CLASSPK_CLASSPK_2);
1003    
1004                            String sql = query.toString();
1005    
1006                            Session session = null;
1007    
1008                            try {
1009                                    session = openSession();
1010    
1011                                    Query q = session.createQuery(sql);
1012    
1013                                    QueryPos qPos = QueryPos.getInstance(q);
1014    
1015                                    qPos.add(classPK);
1016    
1017                                    count = (Long)q.uniqueResult();
1018    
1019                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1020                            }
1021                            catch (Exception e) {
1022                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1023    
1024                                    throw processException(e);
1025                            }
1026                            finally {
1027                                    closeSession(session);
1028                            }
1029                    }
1030    
1031                    return count.intValue();
1032            }
1033    
1034            private static final String _FINDER_COLUMN_CLASSPK_CLASSPK_2 = "expandoRow.classPK = ?";
1035            public static final FinderPath FINDER_PATH_FETCH_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1036                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
1037                            FINDER_CLASS_NAME_ENTITY, "fetchByT_C",
1038                            new String[] { Long.class.getName(), Long.class.getName() },
1039                            ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK |
1040                            ExpandoRowModelImpl.CLASSPK_COLUMN_BITMASK);
1041            public static final FinderPath FINDER_PATH_COUNT_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1042                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
1043                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByT_C",
1044                            new String[] { Long.class.getName(), Long.class.getName() });
1045    
1046            /**
1047             * Returns the expando row where tableId = &#63; and classPK = &#63; or throws a {@link NoSuchRowException} if it could not be found.
1048             *
1049             * @param tableId the table ID
1050             * @param classPK the class p k
1051             * @return the matching expando row
1052             * @throws NoSuchRowException if a matching expando row could not be found
1053             */
1054            @Override
1055            public ExpandoRow findByT_C(long tableId, long classPK)
1056                    throws NoSuchRowException {
1057                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
1058    
1059                    if (expandoRow == null) {
1060                            StringBundler msg = new StringBundler(6);
1061    
1062                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1063    
1064                            msg.append("tableId=");
1065                            msg.append(tableId);
1066    
1067                            msg.append(", classPK=");
1068                            msg.append(classPK);
1069    
1070                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1071    
1072                            if (_log.isWarnEnabled()) {
1073                                    _log.warn(msg.toString());
1074                            }
1075    
1076                            throw new NoSuchRowException(msg.toString());
1077                    }
1078    
1079                    return expandoRow;
1080            }
1081    
1082            /**
1083             * 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.
1084             *
1085             * @param tableId the table ID
1086             * @param classPK the class p k
1087             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
1088             */
1089            @Override
1090            public ExpandoRow fetchByT_C(long tableId, long classPK) {
1091                    return fetchByT_C(tableId, classPK, true);
1092            }
1093    
1094            /**
1095             * 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.
1096             *
1097             * @param tableId the table ID
1098             * @param classPK the class p k
1099             * @param retrieveFromCache whether to use the finder cache
1100             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
1101             */
1102            @Override
1103            public ExpandoRow fetchByT_C(long tableId, long classPK,
1104                    boolean retrieveFromCache) {
1105                    Object[] finderArgs = new Object[] { tableId, classPK };
1106    
1107                    Object result = null;
1108    
1109                    if (retrieveFromCache) {
1110                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
1111                                            finderArgs, this);
1112                    }
1113    
1114                    if (result instanceof ExpandoRow) {
1115                            ExpandoRow expandoRow = (ExpandoRow)result;
1116    
1117                            if ((tableId != expandoRow.getTableId()) ||
1118                                            (classPK != expandoRow.getClassPK())) {
1119                                    result = null;
1120                            }
1121                    }
1122    
1123                    if (result == null) {
1124                            StringBundler query = new StringBundler(4);
1125    
1126                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
1127    
1128                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
1129    
1130                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
1131    
1132                            String sql = query.toString();
1133    
1134                            Session session = null;
1135    
1136                            try {
1137                                    session = openSession();
1138    
1139                                    Query q = session.createQuery(sql);
1140    
1141                                    QueryPos qPos = QueryPos.getInstance(q);
1142    
1143                                    qPos.add(tableId);
1144    
1145                                    qPos.add(classPK);
1146    
1147                                    List<ExpandoRow> list = q.list();
1148    
1149                                    if (list.isEmpty()) {
1150                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
1151                                                    finderArgs, list);
1152                                    }
1153                                    else {
1154                                            ExpandoRow expandoRow = list.get(0);
1155    
1156                                            result = expandoRow;
1157    
1158                                            cacheResult(expandoRow);
1159    
1160                                            if ((expandoRow.getTableId() != tableId) ||
1161                                                            (expandoRow.getClassPK() != classPK)) {
1162                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
1163                                                            finderArgs, expandoRow);
1164                                            }
1165                                    }
1166                            }
1167                            catch (Exception e) {
1168                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
1169                                            finderArgs);
1170    
1171                                    throw processException(e);
1172                            }
1173                            finally {
1174                                    closeSession(session);
1175                            }
1176                    }
1177    
1178                    if (result instanceof List<?>) {
1179                            return null;
1180                    }
1181                    else {
1182                            return (ExpandoRow)result;
1183                    }
1184            }
1185    
1186            /**
1187             * Removes the expando row where tableId = &#63; and classPK = &#63; from the database.
1188             *
1189             * @param tableId the table ID
1190             * @param classPK the class p k
1191             * @return the expando row that was removed
1192             */
1193            @Override
1194            public ExpandoRow removeByT_C(long tableId, long classPK)
1195                    throws NoSuchRowException {
1196                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
1197    
1198                    return remove(expandoRow);
1199            }
1200    
1201            /**
1202             * Returns the number of expando rows where tableId = &#63; and classPK = &#63;.
1203             *
1204             * @param tableId the table ID
1205             * @param classPK the class p k
1206             * @return the number of matching expando rows
1207             */
1208            @Override
1209            public int countByT_C(long tableId, long classPK) {
1210                    FinderPath finderPath = FINDER_PATH_COUNT_BY_T_C;
1211    
1212                    Object[] finderArgs = new Object[] { tableId, classPK };
1213    
1214                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1215                                    this);
1216    
1217                    if (count == null) {
1218                            StringBundler query = new StringBundler(3);
1219    
1220                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1221    
1222                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
1223    
1224                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
1225    
1226                            String sql = query.toString();
1227    
1228                            Session session = null;
1229    
1230                            try {
1231                                    session = openSession();
1232    
1233                                    Query q = session.createQuery(sql);
1234    
1235                                    QueryPos qPos = QueryPos.getInstance(q);
1236    
1237                                    qPos.add(tableId);
1238    
1239                                    qPos.add(classPK);
1240    
1241                                    count = (Long)q.uniqueResult();
1242    
1243                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1244                            }
1245                            catch (Exception e) {
1246                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1247    
1248                                    throw processException(e);
1249                            }
1250                            finally {
1251                                    closeSession(session);
1252                            }
1253                    }
1254    
1255                    return count.intValue();
1256            }
1257    
1258            private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
1259            private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
1260    
1261            public ExpandoRowPersistenceImpl() {
1262                    setModelClass(ExpandoRow.class);
1263            }
1264    
1265            /**
1266             * Caches the expando row in the entity cache if it is enabled.
1267             *
1268             * @param expandoRow the expando row
1269             */
1270            @Override
1271            public void cacheResult(ExpandoRow expandoRow) {
1272                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1273                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
1274    
1275                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
1276                            new Object[] { expandoRow.getTableId(), expandoRow.getClassPK() },
1277                            expandoRow);
1278    
1279                    expandoRow.resetOriginalValues();
1280            }
1281    
1282            /**
1283             * Caches the expando rows in the entity cache if it is enabled.
1284             *
1285             * @param expandoRows the expando rows
1286             */
1287            @Override
1288            public void cacheResult(List<ExpandoRow> expandoRows) {
1289                    for (ExpandoRow expandoRow : expandoRows) {
1290                            if (EntityCacheUtil.getResult(
1291                                                    ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1292                                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey()) == null) {
1293                                    cacheResult(expandoRow);
1294                            }
1295                            else {
1296                                    expandoRow.resetOriginalValues();
1297                            }
1298                    }
1299            }
1300    
1301            /**
1302             * Clears the cache for all expando rows.
1303             *
1304             * <p>
1305             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1306             * </p>
1307             */
1308            @Override
1309            public void clearCache() {
1310                    EntityCacheUtil.clearCache(ExpandoRowImpl.class);
1311    
1312                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1313                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1314                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1315            }
1316    
1317            /**
1318             * Clears the cache for the expando row.
1319             *
1320             * <p>
1321             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1322             * </p>
1323             */
1324            @Override
1325            public void clearCache(ExpandoRow expandoRow) {
1326                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1327                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
1328    
1329                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1330                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1331    
1332                    clearUniqueFindersCache(expandoRow);
1333            }
1334    
1335            @Override
1336            public void clearCache(List<ExpandoRow> expandoRows) {
1337                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1338                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1339    
1340                    for (ExpandoRow expandoRow : expandoRows) {
1341                            EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1342                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey());
1343    
1344                            clearUniqueFindersCache(expandoRow);
1345                    }
1346            }
1347    
1348            protected void cacheUniqueFindersCache(ExpandoRow expandoRow) {
1349                    if (expandoRow.isNew()) {
1350                            Object[] args = new Object[] {
1351                                            expandoRow.getTableId(), expandoRow.getClassPK()
1352                                    };
1353    
1354                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
1355                                    Long.valueOf(1));
1356                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args, expandoRow);
1357                    }
1358                    else {
1359                            ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
1360    
1361                            if ((expandoRowModelImpl.getColumnBitmask() &
1362                                            FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
1363                                    Object[] args = new Object[] {
1364                                                    expandoRow.getTableId(), expandoRow.getClassPK()
1365                                            };
1366    
1367                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
1368                                            Long.valueOf(1));
1369                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args,
1370                                            expandoRow);
1371                            }
1372                    }
1373            }
1374    
1375            protected void clearUniqueFindersCache(ExpandoRow expandoRow) {
1376                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
1377    
1378                    Object[] args = new Object[] {
1379                                    expandoRow.getTableId(), expandoRow.getClassPK()
1380                            };
1381    
1382                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
1383                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
1384    
1385                    if ((expandoRowModelImpl.getColumnBitmask() &
1386                                    FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
1387                            args = new Object[] {
1388                                            expandoRowModelImpl.getOriginalTableId(),
1389                                            expandoRowModelImpl.getOriginalClassPK()
1390                                    };
1391    
1392                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
1393                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
1394                    }
1395            }
1396    
1397            /**
1398             * Creates a new expando row with the primary key. Does not add the expando row to the database.
1399             *
1400             * @param rowId the primary key for the new expando row
1401             * @return the new expando row
1402             */
1403            @Override
1404            public ExpandoRow create(long rowId) {
1405                    ExpandoRow expandoRow = new ExpandoRowImpl();
1406    
1407                    expandoRow.setNew(true);
1408                    expandoRow.setPrimaryKey(rowId);
1409    
1410                    return expandoRow;
1411            }
1412    
1413            /**
1414             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
1415             *
1416             * @param rowId the primary key of the expando row
1417             * @return the expando row that was removed
1418             * @throws NoSuchRowException if a expando row with the primary key could not be found
1419             */
1420            @Override
1421            public ExpandoRow remove(long rowId) throws NoSuchRowException {
1422                    return remove((Serializable)rowId);
1423            }
1424    
1425            /**
1426             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
1427             *
1428             * @param primaryKey the primary key of the expando row
1429             * @return the expando row that was removed
1430             * @throws NoSuchRowException if a expando row with the primary key could not be found
1431             */
1432            @Override
1433            public ExpandoRow remove(Serializable primaryKey) throws NoSuchRowException {
1434                    Session session = null;
1435    
1436                    try {
1437                            session = openSession();
1438    
1439                            ExpandoRow expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1440                                            primaryKey);
1441    
1442                            if (expandoRow == null) {
1443                                    if (_log.isWarnEnabled()) {
1444                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1445                                    }
1446    
1447                                    throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1448                                            primaryKey);
1449                            }
1450    
1451                            return remove(expandoRow);
1452                    }
1453                    catch (NoSuchRowException nsee) {
1454                            throw nsee;
1455                    }
1456                    catch (Exception e) {
1457                            throw processException(e);
1458                    }
1459                    finally {
1460                            closeSession(session);
1461                    }
1462            }
1463    
1464            @Override
1465            protected ExpandoRow removeImpl(ExpandoRow expandoRow) {
1466                    expandoRow = toUnwrappedModel(expandoRow);
1467    
1468                    Session session = null;
1469    
1470                    try {
1471                            session = openSession();
1472    
1473                            if (!session.contains(expandoRow)) {
1474                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1475                                                    expandoRow.getPrimaryKeyObj());
1476                            }
1477    
1478                            if (expandoRow != null) {
1479                                    session.delete(expandoRow);
1480                            }
1481                    }
1482                    catch (Exception e) {
1483                            throw processException(e);
1484                    }
1485                    finally {
1486                            closeSession(session);
1487                    }
1488    
1489                    if (expandoRow != null) {
1490                            clearCache(expandoRow);
1491                    }
1492    
1493                    return expandoRow;
1494            }
1495    
1496            @Override
1497            public ExpandoRow updateImpl(ExpandoRow expandoRow) {
1498                    expandoRow = toUnwrappedModel(expandoRow);
1499    
1500                    boolean isNew = expandoRow.isNew();
1501    
1502                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
1503    
1504                    Session session = null;
1505    
1506                    try {
1507                            session = openSession();
1508    
1509                            if (expandoRow.isNew()) {
1510                                    session.save(expandoRow);
1511    
1512                                    expandoRow.setNew(false);
1513                            }
1514                            else {
1515                                    session.merge(expandoRow);
1516                            }
1517                    }
1518                    catch (Exception e) {
1519                            throw processException(e);
1520                    }
1521                    finally {
1522                            closeSession(session);
1523                    }
1524    
1525                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1526    
1527                    if (isNew || !ExpandoRowModelImpl.COLUMN_BITMASK_ENABLED) {
1528                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1529                    }
1530    
1531                    else {
1532                            if ((expandoRowModelImpl.getColumnBitmask() &
1533                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID.getColumnBitmask()) != 0) {
1534                                    Object[] args = new Object[] {
1535                                                    expandoRowModelImpl.getOriginalTableId()
1536                                            };
1537    
1538                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1539                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1540                                            args);
1541    
1542                                    args = new Object[] { expandoRowModelImpl.getTableId() };
1543    
1544                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1545                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1546                                            args);
1547                            }
1548    
1549                            if ((expandoRowModelImpl.getColumnBitmask() &
1550                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK.getColumnBitmask()) != 0) {
1551                                    Object[] args = new Object[] {
1552                                                    expandoRowModelImpl.getOriginalClassPK()
1553                                            };
1554    
1555                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSPK, args);
1556                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK,
1557                                            args);
1558    
1559                                    args = new Object[] { expandoRowModelImpl.getClassPK() };
1560    
1561                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CLASSPK, args);
1562                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CLASSPK,
1563                                            args);
1564                            }
1565                    }
1566    
1567                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1568                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow, false);
1569    
1570                    clearUniqueFindersCache(expandoRow);
1571                    cacheUniqueFindersCache(expandoRow);
1572    
1573                    expandoRow.resetOriginalValues();
1574    
1575                    return expandoRow;
1576            }
1577    
1578            protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
1579                    if (expandoRow instanceof ExpandoRowImpl) {
1580                            return expandoRow;
1581                    }
1582    
1583                    ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
1584    
1585                    expandoRowImpl.setNew(expandoRow.isNew());
1586                    expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
1587    
1588                    expandoRowImpl.setRowId(expandoRow.getRowId());
1589                    expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
1590                    expandoRowImpl.setModifiedDate(expandoRow.getModifiedDate());
1591                    expandoRowImpl.setTableId(expandoRow.getTableId());
1592                    expandoRowImpl.setClassPK(expandoRow.getClassPK());
1593    
1594                    return expandoRowImpl;
1595            }
1596    
1597            /**
1598             * Returns the expando row with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1599             *
1600             * @param primaryKey the primary key of the expando row
1601             * @return the expando row
1602             * @throws NoSuchRowException if a expando row with the primary key could not be found
1603             */
1604            @Override
1605            public ExpandoRow findByPrimaryKey(Serializable primaryKey)
1606                    throws NoSuchRowException {
1607                    ExpandoRow expandoRow = fetchByPrimaryKey(primaryKey);
1608    
1609                    if (expandoRow == null) {
1610                            if (_log.isWarnEnabled()) {
1611                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1612                            }
1613    
1614                            throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1615                                    primaryKey);
1616                    }
1617    
1618                    return expandoRow;
1619            }
1620    
1621            /**
1622             * Returns the expando row with the primary key or throws a {@link NoSuchRowException} if it could not be found.
1623             *
1624             * @param rowId the primary key of the expando row
1625             * @return the expando row
1626             * @throws NoSuchRowException if a expando row with the primary key could not be found
1627             */
1628            @Override
1629            public ExpandoRow findByPrimaryKey(long rowId) throws NoSuchRowException {
1630                    return findByPrimaryKey((Serializable)rowId);
1631            }
1632    
1633            /**
1634             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
1635             *
1636             * @param primaryKey the primary key of the expando row
1637             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
1638             */
1639            @Override
1640            public ExpandoRow fetchByPrimaryKey(Serializable primaryKey) {
1641                    ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1642                                    ExpandoRowImpl.class, primaryKey);
1643    
1644                    if (expandoRow == _nullExpandoRow) {
1645                            return null;
1646                    }
1647    
1648                    if (expandoRow == null) {
1649                            Session session = null;
1650    
1651                            try {
1652                                    session = openSession();
1653    
1654                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1655                                                    primaryKey);
1656    
1657                                    if (expandoRow != null) {
1658                                            cacheResult(expandoRow);
1659                                    }
1660                                    else {
1661                                            EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1662                                                    ExpandoRowImpl.class, primaryKey, _nullExpandoRow);
1663                                    }
1664                            }
1665                            catch (Exception e) {
1666                                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1667                                            ExpandoRowImpl.class, primaryKey);
1668    
1669                                    throw processException(e);
1670                            }
1671                            finally {
1672                                    closeSession(session);
1673                            }
1674                    }
1675    
1676                    return expandoRow;
1677            }
1678    
1679            /**
1680             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
1681             *
1682             * @param rowId the primary key of the expando row
1683             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
1684             */
1685            @Override
1686            public ExpandoRow fetchByPrimaryKey(long rowId) {
1687                    return fetchByPrimaryKey((Serializable)rowId);
1688            }
1689    
1690            @Override
1691            public Map<Serializable, ExpandoRow> fetchByPrimaryKeys(
1692                    Set<Serializable> primaryKeys) {
1693                    if (primaryKeys.isEmpty()) {
1694                            return Collections.emptyMap();
1695                    }
1696    
1697                    Map<Serializable, ExpandoRow> map = new HashMap<Serializable, ExpandoRow>();
1698    
1699                    if (primaryKeys.size() == 1) {
1700                            Iterator<Serializable> iterator = primaryKeys.iterator();
1701    
1702                            Serializable primaryKey = iterator.next();
1703    
1704                            ExpandoRow expandoRow = fetchByPrimaryKey(primaryKey);
1705    
1706                            if (expandoRow != null) {
1707                                    map.put(primaryKey, expandoRow);
1708                            }
1709    
1710                            return map;
1711                    }
1712    
1713                    Set<Serializable> uncachedPrimaryKeys = null;
1714    
1715                    for (Serializable primaryKey : primaryKeys) {
1716                            ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1717                                            ExpandoRowImpl.class, primaryKey);
1718    
1719                            if (expandoRow == null) {
1720                                    if (uncachedPrimaryKeys == null) {
1721                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1722                                    }
1723    
1724                                    uncachedPrimaryKeys.add(primaryKey);
1725                            }
1726                            else {
1727                                    map.put(primaryKey, expandoRow);
1728                            }
1729                    }
1730    
1731                    if (uncachedPrimaryKeys == null) {
1732                            return map;
1733                    }
1734    
1735                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1736                                    1);
1737    
1738                    query.append(_SQL_SELECT_EXPANDOROW_WHERE_PKS_IN);
1739    
1740                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1741                            query.append(String.valueOf(primaryKey));
1742    
1743                            query.append(StringPool.COMMA);
1744                    }
1745    
1746                    query.setIndex(query.index() - 1);
1747    
1748                    query.append(StringPool.CLOSE_PARENTHESIS);
1749    
1750                    String sql = query.toString();
1751    
1752                    Session session = null;
1753    
1754                    try {
1755                            session = openSession();
1756    
1757                            Query q = session.createQuery(sql);
1758    
1759                            for (ExpandoRow expandoRow : (List<ExpandoRow>)q.list()) {
1760                                    map.put(expandoRow.getPrimaryKeyObj(), expandoRow);
1761    
1762                                    cacheResult(expandoRow);
1763    
1764                                    uncachedPrimaryKeys.remove(expandoRow.getPrimaryKeyObj());
1765                            }
1766    
1767                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1768                                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1769                                            ExpandoRowImpl.class, primaryKey, _nullExpandoRow);
1770                            }
1771                    }
1772                    catch (Exception e) {
1773                            throw processException(e);
1774                    }
1775                    finally {
1776                            closeSession(session);
1777                    }
1778    
1779                    return map;
1780            }
1781    
1782            /**
1783             * Returns all the expando rows.
1784             *
1785             * @return the expando rows
1786             */
1787            @Override
1788            public List<ExpandoRow> findAll() {
1789                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1790            }
1791    
1792            /**
1793             * Returns a range of all the expando rows.
1794             *
1795             * <p>
1796             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1797             * </p>
1798             *
1799             * @param start the lower bound of the range of expando rows
1800             * @param end the upper bound of the range of expando rows (not inclusive)
1801             * @return the range of expando rows
1802             */
1803            @Override
1804            public List<ExpandoRow> findAll(int start, int end) {
1805                    return findAll(start, end, null);
1806            }
1807    
1808            /**
1809             * Returns an ordered range of all the expando rows.
1810             *
1811             * <p>
1812             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1813             * </p>
1814             *
1815             * @param start the lower bound of the range of expando rows
1816             * @param end the upper bound of the range of expando rows (not inclusive)
1817             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1818             * @return the ordered range of expando rows
1819             */
1820            @Override
1821            public List<ExpandoRow> findAll(int start, int end,
1822                    OrderByComparator<ExpandoRow> orderByComparator) {
1823                    boolean pagination = true;
1824                    FinderPath finderPath = null;
1825                    Object[] finderArgs = null;
1826    
1827                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1828                                    (orderByComparator == null)) {
1829                            pagination = false;
1830                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1831                            finderArgs = FINDER_ARGS_EMPTY;
1832                    }
1833                    else {
1834                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1835                            finderArgs = new Object[] { start, end, orderByComparator };
1836                    }
1837    
1838                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
1839                                    finderArgs, this);
1840    
1841                    if (list == null) {
1842                            StringBundler query = null;
1843                            String sql = null;
1844    
1845                            if (orderByComparator != null) {
1846                                    query = new StringBundler(2 +
1847                                                    (orderByComparator.getOrderByFields().length * 3));
1848    
1849                                    query.append(_SQL_SELECT_EXPANDOROW);
1850    
1851                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1852                                            orderByComparator);
1853    
1854                                    sql = query.toString();
1855                            }
1856                            else {
1857                                    sql = _SQL_SELECT_EXPANDOROW;
1858    
1859                                    if (pagination) {
1860                                            sql = sql.concat(ExpandoRowModelImpl.ORDER_BY_JPQL);
1861                                    }
1862                            }
1863    
1864                            Session session = null;
1865    
1866                            try {
1867                                    session = openSession();
1868    
1869                                    Query q = session.createQuery(sql);
1870    
1871                                    if (!pagination) {
1872                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1873                                                            start, end, false);
1874    
1875                                            Collections.sort(list);
1876    
1877                                            list = Collections.unmodifiableList(list);
1878                                    }
1879                                    else {
1880                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1881                                                            start, end);
1882                                    }
1883    
1884                                    cacheResult(list);
1885    
1886                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1887                            }
1888                            catch (Exception e) {
1889                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1890    
1891                                    throw processException(e);
1892                            }
1893                            finally {
1894                                    closeSession(session);
1895                            }
1896                    }
1897    
1898                    return list;
1899            }
1900    
1901            /**
1902             * Removes all the expando rows from the database.
1903             *
1904             */
1905            @Override
1906            public void removeAll() {
1907                    for (ExpandoRow expandoRow : findAll()) {
1908                            remove(expandoRow);
1909                    }
1910            }
1911    
1912            /**
1913             * Returns the number of expando rows.
1914             *
1915             * @return the number of expando rows
1916             */
1917            @Override
1918            public int countAll() {
1919                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1920                                    FINDER_ARGS_EMPTY, this);
1921    
1922                    if (count == null) {
1923                            Session session = null;
1924    
1925                            try {
1926                                    session = openSession();
1927    
1928                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1929    
1930                                    count = (Long)q.uniqueResult();
1931    
1932                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1933                                            FINDER_ARGS_EMPTY, count);
1934                            }
1935                            catch (Exception e) {
1936                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1937                                            FINDER_ARGS_EMPTY);
1938    
1939                                    throw processException(e);
1940                            }
1941                            finally {
1942                                    closeSession(session);
1943                            }
1944                    }
1945    
1946                    return count.intValue();
1947            }
1948    
1949            @Override
1950            protected Set<String> getBadColumnNames() {
1951                    return _badColumnNames;
1952            }
1953    
1954            /**
1955             * Initializes the expando row persistence.
1956             */
1957            public void afterPropertiesSet() {
1958            }
1959    
1960            public void destroy() {
1961                    EntityCacheUtil.removeCache(ExpandoRowImpl.class.getName());
1962                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1963                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1964                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1965            }
1966    
1967            private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
1968            private static final String _SQL_SELECT_EXPANDOROW_WHERE_PKS_IN = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE rowId_ IN (";
1969            private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
1970            private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
1971            private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
1972            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
1973            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
1974            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
1975            private static final Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1976            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1977                                    "rowId"
1978                            });
1979            private static final ExpandoRow _nullExpandoRow = new ExpandoRowImpl() {
1980                            @Override
1981                            public Object clone() {
1982                                    return this;
1983                            }
1984    
1985                            @Override
1986                            public CacheModel<ExpandoRow> toCacheModel() {
1987                                    return _nullExpandoRowCacheModel;
1988                            }
1989                    };
1990    
1991            private static final CacheModel<ExpandoRow> _nullExpandoRowCacheModel = new CacheModel<ExpandoRow>() {
1992                            @Override
1993                            public ExpandoRow toEntityModel() {
1994                                    return _nullExpandoRow;
1995                            }
1996                    };
1997    }