001    /**
002     * Copyright (c) 2000-2011 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 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.getTableId()),
257                                    new Long(expandoRowModelImpl.getClassPK())
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                            StringBundler query = null;
479    
480                            if (orderByComparator != null) {
481                                    query = new StringBundler(3 +
482                                                    (orderByComparator.getOrderByFields().length * 3));
483                            }
484                            else {
485                                    query = new StringBundler(2);
486                            }
487    
488                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
489    
490                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
491    
492                            if (orderByComparator != null) {
493                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
494                                            orderByComparator);
495                            }
496    
497                            String sql = query.toString();
498    
499                            Session session = null;
500    
501                            try {
502                                    session = openSession();
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                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_TABLEID,
519                                                    finderArgs);
520                                    }
521                                    else {
522                                            cacheResult(list);
523    
524                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
525                                                    finderArgs, list);
526                                    }
527    
528                                    closeSession(session);
529                            }
530                    }
531    
532                    return list;
533            }
534    
535            /**
536             * Finds the first expando row in the ordered set where tableId = &#63;.
537             *
538             * <p>
539             * 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.
540             * </p>
541             *
542             * @param tableId the table id to search with
543             * @param orderByComparator the comparator to order the set by
544             * @return the first matching expando row
545             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
546             * @throws SystemException if a system exception occurred
547             */
548            public ExpandoRow findByTableId_First(long tableId,
549                    OrderByComparator orderByComparator)
550                    throws NoSuchRowException, SystemException {
551                    List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
552    
553                    if (list.isEmpty()) {
554                            StringBundler msg = new StringBundler(4);
555    
556                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
557    
558                            msg.append("tableId=");
559                            msg.append(tableId);
560    
561                            msg.append(StringPool.CLOSE_CURLY_BRACE);
562    
563                            throw new NoSuchRowException(msg.toString());
564                    }
565                    else {
566                            return list.get(0);
567                    }
568            }
569    
570            /**
571             * Finds the last expando row in the ordered set where tableId = &#63;.
572             *
573             * <p>
574             * 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.
575             * </p>
576             *
577             * @param tableId the table id to search with
578             * @param orderByComparator the comparator to order the set by
579             * @return the last matching expando row
580             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
581             * @throws SystemException if a system exception occurred
582             */
583            public ExpandoRow findByTableId_Last(long tableId,
584                    OrderByComparator orderByComparator)
585                    throws NoSuchRowException, SystemException {
586                    int count = countByTableId(tableId);
587    
588                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
589                                    orderByComparator);
590    
591                    if (list.isEmpty()) {
592                            StringBundler msg = new StringBundler(4);
593    
594                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
595    
596                            msg.append("tableId=");
597                            msg.append(tableId);
598    
599                            msg.append(StringPool.CLOSE_CURLY_BRACE);
600    
601                            throw new NoSuchRowException(msg.toString());
602                    }
603                    else {
604                            return list.get(0);
605                    }
606            }
607    
608            /**
609             * Finds the expando rows before and after the current expando row in the ordered set where tableId = &#63;.
610             *
611             * <p>
612             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
613             * </p>
614             *
615             * @param rowId the primary key of the current expando row
616             * @param tableId the table id to search with
617             * @param orderByComparator the comparator to order the set by
618             * @return the previous, current, and next expando row
619             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
620             * @throws SystemException if a system exception occurred
621             */
622            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
623                    OrderByComparator orderByComparator)
624                    throws NoSuchRowException, SystemException {
625                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
626    
627                    Session session = null;
628    
629                    try {
630                            session = openSession();
631    
632                            ExpandoRow[] array = new ExpandoRowImpl[3];
633    
634                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
635                                            orderByComparator, true);
636    
637                            array[1] = expandoRow;
638    
639                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
640                                            orderByComparator, false);
641    
642                            return array;
643                    }
644                    catch (Exception e) {
645                            throw processException(e);
646                    }
647                    finally {
648                            closeSession(session);
649                    }
650            }
651    
652            protected ExpandoRow getByTableId_PrevAndNext(Session session,
653                    ExpandoRow expandoRow, long tableId,
654                    OrderByComparator orderByComparator, boolean previous) {
655                    StringBundler query = null;
656    
657                    if (orderByComparator != null) {
658                            query = new StringBundler(6 +
659                                            (orderByComparator.getOrderByFields().length * 6));
660                    }
661                    else {
662                            query = new StringBundler(3);
663                    }
664    
665                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
666    
667                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
668    
669                    if (orderByComparator != null) {
670                            String[] orderByFields = orderByComparator.getOrderByFields();
671    
672                            if (orderByFields.length > 0) {
673                                    query.append(WHERE_AND);
674                            }
675    
676                            for (int i = 0; i < orderByFields.length; i++) {
677                                    query.append(_ORDER_BY_ENTITY_ALIAS);
678                                    query.append(orderByFields[i]);
679    
680                                    if ((i + 1) < orderByFields.length) {
681                                            if (orderByComparator.isAscending() ^ previous) {
682                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
683                                            }
684                                            else {
685                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
686                                            }
687                                    }
688                                    else {
689                                            if (orderByComparator.isAscending() ^ previous) {
690                                                    query.append(WHERE_GREATER_THAN);
691                                            }
692                                            else {
693                                                    query.append(WHERE_LESSER_THAN);
694                                            }
695                                    }
696                            }
697    
698                            query.append(ORDER_BY_CLAUSE);
699    
700                            for (int i = 0; i < orderByFields.length; i++) {
701                                    query.append(_ORDER_BY_ENTITY_ALIAS);
702                                    query.append(orderByFields[i]);
703    
704                                    if ((i + 1) < orderByFields.length) {
705                                            if (orderByComparator.isAscending() ^ previous) {
706                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
707                                            }
708                                            else {
709                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
710                                            }
711                                    }
712                                    else {
713                                            if (orderByComparator.isAscending() ^ previous) {
714                                                    query.append(ORDER_BY_ASC);
715                                            }
716                                            else {
717                                                    query.append(ORDER_BY_DESC);
718                                            }
719                                    }
720                            }
721                    }
722    
723                    String sql = query.toString();
724    
725                    Query q = session.createQuery(sql);
726    
727                    q.setFirstResult(0);
728                    q.setMaxResults(2);
729    
730                    QueryPos qPos = QueryPos.getInstance(q);
731    
732                    qPos.add(tableId);
733    
734                    if (orderByComparator != null) {
735                            Object[] values = orderByComparator.getOrderByValues(expandoRow);
736    
737                            for (Object value : values) {
738                                    qPos.add(value);
739                            }
740                    }
741    
742                    List<ExpandoRow> list = q.list();
743    
744                    if (list.size() == 2) {
745                            return list.get(1);
746                    }
747                    else {
748                            return null;
749                    }
750            }
751    
752            /**
753             * 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.
754             *
755             * @param tableId the table id to search with
756             * @param classPK the class p k to search with
757             * @return the matching expando row
758             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
759             * @throws SystemException if a system exception occurred
760             */
761            public ExpandoRow findByT_C(long tableId, long classPK)
762                    throws NoSuchRowException, SystemException {
763                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
764    
765                    if (expandoRow == null) {
766                            StringBundler msg = new StringBundler(6);
767    
768                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
769    
770                            msg.append("tableId=");
771                            msg.append(tableId);
772    
773                            msg.append(", classPK=");
774                            msg.append(classPK);
775    
776                            msg.append(StringPool.CLOSE_CURLY_BRACE);
777    
778                            if (_log.isWarnEnabled()) {
779                                    _log.warn(msg.toString());
780                            }
781    
782                            throw new NoSuchRowException(msg.toString());
783                    }
784    
785                    return expandoRow;
786            }
787    
788            /**
789             * 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.
790             *
791             * @param tableId the table id to search with
792             * @param classPK the class p k to search with
793             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
794             * @throws SystemException if a system exception occurred
795             */
796            public ExpandoRow fetchByT_C(long tableId, long classPK)
797                    throws SystemException {
798                    return fetchByT_C(tableId, classPK, true);
799            }
800    
801            /**
802             * 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.
803             *
804             * @param tableId the table id to search with
805             * @param classPK the class p k to search with
806             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
807             * @throws SystemException if a system exception occurred
808             */
809            public ExpandoRow fetchByT_C(long tableId, long classPK,
810                    boolean retrieveFromCache) throws SystemException {
811                    Object[] finderArgs = new Object[] { tableId, classPK };
812    
813                    Object result = null;
814    
815                    if (retrieveFromCache) {
816                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
817                                            finderArgs, this);
818                    }
819    
820                    if (result == null) {
821                            StringBundler query = new StringBundler(3);
822    
823                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
824    
825                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
826    
827                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
828    
829                            String sql = query.toString();
830    
831                            Session session = null;
832    
833                            try {
834                                    session = openSession();
835    
836                                    Query q = session.createQuery(sql);
837    
838                                    QueryPos qPos = QueryPos.getInstance(q);
839    
840                                    qPos.add(tableId);
841    
842                                    qPos.add(classPK);
843    
844                                    List<ExpandoRow> list = q.list();
845    
846                                    result = list;
847    
848                                    ExpandoRow expandoRow = null;
849    
850                                    if (list.isEmpty()) {
851                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
852                                                    finderArgs, list);
853                                    }
854                                    else {
855                                            expandoRow = list.get(0);
856    
857                                            cacheResult(expandoRow);
858    
859                                            if ((expandoRow.getTableId() != tableId) ||
860                                                            (expandoRow.getClassPK() != classPK)) {
861                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
862                                                            finderArgs, expandoRow);
863                                            }
864                                    }
865    
866                                    return expandoRow;
867                            }
868                            catch (Exception e) {
869                                    throw processException(e);
870                            }
871                            finally {
872                                    if (result == null) {
873                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
874                                                    finderArgs);
875                                    }
876    
877                                    closeSession(session);
878                            }
879                    }
880                    else {
881                            if (result instanceof List<?>) {
882                                    return null;
883                            }
884                            else {
885                                    return (ExpandoRow)result;
886                            }
887                    }
888            }
889    
890            /**
891             * Finds all the expando rows.
892             *
893             * @return the expando rows
894             * @throws SystemException if a system exception occurred
895             */
896            public List<ExpandoRow> findAll() throws SystemException {
897                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
898            }
899    
900            /**
901             * Finds a range of all the expando rows.
902             *
903             * <p>
904             * 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.
905             * </p>
906             *
907             * @param start the lower bound of the range of expando rows to return
908             * @param end the upper bound of the range of expando rows to return (not inclusive)
909             * @return the range of expando rows
910             * @throws SystemException if a system exception occurred
911             */
912            public List<ExpandoRow> findAll(int start, int end)
913                    throws SystemException {
914                    return findAll(start, end, null);
915            }
916    
917            /**
918             * Finds an ordered range of all the expando rows.
919             *
920             * <p>
921             * 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.
922             * </p>
923             *
924             * @param start the lower bound of the range of expando rows to return
925             * @param end the upper bound of the range of expando rows to return (not inclusive)
926             * @param orderByComparator the comparator to order the results by
927             * @return the ordered range of expando rows
928             * @throws SystemException if a system exception occurred
929             */
930            public List<ExpandoRow> findAll(int start, int end,
931                    OrderByComparator orderByComparator) throws SystemException {
932                    Object[] finderArgs = new Object[] {
933                                    String.valueOf(start), String.valueOf(end),
934                                    String.valueOf(orderByComparator)
935                            };
936    
937                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
938                                    finderArgs, this);
939    
940                    if (list == null) {
941                            StringBundler query = null;
942                            String sql = null;
943    
944                            if (orderByComparator != null) {
945                                    query = new StringBundler(2 +
946                                                    (orderByComparator.getOrderByFields().length * 3));
947    
948                                    query.append(_SQL_SELECT_EXPANDOROW);
949    
950                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
951                                            orderByComparator);
952    
953                                    sql = query.toString();
954                            }
955                            else {
956                                    sql = _SQL_SELECT_EXPANDOROW;
957                            }
958    
959                            Session session = null;
960    
961                            try {
962                                    session = openSession();
963    
964                                    Query q = session.createQuery(sql);
965    
966                                    if (orderByComparator == null) {
967                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
968                                                            start, end, false);
969    
970                                            Collections.sort(list);
971                                    }
972                                    else {
973                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
974                                                            start, end);
975                                    }
976                            }
977                            catch (Exception e) {
978                                    throw processException(e);
979                            }
980                            finally {
981                                    if (list == null) {
982                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
983                                                    finderArgs);
984                                    }
985                                    else {
986                                            cacheResult(list);
987    
988                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
989                                                    list);
990                                    }
991    
992                                    closeSession(session);
993                            }
994                    }
995    
996                    return list;
997            }
998    
999            /**
1000             * Removes all the expando rows where tableId = &#63; from the database.
1001             *
1002             * @param tableId the table id to search with
1003             * @throws SystemException if a system exception occurred
1004             */
1005            public void removeByTableId(long tableId) throws SystemException {
1006                    for (ExpandoRow expandoRow : findByTableId(tableId)) {
1007                            remove(expandoRow);
1008                    }
1009            }
1010    
1011            /**
1012             * Removes the expando row where tableId = &#63; and classPK = &#63; from the database.
1013             *
1014             * @param tableId the table id to search with
1015             * @param classPK the class p k to search with
1016             * @throws SystemException if a system exception occurred
1017             */
1018            public void removeByT_C(long tableId, long classPK)
1019                    throws NoSuchRowException, SystemException {
1020                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
1021    
1022                    remove(expandoRow);
1023            }
1024    
1025            /**
1026             * Removes all the expando rows from the database.
1027             *
1028             * @throws SystemException if a system exception occurred
1029             */
1030            public void removeAll() throws SystemException {
1031                    for (ExpandoRow expandoRow : findAll()) {
1032                            remove(expandoRow);
1033                    }
1034            }
1035    
1036            /**
1037             * Counts all the expando rows where tableId = &#63;.
1038             *
1039             * @param tableId the table id to search with
1040             * @return the number of matching expando rows
1041             * @throws SystemException if a system exception occurred
1042             */
1043            public int countByTableId(long tableId) throws SystemException {
1044                    Object[] finderArgs = new Object[] { tableId };
1045    
1046                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
1047                                    finderArgs, this);
1048    
1049                    if (count == null) {
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                            Session session = null;
1059    
1060                            try {
1061                                    session = openSession();
1062    
1063                                    Query q = session.createQuery(sql);
1064    
1065                                    QueryPos qPos = QueryPos.getInstance(q);
1066    
1067                                    qPos.add(tableId);
1068    
1069                                    count = (Long)q.uniqueResult();
1070                            }
1071                            catch (Exception e) {
1072                                    throw processException(e);
1073                            }
1074                            finally {
1075                                    if (count == null) {
1076                                            count = Long.valueOf(0);
1077                                    }
1078    
1079                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
1080                                            finderArgs, count);
1081    
1082                                    closeSession(session);
1083                            }
1084                    }
1085    
1086                    return count.intValue();
1087            }
1088    
1089            /**
1090             * Counts all the expando rows where tableId = &#63; and classPK = &#63;.
1091             *
1092             * @param tableId the table id to search with
1093             * @param classPK the class p k to search with
1094             * @return the number of matching expando rows
1095             * @throws SystemException if a system exception occurred
1096             */
1097            public int countByT_C(long tableId, long classPK) throws SystemException {
1098                    Object[] finderArgs = new Object[] { tableId, classPK };
1099    
1100                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_C,
1101                                    finderArgs, this);
1102    
1103                    if (count == null) {
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                            Session session = null;
1115    
1116                            try {
1117                                    session = openSession();
1118    
1119                                    Query q = session.createQuery(sql);
1120    
1121                                    QueryPos qPos = QueryPos.getInstance(q);
1122    
1123                                    qPos.add(tableId);
1124    
1125                                    qPos.add(classPK);
1126    
1127                                    count = (Long)q.uniqueResult();
1128                            }
1129                            catch (Exception e) {
1130                                    throw processException(e);
1131                            }
1132                            finally {
1133                                    if (count == null) {
1134                                            count = Long.valueOf(0);
1135                                    }
1136    
1137                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, finderArgs,
1138                                            count);
1139    
1140                                    closeSession(session);
1141                            }
1142                    }
1143    
1144                    return count.intValue();
1145            }
1146    
1147            /**
1148             * Counts all the expando rows.
1149             *
1150             * @return the number of expando rows
1151             * @throws SystemException if a system exception occurred
1152             */
1153            public int countAll() throws SystemException {
1154                    Object[] finderArgs = new Object[0];
1155    
1156                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1157                                    finderArgs, this);
1158    
1159                    if (count == null) {
1160                            Session session = null;
1161    
1162                            try {
1163                                    session = openSession();
1164    
1165                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1166    
1167                                    count = (Long)q.uniqueResult();
1168                            }
1169                            catch (Exception e) {
1170                                    throw processException(e);
1171                            }
1172                            finally {
1173                                    if (count == null) {
1174                                            count = Long.valueOf(0);
1175                                    }
1176    
1177                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1178                                            count);
1179    
1180                                    closeSession(session);
1181                            }
1182                    }
1183    
1184                    return count.intValue();
1185            }
1186    
1187            /**
1188             * Initializes the expando row persistence.
1189             */
1190            public void afterPropertiesSet() {
1191                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1192                                            com.liferay.portal.util.PropsUtil.get(
1193                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1194    
1195                    if (listenerClassNames.length > 0) {
1196                            try {
1197                                    List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1198    
1199                                    for (String listenerClassName : listenerClassNames) {
1200                                            listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
1201                                                            listenerClassName));
1202                                    }
1203    
1204                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1205                            }
1206                            catch (Exception e) {
1207                                    _log.error(e);
1208                            }
1209                    }
1210            }
1211    
1212            public void destroy() {
1213                    EntityCacheUtil.removeCache(ExpandoRowImpl.class.getName());
1214                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1215                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
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    }