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