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