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     * @author    Brian Wing Shun Chan
055     * @see       ExpandoRowPersistence
056     * @see       ExpandoRowUtil
057     * @generated
058     */
059    public class ExpandoRowPersistenceImpl extends BasePersistenceImpl<ExpandoRow>
060            implements ExpandoRowPersistence {
061            public static final String FINDER_CLASS_NAME_ENTITY = ExpandoRowImpl.class.getName();
062            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
063                    ".List";
064            public static final FinderPath FINDER_PATH_FIND_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
065                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
066                            "findByTableId",
067                            new String[] {
068                                    Long.class.getName(),
069                                    
070                            "java.lang.Integer", "java.lang.Integer",
071                                    "com.liferay.portal.kernel.util.OrderByComparator"
072                            });
073            public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
074                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
075                            "countByTableId", new String[] { Long.class.getName() });
076            public static final FinderPath FINDER_PATH_FETCH_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
077                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
078                            "fetchByT_C",
079                            new String[] { Long.class.getName(), Long.class.getName() });
080            public static final FinderPath FINDER_PATH_COUNT_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
081                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
082                            "countByT_C",
083                            new String[] { Long.class.getName(), Long.class.getName() });
084            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
085                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
086                            "findAll", new String[0]);
087            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
088                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
089                            "countAll", new String[0]);
090    
091            public void cacheResult(ExpandoRow expandoRow) {
092                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
093                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
094    
095                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
096                            new Object[] {
097                                    new Long(expandoRow.getTableId()),
098                                    new Long(expandoRow.getClassPK())
099                            }, expandoRow);
100            }
101    
102            public void cacheResult(List<ExpandoRow> expandoRows) {
103                    for (ExpandoRow expandoRow : expandoRows) {
104                            if (EntityCacheUtil.getResult(
105                                                    ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
106                                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey(), this) == null) {
107                                    cacheResult(expandoRow);
108                            }
109                    }
110            }
111    
112            public void clearCache() {
113                    CacheRegistryUtil.clear(ExpandoRowImpl.class.getName());
114                    EntityCacheUtil.clearCache(ExpandoRowImpl.class.getName());
115                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
116                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
117            }
118    
119            public void clearCache(ExpandoRow expandoRow) {
120                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
121                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
122    
123                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
124                            new Object[] {
125                                    new Long(expandoRow.getTableId()),
126                                    new Long(expandoRow.getClassPK())
127                            });
128            }
129    
130            public ExpandoRow create(long rowId) {
131                    ExpandoRow expandoRow = new ExpandoRowImpl();
132    
133                    expandoRow.setNew(true);
134                    expandoRow.setPrimaryKey(rowId);
135    
136                    return expandoRow;
137            }
138    
139            public ExpandoRow remove(Serializable primaryKey)
140                    throws NoSuchModelException, SystemException {
141                    return remove(((Long)primaryKey).longValue());
142            }
143    
144            public ExpandoRow remove(long rowId)
145                    throws NoSuchRowException, SystemException {
146                    Session session = null;
147    
148                    try {
149                            session = openSession();
150    
151                            ExpandoRow expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
152                                            new Long(rowId));
153    
154                            if (expandoRow == null) {
155                                    if (_log.isWarnEnabled()) {
156                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + rowId);
157                                    }
158    
159                                    throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
160                                            rowId);
161                            }
162    
163                            return remove(expandoRow);
164                    }
165                    catch (NoSuchRowException nsee) {
166                            throw nsee;
167                    }
168                    catch (Exception e) {
169                            throw processException(e);
170                    }
171                    finally {
172                            closeSession(session);
173                    }
174            }
175    
176            protected ExpandoRow removeImpl(ExpandoRow expandoRow)
177                    throws SystemException {
178                    expandoRow = toUnwrappedModel(expandoRow);
179    
180                    Session session = null;
181    
182                    try {
183                            session = openSession();
184    
185                            if (expandoRow.isCachedModel() || BatchSessionUtil.isEnabled()) {
186                                    Object staleObject = session.get(ExpandoRowImpl.class,
187                                                    expandoRow.getPrimaryKeyObj());
188    
189                                    if (staleObject != null) {
190                                            session.evict(staleObject);
191                                    }
192                            }
193    
194                            session.delete(expandoRow);
195    
196                            session.flush();
197                    }
198                    catch (Exception e) {
199                            throw processException(e);
200                    }
201                    finally {
202                            closeSession(session);
203                    }
204    
205                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
206    
207                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
208    
209                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
210                            new Object[] {
211                                    new Long(expandoRowModelImpl.getOriginalTableId()),
212                                    new Long(expandoRowModelImpl.getOriginalClassPK())
213                            });
214    
215                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
216                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
217    
218                    return expandoRow;
219            }
220    
221            public ExpandoRow updateImpl(
222                    com.liferay.portlet.expando.model.ExpandoRow expandoRow, boolean merge)
223                    throws SystemException {
224                    expandoRow = toUnwrappedModel(expandoRow);
225    
226                    boolean isNew = expandoRow.isNew();
227    
228                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
229    
230                    Session session = null;
231    
232                    try {
233                            session = openSession();
234    
235                            BatchSessionUtil.update(session, expandoRow, merge);
236    
237                            expandoRow.setNew(false);
238                    }
239                    catch (Exception e) {
240                            throw processException(e);
241                    }
242                    finally {
243                            closeSession(session);
244                    }
245    
246                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
247    
248                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
249                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
250    
251                    if (!isNew &&
252                                    ((expandoRow.getTableId() != expandoRowModelImpl.getOriginalTableId()) ||
253                                    (expandoRow.getClassPK() != expandoRowModelImpl.getOriginalClassPK()))) {
254                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
255                                    new Object[] {
256                                            new Long(expandoRowModelImpl.getOriginalTableId()),
257                                            new Long(expandoRowModelImpl.getOriginalClassPK())
258                                    });
259                    }
260    
261                    if (isNew ||
262                                    ((expandoRow.getTableId() != expandoRowModelImpl.getOriginalTableId()) ||
263                                    (expandoRow.getClassPK() != expandoRowModelImpl.getOriginalClassPK()))) {
264                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
265                                    new Object[] {
266                                            new Long(expandoRow.getTableId()),
267                                            new Long(expandoRow.getClassPK())
268                                    }, expandoRow);
269                    }
270    
271                    return expandoRow;
272            }
273    
274            protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
275                    if (expandoRow instanceof ExpandoRowImpl) {
276                            return expandoRow;
277                    }
278    
279                    ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
280    
281                    expandoRowImpl.setNew(expandoRow.isNew());
282                    expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
283    
284                    expandoRowImpl.setRowId(expandoRow.getRowId());
285                    expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
286                    expandoRowImpl.setTableId(expandoRow.getTableId());
287                    expandoRowImpl.setClassPK(expandoRow.getClassPK());
288    
289                    return expandoRowImpl;
290            }
291    
292            public ExpandoRow findByPrimaryKey(Serializable primaryKey)
293                    throws NoSuchModelException, SystemException {
294                    return findByPrimaryKey(((Long)primaryKey).longValue());
295            }
296    
297            public ExpandoRow findByPrimaryKey(long rowId)
298                    throws NoSuchRowException, SystemException {
299                    ExpandoRow expandoRow = fetchByPrimaryKey(rowId);
300    
301                    if (expandoRow == null) {
302                            if (_log.isWarnEnabled()) {
303                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + rowId);
304                            }
305    
306                            throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
307                                    rowId);
308                    }
309    
310                    return expandoRow;
311            }
312    
313            public ExpandoRow fetchByPrimaryKey(Serializable primaryKey)
314                    throws SystemException {
315                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
316            }
317    
318            public ExpandoRow fetchByPrimaryKey(long rowId) throws SystemException {
319                    ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
320                                    ExpandoRowImpl.class, rowId, this);
321    
322                    if (expandoRow == null) {
323                            Session session = null;
324    
325                            try {
326                                    session = openSession();
327    
328                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
329                                                    new Long(rowId));
330                            }
331                            catch (Exception e) {
332                                    throw processException(e);
333                            }
334                            finally {
335                                    if (expandoRow != null) {
336                                            cacheResult(expandoRow);
337                                    }
338    
339                                    closeSession(session);
340                            }
341                    }
342    
343                    return expandoRow;
344            }
345    
346            public List<ExpandoRow> findByTableId(long tableId)
347                    throws SystemException {
348                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
349            }
350    
351            public List<ExpandoRow> findByTableId(long tableId, int start, int end)
352                    throws SystemException {
353                    return findByTableId(tableId, start, end, null);
354            }
355    
356            public List<ExpandoRow> findByTableId(long tableId, int start, int end,
357                    OrderByComparator orderByComparator) throws SystemException {
358                    Object[] finderArgs = new Object[] {
359                                    tableId,
360                                    
361                                    String.valueOf(start), String.valueOf(end),
362                                    String.valueOf(orderByComparator)
363                            };
364    
365                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_TABLEID,
366                                    finderArgs, this);
367    
368                    if (list == null) {
369                            Session session = null;
370    
371                            try {
372                                    session = openSession();
373    
374                                    StringBundler query = null;
375    
376                                    if (orderByComparator != null) {
377                                            query = new StringBundler(3 +
378                                                            (orderByComparator.getOrderByFields().length * 3));
379                                    }
380                                    else {
381                                            query = new StringBundler(2);
382                                    }
383    
384                                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
385    
386                                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
387    
388                                    if (orderByComparator != null) {
389                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
390                                                    orderByComparator);
391                                    }
392    
393                                    String sql = query.toString();
394    
395                                    Query q = session.createQuery(sql);
396    
397                                    QueryPos qPos = QueryPos.getInstance(q);
398    
399                                    qPos.add(tableId);
400    
401                                    list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(), start,
402                                                    end);
403                            }
404                            catch (Exception e) {
405                                    throw processException(e);
406                            }
407                            finally {
408                                    if (list == null) {
409                                            list = new ArrayList<ExpandoRow>();
410                                    }
411    
412                                    cacheResult(list);
413    
414                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
415                                            finderArgs, list);
416    
417                                    closeSession(session);
418                            }
419                    }
420    
421                    return list;
422            }
423    
424            public ExpandoRow findByTableId_First(long tableId,
425                    OrderByComparator orderByComparator)
426                    throws NoSuchRowException, SystemException {
427                    List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
428    
429                    if (list.isEmpty()) {
430                            StringBundler msg = new StringBundler(4);
431    
432                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
433    
434                            msg.append("tableId=");
435                            msg.append(tableId);
436    
437                            msg.append(StringPool.CLOSE_CURLY_BRACE);
438    
439                            throw new NoSuchRowException(msg.toString());
440                    }
441                    else {
442                            return list.get(0);
443                    }
444            }
445    
446            public ExpandoRow findByTableId_Last(long tableId,
447                    OrderByComparator orderByComparator)
448                    throws NoSuchRowException, SystemException {
449                    int count = countByTableId(tableId);
450    
451                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
452                                    orderByComparator);
453    
454                    if (list.isEmpty()) {
455                            StringBundler msg = new StringBundler(4);
456    
457                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
458    
459                            msg.append("tableId=");
460                            msg.append(tableId);
461    
462                            msg.append(StringPool.CLOSE_CURLY_BRACE);
463    
464                            throw new NoSuchRowException(msg.toString());
465                    }
466                    else {
467                            return list.get(0);
468                    }
469            }
470    
471            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
472                    OrderByComparator orderByComparator)
473                    throws NoSuchRowException, SystemException {
474                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
475    
476                    Session session = null;
477    
478                    try {
479                            session = openSession();
480    
481                            ExpandoRow[] array = new ExpandoRowImpl[3];
482    
483                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
484                                            orderByComparator, true);
485    
486                            array[1] = expandoRow;
487    
488                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
489                                            orderByComparator, false);
490    
491                            return array;
492                    }
493                    catch (Exception e) {
494                            throw processException(e);
495                    }
496                    finally {
497                            closeSession(session);
498                    }
499            }
500    
501            protected ExpandoRow getByTableId_PrevAndNext(Session session,
502                    ExpandoRow expandoRow, long tableId,
503                    OrderByComparator orderByComparator, boolean previous) {
504                    StringBundler query = null;
505    
506                    if (orderByComparator != null) {
507                            query = new StringBundler(6 +
508                                            (orderByComparator.getOrderByFields().length * 6));
509                    }
510                    else {
511                            query = new StringBundler(3);
512                    }
513    
514                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
515    
516                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
517    
518                    if (orderByComparator != null) {
519                            String[] orderByFields = orderByComparator.getOrderByFields();
520    
521                            if (orderByFields.length > 0) {
522                                    query.append(WHERE_AND);
523                            }
524    
525                            for (int i = 0; i < orderByFields.length; i++) {
526                                    query.append(_ORDER_BY_ENTITY_ALIAS);
527                                    query.append(orderByFields[i]);
528    
529                                    if ((i + 1) < orderByFields.length) {
530                                            if (orderByComparator.isAscending() ^ previous) {
531                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
532                                            }
533                                            else {
534                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
535                                            }
536                                    }
537                                    else {
538                                            if (orderByComparator.isAscending() ^ previous) {
539                                                    query.append(WHERE_GREATER_THAN);
540                                            }
541                                            else {
542                                                    query.append(WHERE_LESSER_THAN);
543                                            }
544                                    }
545                            }
546    
547                            query.append(ORDER_BY_CLAUSE);
548    
549                            for (int i = 0; i < orderByFields.length; i++) {
550                                    query.append(_ORDER_BY_ENTITY_ALIAS);
551                                    query.append(orderByFields[i]);
552    
553                                    if ((i + 1) < orderByFields.length) {
554                                            if (orderByComparator.isAscending() ^ previous) {
555                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
556                                            }
557                                            else {
558                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
559                                            }
560                                    }
561                                    else {
562                                            if (orderByComparator.isAscending() ^ previous) {
563                                                    query.append(ORDER_BY_ASC);
564                                            }
565                                            else {
566                                                    query.append(ORDER_BY_DESC);
567                                            }
568                                    }
569                            }
570                    }
571    
572                    String sql = query.toString();
573    
574                    Query q = session.createQuery(sql);
575    
576                    q.setFirstResult(0);
577                    q.setMaxResults(2);
578    
579                    QueryPos qPos = QueryPos.getInstance(q);
580    
581                    qPos.add(tableId);
582    
583                    if (orderByComparator != null) {
584                            Object[] values = orderByComparator.getOrderByValues(expandoRow);
585    
586                            for (Object value : values) {
587                                    qPos.add(value);
588                            }
589                    }
590    
591                    List<ExpandoRow> list = q.list();
592    
593                    if (list.size() == 2) {
594                            return list.get(1);
595                    }
596                    else {
597                            return null;
598                    }
599            }
600    
601            public ExpandoRow findByT_C(long tableId, long classPK)
602                    throws NoSuchRowException, SystemException {
603                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
604    
605                    if (expandoRow == null) {
606                            StringBundler msg = new StringBundler(6);
607    
608                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
609    
610                            msg.append("tableId=");
611                            msg.append(tableId);
612    
613                            msg.append(", classPK=");
614                            msg.append(classPK);
615    
616                            msg.append(StringPool.CLOSE_CURLY_BRACE);
617    
618                            if (_log.isWarnEnabled()) {
619                                    _log.warn(msg.toString());
620                            }
621    
622                            throw new NoSuchRowException(msg.toString());
623                    }
624    
625                    return expandoRow;
626            }
627    
628            public ExpandoRow fetchByT_C(long tableId, long classPK)
629                    throws SystemException {
630                    return fetchByT_C(tableId, classPK, true);
631            }
632    
633            public ExpandoRow fetchByT_C(long tableId, long classPK,
634                    boolean retrieveFromCache) throws SystemException {
635                    Object[] finderArgs = new Object[] { tableId, classPK };
636    
637                    Object result = null;
638    
639                    if (retrieveFromCache) {
640                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
641                                            finderArgs, this);
642                    }
643    
644                    if (result == null) {
645                            Session session = null;
646    
647                            try {
648                                    session = openSession();
649    
650                                    StringBundler query = new StringBundler(3);
651    
652                                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
653    
654                                    query.append(_FINDER_COLUMN_T_C_TABLEID_2);
655    
656                                    query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
657    
658                                    String sql = query.toString();
659    
660                                    Query q = session.createQuery(sql);
661    
662                                    QueryPos qPos = QueryPos.getInstance(q);
663    
664                                    qPos.add(tableId);
665    
666                                    qPos.add(classPK);
667    
668                                    List<ExpandoRow> list = q.list();
669    
670                                    result = list;
671    
672                                    ExpandoRow expandoRow = null;
673    
674                                    if (list.isEmpty()) {
675                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
676                                                    finderArgs, list);
677                                    }
678                                    else {
679                                            expandoRow = list.get(0);
680    
681                                            cacheResult(expandoRow);
682    
683                                            if ((expandoRow.getTableId() != tableId) ||
684                                                            (expandoRow.getClassPK() != classPK)) {
685                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
686                                                            finderArgs, expandoRow);
687                                            }
688                                    }
689    
690                                    return expandoRow;
691                            }
692                            catch (Exception e) {
693                                    throw processException(e);
694                            }
695                            finally {
696                                    if (result == null) {
697                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
698                                                    finderArgs, new ArrayList<ExpandoRow>());
699                                    }
700    
701                                    closeSession(session);
702                            }
703                    }
704                    else {
705                            if (result instanceof List<?>) {
706                                    return null;
707                            }
708                            else {
709                                    return (ExpandoRow)result;
710                            }
711                    }
712            }
713    
714            public List<ExpandoRow> findAll() throws SystemException {
715                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
716            }
717    
718            public List<ExpandoRow> findAll(int start, int end)
719                    throws SystemException {
720                    return findAll(start, end, null);
721            }
722    
723            public List<ExpandoRow> findAll(int start, int end,
724                    OrderByComparator orderByComparator) throws SystemException {
725                    Object[] finderArgs = new Object[] {
726                                    String.valueOf(start), String.valueOf(end),
727                                    String.valueOf(orderByComparator)
728                            };
729    
730                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
731                                    finderArgs, this);
732    
733                    if (list == null) {
734                            Session session = null;
735    
736                            try {
737                                    session = openSession();
738    
739                                    StringBundler query = null;
740                                    String sql = null;
741    
742                                    if (orderByComparator != null) {
743                                            query = new StringBundler(2 +
744                                                            (orderByComparator.getOrderByFields().length * 3));
745    
746                                            query.append(_SQL_SELECT_EXPANDOROW);
747    
748                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
749                                                    orderByComparator);
750    
751                                            sql = query.toString();
752                                    }
753                                    else {
754                                            sql = _SQL_SELECT_EXPANDOROW;
755                                    }
756    
757                                    Query q = session.createQuery(sql);
758    
759                                    if (orderByComparator == null) {
760                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
761                                                            start, end, false);
762    
763                                            Collections.sort(list);
764                                    }
765                                    else {
766                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
767                                                            start, end);
768                                    }
769                            }
770                            catch (Exception e) {
771                                    throw processException(e);
772                            }
773                            finally {
774                                    if (list == null) {
775                                            list = new ArrayList<ExpandoRow>();
776                                    }
777    
778                                    cacheResult(list);
779    
780                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
781    
782                                    closeSession(session);
783                            }
784                    }
785    
786                    return list;
787            }
788    
789            public void removeByTableId(long tableId) throws SystemException {
790                    for (ExpandoRow expandoRow : findByTableId(tableId)) {
791                            remove(expandoRow);
792                    }
793            }
794    
795            public void removeByT_C(long tableId, long classPK)
796                    throws NoSuchRowException, SystemException {
797                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
798    
799                    remove(expandoRow);
800            }
801    
802            public void removeAll() throws SystemException {
803                    for (ExpandoRow expandoRow : findAll()) {
804                            remove(expandoRow);
805                    }
806            }
807    
808            public int countByTableId(long tableId) throws SystemException {
809                    Object[] finderArgs = new Object[] { tableId };
810    
811                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
812                                    finderArgs, this);
813    
814                    if (count == null) {
815                            Session session = null;
816    
817                            try {
818                                    session = openSession();
819    
820                                    StringBundler query = new StringBundler(2);
821    
822                                    query.append(_SQL_COUNT_EXPANDOROW_WHERE);
823    
824                                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
825    
826                                    String sql = query.toString();
827    
828                                    Query q = session.createQuery(sql);
829    
830                                    QueryPos qPos = QueryPos.getInstance(q);
831    
832                                    qPos.add(tableId);
833    
834                                    count = (Long)q.uniqueResult();
835                            }
836                            catch (Exception e) {
837                                    throw processException(e);
838                            }
839                            finally {
840                                    if (count == null) {
841                                            count = Long.valueOf(0);
842                                    }
843    
844                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
845                                            finderArgs, count);
846    
847                                    closeSession(session);
848                            }
849                    }
850    
851                    return count.intValue();
852            }
853    
854            public int countByT_C(long tableId, long classPK) throws SystemException {
855                    Object[] finderArgs = new Object[] { tableId, classPK };
856    
857                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_C,
858                                    finderArgs, this);
859    
860                    if (count == null) {
861                            Session session = null;
862    
863                            try {
864                                    session = openSession();
865    
866                                    StringBundler query = new StringBundler(3);
867    
868                                    query.append(_SQL_COUNT_EXPANDOROW_WHERE);
869    
870                                    query.append(_FINDER_COLUMN_T_C_TABLEID_2);
871    
872                                    query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
873    
874                                    String sql = query.toString();
875    
876                                    Query q = session.createQuery(sql);
877    
878                                    QueryPos qPos = QueryPos.getInstance(q);
879    
880                                    qPos.add(tableId);
881    
882                                    qPos.add(classPK);
883    
884                                    count = (Long)q.uniqueResult();
885                            }
886                            catch (Exception e) {
887                                    throw processException(e);
888                            }
889                            finally {
890                                    if (count == null) {
891                                            count = Long.valueOf(0);
892                                    }
893    
894                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, finderArgs,
895                                            count);
896    
897                                    closeSession(session);
898                            }
899                    }
900    
901                    return count.intValue();
902            }
903    
904            public int countAll() throws SystemException {
905                    Object[] finderArgs = new Object[0];
906    
907                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
908                                    finderArgs, this);
909    
910                    if (count == null) {
911                            Session session = null;
912    
913                            try {
914                                    session = openSession();
915    
916                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
917    
918                                    count = (Long)q.uniqueResult();
919                            }
920                            catch (Exception e) {
921                                    throw processException(e);
922                            }
923                            finally {
924                                    if (count == null) {
925                                            count = Long.valueOf(0);
926                                    }
927    
928                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
929                                            count);
930    
931                                    closeSession(session);
932                            }
933                    }
934    
935                    return count.intValue();
936            }
937    
938            public void afterPropertiesSet() {
939                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
940                                            com.liferay.portal.util.PropsUtil.get(
941                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
942    
943                    if (listenerClassNames.length > 0) {
944                            try {
945                                    List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
946    
947                                    for (String listenerClassName : listenerClassNames) {
948                                            listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
949                                                            listenerClassName));
950                                    }
951    
952                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
953                            }
954                            catch (Exception e) {
955                                    _log.error(e);
956                            }
957                    }
958            }
959    
960            @BeanReference(type = ExpandoColumnPersistence.class)
961            protected ExpandoColumnPersistence expandoColumnPersistence;
962            @BeanReference(type = ExpandoRowPersistence.class)
963            protected ExpandoRowPersistence expandoRowPersistence;
964            @BeanReference(type = ExpandoTablePersistence.class)
965            protected ExpandoTablePersistence expandoTablePersistence;
966            @BeanReference(type = ExpandoValuePersistence.class)
967            protected ExpandoValuePersistence expandoValuePersistence;
968            @BeanReference(type = ResourcePersistence.class)
969            protected ResourcePersistence resourcePersistence;
970            @BeanReference(type = UserPersistence.class)
971            protected UserPersistence userPersistence;
972            private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
973            private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
974            private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
975            private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
976            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
977            private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
978            private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
979            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
980            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
981            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
982            private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
983    }