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