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.SQLQuery;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.ModelListener;
039    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
040    import com.liferay.portal.service.persistence.BatchSessionUtil;
041    import com.liferay.portal.service.persistence.ResourcePersistence;
042    import com.liferay.portal.service.persistence.UserPersistence;
043    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044    
045    import com.liferay.portlet.expando.NoSuchColumnException;
046    import com.liferay.portlet.expando.model.ExpandoColumn;
047    import com.liferay.portlet.expando.model.impl.ExpandoColumnImpl;
048    import com.liferay.portlet.expando.model.impl.ExpandoColumnModelImpl;
049    
050    import java.io.Serializable;
051    
052    import java.util.ArrayList;
053    import java.util.Collections;
054    import java.util.List;
055    
056    /**
057     * The persistence implementation for the expando column service.
058     *
059     * <p>
060     * Never modify or reference this class directly. Always use {@link ExpandoColumnUtil} to access the expando column persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
061     * </p>
062     *
063     * <p>
064     * Caching information and settings can be found in <code>portal.properties</code>
065     * </p>
066     *
067     * @author Brian Wing Shun Chan
068     * @see ExpandoColumnPersistence
069     * @see ExpandoColumnUtil
070     * @generated
071     */
072    public class ExpandoColumnPersistenceImpl extends BasePersistenceImpl<ExpandoColumn>
073            implements ExpandoColumnPersistence {
074            public static final String FINDER_CLASS_NAME_ENTITY = ExpandoColumnImpl.class.getName();
075            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
076                    ".List";
077            public static final FinderPath FINDER_PATH_FIND_BY_TABLEID = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
078                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
079                            FINDER_CLASS_NAME_LIST, "findByTableId",
080                            new String[] {
081                                    Long.class.getName(),
082                                    
083                            "java.lang.Integer", "java.lang.Integer",
084                                    "com.liferay.portal.kernel.util.OrderByComparator"
085                            });
086            public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
087                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
088                            FINDER_CLASS_NAME_LIST, "countByTableId",
089                            new String[] { Long.class.getName() });
090            public static final FinderPath FINDER_PATH_FETCH_BY_T_N = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
091                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
092                            FINDER_CLASS_NAME_ENTITY, "fetchByT_N",
093                            new String[] { Long.class.getName(), String.class.getName() });
094            public static final FinderPath FINDER_PATH_COUNT_BY_T_N = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
095                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
096                            FINDER_CLASS_NAME_LIST, "countByT_N",
097                            new String[] { Long.class.getName(), String.class.getName() });
098            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
099                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
100                            FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
101            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
102                            ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
103                            FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
104    
105            /**
106             * Caches the expando column in the entity cache if it is enabled.
107             *
108             * @param expandoColumn the expando column to cache
109             */
110            public void cacheResult(ExpandoColumn expandoColumn) {
111                    EntityCacheUtil.putResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
112                            ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
113                            expandoColumn);
114    
115                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
116                            new Object[] {
117                                    new Long(expandoColumn.getTableId()),
118                                    
119                            expandoColumn.getName()
120                            }, expandoColumn);
121            }
122    
123            /**
124             * Caches the expando columns in the entity cache if it is enabled.
125             *
126             * @param expandoColumns the expando columns to cache
127             */
128            public void cacheResult(List<ExpandoColumn> expandoColumns) {
129                    for (ExpandoColumn expandoColumn : expandoColumns) {
130                            if (EntityCacheUtil.getResult(
131                                                    ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
132                                                    ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
133                                                    this) == null) {
134                                    cacheResult(expandoColumn);
135                            }
136                    }
137            }
138    
139            /**
140             * Clears the cache for all expando columns.
141             *
142             * <p>
143             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
144             * </p>
145             */
146            public void clearCache() {
147                    CacheRegistryUtil.clear(ExpandoColumnImpl.class.getName());
148                    EntityCacheUtil.clearCache(ExpandoColumnImpl.class.getName());
149                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
150                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
151            }
152    
153            /**
154             * Clears the cache for the expando column.
155             *
156             * <p>
157             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
158             * </p>
159             */
160            public void clearCache(ExpandoColumn expandoColumn) {
161                    EntityCacheUtil.removeResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
162                            ExpandoColumnImpl.class, expandoColumn.getPrimaryKey());
163    
164                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
165                            new Object[] {
166                                    new Long(expandoColumn.getTableId()),
167                                    
168                            expandoColumn.getName()
169                            });
170            }
171    
172            /**
173             * Creates a new expando column with the primary key. Does not add the expando column to the database.
174             *
175             * @param columnId the primary key for the new expando column
176             * @return the new expando column
177             */
178            public ExpandoColumn create(long columnId) {
179                    ExpandoColumn expandoColumn = new ExpandoColumnImpl();
180    
181                    expandoColumn.setNew(true);
182                    expandoColumn.setPrimaryKey(columnId);
183    
184                    return expandoColumn;
185            }
186    
187            /**
188             * Removes the expando column with the primary key from the database. Also notifies the appropriate model listeners.
189             *
190             * @param primaryKey the primary key of the expando column to remove
191             * @return the expando column that was removed
192             * @throws com.liferay.portal.NoSuchModelException if a expando column with the primary key could not be found
193             * @throws SystemException if a system exception occurred
194             */
195            public ExpandoColumn remove(Serializable primaryKey)
196                    throws NoSuchModelException, SystemException {
197                    return remove(((Long)primaryKey).longValue());
198            }
199    
200            /**
201             * Removes the expando column with the primary key from the database. Also notifies the appropriate model listeners.
202             *
203             * @param columnId the primary key of the expando column to remove
204             * @return the expando column that was removed
205             * @throws com.liferay.portlet.expando.NoSuchColumnException if a expando column with the primary key could not be found
206             * @throws SystemException if a system exception occurred
207             */
208            public ExpandoColumn remove(long columnId)
209                    throws NoSuchColumnException, SystemException {
210                    Session session = null;
211    
212                    try {
213                            session = openSession();
214    
215                            ExpandoColumn expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
216                                            new Long(columnId));
217    
218                            if (expandoColumn == null) {
219                                    if (_log.isWarnEnabled()) {
220                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + columnId);
221                                    }
222    
223                                    throw new NoSuchColumnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
224                                            columnId);
225                            }
226    
227                            return remove(expandoColumn);
228                    }
229                    catch (NoSuchColumnException nsee) {
230                            throw nsee;
231                    }
232                    catch (Exception e) {
233                            throw processException(e);
234                    }
235                    finally {
236                            closeSession(session);
237                    }
238            }
239    
240            protected ExpandoColumn removeImpl(ExpandoColumn expandoColumn)
241                    throws SystemException {
242                    expandoColumn = toUnwrappedModel(expandoColumn);
243    
244                    Session session = null;
245    
246                    try {
247                            session = openSession();
248    
249                            BatchSessionUtil.delete(session, expandoColumn);
250                    }
251                    catch (Exception e) {
252                            throw processException(e);
253                    }
254                    finally {
255                            closeSession(session);
256                    }
257    
258                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
259    
260                    ExpandoColumnModelImpl expandoColumnModelImpl = (ExpandoColumnModelImpl)expandoColumn;
261    
262                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
263                            new Object[] {
264                                    new Long(expandoColumnModelImpl.getTableId()),
265                                    
266                            expandoColumnModelImpl.getName()
267                            });
268    
269                    EntityCacheUtil.removeResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
270                            ExpandoColumnImpl.class, expandoColumn.getPrimaryKey());
271    
272                    return expandoColumn;
273            }
274    
275            public ExpandoColumn updateImpl(
276                    com.liferay.portlet.expando.model.ExpandoColumn expandoColumn,
277                    boolean merge) throws SystemException {
278                    expandoColumn = toUnwrappedModel(expandoColumn);
279    
280                    boolean isNew = expandoColumn.isNew();
281    
282                    ExpandoColumnModelImpl expandoColumnModelImpl = (ExpandoColumnModelImpl)expandoColumn;
283    
284                    Session session = null;
285    
286                    try {
287                            session = openSession();
288    
289                            BatchSessionUtil.update(session, expandoColumn, merge);
290    
291                            expandoColumn.setNew(false);
292                    }
293                    catch (Exception e) {
294                            throw processException(e);
295                    }
296                    finally {
297                            closeSession(session);
298                    }
299    
300                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
301    
302                    EntityCacheUtil.putResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
303                            ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
304                            expandoColumn);
305    
306                    if (!isNew &&
307                                    ((expandoColumn.getTableId() != expandoColumnModelImpl.getOriginalTableId()) ||
308                                    !Validator.equals(expandoColumn.getName(),
309                                            expandoColumnModelImpl.getOriginalName()))) {
310                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
311                                    new Object[] {
312                                            new Long(expandoColumnModelImpl.getOriginalTableId()),
313                                            
314                                    expandoColumnModelImpl.getOriginalName()
315                                    });
316                    }
317    
318                    if (isNew ||
319                                    ((expandoColumn.getTableId() != expandoColumnModelImpl.getOriginalTableId()) ||
320                                    !Validator.equals(expandoColumn.getName(),
321                                            expandoColumnModelImpl.getOriginalName()))) {
322                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
323                                    new Object[] {
324                                            new Long(expandoColumn.getTableId()),
325                                            
326                                    expandoColumn.getName()
327                                    }, expandoColumn);
328                    }
329    
330                    return expandoColumn;
331            }
332    
333            protected ExpandoColumn toUnwrappedModel(ExpandoColumn expandoColumn) {
334                    if (expandoColumn instanceof ExpandoColumnImpl) {
335                            return expandoColumn;
336                    }
337    
338                    ExpandoColumnImpl expandoColumnImpl = new ExpandoColumnImpl();
339    
340                    expandoColumnImpl.setNew(expandoColumn.isNew());
341                    expandoColumnImpl.setPrimaryKey(expandoColumn.getPrimaryKey());
342    
343                    expandoColumnImpl.setColumnId(expandoColumn.getColumnId());
344                    expandoColumnImpl.setCompanyId(expandoColumn.getCompanyId());
345                    expandoColumnImpl.setTableId(expandoColumn.getTableId());
346                    expandoColumnImpl.setName(expandoColumn.getName());
347                    expandoColumnImpl.setType(expandoColumn.getType());
348                    expandoColumnImpl.setDefaultData(expandoColumn.getDefaultData());
349                    expandoColumnImpl.setTypeSettings(expandoColumn.getTypeSettings());
350    
351                    return expandoColumnImpl;
352            }
353    
354            /**
355             * Finds the expando column with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
356             *
357             * @param primaryKey the primary key of the expando column to find
358             * @return the expando column
359             * @throws com.liferay.portal.NoSuchModelException if a expando column with the primary key could not be found
360             * @throws SystemException if a system exception occurred
361             */
362            public ExpandoColumn findByPrimaryKey(Serializable primaryKey)
363                    throws NoSuchModelException, SystemException {
364                    return findByPrimaryKey(((Long)primaryKey).longValue());
365            }
366    
367            /**
368             * Finds the expando column with the primary key or throws a {@link com.liferay.portlet.expando.NoSuchColumnException} if it could not be found.
369             *
370             * @param columnId the primary key of the expando column to find
371             * @return the expando column
372             * @throws com.liferay.portlet.expando.NoSuchColumnException if a expando column with the primary key could not be found
373             * @throws SystemException if a system exception occurred
374             */
375            public ExpandoColumn findByPrimaryKey(long columnId)
376                    throws NoSuchColumnException, SystemException {
377                    ExpandoColumn expandoColumn = fetchByPrimaryKey(columnId);
378    
379                    if (expandoColumn == null) {
380                            if (_log.isWarnEnabled()) {
381                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + columnId);
382                            }
383    
384                            throw new NoSuchColumnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
385                                    columnId);
386                    }
387    
388                    return expandoColumn;
389            }
390    
391            /**
392             * Finds the expando column with the primary key or returns <code>null</code> if it could not be found.
393             *
394             * @param primaryKey the primary key of the expando column to find
395             * @return the expando column, or <code>null</code> if a expando column with the primary key could not be found
396             * @throws SystemException if a system exception occurred
397             */
398            public ExpandoColumn fetchByPrimaryKey(Serializable primaryKey)
399                    throws SystemException {
400                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
401            }
402    
403            /**
404             * Finds the expando column with the primary key or returns <code>null</code> if it could not be found.
405             *
406             * @param columnId the primary key of the expando column to find
407             * @return the expando column, or <code>null</code> if a expando column with the primary key could not be found
408             * @throws SystemException if a system exception occurred
409             */
410            public ExpandoColumn fetchByPrimaryKey(long columnId)
411                    throws SystemException {
412                    ExpandoColumn expandoColumn = (ExpandoColumn)EntityCacheUtil.getResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
413                                    ExpandoColumnImpl.class, columnId, this);
414    
415                    if (expandoColumn == null) {
416                            Session session = null;
417    
418                            try {
419                                    session = openSession();
420    
421                                    expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
422                                                    new Long(columnId));
423                            }
424                            catch (Exception e) {
425                                    throw processException(e);
426                            }
427                            finally {
428                                    if (expandoColumn != null) {
429                                            cacheResult(expandoColumn);
430                                    }
431    
432                                    closeSession(session);
433                            }
434                    }
435    
436                    return expandoColumn;
437            }
438    
439            /**
440             * Finds all the expando columns where tableId = &#63;.
441             *
442             * @param tableId the table id to search with
443             * @return the matching expando columns
444             * @throws SystemException if a system exception occurred
445             */
446            public List<ExpandoColumn> findByTableId(long tableId)
447                    throws SystemException {
448                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
449            }
450    
451            /**
452             * Finds a range of all the expando columns 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 columns to return
460             * @param end the upper bound of the range of expando columns to return (not inclusive)
461             * @return the range of matching expando columns
462             * @throws SystemException if a system exception occurred
463             */
464            public List<ExpandoColumn> findByTableId(long tableId, int start, int end)
465                    throws SystemException {
466                    return findByTableId(tableId, start, end, null);
467            }
468    
469            /**
470             * Finds an ordered range of all the expando columns where tableId = &#63;.
471             *
472             * <p>
473             * 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.
474             * </p>
475             *
476             * @param tableId the table id to search with
477             * @param start the lower bound of the range of expando columns to return
478             * @param end the upper bound of the range of expando columns to return (not inclusive)
479             * @param orderByComparator the comparator to order the results by
480             * @return the ordered range of matching expando columns
481             * @throws SystemException if a system exception occurred
482             */
483            public List<ExpandoColumn> findByTableId(long tableId, int start, int end,
484                    OrderByComparator orderByComparator) throws SystemException {
485                    Object[] finderArgs = new Object[] {
486                                    tableId,
487                                    
488                                    String.valueOf(start), String.valueOf(end),
489                                    String.valueOf(orderByComparator)
490                            };
491    
492                    List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_TABLEID,
493                                    finderArgs, this);
494    
495                    if (list == null) {
496                            StringBundler query = null;
497    
498                            if (orderByComparator != null) {
499                                    query = new StringBundler(3 +
500                                                    (orderByComparator.getOrderByFields().length * 3));
501                            }
502                            else {
503                                    query = new StringBundler(3);
504                            }
505    
506                            query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
507    
508                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
509    
510                            if (orderByComparator != null) {
511                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
512                                            orderByComparator);
513                            }
514    
515                            else {
516                                    query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
517                            }
518    
519                            String sql = query.toString();
520    
521                            Session session = null;
522    
523                            try {
524                                    session = openSession();
525    
526                                    Query q = session.createQuery(sql);
527    
528                                    QueryPos qPos = QueryPos.getInstance(q);
529    
530                                    qPos.add(tableId);
531    
532                                    list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
533                                                    start, end);
534                            }
535                            catch (Exception e) {
536                                    throw processException(e);
537                            }
538                            finally {
539                                    if (list == null) {
540                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_TABLEID,
541                                                    finderArgs);
542                                    }
543                                    else {
544                                            cacheResult(list);
545    
546                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
547                                                    finderArgs, list);
548                                    }
549    
550                                    closeSession(session);
551                            }
552                    }
553    
554                    return list;
555            }
556    
557            /**
558             * Finds the first expando column in the ordered set where tableId = &#63;.
559             *
560             * <p>
561             * 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.
562             * </p>
563             *
564             * @param tableId the table id to search with
565             * @param orderByComparator the comparator to order the set by
566             * @return the first matching expando column
567             * @throws com.liferay.portlet.expando.NoSuchColumnException if a matching expando column could not be found
568             * @throws SystemException if a system exception occurred
569             */
570            public ExpandoColumn findByTableId_First(long tableId,
571                    OrderByComparator orderByComparator)
572                    throws NoSuchColumnException, SystemException {
573                    List<ExpandoColumn> list = findByTableId(tableId, 0, 1,
574                                    orderByComparator);
575    
576                    if (list.isEmpty()) {
577                            StringBundler msg = new StringBundler(4);
578    
579                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
580    
581                            msg.append("tableId=");
582                            msg.append(tableId);
583    
584                            msg.append(StringPool.CLOSE_CURLY_BRACE);
585    
586                            throw new NoSuchColumnException(msg.toString());
587                    }
588                    else {
589                            return list.get(0);
590                    }
591            }
592    
593            /**
594             * Finds the last expando column in the ordered set where tableId = &#63;.
595             *
596             * <p>
597             * 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.
598             * </p>
599             *
600             * @param tableId the table id to search with
601             * @param orderByComparator the comparator to order the set by
602             * @return the last matching expando column
603             * @throws com.liferay.portlet.expando.NoSuchColumnException if a matching expando column could not be found
604             * @throws SystemException if a system exception occurred
605             */
606            public ExpandoColumn findByTableId_Last(long tableId,
607                    OrderByComparator orderByComparator)
608                    throws NoSuchColumnException, SystemException {
609                    int count = countByTableId(tableId);
610    
611                    List<ExpandoColumn> list = findByTableId(tableId, count - 1, count,
612                                    orderByComparator);
613    
614                    if (list.isEmpty()) {
615                            StringBundler msg = new StringBundler(4);
616    
617                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
618    
619                            msg.append("tableId=");
620                            msg.append(tableId);
621    
622                            msg.append(StringPool.CLOSE_CURLY_BRACE);
623    
624                            throw new NoSuchColumnException(msg.toString());
625                    }
626                    else {
627                            return list.get(0);
628                    }
629            }
630    
631            /**
632             * Finds the expando columns before and after the current expando column in the ordered set where tableId = &#63;.
633             *
634             * <p>
635             * 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.
636             * </p>
637             *
638             * @param columnId the primary key of the current expando column
639             * @param tableId the table id to search with
640             * @param orderByComparator the comparator to order the set by
641             * @return the previous, current, and next expando column
642             * @throws com.liferay.portlet.expando.NoSuchColumnException if a expando column with the primary key could not be found
643             * @throws SystemException if a system exception occurred
644             */
645            public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
646                    long tableId, OrderByComparator orderByComparator)
647                    throws NoSuchColumnException, SystemException {
648                    ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
649    
650                    Session session = null;
651    
652                    try {
653                            session = openSession();
654    
655                            ExpandoColumn[] array = new ExpandoColumnImpl[3];
656    
657                            array[0] = getByTableId_PrevAndNext(session, expandoColumn,
658                                            tableId, orderByComparator, true);
659    
660                            array[1] = expandoColumn;
661    
662                            array[2] = getByTableId_PrevAndNext(session, expandoColumn,
663                                            tableId, orderByComparator, false);
664    
665                            return array;
666                    }
667                    catch (Exception e) {
668                            throw processException(e);
669                    }
670                    finally {
671                            closeSession(session);
672                    }
673            }
674    
675            protected ExpandoColumn getByTableId_PrevAndNext(Session session,
676                    ExpandoColumn expandoColumn, long tableId,
677                    OrderByComparator orderByComparator, boolean previous) {
678                    StringBundler query = null;
679    
680                    if (orderByComparator != null) {
681                            query = new StringBundler(6 +
682                                            (orderByComparator.getOrderByFields().length * 6));
683                    }
684                    else {
685                            query = new StringBundler(3);
686                    }
687    
688                    query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
689    
690                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
691    
692                    if (orderByComparator != null) {
693                            String[] orderByFields = orderByComparator.getOrderByFields();
694    
695                            if (orderByFields.length > 0) {
696                                    query.append(WHERE_AND);
697                            }
698    
699                            for (int i = 0; i < orderByFields.length; i++) {
700                                    query.append(_ORDER_BY_ENTITY_ALIAS);
701                                    query.append(orderByFields[i]);
702    
703                                    if ((i + 1) < orderByFields.length) {
704                                            if (orderByComparator.isAscending() ^ previous) {
705                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
706                                            }
707                                            else {
708                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
709                                            }
710                                    }
711                                    else {
712                                            if (orderByComparator.isAscending() ^ previous) {
713                                                    query.append(WHERE_GREATER_THAN);
714                                            }
715                                            else {
716                                                    query.append(WHERE_LESSER_THAN);
717                                            }
718                                    }
719                            }
720    
721                            query.append(ORDER_BY_CLAUSE);
722    
723                            for (int i = 0; i < orderByFields.length; i++) {
724                                    query.append(_ORDER_BY_ENTITY_ALIAS);
725                                    query.append(orderByFields[i]);
726    
727                                    if ((i + 1) < orderByFields.length) {
728                                            if (orderByComparator.isAscending() ^ previous) {
729                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
730                                            }
731                                            else {
732                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
733                                            }
734                                    }
735                                    else {
736                                            if (orderByComparator.isAscending() ^ previous) {
737                                                    query.append(ORDER_BY_ASC);
738                                            }
739                                            else {
740                                                    query.append(ORDER_BY_DESC);
741                                            }
742                                    }
743                            }
744                    }
745    
746                    else {
747                            query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
748                    }
749    
750                    String sql = query.toString();
751    
752                    Query q = session.createQuery(sql);
753    
754                    q.setFirstResult(0);
755                    q.setMaxResults(2);
756    
757                    QueryPos qPos = QueryPos.getInstance(q);
758    
759                    qPos.add(tableId);
760    
761                    if (orderByComparator != null) {
762                            Object[] values = orderByComparator.getOrderByValues(expandoColumn);
763    
764                            for (Object value : values) {
765                                    qPos.add(value);
766                            }
767                    }
768    
769                    List<ExpandoColumn> list = q.list();
770    
771                    if (list.size() == 2) {
772                            return list.get(1);
773                    }
774                    else {
775                            return null;
776                    }
777            }
778    
779            /**
780             * Filters by the user's permissions and finds all the expando columns where tableId = &#63;.
781             *
782             * @param tableId the table id to search with
783             * @return the matching expando columns that the user has permission to view
784             * @throws SystemException if a system exception occurred
785             */
786            public List<ExpandoColumn> filterFindByTableId(long tableId)
787                    throws SystemException {
788                    return filterFindByTableId(tableId, QueryUtil.ALL_POS,
789                            QueryUtil.ALL_POS, null);
790            }
791    
792            /**
793             * Filters by the user's permissions and finds a range of all the expando columns where tableId = &#63;.
794             *
795             * <p>
796             * 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.
797             * </p>
798             *
799             * @param tableId the table id to search with
800             * @param start the lower bound of the range of expando columns to return
801             * @param end the upper bound of the range of expando columns to return (not inclusive)
802             * @return the range of matching expando columns that the user has permission to view
803             * @throws SystemException if a system exception occurred
804             */
805            public List<ExpandoColumn> filterFindByTableId(long tableId, int start,
806                    int end) throws SystemException {
807                    return filterFindByTableId(tableId, start, end, null);
808            }
809    
810            /**
811             * Filters by the user's permissions and finds an ordered range of all the expando columns where tableId = &#63;.
812             *
813             * <p>
814             * 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.
815             * </p>
816             *
817             * @param tableId the table id to search with
818             * @param start the lower bound of the range of expando columns to return
819             * @param end the upper bound of the range of expando columns to return (not inclusive)
820             * @param orderByComparator the comparator to order the results by
821             * @return the ordered range of matching expando columns that the user has permission to view
822             * @throws SystemException if a system exception occurred
823             */
824            public List<ExpandoColumn> filterFindByTableId(long tableId, int start,
825                    int end, OrderByComparator orderByComparator) throws SystemException {
826                    if (!InlineSQLHelperUtil.isEnabled()) {
827                            return findByTableId(tableId, start, end, orderByComparator);
828                    }
829    
830                    StringBundler query = null;
831    
832                    if (orderByComparator != null) {
833                            query = new StringBundler(3 +
834                                            (orderByComparator.getOrderByFields().length * 3));
835                    }
836                    else {
837                            query = new StringBundler(3);
838                    }
839    
840                    if (getDB().isSupportsInlineDistinct()) {
841                            query.append(_FILTER_SQL_SELECT_EXPANDOCOLUMN_WHERE);
842                    }
843                    else {
844                            query.append(_FILTER_SQL_SELECT_EXPANDOCOLUMN_NO_INLINE_DISTINCT_WHERE_1);
845                    }
846    
847                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
848    
849                    if (!getDB().isSupportsInlineDistinct()) {
850                            query.append(_FILTER_SQL_SELECT_EXPANDOCOLUMN_NO_INLINE_DISTINCT_WHERE_2);
851                    }
852    
853                    if (orderByComparator != null) {
854                            if (getDB().isSupportsInlineDistinct()) {
855                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
856                                            orderByComparator);
857                            }
858                            else {
859                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
860                                            orderByComparator);
861                            }
862                    }
863    
864                    else {
865                            if (getDB().isSupportsInlineDistinct()) {
866                                    query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
867                            }
868                            else {
869                                    query.append(ExpandoColumnModelImpl.ORDER_BY_SQL);
870                            }
871                    }
872    
873                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
874                                    ExpandoColumn.class.getName(), _FILTER_COLUMN_PK,
875                                    _FILTER_COLUMN_USERID);
876    
877                    Session session = null;
878    
879                    try {
880                            session = openSession();
881    
882                            SQLQuery q = session.createSQLQuery(sql);
883    
884                            if (getDB().isSupportsInlineDistinct()) {
885                                    q.addEntity(_FILTER_ENTITY_ALIAS, ExpandoColumnImpl.class);
886                            }
887                            else {
888                                    q.addEntity(_FILTER_ENTITY_TABLE, ExpandoColumnImpl.class);
889                            }
890    
891                            QueryPos qPos = QueryPos.getInstance(q);
892    
893                            qPos.add(tableId);
894    
895                            return (List<ExpandoColumn>)QueryUtil.list(q, getDialect(), start,
896                                    end);
897                    }
898                    catch (Exception e) {
899                            throw processException(e);
900                    }
901                    finally {
902                            closeSession(session);
903                    }
904            }
905    
906            /**
907             * Finds the expando column where tableId = &#63; and name = &#63; or throws a {@link com.liferay.portlet.expando.NoSuchColumnException} if it could not be found.
908             *
909             * @param tableId the table id to search with
910             * @param name the name to search with
911             * @return the matching expando column
912             * @throws com.liferay.portlet.expando.NoSuchColumnException if a matching expando column could not be found
913             * @throws SystemException if a system exception occurred
914             */
915            public ExpandoColumn findByT_N(long tableId, String name)
916                    throws NoSuchColumnException, SystemException {
917                    ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
918    
919                    if (expandoColumn == null) {
920                            StringBundler msg = new StringBundler(6);
921    
922                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
923    
924                            msg.append("tableId=");
925                            msg.append(tableId);
926    
927                            msg.append(", name=");
928                            msg.append(name);
929    
930                            msg.append(StringPool.CLOSE_CURLY_BRACE);
931    
932                            if (_log.isWarnEnabled()) {
933                                    _log.warn(msg.toString());
934                            }
935    
936                            throw new NoSuchColumnException(msg.toString());
937                    }
938    
939                    return expandoColumn;
940            }
941    
942            /**
943             * Finds the expando column where tableId = &#63; and name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
944             *
945             * @param tableId the table id to search with
946             * @param name the name to search with
947             * @return the matching expando column, or <code>null</code> if a matching expando column could not be found
948             * @throws SystemException if a system exception occurred
949             */
950            public ExpandoColumn fetchByT_N(long tableId, String name)
951                    throws SystemException {
952                    return fetchByT_N(tableId, name, true);
953            }
954    
955            /**
956             * Finds the expando column where tableId = &#63; and name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
957             *
958             * @param tableId the table id to search with
959             * @param name the name to search with
960             * @return the matching expando column, or <code>null</code> if a matching expando column could not be found
961             * @throws SystemException if a system exception occurred
962             */
963            public ExpandoColumn fetchByT_N(long tableId, String name,
964                    boolean retrieveFromCache) throws SystemException {
965                    Object[] finderArgs = new Object[] { tableId, name };
966    
967                    Object result = null;
968    
969                    if (retrieveFromCache) {
970                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_N,
971                                            finderArgs, this);
972                    }
973    
974                    if (result == null) {
975                            StringBundler query = new StringBundler(4);
976    
977                            query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
978    
979                            query.append(_FINDER_COLUMN_T_N_TABLEID_2);
980    
981                            if (name == null) {
982                                    query.append(_FINDER_COLUMN_T_N_NAME_1);
983                            }
984                            else {
985                                    if (name.equals(StringPool.BLANK)) {
986                                            query.append(_FINDER_COLUMN_T_N_NAME_3);
987                                    }
988                                    else {
989                                            query.append(_FINDER_COLUMN_T_N_NAME_2);
990                                    }
991                            }
992    
993                            query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
994    
995                            String sql = query.toString();
996    
997                            Session session = null;
998    
999                            try {
1000                                    session = openSession();
1001    
1002                                    Query q = session.createQuery(sql);
1003    
1004                                    QueryPos qPos = QueryPos.getInstance(q);
1005    
1006                                    qPos.add(tableId);
1007    
1008                                    if (name != null) {
1009                                            qPos.add(name);
1010                                    }
1011    
1012                                    List<ExpandoColumn> list = q.list();
1013    
1014                                    result = list;
1015    
1016                                    ExpandoColumn expandoColumn = null;
1017    
1018                                    if (list.isEmpty()) {
1019                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
1020                                                    finderArgs, list);
1021                                    }
1022                                    else {
1023                                            expandoColumn = list.get(0);
1024    
1025                                            cacheResult(expandoColumn);
1026    
1027                                            if ((expandoColumn.getTableId() != tableId) ||
1028                                                            (expandoColumn.getName() == null) ||
1029                                                            !expandoColumn.getName().equals(name)) {
1030                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
1031                                                            finderArgs, expandoColumn);
1032                                            }
1033                                    }
1034    
1035                                    return expandoColumn;
1036                            }
1037                            catch (Exception e) {
1038                                    throw processException(e);
1039                            }
1040                            finally {
1041                                    if (result == null) {
1042                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
1043                                                    finderArgs);
1044                                    }
1045    
1046                                    closeSession(session);
1047                            }
1048                    }
1049                    else {
1050                            if (result instanceof List<?>) {
1051                                    return null;
1052                            }
1053                            else {
1054                                    return (ExpandoColumn)result;
1055                            }
1056                    }
1057            }
1058    
1059            /**
1060             * Finds all the expando columns.
1061             *
1062             * @return the expando columns
1063             * @throws SystemException if a system exception occurred
1064             */
1065            public List<ExpandoColumn> findAll() throws SystemException {
1066                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1067            }
1068    
1069            /**
1070             * Finds a range of all the expando columns.
1071             *
1072             * <p>
1073             * 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.
1074             * </p>
1075             *
1076             * @param start the lower bound of the range of expando columns to return
1077             * @param end the upper bound of the range of expando columns to return (not inclusive)
1078             * @return the range of expando columns
1079             * @throws SystemException if a system exception occurred
1080             */
1081            public List<ExpandoColumn> findAll(int start, int end)
1082                    throws SystemException {
1083                    return findAll(start, end, null);
1084            }
1085    
1086            /**
1087             * Finds an ordered range of all the expando columns.
1088             *
1089             * <p>
1090             * 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.
1091             * </p>
1092             *
1093             * @param start the lower bound of the range of expando columns to return
1094             * @param end the upper bound of the range of expando columns to return (not inclusive)
1095             * @param orderByComparator the comparator to order the results by
1096             * @return the ordered range of expando columns
1097             * @throws SystemException if a system exception occurred
1098             */
1099            public List<ExpandoColumn> findAll(int start, int end,
1100                    OrderByComparator orderByComparator) throws SystemException {
1101                    Object[] finderArgs = new Object[] {
1102                                    String.valueOf(start), String.valueOf(end),
1103                                    String.valueOf(orderByComparator)
1104                            };
1105    
1106                    List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1107                                    finderArgs, this);
1108    
1109                    if (list == null) {
1110                            StringBundler query = null;
1111                            String sql = null;
1112    
1113                            if (orderByComparator != null) {
1114                                    query = new StringBundler(2 +
1115                                                    (orderByComparator.getOrderByFields().length * 3));
1116    
1117                                    query.append(_SQL_SELECT_EXPANDOCOLUMN);
1118    
1119                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1120                                            orderByComparator);
1121    
1122                                    sql = query.toString();
1123                            }
1124                            else {
1125                                    sql = _SQL_SELECT_EXPANDOCOLUMN.concat(ExpandoColumnModelImpl.ORDER_BY_JPQL);
1126                            }
1127    
1128                            Session session = null;
1129    
1130                            try {
1131                                    session = openSession();
1132    
1133                                    Query q = session.createQuery(sql);
1134    
1135                                    if (orderByComparator == null) {
1136                                            list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
1137                                                            start, end, false);
1138    
1139                                            Collections.sort(list);
1140                                    }
1141                                    else {
1142                                            list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
1143                                                            start, end);
1144                                    }
1145                            }
1146                            catch (Exception e) {
1147                                    throw processException(e);
1148                            }
1149                            finally {
1150                                    if (list == null) {
1151                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
1152                                                    finderArgs);
1153                                    }
1154                                    else {
1155                                            cacheResult(list);
1156    
1157                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
1158                                                    list);
1159                                    }
1160    
1161                                    closeSession(session);
1162                            }
1163                    }
1164    
1165                    return list;
1166            }
1167    
1168            /**
1169             * Removes all the expando columns where tableId = &#63; from the database.
1170             *
1171             * @param tableId the table id to search with
1172             * @throws SystemException if a system exception occurred
1173             */
1174            public void removeByTableId(long tableId) throws SystemException {
1175                    for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
1176                            remove(expandoColumn);
1177                    }
1178            }
1179    
1180            /**
1181             * Removes the expando column where tableId = &#63; and name = &#63; from the database.
1182             *
1183             * @param tableId the table id to search with
1184             * @param name the name to search with
1185             * @throws SystemException if a system exception occurred
1186             */
1187            public void removeByT_N(long tableId, String name)
1188                    throws NoSuchColumnException, SystemException {
1189                    ExpandoColumn expandoColumn = findByT_N(tableId, name);
1190    
1191                    remove(expandoColumn);
1192            }
1193    
1194            /**
1195             * Removes all the expando columns from the database.
1196             *
1197             * @throws SystemException if a system exception occurred
1198             */
1199            public void removeAll() throws SystemException {
1200                    for (ExpandoColumn expandoColumn : findAll()) {
1201                            remove(expandoColumn);
1202                    }
1203            }
1204    
1205            /**
1206             * Counts all the expando columns where tableId = &#63;.
1207             *
1208             * @param tableId the table id to search with
1209             * @return the number of matching expando columns
1210             * @throws SystemException if a system exception occurred
1211             */
1212            public int countByTableId(long tableId) throws SystemException {
1213                    Object[] finderArgs = new Object[] { tableId };
1214    
1215                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
1216                                    finderArgs, this);
1217    
1218                    if (count == null) {
1219                            StringBundler query = new StringBundler(2);
1220    
1221                            query.append(_SQL_COUNT_EXPANDOCOLUMN_WHERE);
1222    
1223                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
1224    
1225                            String sql = query.toString();
1226    
1227                            Session session = null;
1228    
1229                            try {
1230                                    session = openSession();
1231    
1232                                    Query q = session.createQuery(sql);
1233    
1234                                    QueryPos qPos = QueryPos.getInstance(q);
1235    
1236                                    qPos.add(tableId);
1237    
1238                                    count = (Long)q.uniqueResult();
1239                            }
1240                            catch (Exception e) {
1241                                    throw processException(e);
1242                            }
1243                            finally {
1244                                    if (count == null) {
1245                                            count = Long.valueOf(0);
1246                                    }
1247    
1248                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
1249                                            finderArgs, count);
1250    
1251                                    closeSession(session);
1252                            }
1253                    }
1254    
1255                    return count.intValue();
1256            }
1257    
1258            /**
1259             * Filters by the user's permissions and counts all the expando columns where tableId = &#63;.
1260             *
1261             * @param tableId the table id to search with
1262             * @return the number of matching expando columns that the user has permission to view
1263             * @throws SystemException if a system exception occurred
1264             */
1265            public int filterCountByTableId(long tableId) throws SystemException {
1266                    if (!InlineSQLHelperUtil.isEnabled()) {
1267                            return countByTableId(tableId);
1268                    }
1269    
1270                    StringBundler query = new StringBundler(2);
1271    
1272                    query.append(_FILTER_SQL_COUNT_EXPANDOCOLUMN_WHERE);
1273    
1274                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
1275    
1276                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
1277                                    ExpandoColumn.class.getName(), _FILTER_COLUMN_PK,
1278                                    _FILTER_COLUMN_USERID);
1279    
1280                    Session session = null;
1281    
1282                    try {
1283                            session = openSession();
1284    
1285                            SQLQuery q = session.createSQLQuery(sql);
1286    
1287                            q.addScalar(COUNT_COLUMN_NAME,
1288                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
1289    
1290                            QueryPos qPos = QueryPos.getInstance(q);
1291    
1292                            qPos.add(tableId);
1293    
1294                            Long count = (Long)q.uniqueResult();
1295    
1296                            return count.intValue();
1297                    }
1298                    catch (Exception e) {
1299                            throw processException(e);
1300                    }
1301                    finally {
1302                            closeSession(session);
1303                    }
1304            }
1305    
1306            /**
1307             * Counts all the expando columns where tableId = &#63; and name = &#63;.
1308             *
1309             * @param tableId the table id to search with
1310             * @param name the name to search with
1311             * @return the number of matching expando columns
1312             * @throws SystemException if a system exception occurred
1313             */
1314            public int countByT_N(long tableId, String name) throws SystemException {
1315                    Object[] finderArgs = new Object[] { tableId, name };
1316    
1317                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_N,
1318                                    finderArgs, this);
1319    
1320                    if (count == null) {
1321                            StringBundler query = new StringBundler(3);
1322    
1323                            query.append(_SQL_COUNT_EXPANDOCOLUMN_WHERE);
1324    
1325                            query.append(_FINDER_COLUMN_T_N_TABLEID_2);
1326    
1327                            if (name == null) {
1328                                    query.append(_FINDER_COLUMN_T_N_NAME_1);
1329                            }
1330                            else {
1331                                    if (name.equals(StringPool.BLANK)) {
1332                                            query.append(_FINDER_COLUMN_T_N_NAME_3);
1333                                    }
1334                                    else {
1335                                            query.append(_FINDER_COLUMN_T_N_NAME_2);
1336                                    }
1337                            }
1338    
1339                            String sql = query.toString();
1340    
1341                            Session session = null;
1342    
1343                            try {
1344                                    session = openSession();
1345    
1346                                    Query q = session.createQuery(sql);
1347    
1348                                    QueryPos qPos = QueryPos.getInstance(q);
1349    
1350                                    qPos.add(tableId);
1351    
1352                                    if (name != null) {
1353                                            qPos.add(name);
1354                                    }
1355    
1356                                    count = (Long)q.uniqueResult();
1357                            }
1358                            catch (Exception e) {
1359                                    throw processException(e);
1360                            }
1361                            finally {
1362                                    if (count == null) {
1363                                            count = Long.valueOf(0);
1364                                    }
1365    
1366                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_N, finderArgs,
1367                                            count);
1368    
1369                                    closeSession(session);
1370                            }
1371                    }
1372    
1373                    return count.intValue();
1374            }
1375    
1376            /**
1377             * Counts all the expando columns.
1378             *
1379             * @return the number of expando columns
1380             * @throws SystemException if a system exception occurred
1381             */
1382            public int countAll() throws SystemException {
1383                    Object[] finderArgs = new Object[0];
1384    
1385                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1386                                    finderArgs, this);
1387    
1388                    if (count == null) {
1389                            Session session = null;
1390    
1391                            try {
1392                                    session = openSession();
1393    
1394                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOCOLUMN);
1395    
1396                                    count = (Long)q.uniqueResult();
1397                            }
1398                            catch (Exception e) {
1399                                    throw processException(e);
1400                            }
1401                            finally {
1402                                    if (count == null) {
1403                                            count = Long.valueOf(0);
1404                                    }
1405    
1406                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1407                                            count);
1408    
1409                                    closeSession(session);
1410                            }
1411                    }
1412    
1413                    return count.intValue();
1414            }
1415    
1416            /**
1417             * Initializes the expando column persistence.
1418             */
1419            public void afterPropertiesSet() {
1420                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1421                                            com.liferay.portal.util.PropsUtil.get(
1422                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
1423    
1424                    if (listenerClassNames.length > 0) {
1425                            try {
1426                                    List<ModelListener<ExpandoColumn>> listenersList = new ArrayList<ModelListener<ExpandoColumn>>();
1427    
1428                                    for (String listenerClassName : listenerClassNames) {
1429                                            listenersList.add((ModelListener<ExpandoColumn>)InstanceFactory.newInstance(
1430                                                            listenerClassName));
1431                                    }
1432    
1433                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1434                            }
1435                            catch (Exception e) {
1436                                    _log.error(e);
1437                            }
1438                    }
1439            }
1440    
1441            public void destroy() {
1442                    EntityCacheUtil.removeCache(ExpandoColumnImpl.class.getName());
1443                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1444                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1445            }
1446    
1447            @BeanReference(type = ExpandoColumnPersistence.class)
1448            protected ExpandoColumnPersistence expandoColumnPersistence;
1449            @BeanReference(type = ExpandoRowPersistence.class)
1450            protected ExpandoRowPersistence expandoRowPersistence;
1451            @BeanReference(type = ExpandoTablePersistence.class)
1452            protected ExpandoTablePersistence expandoTablePersistence;
1453            @BeanReference(type = ExpandoValuePersistence.class)
1454            protected ExpandoValuePersistence expandoValuePersistence;
1455            @BeanReference(type = ResourcePersistence.class)
1456            protected ResourcePersistence resourcePersistence;
1457            @BeanReference(type = UserPersistence.class)
1458            protected UserPersistence userPersistence;
1459            private static final String _SQL_SELECT_EXPANDOCOLUMN = "SELECT expandoColumn FROM ExpandoColumn expandoColumn";
1460            private static final String _SQL_SELECT_EXPANDOCOLUMN_WHERE = "SELECT expandoColumn FROM ExpandoColumn expandoColumn WHERE ";
1461            private static final String _SQL_COUNT_EXPANDOCOLUMN = "SELECT COUNT(expandoColumn) FROM ExpandoColumn expandoColumn";
1462            private static final String _SQL_COUNT_EXPANDOCOLUMN_WHERE = "SELECT COUNT(expandoColumn) FROM ExpandoColumn expandoColumn WHERE ";
1463            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoColumn.tableId = ?";
1464            private static final String _FINDER_COLUMN_T_N_TABLEID_2 = "expandoColumn.tableId = ? AND ";
1465            private static final String _FINDER_COLUMN_T_N_NAME_1 = "expandoColumn.name IS NULL";
1466            private static final String _FINDER_COLUMN_T_N_NAME_2 = "expandoColumn.name = ?";
1467            private static final String _FINDER_COLUMN_T_N_NAME_3 = "(expandoColumn.name IS NULL OR expandoColumn.name = ?)";
1468            private static final String _FILTER_SQL_SELECT_EXPANDOCOLUMN_WHERE = "SELECT DISTINCT {expandoColumn.*} FROM ExpandoColumn expandoColumn WHERE ";
1469            private static final String _FILTER_SQL_SELECT_EXPANDOCOLUMN_NO_INLINE_DISTINCT_WHERE_1 =
1470                    "SELECT {ExpandoColumn.*} FROM (SELECT DISTINCT expandoColumn.columnId FROM ExpandoColumn expandoColumn WHERE ";
1471            private static final String _FILTER_SQL_SELECT_EXPANDOCOLUMN_NO_INLINE_DISTINCT_WHERE_2 =
1472                    ") TEMP_TABLE INNER JOIN ExpandoColumn ON TEMP_TABLE.columnId = ExpandoColumn.columnId";
1473            private static final String _FILTER_SQL_COUNT_EXPANDOCOLUMN_WHERE = "SELECT COUNT(DISTINCT expandoColumn.columnId) AS COUNT_VALUE FROM ExpandoColumn expandoColumn WHERE ";
1474            private static final String _FILTER_COLUMN_PK = "expandoColumn.columnId";
1475            private static final String _FILTER_COLUMN_USERID = null;
1476            private static final String _FILTER_ENTITY_ALIAS = "expandoColumn";
1477            private static final String _FILTER_ENTITY_TABLE = "ExpandoColumn";
1478            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoColumn.";
1479            private static final String _ORDER_BY_ENTITY_TABLE = "ExpandoColumn.";
1480            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoColumn exists with the primary key ";
1481            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoColumn exists with the key {";
1482            private static Log _log = LogFactoryUtil.getLog(ExpandoColumnPersistenceImpl.class);
1483    }