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