001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.service.persistence;
016    
017    import com.liferay.portal.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 for the expando row service.
055     *
056     * <p>
057     * Never modify this class directly. Modify <code>service.xml</code> and rerun ServiceBuilder to regnerate 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.
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                            if (expandoRow.isCachedModel() || BatchSessionUtil.isEnabled()) {
242                                    Object staleObject = session.get(ExpandoRowImpl.class,
243                                                    expandoRow.getPrimaryKeyObj());
244    
245                                    if (staleObject != null) {
246                                            session.evict(staleObject);
247                                    }
248                            }
249    
250                            session.delete(expandoRow);
251    
252                            session.flush();
253                    }
254                    catch (Exception e) {
255                            throw processException(e);
256                    }
257                    finally {
258                            closeSession(session);
259                    }
260    
261                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
262    
263                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
264    
265                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
266                            new Object[] {
267                                    new Long(expandoRowModelImpl.getOriginalTableId()),
268                                    new Long(expandoRowModelImpl.getOriginalClassPK())
269                            });
270    
271                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
272                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
273    
274                    return expandoRow;
275            }
276    
277            public ExpandoRow updateImpl(
278                    com.liferay.portlet.expando.model.ExpandoRow expandoRow, boolean merge)
279                    throws SystemException {
280                    expandoRow = toUnwrappedModel(expandoRow);
281    
282                    boolean isNew = expandoRow.isNew();
283    
284                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
285    
286                    Session session = null;
287    
288                    try {
289                            session = openSession();
290    
291                            BatchSessionUtil.update(session, expandoRow, merge);
292    
293                            expandoRow.setNew(false);
294                    }
295                    catch (Exception e) {
296                            throw processException(e);
297                    }
298                    finally {
299                            closeSession(session);
300                    }
301    
302                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
303    
304                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
305                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
306    
307                    if (!isNew &&
308                                    ((expandoRow.getTableId() != expandoRowModelImpl.getOriginalTableId()) ||
309                                    (expandoRow.getClassPK() != expandoRowModelImpl.getOriginalClassPK()))) {
310                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
311                                    new Object[] {
312                                            new Long(expandoRowModelImpl.getOriginalTableId()),
313                                            new Long(expandoRowModelImpl.getOriginalClassPK())
314                                    });
315                    }
316    
317                    if (isNew ||
318                                    ((expandoRow.getTableId() != expandoRowModelImpl.getOriginalTableId()) ||
319                                    (expandoRow.getClassPK() != expandoRowModelImpl.getOriginalClassPK()))) {
320                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
321                                    new Object[] {
322                                            new Long(expandoRow.getTableId()),
323                                            new Long(expandoRow.getClassPK())
324                                    }, expandoRow);
325                    }
326    
327                    return expandoRow;
328            }
329    
330            protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
331                    if (expandoRow instanceof ExpandoRowImpl) {
332                            return expandoRow;
333                    }
334    
335                    ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
336    
337                    expandoRowImpl.setNew(expandoRow.isNew());
338                    expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
339    
340                    expandoRowImpl.setRowId(expandoRow.getRowId());
341                    expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
342                    expandoRowImpl.setTableId(expandoRow.getTableId());
343                    expandoRowImpl.setClassPK(expandoRow.getClassPK());
344    
345                    return expandoRowImpl;
346            }
347    
348            /**
349             * Finds the expando row with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
350             *
351             * @param primaryKey the primary key of the expando row to find
352             * @return the expando row
353             * @throws com.liferay.portal.NoSuchModelException if a expando row with the primary key could not be found
354             * @throws SystemException if a system exception occurred
355             */
356            public ExpandoRow findByPrimaryKey(Serializable primaryKey)
357                    throws NoSuchModelException, SystemException {
358                    return findByPrimaryKey(((Long)primaryKey).longValue());
359            }
360    
361            /**
362             * Finds the expando row with the primary key or throws a {@link com.liferay.portlet.expando.NoSuchRowException} if it could not be found.
363             *
364             * @param rowId the primary key of the expando row to find
365             * @return the expando row
366             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
367             * @throws SystemException if a system exception occurred
368             */
369            public ExpandoRow findByPrimaryKey(long rowId)
370                    throws NoSuchRowException, SystemException {
371                    ExpandoRow expandoRow = fetchByPrimaryKey(rowId);
372    
373                    if (expandoRow == null) {
374                            if (_log.isWarnEnabled()) {
375                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + rowId);
376                            }
377    
378                            throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
379                                    rowId);
380                    }
381    
382                    return expandoRow;
383            }
384    
385            /**
386             * Finds the expando row with the primary key or returns <code>null</code> if it could not be found.
387             *
388             * @param primaryKey the primary key of the expando row to find
389             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
390             * @throws SystemException if a system exception occurred
391             */
392            public ExpandoRow fetchByPrimaryKey(Serializable primaryKey)
393                    throws SystemException {
394                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
395            }
396    
397            /**
398             * Finds the expando row with the primary key or returns <code>null</code> if it could not be found.
399             *
400             * @param rowId the primary key of the expando row to find
401             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
402             * @throws SystemException if a system exception occurred
403             */
404            public ExpandoRow fetchByPrimaryKey(long rowId) throws SystemException {
405                    ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
406                                    ExpandoRowImpl.class, rowId, this);
407    
408                    if (expandoRow == null) {
409                            Session session = null;
410    
411                            try {
412                                    session = openSession();
413    
414                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
415                                                    new Long(rowId));
416                            }
417                            catch (Exception e) {
418                                    throw processException(e);
419                            }
420                            finally {
421                                    if (expandoRow != null) {
422                                            cacheResult(expandoRow);
423                                    }
424    
425                                    closeSession(session);
426                            }
427                    }
428    
429                    return expandoRow;
430            }
431    
432            /**
433             * Finds all the expando rows where tableId = &#63;.
434             *
435             * @param tableId the table id to search with
436             * @return the matching expando rows
437             * @throws SystemException if a system exception occurred
438             */
439            public List<ExpandoRow> findByTableId(long tableId)
440                    throws SystemException {
441                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
442            }
443    
444            /**
445             * Finds a range of all the expando rows where tableId = &#63;.
446             *
447             * <p>
448             * 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.
449             * </p>
450             *
451             * @param tableId the table id to search with
452             * @param start the lower bound of the range of expando rows to return
453             * @param end the upper bound of the range of expando rows to return (not inclusive)
454             * @return the range of matching expando rows
455             * @throws SystemException if a system exception occurred
456             */
457            public List<ExpandoRow> findByTableId(long tableId, int start, int end)
458                    throws SystemException {
459                    return findByTableId(tableId, start, end, null);
460            }
461    
462            /**
463             * Finds an ordered range of all the expando rows where tableId = &#63;.
464             *
465             * <p>
466             * 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.
467             * </p>
468             *
469             * @param tableId the table id to search with
470             * @param start the lower bound of the range of expando rows to return
471             * @param end the upper bound of the range of expando rows to return (not inclusive)
472             * @param orderByComparator the comparator to order the results by
473             * @return the ordered range of matching expando rows
474             * @throws SystemException if a system exception occurred
475             */
476            public List<ExpandoRow> findByTableId(long tableId, int start, int end,
477                    OrderByComparator orderByComparator) throws SystemException {
478                    Object[] finderArgs = new Object[] {
479                                    tableId,
480                                    
481                                    String.valueOf(start), String.valueOf(end),
482                                    String.valueOf(orderByComparator)
483                            };
484    
485                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_TABLEID,
486                                    finderArgs, this);
487    
488                    if (list == null) {
489                            Session session = null;
490    
491                            try {
492                                    session = openSession();
493    
494                                    StringBundler query = null;
495    
496                                    if (orderByComparator != null) {
497                                            query = new StringBundler(3 +
498                                                            (orderByComparator.getOrderByFields().length * 3));
499                                    }
500                                    else {
501                                            query = new StringBundler(2);
502                                    }
503    
504                                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
505    
506                                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
507    
508                                    if (orderByComparator != null) {
509                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
510                                                    orderByComparator);
511                                    }
512    
513                                    String sql = query.toString();
514    
515                                    Query q = session.createQuery(sql);
516    
517                                    QueryPos qPos = QueryPos.getInstance(q);
518    
519                                    qPos.add(tableId);
520    
521                                    list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(), start,
522                                                    end);
523                            }
524                            catch (Exception e) {
525                                    throw processException(e);
526                            }
527                            finally {
528                                    if (list == null) {
529                                            list = new ArrayList<ExpandoRow>();
530                                    }
531    
532                                    cacheResult(list);
533    
534                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
535                                            finderArgs, list);
536    
537                                    closeSession(session);
538                            }
539                    }
540    
541                    return list;
542            }
543    
544            /**
545             * Finds the first expando row in the ordered set where tableId = &#63;.
546             *
547             * <p>
548             * 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.
549             * </p>
550             *
551             * @param tableId the table id to search with
552             * @param orderByComparator the comparator to order the set by
553             * @return the first matching expando row
554             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
555             * @throws SystemException if a system exception occurred
556             */
557            public ExpandoRow findByTableId_First(long tableId,
558                    OrderByComparator orderByComparator)
559                    throws NoSuchRowException, SystemException {
560                    List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
561    
562                    if (list.isEmpty()) {
563                            StringBundler msg = new StringBundler(4);
564    
565                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
566    
567                            msg.append("tableId=");
568                            msg.append(tableId);
569    
570                            msg.append(StringPool.CLOSE_CURLY_BRACE);
571    
572                            throw new NoSuchRowException(msg.toString());
573                    }
574                    else {
575                            return list.get(0);
576                    }
577            }
578    
579            /**
580             * Finds the last expando row in the ordered set where tableId = &#63;.
581             *
582             * <p>
583             * 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.
584             * </p>
585             *
586             * @param tableId the table id to search with
587             * @param orderByComparator the comparator to order the set by
588             * @return the last matching expando row
589             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
590             * @throws SystemException if a system exception occurred
591             */
592            public ExpandoRow findByTableId_Last(long tableId,
593                    OrderByComparator orderByComparator)
594                    throws NoSuchRowException, SystemException {
595                    int count = countByTableId(tableId);
596    
597                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
598                                    orderByComparator);
599    
600                    if (list.isEmpty()) {
601                            StringBundler msg = new StringBundler(4);
602    
603                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
604    
605                            msg.append("tableId=");
606                            msg.append(tableId);
607    
608                            msg.append(StringPool.CLOSE_CURLY_BRACE);
609    
610                            throw new NoSuchRowException(msg.toString());
611                    }
612                    else {
613                            return list.get(0);
614                    }
615            }
616    
617            /**
618             * Finds the expando rows before and after the current expando row in the ordered set where tableId = &#63;.
619             *
620             * <p>
621             * 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.
622             * </p>
623             *
624             * @param rowId the primary key of the current expando row
625             * @param tableId the table id to search with
626             * @param orderByComparator the comparator to order the set by
627             * @return the previous, current, and next expando row
628             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
629             * @throws SystemException if a system exception occurred
630             */
631            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
632                    OrderByComparator orderByComparator)
633                    throws NoSuchRowException, SystemException {
634                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
635    
636                    Session session = null;
637    
638                    try {
639                            session = openSession();
640    
641                            ExpandoRow[] array = new ExpandoRowImpl[3];
642    
643                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
644                                            orderByComparator, true);
645    
646                            array[1] = expandoRow;
647    
648                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
649                                            orderByComparator, false);
650    
651                            return array;
652                    }
653                    catch (Exception e) {
654                            throw processException(e);
655                    }
656                    finally {
657                            closeSession(session);
658                    }
659            }
660    
661            protected ExpandoRow getByTableId_PrevAndNext(Session session,
662                    ExpandoRow expandoRow, long tableId,
663                    OrderByComparator orderByComparator, boolean previous) {
664                    StringBundler query = null;
665    
666                    if (orderByComparator != null) {
667                            query = new StringBundler(6 +
668                                            (orderByComparator.getOrderByFields().length * 6));
669                    }
670                    else {
671                            query = new StringBundler(3);
672                    }
673    
674                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
675    
676                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
677    
678                    if (orderByComparator != null) {
679                            String[] orderByFields = orderByComparator.getOrderByFields();
680    
681                            if (orderByFields.length > 0) {
682                                    query.append(WHERE_AND);
683                            }
684    
685                            for (int i = 0; i < orderByFields.length; i++) {
686                                    query.append(_ORDER_BY_ENTITY_ALIAS);
687                                    query.append(orderByFields[i]);
688    
689                                    if ((i + 1) < orderByFields.length) {
690                                            if (orderByComparator.isAscending() ^ previous) {
691                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
692                                            }
693                                            else {
694                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
695                                            }
696                                    }
697                                    else {
698                                            if (orderByComparator.isAscending() ^ previous) {
699                                                    query.append(WHERE_GREATER_THAN);
700                                            }
701                                            else {
702                                                    query.append(WHERE_LESSER_THAN);
703                                            }
704                                    }
705                            }
706    
707                            query.append(ORDER_BY_CLAUSE);
708    
709                            for (int i = 0; i < orderByFields.length; i++) {
710                                    query.append(_ORDER_BY_ENTITY_ALIAS);
711                                    query.append(orderByFields[i]);
712    
713                                    if ((i + 1) < orderByFields.length) {
714                                            if (orderByComparator.isAscending() ^ previous) {
715                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
716                                            }
717                                            else {
718                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
719                                            }
720                                    }
721                                    else {
722                                            if (orderByComparator.isAscending() ^ previous) {
723                                                    query.append(ORDER_BY_ASC);
724                                            }
725                                            else {
726                                                    query.append(ORDER_BY_DESC);
727                                            }
728                                    }
729                            }
730                    }
731    
732                    String sql = query.toString();
733    
734                    Query q = session.createQuery(sql);
735    
736                    q.setFirstResult(0);
737                    q.setMaxResults(2);
738    
739                    QueryPos qPos = QueryPos.getInstance(q);
740    
741                    qPos.add(tableId);
742    
743                    if (orderByComparator != null) {
744                            Object[] values = orderByComparator.getOrderByValues(expandoRow);
745    
746                            for (Object value : values) {
747                                    qPos.add(value);
748                            }
749                    }
750    
751                    List<ExpandoRow> list = q.list();
752    
753                    if (list.size() == 2) {
754                            return list.get(1);
755                    }
756                    else {
757                            return null;
758                    }
759            }
760    
761            /**
762             * 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.
763             *
764             * @param tableId the table id to search with
765             * @param classPK the class p k to search with
766             * @return the matching expando row
767             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
768             * @throws SystemException if a system exception occurred
769             */
770            public ExpandoRow findByT_C(long tableId, long classPK)
771                    throws NoSuchRowException, SystemException {
772                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
773    
774                    if (expandoRow == null) {
775                            StringBundler msg = new StringBundler(6);
776    
777                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
778    
779                            msg.append("tableId=");
780                            msg.append(tableId);
781    
782                            msg.append(", classPK=");
783                            msg.append(classPK);
784    
785                            msg.append(StringPool.CLOSE_CURLY_BRACE);
786    
787                            if (_log.isWarnEnabled()) {
788                                    _log.warn(msg.toString());
789                            }
790    
791                            throw new NoSuchRowException(msg.toString());
792                    }
793    
794                    return expandoRow;
795            }
796    
797            /**
798             * 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.
799             *
800             * @param tableId the table id to search with
801             * @param classPK the class p k to search with
802             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
803             * @throws SystemException if a system exception occurred
804             */
805            public ExpandoRow fetchByT_C(long tableId, long classPK)
806                    throws SystemException {
807                    return fetchByT_C(tableId, classPK, true);
808            }
809    
810            /**
811             * 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.
812             *
813             * @param tableId the table id to search with
814             * @param classPK the class p k to search with
815             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
816             * @throws SystemException if a system exception occurred
817             */
818            public ExpandoRow fetchByT_C(long tableId, long classPK,
819                    boolean retrieveFromCache) throws SystemException {
820                    Object[] finderArgs = new Object[] { tableId, classPK };
821    
822                    Object result = null;
823    
824                    if (retrieveFromCache) {
825                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
826                                            finderArgs, this);
827                    }
828    
829                    if (result == null) {
830                            Session session = null;
831    
832                            try {
833                                    session = openSession();
834    
835                                    StringBundler query = new StringBundler(3);
836    
837                                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
838    
839                                    query.append(_FINDER_COLUMN_T_C_TABLEID_2);
840    
841                                    query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
842    
843                                    String sql = query.toString();
844    
845                                    Query q = session.createQuery(sql);
846    
847                                    QueryPos qPos = QueryPos.getInstance(q);
848    
849                                    qPos.add(tableId);
850    
851                                    qPos.add(classPK);
852    
853                                    List<ExpandoRow> list = q.list();
854    
855                                    result = list;
856    
857                                    ExpandoRow expandoRow = null;
858    
859                                    if (list.isEmpty()) {
860                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
861                                                    finderArgs, list);
862                                    }
863                                    else {
864                                            expandoRow = list.get(0);
865    
866                                            cacheResult(expandoRow);
867    
868                                            if ((expandoRow.getTableId() != tableId) ||
869                                                            (expandoRow.getClassPK() != classPK)) {
870                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
871                                                            finderArgs, expandoRow);
872                                            }
873                                    }
874    
875                                    return expandoRow;
876                            }
877                            catch (Exception e) {
878                                    throw processException(e);
879                            }
880                            finally {
881                                    if (result == null) {
882                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
883                                                    finderArgs, new ArrayList<ExpandoRow>());
884                                    }
885    
886                                    closeSession(session);
887                            }
888                    }
889                    else {
890                            if (result instanceof List<?>) {
891                                    return null;
892                            }
893                            else {
894                                    return (ExpandoRow)result;
895                            }
896                    }
897            }
898    
899            /**
900             * Finds all the expando rows.
901             *
902             * @return the expando rows
903             * @throws SystemException if a system exception occurred
904             */
905            public List<ExpandoRow> findAll() throws SystemException {
906                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
907            }
908    
909            /**
910             * Finds a range of all the expando rows.
911             *
912             * <p>
913             * 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.
914             * </p>
915             *
916             * @param start the lower bound of the range of expando rows to return
917             * @param end the upper bound of the range of expando rows to return (not inclusive)
918             * @return the range of expando rows
919             * @throws SystemException if a system exception occurred
920             */
921            public List<ExpandoRow> findAll(int start, int end)
922                    throws SystemException {
923                    return findAll(start, end, null);
924            }
925    
926            /**
927             * Finds an ordered range of all the expando rows.
928             *
929             * <p>
930             * 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.
931             * </p>
932             *
933             * @param start the lower bound of the range of expando rows to return
934             * @param end the upper bound of the range of expando rows to return (not inclusive)
935             * @param orderByComparator the comparator to order the results by
936             * @return the ordered range of expando rows
937             * @throws SystemException if a system exception occurred
938             */
939            public List<ExpandoRow> findAll(int start, int end,
940                    OrderByComparator orderByComparator) throws SystemException {
941                    Object[] finderArgs = new Object[] {
942                                    String.valueOf(start), String.valueOf(end),
943                                    String.valueOf(orderByComparator)
944                            };
945    
946                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
947                                    finderArgs, this);
948    
949                    if (list == null) {
950                            Session session = null;
951    
952                            try {
953                                    session = openSession();
954    
955                                    StringBundler query = null;
956                                    String sql = null;
957    
958                                    if (orderByComparator != null) {
959                                            query = new StringBundler(2 +
960                                                            (orderByComparator.getOrderByFields().length * 3));
961    
962                                            query.append(_SQL_SELECT_EXPANDOROW);
963    
964                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
965                                                    orderByComparator);
966    
967                                            sql = query.toString();
968                                    }
969                                    else {
970                                            sql = _SQL_SELECT_EXPANDOROW;
971                                    }
972    
973                                    Query q = session.createQuery(sql);
974    
975                                    if (orderByComparator == null) {
976                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
977                                                            start, end, false);
978    
979                                            Collections.sort(list);
980                                    }
981                                    else {
982                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
983                                                            start, end);
984                                    }
985                            }
986                            catch (Exception e) {
987                                    throw processException(e);
988                            }
989                            finally {
990                                    if (list == null) {
991                                            list = new ArrayList<ExpandoRow>();
992                                    }
993    
994                                    cacheResult(list);
995    
996                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
997    
998                                    closeSession(session);
999                            }
1000                    }
1001    
1002                    return list;
1003            }
1004    
1005            /**
1006             * Removes all the expando rows where tableId = &#63; from the database.
1007             *
1008             * @param tableId the table id to search with
1009             * @throws SystemException if a system exception occurred
1010             */
1011            public void removeByTableId(long tableId) throws SystemException {
1012                    for (ExpandoRow expandoRow : findByTableId(tableId)) {
1013                            remove(expandoRow);
1014                    }
1015            }
1016    
1017            /**
1018             * Removes the expando row where tableId = &#63; and classPK = &#63; from the database.
1019             *
1020             * @param tableId the table id to search with
1021             * @param classPK the class p k to search with
1022             * @throws SystemException if a system exception occurred
1023             */
1024            public void removeByT_C(long tableId, long classPK)
1025                    throws NoSuchRowException, SystemException {
1026                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
1027    
1028                    remove(expandoRow);
1029            }
1030    
1031            /**
1032             * Removes all the expando rows from the database.
1033             *
1034             * @throws SystemException if a system exception occurred
1035             */
1036            public void removeAll() throws SystemException {
1037                    for (ExpandoRow expandoRow : findAll()) {
1038                            remove(expandoRow);
1039                    }
1040            }
1041    
1042            /**
1043             * Counts all the expando rows where tableId = &#63;.
1044             *
1045             * @param tableId the table id to search with
1046             * @return the number of matching expando rows
1047             * @throws SystemException if a system exception occurred
1048             */
1049            public int countByTableId(long tableId) throws SystemException {
1050                    Object[] finderArgs = new Object[] { tableId };
1051    
1052                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
1053                                    finderArgs, this);
1054    
1055                    if (count == null) {
1056                            Session session = null;
1057    
1058                            try {
1059                                    session = openSession();
1060    
1061                                    StringBundler query = new StringBundler(2);
1062    
1063                                    query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1064    
1065                                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
1066    
1067                                    String sql = query.toString();
1068    
1069                                    Query q = session.createQuery(sql);
1070    
1071                                    QueryPos qPos = QueryPos.getInstance(q);
1072    
1073                                    qPos.add(tableId);
1074    
1075                                    count = (Long)q.uniqueResult();
1076                            }
1077                            catch (Exception e) {
1078                                    throw processException(e);
1079                            }
1080                            finally {
1081                                    if (count == null) {
1082                                            count = Long.valueOf(0);
1083                                    }
1084    
1085                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
1086                                            finderArgs, count);
1087    
1088                                    closeSession(session);
1089                            }
1090                    }
1091    
1092                    return count.intValue();
1093            }
1094    
1095            /**
1096             * Counts all the expando rows where tableId = &#63; and classPK = &#63;.
1097             *
1098             * @param tableId the table id to search with
1099             * @param classPK the class p k to search with
1100             * @return the number of matching expando rows
1101             * @throws SystemException if a system exception occurred
1102             */
1103            public int countByT_C(long tableId, long classPK) throws SystemException {
1104                    Object[] finderArgs = new Object[] { tableId, classPK };
1105    
1106                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_C,
1107                                    finderArgs, this);
1108    
1109                    if (count == null) {
1110                            Session session = null;
1111    
1112                            try {
1113                                    session = openSession();
1114    
1115                                    StringBundler query = new StringBundler(3);
1116    
1117                                    query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1118    
1119                                    query.append(_FINDER_COLUMN_T_C_TABLEID_2);
1120    
1121                                    query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
1122    
1123                                    String sql = query.toString();
1124    
1125                                    Query q = session.createQuery(sql);
1126    
1127                                    QueryPos qPos = QueryPos.getInstance(q);
1128    
1129                                    qPos.add(tableId);
1130    
1131                                    qPos.add(classPK);
1132    
1133                                    count = (Long)q.uniqueResult();
1134                            }
1135                            catch (Exception e) {
1136                                    throw processException(e);
1137                            }
1138                            finally {
1139                                    if (count == null) {
1140                                            count = Long.valueOf(0);
1141                                    }
1142    
1143                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, finderArgs,
1144                                            count);
1145    
1146                                    closeSession(session);
1147                            }
1148                    }
1149    
1150                    return count.intValue();
1151            }
1152    
1153            /**
1154             * Counts all the expando rows.
1155             *
1156             * @return the number of expando rows
1157             * @throws SystemException if a system exception occurred
1158             */
1159            public int countAll() throws SystemException {
1160                    Object[] finderArgs = new Object[0];
1161    
1162                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1163                                    finderArgs, this);
1164    
1165                    if (count == null) {
1166                            Session session = null;
1167    
1168                            try {
1169                                    session = openSession();
1170    
1171                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1172    
1173                                    count = (Long)q.uniqueResult();
1174                            }
1175                            catch (Exception e) {
1176                                    throw processException(e);
1177                            }
1178                            finally {
1179                                    if (count == null) {
1180                                            count = Long.valueOf(0);
1181                                    }
1182    
1183                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1184                                            count);
1185    
1186                                    closeSession(session);
1187                            }
1188                    }
1189    
1190                    return count.intValue();
1191            }
1192    
1193            /**
1194             * Initializes the expando row persistence.
1195             */
1196            public void afterPropertiesSet() {
1197                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1198                                            com.liferay.portal.util.PropsUtil.get(
1199                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1200    
1201                    if (listenerClassNames.length > 0) {
1202                            try {
1203                                    List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1204    
1205                                    for (String listenerClassName : listenerClassNames) {
1206                                            listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
1207                                                            listenerClassName));
1208                                    }
1209    
1210                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1211                            }
1212                            catch (Exception e) {
1213                                    _log.error(e);
1214                            }
1215                    }
1216            }
1217    
1218            @BeanReference(type = ExpandoColumnPersistence.class)
1219            protected ExpandoColumnPersistence expandoColumnPersistence;
1220            @BeanReference(type = ExpandoRowPersistence.class)
1221            protected ExpandoRowPersistence expandoRowPersistence;
1222            @BeanReference(type = ExpandoTablePersistence.class)
1223            protected ExpandoTablePersistence expandoTablePersistence;
1224            @BeanReference(type = ExpandoValuePersistence.class)
1225            protected ExpandoValuePersistence expandoValuePersistence;
1226            @BeanReference(type = ResourcePersistence.class)
1227            protected ResourcePersistence resourcePersistence;
1228            @BeanReference(type = UserPersistence.class)
1229            protected UserPersistence userPersistence;
1230            private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
1231            private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
1232            private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
1233            private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
1234            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
1235            private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
1236            private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
1237            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
1238            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
1239            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
1240            private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1241    }