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