001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.model.CacheModel;
034    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
035    
036    import com.liferay.portlet.expando.NoSuchTableException;
037    import com.liferay.portlet.expando.model.ExpandoTable;
038    import com.liferay.portlet.expando.model.impl.ExpandoTableImpl;
039    import com.liferay.portlet.expando.model.impl.ExpandoTableModelImpl;
040    import com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence;
041    
042    import java.io.Serializable;
043    
044    import java.util.Collections;
045    import java.util.HashMap;
046    import java.util.HashSet;
047    import java.util.Iterator;
048    import java.util.List;
049    import java.util.Map;
050    import java.util.Set;
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    @ProviderType
065    public class ExpandoTablePersistenceImpl extends BasePersistenceImpl<ExpandoTable>
066            implements ExpandoTablePersistence {
067            /*
068             * NOTE FOR DEVELOPERS:
069             *
070             * 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.
071             */
072            public static final String FINDER_CLASS_NAME_ENTITY = ExpandoTableImpl.class.getName();
073            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List1";
075            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076                    ".List2";
077            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
078                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, ExpandoTableImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
081                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, ExpandoTableImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
084                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, Long.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
087                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, ExpandoTableImpl.class,
088                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C",
089                            new String[] {
090                                    Long.class.getName(), Long.class.getName(),
091                                    
092                            Integer.class.getName(), Integer.class.getName(),
093                                    OrderByComparator.class.getName()
094                            });
095            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
096                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, ExpandoTableImpl.class,
097                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C",
098                            new String[] { Long.class.getName(), Long.class.getName() },
099                            ExpandoTableModelImpl.COMPANYID_COLUMN_BITMASK |
100                            ExpandoTableModelImpl.CLASSNAMEID_COLUMN_BITMASK);
101            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
102                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, Long.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
104                            new String[] { Long.class.getName(), Long.class.getName() });
105    
106            /**
107             * Returns all the expando tables where companyId = &#63; and classNameId = &#63;.
108             *
109             * @param companyId the company ID
110             * @param classNameId the class name ID
111             * @return the matching expando tables
112             */
113            @Override
114            public List<ExpandoTable> findByC_C(long companyId, long classNameId) {
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             */
132            @Override
133            public List<ExpandoTable> findByC_C(long companyId, long classNameId,
134                    int start, int end) {
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             */
152            @Override
153            public List<ExpandoTable> findByC_C(long companyId, long classNameId,
154                    int start, int end, OrderByComparator<ExpandoTable> orderByComparator) {
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 = Collections.unmodifiableList(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             */
268            @Override
269            public ExpandoTable findByC_C_First(long companyId, long classNameId,
270                    OrderByComparator<ExpandoTable> orderByComparator)
271                    throws NoSuchTableException {
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             */
302            @Override
303            public ExpandoTable fetchByC_C_First(long companyId, long classNameId,
304                    OrderByComparator<ExpandoTable> orderByComparator) {
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             */
324            @Override
325            public ExpandoTable findByC_C_Last(long companyId, long classNameId,
326                    OrderByComparator<ExpandoTable> orderByComparator)
327                    throws NoSuchTableException {
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             */
358            @Override
359            public ExpandoTable fetchByC_C_Last(long companyId, long classNameId,
360                    OrderByComparator<ExpandoTable> orderByComparator) {
361                    int count = countByC_C(companyId, classNameId);
362    
363                    if (count == 0) {
364                            return null;
365                    }
366    
367                    List<ExpandoTable> list = findByC_C(companyId, classNameId, count - 1,
368                                    count, orderByComparator);
369    
370                    if (!list.isEmpty()) {
371                            return list.get(0);
372                    }
373    
374                    return null;
375            }
376    
377            /**
378             * Returns the expando tables before and after the current expando table in the ordered set where companyId = &#63; and classNameId = &#63;.
379             *
380             * @param tableId the primary key of the current expando table
381             * @param companyId the company ID
382             * @param classNameId the class name ID
383             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
384             * @return the previous, current, and next expando table
385             * @throws com.liferay.portlet.expando.NoSuchTableException if a expando table with the primary key could not be found
386             */
387            @Override
388            public ExpandoTable[] findByC_C_PrevAndNext(long tableId, long companyId,
389                    long classNameId, OrderByComparator<ExpandoTable> orderByComparator)
390                    throws NoSuchTableException {
391                    ExpandoTable expandoTable = findByPrimaryKey(tableId);
392    
393                    Session session = null;
394    
395                    try {
396                            session = openSession();
397    
398                            ExpandoTable[] array = new ExpandoTableImpl[3];
399    
400                            array[0] = getByC_C_PrevAndNext(session, expandoTable, companyId,
401                                            classNameId, orderByComparator, true);
402    
403                            array[1] = expandoTable;
404    
405                            array[2] = getByC_C_PrevAndNext(session, expandoTable, companyId,
406                                            classNameId, orderByComparator, false);
407    
408                            return array;
409                    }
410                    catch (Exception e) {
411                            throw processException(e);
412                    }
413                    finally {
414                            closeSession(session);
415                    }
416            }
417    
418            protected ExpandoTable getByC_C_PrevAndNext(Session session,
419                    ExpandoTable expandoTable, long companyId, long classNameId,
420                    OrderByComparator<ExpandoTable> orderByComparator, boolean previous) {
421                    StringBundler query = null;
422    
423                    if (orderByComparator != null) {
424                            query = new StringBundler(6 +
425                                            (orderByComparator.getOrderByFields().length * 6));
426                    }
427                    else {
428                            query = new StringBundler(3);
429                    }
430    
431                    query.append(_SQL_SELECT_EXPANDOTABLE_WHERE);
432    
433                    query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
434    
435                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
436    
437                    if (orderByComparator != null) {
438                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
439    
440                            if (orderByConditionFields.length > 0) {
441                                    query.append(WHERE_AND);
442                            }
443    
444                            for (int i = 0; i < orderByConditionFields.length; i++) {
445                                    query.append(_ORDER_BY_ENTITY_ALIAS);
446                                    query.append(orderByConditionFields[i]);
447    
448                                    if ((i + 1) < orderByConditionFields.length) {
449                                            if (orderByComparator.isAscending() ^ previous) {
450                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
451                                            }
452                                            else {
453                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
454                                            }
455                                    }
456                                    else {
457                                            if (orderByComparator.isAscending() ^ previous) {
458                                                    query.append(WHERE_GREATER_THAN);
459                                            }
460                                            else {
461                                                    query.append(WHERE_LESSER_THAN);
462                                            }
463                                    }
464                            }
465    
466                            query.append(ORDER_BY_CLAUSE);
467    
468                            String[] orderByFields = orderByComparator.getOrderByFields();
469    
470                            for (int i = 0; i < orderByFields.length; i++) {
471                                    query.append(_ORDER_BY_ENTITY_ALIAS);
472                                    query.append(orderByFields[i]);
473    
474                                    if ((i + 1) < orderByFields.length) {
475                                            if (orderByComparator.isAscending() ^ previous) {
476                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
477                                            }
478                                            else {
479                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
480                                            }
481                                    }
482                                    else {
483                                            if (orderByComparator.isAscending() ^ previous) {
484                                                    query.append(ORDER_BY_ASC);
485                                            }
486                                            else {
487                                                    query.append(ORDER_BY_DESC);
488                                            }
489                                    }
490                            }
491                    }
492                    else {
493                            query.append(ExpandoTableModelImpl.ORDER_BY_JPQL);
494                    }
495    
496                    String sql = query.toString();
497    
498                    Query q = session.createQuery(sql);
499    
500                    q.setFirstResult(0);
501                    q.setMaxResults(2);
502    
503                    QueryPos qPos = QueryPos.getInstance(q);
504    
505                    qPos.add(companyId);
506    
507                    qPos.add(classNameId);
508    
509                    if (orderByComparator != null) {
510                            Object[] values = orderByComparator.getOrderByConditionValues(expandoTable);
511    
512                            for (Object value : values) {
513                                    qPos.add(value);
514                            }
515                    }
516    
517                    List<ExpandoTable> list = q.list();
518    
519                    if (list.size() == 2) {
520                            return list.get(1);
521                    }
522                    else {
523                            return null;
524                    }
525            }
526    
527            /**
528             * Removes all the expando tables where companyId = &#63; and classNameId = &#63; from the database.
529             *
530             * @param companyId the company ID
531             * @param classNameId the class name ID
532             */
533            @Override
534            public void removeByC_C(long companyId, long classNameId) {
535                    for (ExpandoTable expandoTable : findByC_C(companyId, classNameId,
536                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
537                            remove(expandoTable);
538                    }
539            }
540    
541            /**
542             * Returns the number of expando tables where companyId = &#63; and classNameId = &#63;.
543             *
544             * @param companyId the company ID
545             * @param classNameId the class name ID
546             * @return the number of matching expando tables
547             */
548            @Override
549            public int countByC_C(long companyId, long classNameId) {
550                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
551    
552                    Object[] finderArgs = new Object[] { companyId, classNameId };
553    
554                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
555                                    this);
556    
557                    if (count == null) {
558                            StringBundler query = new StringBundler(3);
559    
560                            query.append(_SQL_COUNT_EXPANDOTABLE_WHERE);
561    
562                            query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
563    
564                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
565    
566                            String sql = query.toString();
567    
568                            Session session = null;
569    
570                            try {
571                                    session = openSession();
572    
573                                    Query q = session.createQuery(sql);
574    
575                                    QueryPos qPos = QueryPos.getInstance(q);
576    
577                                    qPos.add(companyId);
578    
579                                    qPos.add(classNameId);
580    
581                                    count = (Long)q.uniqueResult();
582    
583                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
584                            }
585                            catch (Exception e) {
586                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
587    
588                                    throw processException(e);
589                            }
590                            finally {
591                                    closeSession(session);
592                            }
593                    }
594    
595                    return count.intValue();
596            }
597    
598            private static final String _FINDER_COLUMN_C_C_COMPANYID_2 = "expandoTable.companyId = ? AND ";
599            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "expandoTable.classNameId = ?";
600            public static final FinderPath FINDER_PATH_FETCH_BY_C_C_N = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
601                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, ExpandoTableImpl.class,
602                            FINDER_CLASS_NAME_ENTITY, "fetchByC_C_N",
603                            new String[] {
604                                    Long.class.getName(), Long.class.getName(),
605                                    String.class.getName()
606                            },
607                            ExpandoTableModelImpl.COMPANYID_COLUMN_BITMASK |
608                            ExpandoTableModelImpl.CLASSNAMEID_COLUMN_BITMASK |
609                            ExpandoTableModelImpl.NAME_COLUMN_BITMASK);
610            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_N = new FinderPath(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
611                            ExpandoTableModelImpl.FINDER_CACHE_ENABLED, Long.class,
612                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_N",
613                            new String[] {
614                                    Long.class.getName(), Long.class.getName(),
615                                    String.class.getName()
616                            });
617    
618            /**
619             * 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.
620             *
621             * @param companyId the company ID
622             * @param classNameId the class name ID
623             * @param name the name
624             * @return the matching expando table
625             * @throws com.liferay.portlet.expando.NoSuchTableException if a matching expando table could not be found
626             */
627            @Override
628            public ExpandoTable findByC_C_N(long companyId, long classNameId,
629                    String name) throws NoSuchTableException {
630                    ExpandoTable expandoTable = fetchByC_C_N(companyId, classNameId, name);
631    
632                    if (expandoTable == null) {
633                            StringBundler msg = new StringBundler(8);
634    
635                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
636    
637                            msg.append("companyId=");
638                            msg.append(companyId);
639    
640                            msg.append(", classNameId=");
641                            msg.append(classNameId);
642    
643                            msg.append(", name=");
644                            msg.append(name);
645    
646                            msg.append(StringPool.CLOSE_CURLY_BRACE);
647    
648                            if (_log.isWarnEnabled()) {
649                                    _log.warn(msg.toString());
650                            }
651    
652                            throw new NoSuchTableException(msg.toString());
653                    }
654    
655                    return expandoTable;
656            }
657    
658            /**
659             * 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.
660             *
661             * @param companyId the company ID
662             * @param classNameId the class name ID
663             * @param name the name
664             * @return the matching expando table, or <code>null</code> if a matching expando table could not be found
665             */
666            @Override
667            public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
668                    String name) {
669                    return fetchByC_C_N(companyId, classNameId, name, true);
670            }
671    
672            /**
673             * 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.
674             *
675             * @param companyId the company ID
676             * @param classNameId the class name ID
677             * @param name the name
678             * @param retrieveFromCache whether to use the finder cache
679             * @return the matching expando table, or <code>null</code> if a matching expando table could not be found
680             */
681            @Override
682            public ExpandoTable fetchByC_C_N(long companyId, long classNameId,
683                    String name, boolean retrieveFromCache) {
684                    Object[] finderArgs = new Object[] { companyId, classNameId, name };
685    
686                    Object result = null;
687    
688                    if (retrieveFromCache) {
689                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C_N,
690                                            finderArgs, this);
691                    }
692    
693                    if (result instanceof ExpandoTable) {
694                            ExpandoTable expandoTable = (ExpandoTable)result;
695    
696                            if ((companyId != expandoTable.getCompanyId()) ||
697                                            (classNameId != expandoTable.getClassNameId()) ||
698                                            !Validator.equals(name, expandoTable.getName())) {
699                                    result = null;
700                            }
701                    }
702    
703                    if (result == null) {
704                            StringBundler query = new StringBundler(5);
705    
706                            query.append(_SQL_SELECT_EXPANDOTABLE_WHERE);
707    
708                            query.append(_FINDER_COLUMN_C_C_N_COMPANYID_2);
709    
710                            query.append(_FINDER_COLUMN_C_C_N_CLASSNAMEID_2);
711    
712                            boolean bindName = false;
713    
714                            if (name == null) {
715                                    query.append(_FINDER_COLUMN_C_C_N_NAME_1);
716                            }
717                            else if (name.equals(StringPool.BLANK)) {
718                                    query.append(_FINDER_COLUMN_C_C_N_NAME_3);
719                            }
720                            else {
721                                    bindName = true;
722    
723                                    query.append(_FINDER_COLUMN_C_C_N_NAME_2);
724                            }
725    
726                            String sql = query.toString();
727    
728                            Session session = null;
729    
730                            try {
731                                    session = openSession();
732    
733                                    Query q = session.createQuery(sql);
734    
735                                    QueryPos qPos = QueryPos.getInstance(q);
736    
737                                    qPos.add(companyId);
738    
739                                    qPos.add(classNameId);
740    
741                                    if (bindName) {
742                                            qPos.add(name);
743                                    }
744    
745                                    List<ExpandoTable> list = q.list();
746    
747                                    if (list.isEmpty()) {
748                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
749                                                    finderArgs, list);
750                                    }
751                                    else {
752                                            ExpandoTable expandoTable = list.get(0);
753    
754                                            result = expandoTable;
755    
756                                            cacheResult(expandoTable);
757    
758                                            if ((expandoTable.getCompanyId() != companyId) ||
759                                                            (expandoTable.getClassNameId() != classNameId) ||
760                                                            (expandoTable.getName() == null) ||
761                                                            !expandoTable.getName().equals(name)) {
762                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
763                                                            finderArgs, expandoTable);
764                                            }
765                                    }
766                            }
767                            catch (Exception e) {
768                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N,
769                                            finderArgs);
770    
771                                    throw processException(e);
772                            }
773                            finally {
774                                    closeSession(session);
775                            }
776                    }
777    
778                    if (result instanceof List<?>) {
779                            return null;
780                    }
781                    else {
782                            return (ExpandoTable)result;
783                    }
784            }
785    
786            /**
787             * Removes the expando table where companyId = &#63; and classNameId = &#63; and name = &#63; from the database.
788             *
789             * @param companyId the company ID
790             * @param classNameId the class name ID
791             * @param name the name
792             * @return the expando table that was removed
793             */
794            @Override
795            public ExpandoTable removeByC_C_N(long companyId, long classNameId,
796                    String name) throws NoSuchTableException {
797                    ExpandoTable expandoTable = findByC_C_N(companyId, classNameId, name);
798    
799                    return remove(expandoTable);
800            }
801    
802            /**
803             * Returns the number of expando tables where companyId = &#63; and classNameId = &#63; and name = &#63;.
804             *
805             * @param companyId the company ID
806             * @param classNameId the class name ID
807             * @param name the name
808             * @return the number of matching expando tables
809             */
810            @Override
811            public int countByC_C_N(long companyId, long classNameId, String name) {
812                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_N;
813    
814                    Object[] finderArgs = new Object[] { companyId, classNameId, name };
815    
816                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
817                                    this);
818    
819                    if (count == null) {
820                            StringBundler query = new StringBundler(4);
821    
822                            query.append(_SQL_COUNT_EXPANDOTABLE_WHERE);
823    
824                            query.append(_FINDER_COLUMN_C_C_N_COMPANYID_2);
825    
826                            query.append(_FINDER_COLUMN_C_C_N_CLASSNAMEID_2);
827    
828                            boolean bindName = false;
829    
830                            if (name == null) {
831                                    query.append(_FINDER_COLUMN_C_C_N_NAME_1);
832                            }
833                            else if (name.equals(StringPool.BLANK)) {
834                                    query.append(_FINDER_COLUMN_C_C_N_NAME_3);
835                            }
836                            else {
837                                    bindName = true;
838    
839                                    query.append(_FINDER_COLUMN_C_C_N_NAME_2);
840                            }
841    
842                            String sql = query.toString();
843    
844                            Session session = null;
845    
846                            try {
847                                    session = openSession();
848    
849                                    Query q = session.createQuery(sql);
850    
851                                    QueryPos qPos = QueryPos.getInstance(q);
852    
853                                    qPos.add(companyId);
854    
855                                    qPos.add(classNameId);
856    
857                                    if (bindName) {
858                                            qPos.add(name);
859                                    }
860    
861                                    count = (Long)q.uniqueResult();
862    
863                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
864                            }
865                            catch (Exception e) {
866                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
867    
868                                    throw processException(e);
869                            }
870                            finally {
871                                    closeSession(session);
872                            }
873                    }
874    
875                    return count.intValue();
876            }
877    
878            private static final String _FINDER_COLUMN_C_C_N_COMPANYID_2 = "expandoTable.companyId = ? AND ";
879            private static final String _FINDER_COLUMN_C_C_N_CLASSNAMEID_2 = "expandoTable.classNameId = ? AND ";
880            private static final String _FINDER_COLUMN_C_C_N_NAME_1 = "expandoTable.name IS NULL";
881            private static final String _FINDER_COLUMN_C_C_N_NAME_2 = "expandoTable.name = ?";
882            private static final String _FINDER_COLUMN_C_C_N_NAME_3 = "(expandoTable.name IS NULL OR expandoTable.name = '')";
883    
884            public ExpandoTablePersistenceImpl() {
885                    setModelClass(ExpandoTable.class);
886            }
887    
888            /**
889             * Caches the expando table in the entity cache if it is enabled.
890             *
891             * @param expandoTable the expando table
892             */
893            @Override
894            public void cacheResult(ExpandoTable expandoTable) {
895                    EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
896                            ExpandoTableImpl.class, expandoTable.getPrimaryKey(), expandoTable);
897    
898                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N,
899                            new Object[] {
900                                    expandoTable.getCompanyId(), expandoTable.getClassNameId(),
901                                    expandoTable.getName()
902                            }, expandoTable);
903    
904                    expandoTable.resetOriginalValues();
905            }
906    
907            /**
908             * Caches the expando tables in the entity cache if it is enabled.
909             *
910             * @param expandoTables the expando tables
911             */
912            @Override
913            public void cacheResult(List<ExpandoTable> expandoTables) {
914                    for (ExpandoTable expandoTable : expandoTables) {
915                            if (EntityCacheUtil.getResult(
916                                                    ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
917                                                    ExpandoTableImpl.class, expandoTable.getPrimaryKey()) == null) {
918                                    cacheResult(expandoTable);
919                            }
920                            else {
921                                    expandoTable.resetOriginalValues();
922                            }
923                    }
924            }
925    
926            /**
927             * Clears the cache for all expando tables.
928             *
929             * <p>
930             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
931             * </p>
932             */
933            @Override
934            public void clearCache() {
935                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
936                            CacheRegistryUtil.clear(ExpandoTableImpl.class.getName());
937                    }
938    
939                    EntityCacheUtil.clearCache(ExpandoTableImpl.class);
940    
941                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
942                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
943                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
944            }
945    
946            /**
947             * Clears the cache for the expando table.
948             *
949             * <p>
950             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
951             * </p>
952             */
953            @Override
954            public void clearCache(ExpandoTable expandoTable) {
955                    EntityCacheUtil.removeResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
956                            ExpandoTableImpl.class, expandoTable.getPrimaryKey());
957    
958                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
959                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
960    
961                    clearUniqueFindersCache(expandoTable);
962            }
963    
964            @Override
965            public void clearCache(List<ExpandoTable> expandoTables) {
966                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
967                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
968    
969                    for (ExpandoTable expandoTable : expandoTables) {
970                            EntityCacheUtil.removeResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
971                                    ExpandoTableImpl.class, expandoTable.getPrimaryKey());
972    
973                            clearUniqueFindersCache(expandoTable);
974                    }
975            }
976    
977            protected void cacheUniqueFindersCache(ExpandoTable expandoTable) {
978                    if (expandoTable.isNew()) {
979                            Object[] args = new Object[] {
980                                            expandoTable.getCompanyId(), expandoTable.getClassNameId(),
981                                            expandoTable.getName()
982                                    };
983    
984                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C_N, args,
985                                    Long.valueOf(1));
986                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N, args,
987                                    expandoTable);
988                    }
989                    else {
990                            ExpandoTableModelImpl expandoTableModelImpl = (ExpandoTableModelImpl)expandoTable;
991    
992                            if ((expandoTableModelImpl.getColumnBitmask() &
993                                            FINDER_PATH_FETCH_BY_C_C_N.getColumnBitmask()) != 0) {
994                                    Object[] args = new Object[] {
995                                                    expandoTable.getCompanyId(),
996                                                    expandoTable.getClassNameId(), expandoTable.getName()
997                                            };
998    
999                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C_N, args,
1000                                            Long.valueOf(1));
1001                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C_N, args,
1002                                            expandoTable);
1003                            }
1004                    }
1005            }
1006    
1007            protected void clearUniqueFindersCache(ExpandoTable expandoTable) {
1008                    ExpandoTableModelImpl expandoTableModelImpl = (ExpandoTableModelImpl)expandoTable;
1009    
1010                    Object[] args = new Object[] {
1011                                    expandoTable.getCompanyId(), expandoTable.getClassNameId(),
1012                                    expandoTable.getName()
1013                            };
1014    
1015                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_N, args);
1016                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N, args);
1017    
1018                    if ((expandoTableModelImpl.getColumnBitmask() &
1019                                    FINDER_PATH_FETCH_BY_C_C_N.getColumnBitmask()) != 0) {
1020                            args = new Object[] {
1021                                            expandoTableModelImpl.getOriginalCompanyId(),
1022                                            expandoTableModelImpl.getOriginalClassNameId(),
1023                                            expandoTableModelImpl.getOriginalName()
1024                                    };
1025    
1026                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C_N, args);
1027                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C_N, args);
1028                    }
1029            }
1030    
1031            /**
1032             * Creates a new expando table with the primary key. Does not add the expando table to the database.
1033             *
1034             * @param tableId the primary key for the new expando table
1035             * @return the new expando table
1036             */
1037            @Override
1038            public ExpandoTable create(long tableId) {
1039                    ExpandoTable expandoTable = new ExpandoTableImpl();
1040    
1041                    expandoTable.setNew(true);
1042                    expandoTable.setPrimaryKey(tableId);
1043    
1044                    return expandoTable;
1045            }
1046    
1047            /**
1048             * Removes the expando table with the primary key from the database. Also notifies the appropriate model listeners.
1049             *
1050             * @param tableId the primary key of the expando table
1051             * @return the expando table that was removed
1052             * @throws com.liferay.portlet.expando.NoSuchTableException if a expando table with the primary key could not be found
1053             */
1054            @Override
1055            public ExpandoTable remove(long tableId) throws NoSuchTableException {
1056                    return remove((Serializable)tableId);
1057            }
1058    
1059            /**
1060             * Removes the expando table with the primary key from the database. Also notifies the appropriate model listeners.
1061             *
1062             * @param primaryKey the primary key of the expando table
1063             * @return the expando table that was removed
1064             * @throws com.liferay.portlet.expando.NoSuchTableException if a expando table with the primary key could not be found
1065             */
1066            @Override
1067            public ExpandoTable remove(Serializable primaryKey)
1068                    throws NoSuchTableException {
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                    expandoTable = toUnwrappedModel(expandoTable);
1102    
1103                    Session session = null;
1104    
1105                    try {
1106                            session = openSession();
1107    
1108                            if (!session.contains(expandoTable)) {
1109                                    expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
1110                                                    expandoTable.getPrimaryKeyObj());
1111                            }
1112    
1113                            if (expandoTable != null) {
1114                                    session.delete(expandoTable);
1115                            }
1116                    }
1117                    catch (Exception e) {
1118                            throw processException(e);
1119                    }
1120                    finally {
1121                            closeSession(session);
1122                    }
1123    
1124                    if (expandoTable != null) {
1125                            clearCache(expandoTable);
1126                    }
1127    
1128                    return expandoTable;
1129            }
1130    
1131            @Override
1132            public ExpandoTable updateImpl(
1133                    com.liferay.portlet.expando.model.ExpandoTable expandoTable) {
1134                    expandoTable = toUnwrappedModel(expandoTable);
1135    
1136                    boolean isNew = expandoTable.isNew();
1137    
1138                    ExpandoTableModelImpl expandoTableModelImpl = (ExpandoTableModelImpl)expandoTable;
1139    
1140                    Session session = null;
1141    
1142                    try {
1143                            session = openSession();
1144    
1145                            if (expandoTable.isNew()) {
1146                                    session.save(expandoTable);
1147    
1148                                    expandoTable.setNew(false);
1149                            }
1150                            else {
1151                                    session.merge(expandoTable);
1152                            }
1153                    }
1154                    catch (Exception e) {
1155                            throw processException(e);
1156                    }
1157                    finally {
1158                            closeSession(session);
1159                    }
1160    
1161                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1162    
1163                    if (isNew || !ExpandoTableModelImpl.COLUMN_BITMASK_ENABLED) {
1164                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1165                    }
1166    
1167                    else {
1168                            if ((expandoTableModelImpl.getColumnBitmask() &
1169                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
1170                                    Object[] args = new Object[] {
1171                                                    expandoTableModelImpl.getOriginalCompanyId(),
1172                                                    expandoTableModelImpl.getOriginalClassNameId()
1173                                            };
1174    
1175                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1176                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1177                                            args);
1178    
1179                                    args = new Object[] {
1180                                                    expandoTableModelImpl.getCompanyId(),
1181                                                    expandoTableModelImpl.getClassNameId()
1182                                            };
1183    
1184                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
1185                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
1186                                            args);
1187                            }
1188                    }
1189    
1190                    EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1191                            ExpandoTableImpl.class, expandoTable.getPrimaryKey(), expandoTable,
1192                            false);
1193    
1194                    clearUniqueFindersCache(expandoTable);
1195                    cacheUniqueFindersCache(expandoTable);
1196    
1197                    expandoTable.resetOriginalValues();
1198    
1199                    return expandoTable;
1200            }
1201    
1202            protected ExpandoTable toUnwrappedModel(ExpandoTable expandoTable) {
1203                    if (expandoTable instanceof ExpandoTableImpl) {
1204                            return expandoTable;
1205                    }
1206    
1207                    ExpandoTableImpl expandoTableImpl = new ExpandoTableImpl();
1208    
1209                    expandoTableImpl.setNew(expandoTable.isNew());
1210                    expandoTableImpl.setPrimaryKey(expandoTable.getPrimaryKey());
1211    
1212                    expandoTableImpl.setTableId(expandoTable.getTableId());
1213                    expandoTableImpl.setCompanyId(expandoTable.getCompanyId());
1214                    expandoTableImpl.setClassNameId(expandoTable.getClassNameId());
1215                    expandoTableImpl.setName(expandoTable.getName());
1216    
1217                    return expandoTableImpl;
1218            }
1219    
1220            /**
1221             * Returns the expando table with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1222             *
1223             * @param primaryKey the primary key of the expando table
1224             * @return the expando table
1225             * @throws com.liferay.portlet.expando.NoSuchTableException if a expando table with the primary key could not be found
1226             */
1227            @Override
1228            public ExpandoTable findByPrimaryKey(Serializable primaryKey)
1229                    throws NoSuchTableException {
1230                    ExpandoTable expandoTable = fetchByPrimaryKey(primaryKey);
1231    
1232                    if (expandoTable == null) {
1233                            if (_log.isWarnEnabled()) {
1234                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1235                            }
1236    
1237                            throw new NoSuchTableException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1238                                    primaryKey);
1239                    }
1240    
1241                    return expandoTable;
1242            }
1243    
1244            /**
1245             * Returns the expando table with the primary key or throws a {@link com.liferay.portlet.expando.NoSuchTableException} if it could not be found.
1246             *
1247             * @param tableId the primary key of the expando table
1248             * @return the expando table
1249             * @throws com.liferay.portlet.expando.NoSuchTableException if a expando table with the primary key could not be found
1250             */
1251            @Override
1252            public ExpandoTable findByPrimaryKey(long tableId)
1253                    throws NoSuchTableException {
1254                    return findByPrimaryKey((Serializable)tableId);
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             */
1263            @Override
1264            public ExpandoTable fetchByPrimaryKey(Serializable primaryKey) {
1265                    ExpandoTable expandoTable = (ExpandoTable)EntityCacheUtil.getResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1266                                    ExpandoTableImpl.class, primaryKey);
1267    
1268                    if (expandoTable == _nullExpandoTable) {
1269                            return null;
1270                    }
1271    
1272                    if (expandoTable == null) {
1273                            Session session = null;
1274    
1275                            try {
1276                                    session = openSession();
1277    
1278                                    expandoTable = (ExpandoTable)session.get(ExpandoTableImpl.class,
1279                                                    primaryKey);
1280    
1281                                    if (expandoTable != null) {
1282                                            cacheResult(expandoTable);
1283                                    }
1284                                    else {
1285                                            EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1286                                                    ExpandoTableImpl.class, primaryKey, _nullExpandoTable);
1287                                    }
1288                            }
1289                            catch (Exception e) {
1290                                    EntityCacheUtil.removeResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1291                                            ExpandoTableImpl.class, primaryKey);
1292    
1293                                    throw processException(e);
1294                            }
1295                            finally {
1296                                    closeSession(session);
1297                            }
1298                    }
1299    
1300                    return expandoTable;
1301            }
1302    
1303            /**
1304             * Returns the expando table with the primary key or returns <code>null</code> if it could not be found.
1305             *
1306             * @param tableId the primary key of the expando table
1307             * @return the expando table, or <code>null</code> if a expando table with the primary key could not be found
1308             */
1309            @Override
1310            public ExpandoTable fetchByPrimaryKey(long tableId) {
1311                    return fetchByPrimaryKey((Serializable)tableId);
1312            }
1313    
1314            @Override
1315            public Map<Serializable, ExpandoTable> fetchByPrimaryKeys(
1316                    Set<Serializable> primaryKeys) {
1317                    if (primaryKeys.isEmpty()) {
1318                            return Collections.emptyMap();
1319                    }
1320    
1321                    Map<Serializable, ExpandoTable> map = new HashMap<Serializable, ExpandoTable>();
1322    
1323                    if (primaryKeys.size() == 1) {
1324                            Iterator<Serializable> iterator = primaryKeys.iterator();
1325    
1326                            Serializable primaryKey = iterator.next();
1327    
1328                            ExpandoTable expandoTable = fetchByPrimaryKey(primaryKey);
1329    
1330                            if (expandoTable != null) {
1331                                    map.put(primaryKey, expandoTable);
1332                            }
1333    
1334                            return map;
1335                    }
1336    
1337                    Set<Serializable> uncachedPrimaryKeys = null;
1338    
1339                    for (Serializable primaryKey : primaryKeys) {
1340                            ExpandoTable expandoTable = (ExpandoTable)EntityCacheUtil.getResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1341                                            ExpandoTableImpl.class, primaryKey);
1342    
1343                            if (expandoTable == null) {
1344                                    if (uncachedPrimaryKeys == null) {
1345                                            uncachedPrimaryKeys = new HashSet<Serializable>();
1346                                    }
1347    
1348                                    uncachedPrimaryKeys.add(primaryKey);
1349                            }
1350                            else {
1351                                    map.put(primaryKey, expandoTable);
1352                            }
1353                    }
1354    
1355                    if (uncachedPrimaryKeys == null) {
1356                            return map;
1357                    }
1358    
1359                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1360                                    1);
1361    
1362                    query.append(_SQL_SELECT_EXPANDOTABLE_WHERE_PKS_IN);
1363    
1364                    for (Serializable primaryKey : uncachedPrimaryKeys) {
1365                            query.append(String.valueOf(primaryKey));
1366    
1367                            query.append(StringPool.COMMA);
1368                    }
1369    
1370                    query.setIndex(query.index() - 1);
1371    
1372                    query.append(StringPool.CLOSE_PARENTHESIS);
1373    
1374                    String sql = query.toString();
1375    
1376                    Session session = null;
1377    
1378                    try {
1379                            session = openSession();
1380    
1381                            Query q = session.createQuery(sql);
1382    
1383                            for (ExpandoTable expandoTable : (List<ExpandoTable>)q.list()) {
1384                                    map.put(expandoTable.getPrimaryKeyObj(), expandoTable);
1385    
1386                                    cacheResult(expandoTable);
1387    
1388                                    uncachedPrimaryKeys.remove(expandoTable.getPrimaryKeyObj());
1389                            }
1390    
1391                            for (Serializable primaryKey : uncachedPrimaryKeys) {
1392                                    EntityCacheUtil.putResult(ExpandoTableModelImpl.ENTITY_CACHE_ENABLED,
1393                                            ExpandoTableImpl.class, primaryKey, _nullExpandoTable);
1394                            }
1395                    }
1396                    catch (Exception e) {
1397                            throw processException(e);
1398                    }
1399                    finally {
1400                            closeSession(session);
1401                    }
1402    
1403                    return map;
1404            }
1405    
1406            /**
1407             * Returns all the expando tables.
1408             *
1409             * @return the expando tables
1410             */
1411            @Override
1412            public List<ExpandoTable> findAll() {
1413                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1414            }
1415    
1416            /**
1417             * Returns a range of all the expando tables.
1418             *
1419             * <p>
1420             * 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.
1421             * </p>
1422             *
1423             * @param start the lower bound of the range of expando tables
1424             * @param end the upper bound of the range of expando tables (not inclusive)
1425             * @return the range of expando tables
1426             */
1427            @Override
1428            public List<ExpandoTable> findAll(int start, int end) {
1429                    return findAll(start, end, null);
1430            }
1431    
1432            /**
1433             * Returns an ordered range of all the expando tables.
1434             *
1435             * <p>
1436             * 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.
1437             * </p>
1438             *
1439             * @param start the lower bound of the range of expando tables
1440             * @param end the upper bound of the range of expando tables (not inclusive)
1441             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1442             * @return the ordered range of expando tables
1443             */
1444            @Override
1445            public List<ExpandoTable> findAll(int start, int end,
1446                    OrderByComparator<ExpandoTable> orderByComparator) {
1447                    boolean pagination = true;
1448                    FinderPath finderPath = null;
1449                    Object[] finderArgs = null;
1450    
1451                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1452                                    (orderByComparator == null)) {
1453                            pagination = false;
1454                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1455                            finderArgs = FINDER_ARGS_EMPTY;
1456                    }
1457                    else {
1458                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1459                            finderArgs = new Object[] { start, end, orderByComparator };
1460                    }
1461    
1462                    List<ExpandoTable> list = (List<ExpandoTable>)FinderCacheUtil.getResult(finderPath,
1463                                    finderArgs, this);
1464    
1465                    if (list == null) {
1466                            StringBundler query = null;
1467                            String sql = null;
1468    
1469                            if (orderByComparator != null) {
1470                                    query = new StringBundler(2 +
1471                                                    (orderByComparator.getOrderByFields().length * 3));
1472    
1473                                    query.append(_SQL_SELECT_EXPANDOTABLE);
1474    
1475                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1476                                            orderByComparator);
1477    
1478                                    sql = query.toString();
1479                            }
1480                            else {
1481                                    sql = _SQL_SELECT_EXPANDOTABLE;
1482    
1483                                    if (pagination) {
1484                                            sql = sql.concat(ExpandoTableModelImpl.ORDER_BY_JPQL);
1485                                    }
1486                            }
1487    
1488                            Session session = null;
1489    
1490                            try {
1491                                    session = openSession();
1492    
1493                                    Query q = session.createQuery(sql);
1494    
1495                                    if (!pagination) {
1496                                            list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
1497                                                            start, end, false);
1498    
1499                                            Collections.sort(list);
1500    
1501                                            list = Collections.unmodifiableList(list);
1502                                    }
1503                                    else {
1504                                            list = (List<ExpandoTable>)QueryUtil.list(q, getDialect(),
1505                                                            start, end);
1506                                    }
1507    
1508                                    cacheResult(list);
1509    
1510                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1511                            }
1512                            catch (Exception e) {
1513                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1514    
1515                                    throw processException(e);
1516                            }
1517                            finally {
1518                                    closeSession(session);
1519                            }
1520                    }
1521    
1522                    return list;
1523            }
1524    
1525            /**
1526             * Removes all the expando tables from the database.
1527             *
1528             */
1529            @Override
1530            public void removeAll() {
1531                    for (ExpandoTable expandoTable : findAll()) {
1532                            remove(expandoTable);
1533                    }
1534            }
1535    
1536            /**
1537             * Returns the number of expando tables.
1538             *
1539             * @return the number of expando tables
1540             */
1541            @Override
1542            public int countAll() {
1543                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1544                                    FINDER_ARGS_EMPTY, this);
1545    
1546                    if (count == null) {
1547                            Session session = null;
1548    
1549                            try {
1550                                    session = openSession();
1551    
1552                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOTABLE);
1553    
1554                                    count = (Long)q.uniqueResult();
1555    
1556                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1557                                            FINDER_ARGS_EMPTY, count);
1558                            }
1559                            catch (Exception e) {
1560                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1561                                            FINDER_ARGS_EMPTY);
1562    
1563                                    throw processException(e);
1564                            }
1565                            finally {
1566                                    closeSession(session);
1567                            }
1568                    }
1569    
1570                    return count.intValue();
1571            }
1572    
1573            /**
1574             * Initializes the expando table persistence.
1575             */
1576            public void afterPropertiesSet() {
1577            }
1578    
1579            public void destroy() {
1580                    EntityCacheUtil.removeCache(ExpandoTableImpl.class.getName());
1581                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1582                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1583                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1584            }
1585    
1586            private static final String _SQL_SELECT_EXPANDOTABLE = "SELECT expandoTable FROM ExpandoTable expandoTable";
1587            private static final String _SQL_SELECT_EXPANDOTABLE_WHERE_PKS_IN = "SELECT expandoTable FROM ExpandoTable expandoTable WHERE tableId IN (";
1588            private static final String _SQL_SELECT_EXPANDOTABLE_WHERE = "SELECT expandoTable FROM ExpandoTable expandoTable WHERE ";
1589            private static final String _SQL_COUNT_EXPANDOTABLE = "SELECT COUNT(expandoTable) FROM ExpandoTable expandoTable";
1590            private static final String _SQL_COUNT_EXPANDOTABLE_WHERE = "SELECT COUNT(expandoTable) FROM ExpandoTable expandoTable WHERE ";
1591            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoTable.";
1592            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoTable exists with the primary key ";
1593            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoTable exists with the key {";
1594            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1595            private static final Log _log = LogFactoryUtil.getLog(ExpandoTablePersistenceImpl.class);
1596            private static final ExpandoTable _nullExpandoTable = new ExpandoTableImpl() {
1597                            @Override
1598                            public Object clone() {
1599                                    return this;
1600                            }
1601    
1602                            @Override
1603                            public CacheModel<ExpandoTable> toCacheModel() {
1604                                    return _nullExpandoTableCacheModel;
1605                            }
1606                    };
1607    
1608            private static final CacheModel<ExpandoTable> _nullExpandoTableCacheModel = new CacheModel<ExpandoTable>() {
1609                            @Override
1610                            public ExpandoTable toEntityModel() {
1611                                    return _nullExpandoTable;
1612                            }
1613                    };
1614    }