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