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.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchPortletPreferencesException;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
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.model.MVCCModel;
036    import com.liferay.portal.model.PortletPreferences;
037    import com.liferay.portal.model.impl.PortletPreferencesImpl;
038    import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
039    import com.liferay.portal.service.persistence.PortletPreferencesPersistence;
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 portlet preferences 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 PortletPreferencesPersistence
060     * @see PortletPreferencesUtil
061     * @generated
062     */
063    @ProviderType
064    public class PortletPreferencesPersistenceImpl extends BasePersistenceImpl<PortletPreferences>
065            implements PortletPreferencesPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * Never modify or reference this class directly. Always use {@link PortletPreferencesUtil} to access the portlet preferences persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = PortletPreferencesImpl.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(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
077                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
078                            PortletPreferencesImpl.class,
079                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
080            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
081                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
082                            PortletPreferencesImpl.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
084            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
085                            PortletPreferencesModelImpl.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_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
088                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
089                            PortletPreferencesImpl.class,
090                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPlid",
091                            new String[] {
092                                    Long.class.getName(),
093                                    
094                            Integer.class.getName(), Integer.class.getName(),
095                                    OrderByComparator.class.getName()
096                            });
097            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
098                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
099                            PortletPreferencesImpl.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPlid",
101                            new String[] { Long.class.getName() },
102                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK);
103            public static final FinderPath FINDER_PATH_COUNT_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
104                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
105                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPlid",
106                            new String[] { Long.class.getName() });
107    
108            /**
109             * Returns all the portlet preferenceses where plid = &#63;.
110             *
111             * @param plid the plid
112             * @return the matching portlet preferenceses
113             */
114            @Override
115            public List<PortletPreferences> findByPlid(long plid) {
116                    return findByPlid(plid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
117            }
118    
119            /**
120             * Returns a range of all the portlet preferenceses where plid = &#63;.
121             *
122             * <p>
123             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
124             * </p>
125             *
126             * @param plid the plid
127             * @param start the lower bound of the range of portlet preferenceses
128             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
129             * @return the range of matching portlet preferenceses
130             */
131            @Override
132            public List<PortletPreferences> findByPlid(long plid, int start, int end) {
133                    return findByPlid(plid, start, end, null);
134            }
135    
136            /**
137             * Returns an ordered range of all the portlet preferenceses where plid = &#63;.
138             *
139             * <p>
140             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
141             * </p>
142             *
143             * @param plid the plid
144             * @param start the lower bound of the range of portlet preferenceses
145             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
146             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
147             * @return the ordered range of matching portlet preferenceses
148             */
149            @Override
150            public List<PortletPreferences> findByPlid(long plid, int start, int end,
151                    OrderByComparator<PortletPreferences> orderByComparator) {
152                    boolean pagination = true;
153                    FinderPath finderPath = null;
154                    Object[] finderArgs = null;
155    
156                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
157                                    (orderByComparator == null)) {
158                            pagination = false;
159                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID;
160                            finderArgs = new Object[] { plid };
161                    }
162                    else {
163                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PLID;
164                            finderArgs = new Object[] { plid, start, end, orderByComparator };
165                    }
166    
167                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
168                                    finderArgs, this);
169    
170                    if ((list != null) && !list.isEmpty()) {
171                            for (PortletPreferences portletPreferences : list) {
172                                    if ((plid != portletPreferences.getPlid())) {
173                                            list = null;
174    
175                                            break;
176                                    }
177                            }
178                    }
179    
180                    if (list == null) {
181                            StringBundler query = null;
182    
183                            if (orderByComparator != null) {
184                                    query = new StringBundler(3 +
185                                                    (orderByComparator.getOrderByFields().length * 3));
186                            }
187                            else {
188                                    query = new StringBundler(3);
189                            }
190    
191                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
192    
193                            query.append(_FINDER_COLUMN_PLID_PLID_2);
194    
195                            if (orderByComparator != null) {
196                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
197                                            orderByComparator);
198                            }
199                            else
200                             if (pagination) {
201                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
202                            }
203    
204                            String sql = query.toString();
205    
206                            Session session = null;
207    
208                            try {
209                                    session = openSession();
210    
211                                    Query q = session.createQuery(sql);
212    
213                                    QueryPos qPos = QueryPos.getInstance(q);
214    
215                                    qPos.add(plid);
216    
217                                    if (!pagination) {
218                                            list = (List<PortletPreferences>)QueryUtil.list(q,
219                                                            getDialect(), start, end, false);
220    
221                                            Collections.sort(list);
222    
223                                            list = Collections.unmodifiableList(list);
224                                    }
225                                    else {
226                                            list = (List<PortletPreferences>)QueryUtil.list(q,
227                                                            getDialect(), start, end);
228                                    }
229    
230                                    cacheResult(list);
231    
232                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
233                            }
234                            catch (Exception e) {
235                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
236    
237                                    throw processException(e);
238                            }
239                            finally {
240                                    closeSession(session);
241                            }
242                    }
243    
244                    return list;
245            }
246    
247            /**
248             * Returns the first portlet preferences in the ordered set where plid = &#63;.
249             *
250             * @param plid the plid
251             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
252             * @return the first matching portlet preferences
253             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
254             */
255            @Override
256            public PortletPreferences findByPlid_First(long plid,
257                    OrderByComparator<PortletPreferences> orderByComparator)
258                    throws NoSuchPortletPreferencesException {
259                    PortletPreferences portletPreferences = fetchByPlid_First(plid,
260                                    orderByComparator);
261    
262                    if (portletPreferences != null) {
263                            return portletPreferences;
264                    }
265    
266                    StringBundler msg = new StringBundler(4);
267    
268                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
269    
270                    msg.append("plid=");
271                    msg.append(plid);
272    
273                    msg.append(StringPool.CLOSE_CURLY_BRACE);
274    
275                    throw new NoSuchPortletPreferencesException(msg.toString());
276            }
277    
278            /**
279             * Returns the first portlet preferences in the ordered set where plid = &#63;.
280             *
281             * @param plid the plid
282             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
283             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
284             */
285            @Override
286            public PortletPreferences fetchByPlid_First(long plid,
287                    OrderByComparator<PortletPreferences> orderByComparator) {
288                    List<PortletPreferences> list = findByPlid(plid, 0, 1, orderByComparator);
289    
290                    if (!list.isEmpty()) {
291                            return list.get(0);
292                    }
293    
294                    return null;
295            }
296    
297            /**
298             * Returns the last portlet preferences in the ordered set where plid = &#63;.
299             *
300             * @param plid the plid
301             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
302             * @return the last matching portlet preferences
303             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
304             */
305            @Override
306            public PortletPreferences findByPlid_Last(long plid,
307                    OrderByComparator<PortletPreferences> orderByComparator)
308                    throws NoSuchPortletPreferencesException {
309                    PortletPreferences portletPreferences = fetchByPlid_Last(plid,
310                                    orderByComparator);
311    
312                    if (portletPreferences != null) {
313                            return portletPreferences;
314                    }
315    
316                    StringBundler msg = new StringBundler(4);
317    
318                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
319    
320                    msg.append("plid=");
321                    msg.append(plid);
322    
323                    msg.append(StringPool.CLOSE_CURLY_BRACE);
324    
325                    throw new NoSuchPortletPreferencesException(msg.toString());
326            }
327    
328            /**
329             * Returns the last portlet preferences in the ordered set where plid = &#63;.
330             *
331             * @param plid the plid
332             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
333             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
334             */
335            @Override
336            public PortletPreferences fetchByPlid_Last(long plid,
337                    OrderByComparator<PortletPreferences> orderByComparator) {
338                    int count = countByPlid(plid);
339    
340                    if (count == 0) {
341                            return null;
342                    }
343    
344                    List<PortletPreferences> list = findByPlid(plid, count - 1, count,
345                                    orderByComparator);
346    
347                    if (!list.isEmpty()) {
348                            return list.get(0);
349                    }
350    
351                    return null;
352            }
353    
354            /**
355             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where plid = &#63;.
356             *
357             * @param portletPreferencesId the primary key of the current portlet preferences
358             * @param plid the plid
359             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
360             * @return the previous, current, and next portlet preferences
361             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
362             */
363            @Override
364            public PortletPreferences[] findByPlid_PrevAndNext(
365                    long portletPreferencesId, long plid,
366                    OrderByComparator<PortletPreferences> orderByComparator)
367                    throws NoSuchPortletPreferencesException {
368                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
369    
370                    Session session = null;
371    
372                    try {
373                            session = openSession();
374    
375                            PortletPreferences[] array = new PortletPreferencesImpl[3];
376    
377                            array[0] = getByPlid_PrevAndNext(session, portletPreferences, plid,
378                                            orderByComparator, true);
379    
380                            array[1] = portletPreferences;
381    
382                            array[2] = getByPlid_PrevAndNext(session, portletPreferences, plid,
383                                            orderByComparator, false);
384    
385                            return array;
386                    }
387                    catch (Exception e) {
388                            throw processException(e);
389                    }
390                    finally {
391                            closeSession(session);
392                    }
393            }
394    
395            protected PortletPreferences getByPlid_PrevAndNext(Session session,
396                    PortletPreferences portletPreferences, long plid,
397                    OrderByComparator<PortletPreferences> orderByComparator,
398                    boolean previous) {
399                    StringBundler query = null;
400    
401                    if (orderByComparator != null) {
402                            query = new StringBundler(6 +
403                                            (orderByComparator.getOrderByFields().length * 6));
404                    }
405                    else {
406                            query = new StringBundler(3);
407                    }
408    
409                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
410    
411                    query.append(_FINDER_COLUMN_PLID_PLID_2);
412    
413                    if (orderByComparator != null) {
414                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
415    
416                            if (orderByConditionFields.length > 0) {
417                                    query.append(WHERE_AND);
418                            }
419    
420                            for (int i = 0; i < orderByConditionFields.length; i++) {
421                                    query.append(_ORDER_BY_ENTITY_ALIAS);
422                                    query.append(orderByConditionFields[i]);
423    
424                                    if ((i + 1) < orderByConditionFields.length) {
425                                            if (orderByComparator.isAscending() ^ previous) {
426                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
427                                            }
428                                            else {
429                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
430                                            }
431                                    }
432                                    else {
433                                            if (orderByComparator.isAscending() ^ previous) {
434                                                    query.append(WHERE_GREATER_THAN);
435                                            }
436                                            else {
437                                                    query.append(WHERE_LESSER_THAN);
438                                            }
439                                    }
440                            }
441    
442                            query.append(ORDER_BY_CLAUSE);
443    
444                            String[] orderByFields = orderByComparator.getOrderByFields();
445    
446                            for (int i = 0; i < orderByFields.length; i++) {
447                                    query.append(_ORDER_BY_ENTITY_ALIAS);
448                                    query.append(orderByFields[i]);
449    
450                                    if ((i + 1) < orderByFields.length) {
451                                            if (orderByComparator.isAscending() ^ previous) {
452                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
453                                            }
454                                            else {
455                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
456                                            }
457                                    }
458                                    else {
459                                            if (orderByComparator.isAscending() ^ previous) {
460                                                    query.append(ORDER_BY_ASC);
461                                            }
462                                            else {
463                                                    query.append(ORDER_BY_DESC);
464                                            }
465                                    }
466                            }
467                    }
468                    else {
469                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
470                    }
471    
472                    String sql = query.toString();
473    
474                    Query q = session.createQuery(sql);
475    
476                    q.setFirstResult(0);
477                    q.setMaxResults(2);
478    
479                    QueryPos qPos = QueryPos.getInstance(q);
480    
481                    qPos.add(plid);
482    
483                    if (orderByComparator != null) {
484                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
485    
486                            for (Object value : values) {
487                                    qPos.add(value);
488                            }
489                    }
490    
491                    List<PortletPreferences> list = q.list();
492    
493                    if (list.size() == 2) {
494                            return list.get(1);
495                    }
496                    else {
497                            return null;
498                    }
499            }
500    
501            /**
502             * Removes all the portlet preferenceses where plid = &#63; from the database.
503             *
504             * @param plid the plid
505             */
506            @Override
507            public void removeByPlid(long plid) {
508                    for (PortletPreferences portletPreferences : findByPlid(plid,
509                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
510                            remove(portletPreferences);
511                    }
512            }
513    
514            /**
515             * Returns the number of portlet preferenceses where plid = &#63;.
516             *
517             * @param plid the plid
518             * @return the number of matching portlet preferenceses
519             */
520            @Override
521            public int countByPlid(long plid) {
522                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PLID;
523    
524                    Object[] finderArgs = new Object[] { plid };
525    
526                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
527                                    this);
528    
529                    if (count == null) {
530                            StringBundler query = new StringBundler(2);
531    
532                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
533    
534                            query.append(_FINDER_COLUMN_PLID_PLID_2);
535    
536                            String sql = query.toString();
537    
538                            Session session = null;
539    
540                            try {
541                                    session = openSession();
542    
543                                    Query q = session.createQuery(sql);
544    
545                                    QueryPos qPos = QueryPos.getInstance(q);
546    
547                                    qPos.add(plid);
548    
549                                    count = (Long)q.uniqueResult();
550    
551                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
552                            }
553                            catch (Exception e) {
554                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
555    
556                                    throw processException(e);
557                            }
558                            finally {
559                                    closeSession(session);
560                            }
561                    }
562    
563                    return count.intValue();
564            }
565    
566            private static final String _FINDER_COLUMN_PLID_PLID_2 = "portletPreferences.plid = ?";
567            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PORTLETID =
568                    new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
569                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
570                            PortletPreferencesImpl.class,
571                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPortletId",
572                            new String[] {
573                                    String.class.getName(),
574                                    
575                            Integer.class.getName(), Integer.class.getName(),
576                                    OrderByComparator.class.getName()
577                            });
578            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID =
579                    new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
580                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
581                            PortletPreferencesImpl.class,
582                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPortletId",
583                            new String[] { String.class.getName() },
584                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
585            public static final FinderPath FINDER_PATH_COUNT_BY_PORTLETID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
586                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
587                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPortletId",
588                            new String[] { String.class.getName() });
589    
590            /**
591             * Returns all the portlet preferenceses where portletId = &#63;.
592             *
593             * @param portletId the portlet ID
594             * @return the matching portlet preferenceses
595             */
596            @Override
597            public List<PortletPreferences> findByPortletId(String portletId) {
598                    return findByPortletId(portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
599                            null);
600            }
601    
602            /**
603             * Returns a range of all the portlet preferenceses where portletId = &#63;.
604             *
605             * <p>
606             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
607             * </p>
608             *
609             * @param portletId the portlet ID
610             * @param start the lower bound of the range of portlet preferenceses
611             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
612             * @return the range of matching portlet preferenceses
613             */
614            @Override
615            public List<PortletPreferences> findByPortletId(String portletId,
616                    int start, int end) {
617                    return findByPortletId(portletId, start, end, null);
618            }
619    
620            /**
621             * Returns an ordered range of all the portlet preferenceses where portletId = &#63;.
622             *
623             * <p>
624             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
625             * </p>
626             *
627             * @param portletId the portlet ID
628             * @param start the lower bound of the range of portlet preferenceses
629             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
630             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
631             * @return the ordered range of matching portlet preferenceses
632             */
633            @Override
634            public List<PortletPreferences> findByPortletId(String portletId,
635                    int start, int end,
636                    OrderByComparator<PortletPreferences> orderByComparator) {
637                    boolean pagination = true;
638                    FinderPath finderPath = null;
639                    Object[] finderArgs = null;
640    
641                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
642                                    (orderByComparator == null)) {
643                            pagination = false;
644                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID;
645                            finderArgs = new Object[] { portletId };
646                    }
647                    else {
648                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PORTLETID;
649                            finderArgs = new Object[] { portletId, start, end, orderByComparator };
650                    }
651    
652                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
653                                    finderArgs, this);
654    
655                    if ((list != null) && !list.isEmpty()) {
656                            for (PortletPreferences portletPreferences : list) {
657                                    if (!Validator.equals(portletId,
658                                                            portletPreferences.getPortletId())) {
659                                            list = null;
660    
661                                            break;
662                                    }
663                            }
664                    }
665    
666                    if (list == null) {
667                            StringBundler query = null;
668    
669                            if (orderByComparator != null) {
670                                    query = new StringBundler(3 +
671                                                    (orderByComparator.getOrderByFields().length * 3));
672                            }
673                            else {
674                                    query = new StringBundler(3);
675                            }
676    
677                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
678    
679                            boolean bindPortletId = false;
680    
681                            if (portletId == null) {
682                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
683                            }
684                            else if (portletId.equals(StringPool.BLANK)) {
685                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
686                            }
687                            else {
688                                    bindPortletId = true;
689    
690                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
691                            }
692    
693                            if (orderByComparator != null) {
694                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
695                                            orderByComparator);
696                            }
697                            else
698                             if (pagination) {
699                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
700                            }
701    
702                            String sql = query.toString();
703    
704                            Session session = null;
705    
706                            try {
707                                    session = openSession();
708    
709                                    Query q = session.createQuery(sql);
710    
711                                    QueryPos qPos = QueryPos.getInstance(q);
712    
713                                    if (bindPortletId) {
714                                            qPos.add(portletId);
715                                    }
716    
717                                    if (!pagination) {
718                                            list = (List<PortletPreferences>)QueryUtil.list(q,
719                                                            getDialect(), start, end, false);
720    
721                                            Collections.sort(list);
722    
723                                            list = Collections.unmodifiableList(list);
724                                    }
725                                    else {
726                                            list = (List<PortletPreferences>)QueryUtil.list(q,
727                                                            getDialect(), start, end);
728                                    }
729    
730                                    cacheResult(list);
731    
732                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
733                            }
734                            catch (Exception e) {
735                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
736    
737                                    throw processException(e);
738                            }
739                            finally {
740                                    closeSession(session);
741                            }
742                    }
743    
744                    return list;
745            }
746    
747            /**
748             * Returns the first portlet preferences in the ordered set where portletId = &#63;.
749             *
750             * @param portletId the portlet ID
751             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
752             * @return the first matching portlet preferences
753             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
754             */
755            @Override
756            public PortletPreferences findByPortletId_First(String portletId,
757                    OrderByComparator<PortletPreferences> orderByComparator)
758                    throws NoSuchPortletPreferencesException {
759                    PortletPreferences portletPreferences = fetchByPortletId_First(portletId,
760                                    orderByComparator);
761    
762                    if (portletPreferences != null) {
763                            return portletPreferences;
764                    }
765    
766                    StringBundler msg = new StringBundler(4);
767    
768                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
769    
770                    msg.append("portletId=");
771                    msg.append(portletId);
772    
773                    msg.append(StringPool.CLOSE_CURLY_BRACE);
774    
775                    throw new NoSuchPortletPreferencesException(msg.toString());
776            }
777    
778            /**
779             * Returns the first portlet preferences in the ordered set where portletId = &#63;.
780             *
781             * @param portletId the portlet ID
782             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
783             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
784             */
785            @Override
786            public PortletPreferences fetchByPortletId_First(String portletId,
787                    OrderByComparator<PortletPreferences> orderByComparator) {
788                    List<PortletPreferences> list = findByPortletId(portletId, 0, 1,
789                                    orderByComparator);
790    
791                    if (!list.isEmpty()) {
792                            return list.get(0);
793                    }
794    
795                    return null;
796            }
797    
798            /**
799             * Returns the last portlet preferences in the ordered set where portletId = &#63;.
800             *
801             * @param portletId the portlet ID
802             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
803             * @return the last matching portlet preferences
804             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
805             */
806            @Override
807            public PortletPreferences findByPortletId_Last(String portletId,
808                    OrderByComparator<PortletPreferences> orderByComparator)
809                    throws NoSuchPortletPreferencesException {
810                    PortletPreferences portletPreferences = fetchByPortletId_Last(portletId,
811                                    orderByComparator);
812    
813                    if (portletPreferences != null) {
814                            return portletPreferences;
815                    }
816    
817                    StringBundler msg = new StringBundler(4);
818    
819                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
820    
821                    msg.append("portletId=");
822                    msg.append(portletId);
823    
824                    msg.append(StringPool.CLOSE_CURLY_BRACE);
825    
826                    throw new NoSuchPortletPreferencesException(msg.toString());
827            }
828    
829            /**
830             * Returns the last portlet preferences in the ordered set where portletId = &#63;.
831             *
832             * @param portletId the portlet ID
833             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
834             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
835             */
836            @Override
837            public PortletPreferences fetchByPortletId_Last(String portletId,
838                    OrderByComparator<PortletPreferences> orderByComparator) {
839                    int count = countByPortletId(portletId);
840    
841                    if (count == 0) {
842                            return null;
843                    }
844    
845                    List<PortletPreferences> list = findByPortletId(portletId, count - 1,
846                                    count, orderByComparator);
847    
848                    if (!list.isEmpty()) {
849                            return list.get(0);
850                    }
851    
852                    return null;
853            }
854    
855            /**
856             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where portletId = &#63;.
857             *
858             * @param portletPreferencesId the primary key of the current portlet preferences
859             * @param portletId the portlet ID
860             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
861             * @return the previous, current, and next portlet preferences
862             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
863             */
864            @Override
865            public PortletPreferences[] findByPortletId_PrevAndNext(
866                    long portletPreferencesId, String portletId,
867                    OrderByComparator<PortletPreferences> orderByComparator)
868                    throws NoSuchPortletPreferencesException {
869                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
870    
871                    Session session = null;
872    
873                    try {
874                            session = openSession();
875    
876                            PortletPreferences[] array = new PortletPreferencesImpl[3];
877    
878                            array[0] = getByPortletId_PrevAndNext(session, portletPreferences,
879                                            portletId, orderByComparator, true);
880    
881                            array[1] = portletPreferences;
882    
883                            array[2] = getByPortletId_PrevAndNext(session, portletPreferences,
884                                            portletId, orderByComparator, false);
885    
886                            return array;
887                    }
888                    catch (Exception e) {
889                            throw processException(e);
890                    }
891                    finally {
892                            closeSession(session);
893                    }
894            }
895    
896            protected PortletPreferences getByPortletId_PrevAndNext(Session session,
897                    PortletPreferences portletPreferences, String portletId,
898                    OrderByComparator<PortletPreferences> orderByComparator,
899                    boolean previous) {
900                    StringBundler query = null;
901    
902                    if (orderByComparator != null) {
903                            query = new StringBundler(6 +
904                                            (orderByComparator.getOrderByFields().length * 6));
905                    }
906                    else {
907                            query = new StringBundler(3);
908                    }
909    
910                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
911    
912                    boolean bindPortletId = false;
913    
914                    if (portletId == null) {
915                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
916                    }
917                    else if (portletId.equals(StringPool.BLANK)) {
918                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
919                    }
920                    else {
921                            bindPortletId = true;
922    
923                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
924                    }
925    
926                    if (orderByComparator != null) {
927                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
928    
929                            if (orderByConditionFields.length > 0) {
930                                    query.append(WHERE_AND);
931                            }
932    
933                            for (int i = 0; i < orderByConditionFields.length; i++) {
934                                    query.append(_ORDER_BY_ENTITY_ALIAS);
935                                    query.append(orderByConditionFields[i]);
936    
937                                    if ((i + 1) < orderByConditionFields.length) {
938                                            if (orderByComparator.isAscending() ^ previous) {
939                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
940                                            }
941                                            else {
942                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
943                                            }
944                                    }
945                                    else {
946                                            if (orderByComparator.isAscending() ^ previous) {
947                                                    query.append(WHERE_GREATER_THAN);
948                                            }
949                                            else {
950                                                    query.append(WHERE_LESSER_THAN);
951                                            }
952                                    }
953                            }
954    
955                            query.append(ORDER_BY_CLAUSE);
956    
957                            String[] orderByFields = orderByComparator.getOrderByFields();
958    
959                            for (int i = 0; i < orderByFields.length; i++) {
960                                    query.append(_ORDER_BY_ENTITY_ALIAS);
961                                    query.append(orderByFields[i]);
962    
963                                    if ((i + 1) < orderByFields.length) {
964                                            if (orderByComparator.isAscending() ^ previous) {
965                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
966                                            }
967                                            else {
968                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
969                                            }
970                                    }
971                                    else {
972                                            if (orderByComparator.isAscending() ^ previous) {
973                                                    query.append(ORDER_BY_ASC);
974                                            }
975                                            else {
976                                                    query.append(ORDER_BY_DESC);
977                                            }
978                                    }
979                            }
980                    }
981                    else {
982                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
983                    }
984    
985                    String sql = query.toString();
986    
987                    Query q = session.createQuery(sql);
988    
989                    q.setFirstResult(0);
990                    q.setMaxResults(2);
991    
992                    QueryPos qPos = QueryPos.getInstance(q);
993    
994                    if (bindPortletId) {
995                            qPos.add(portletId);
996                    }
997    
998                    if (orderByComparator != null) {
999                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
1000    
1001                            for (Object value : values) {
1002                                    qPos.add(value);
1003                            }
1004                    }
1005    
1006                    List<PortletPreferences> list = q.list();
1007    
1008                    if (list.size() == 2) {
1009                            return list.get(1);
1010                    }
1011                    else {
1012                            return null;
1013                    }
1014            }
1015    
1016            /**
1017             * Removes all the portlet preferenceses where portletId = &#63; from the database.
1018             *
1019             * @param portletId the portlet ID
1020             */
1021            @Override
1022            public void removeByPortletId(String portletId) {
1023                    for (PortletPreferences portletPreferences : findByPortletId(
1024                                    portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1025                            remove(portletPreferences);
1026                    }
1027            }
1028    
1029            /**
1030             * Returns the number of portlet preferenceses where portletId = &#63;.
1031             *
1032             * @param portletId the portlet ID
1033             * @return the number of matching portlet preferenceses
1034             */
1035            @Override
1036            public int countByPortletId(String portletId) {
1037                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PORTLETID;
1038    
1039                    Object[] finderArgs = new Object[] { portletId };
1040    
1041                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1042                                    this);
1043    
1044                    if (count == null) {
1045                            StringBundler query = new StringBundler(2);
1046    
1047                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1048    
1049                            boolean bindPortletId = false;
1050    
1051                            if (portletId == null) {
1052                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
1053                            }
1054                            else if (portletId.equals(StringPool.BLANK)) {
1055                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
1056                            }
1057                            else {
1058                                    bindPortletId = true;
1059    
1060                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
1061                            }
1062    
1063                            String sql = query.toString();
1064    
1065                            Session session = null;
1066    
1067                            try {
1068                                    session = openSession();
1069    
1070                                    Query q = session.createQuery(sql);
1071    
1072                                    QueryPos qPos = QueryPos.getInstance(q);
1073    
1074                                    if (bindPortletId) {
1075                                            qPos.add(portletId);
1076                                    }
1077    
1078                                    count = (Long)q.uniqueResult();
1079    
1080                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1081                            }
1082                            catch (Exception e) {
1083                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1084    
1085                                    throw processException(e);
1086                            }
1087                            finally {
1088                                    closeSession(session);
1089                            }
1090                    }
1091    
1092                    return count.intValue();
1093            }
1094    
1095            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1096            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_2 = "portletPreferences.portletId = ?";
1097            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
1098            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1099                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1100                            PortletPreferencesImpl.class,
1101                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_P",
1102                            new String[] {
1103                                    Integer.class.getName(), String.class.getName(),
1104                                    
1105                            Integer.class.getName(), Integer.class.getName(),
1106                                    OrderByComparator.class.getName()
1107                            });
1108            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1109                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1110                            PortletPreferencesImpl.class,
1111                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_P",
1112                            new String[] { Integer.class.getName(), String.class.getName() },
1113                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
1114                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
1115            public static final FinderPath FINDER_PATH_COUNT_BY_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1116                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
1117                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_P",
1118                            new String[] { Integer.class.getName(), String.class.getName() });
1119    
1120            /**
1121             * Returns all the portlet preferenceses where ownerType = &#63; and portletId = &#63;.
1122             *
1123             * @param ownerType the owner type
1124             * @param portletId the portlet ID
1125             * @return the matching portlet preferenceses
1126             */
1127            @Override
1128            public List<PortletPreferences> findByO_P(int ownerType, String portletId) {
1129                    return findByO_P(ownerType, portletId, QueryUtil.ALL_POS,
1130                            QueryUtil.ALL_POS, null);
1131            }
1132    
1133            /**
1134             * Returns a range of all the portlet preferenceses where ownerType = &#63; and portletId = &#63;.
1135             *
1136             * <p>
1137             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
1138             * </p>
1139             *
1140             * @param ownerType the owner type
1141             * @param portletId the portlet ID
1142             * @param start the lower bound of the range of portlet preferenceses
1143             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1144             * @return the range of matching portlet preferenceses
1145             */
1146            @Override
1147            public List<PortletPreferences> findByO_P(int ownerType, String portletId,
1148                    int start, int end) {
1149                    return findByO_P(ownerType, portletId, start, end, null);
1150            }
1151    
1152            /**
1153             * Returns an ordered range of all the portlet preferenceses where ownerType = &#63; and portletId = &#63;.
1154             *
1155             * <p>
1156             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
1157             * </p>
1158             *
1159             * @param ownerType the owner type
1160             * @param portletId the portlet ID
1161             * @param start the lower bound of the range of portlet preferenceses
1162             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1163             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1164             * @return the ordered range of matching portlet preferenceses
1165             */
1166            @Override
1167            public List<PortletPreferences> findByO_P(int ownerType, String portletId,
1168                    int start, int end,
1169                    OrderByComparator<PortletPreferences> orderByComparator) {
1170                    boolean pagination = true;
1171                    FinderPath finderPath = null;
1172                    Object[] finderArgs = null;
1173    
1174                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1175                                    (orderByComparator == null)) {
1176                            pagination = false;
1177                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P;
1178                            finderArgs = new Object[] { ownerType, portletId };
1179                    }
1180                    else {
1181                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P;
1182                            finderArgs = new Object[] {
1183                                            ownerType, portletId,
1184                                            
1185                                            start, end, orderByComparator
1186                                    };
1187                    }
1188    
1189                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
1190                                    finderArgs, this);
1191    
1192                    if ((list != null) && !list.isEmpty()) {
1193                            for (PortletPreferences portletPreferences : list) {
1194                                    if ((ownerType != portletPreferences.getOwnerType()) ||
1195                                                    !Validator.equals(portletId,
1196                                                            portletPreferences.getPortletId())) {
1197                                            list = null;
1198    
1199                                            break;
1200                                    }
1201                            }
1202                    }
1203    
1204                    if (list == null) {
1205                            StringBundler query = null;
1206    
1207                            if (orderByComparator != null) {
1208                                    query = new StringBundler(4 +
1209                                                    (orderByComparator.getOrderByFields().length * 3));
1210                            }
1211                            else {
1212                                    query = new StringBundler(4);
1213                            }
1214    
1215                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1216    
1217                            query.append(_FINDER_COLUMN_O_P_OWNERTYPE_2);
1218    
1219                            boolean bindPortletId = false;
1220    
1221                            if (portletId == null) {
1222                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_1);
1223                            }
1224                            else if (portletId.equals(StringPool.BLANK)) {
1225                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_3);
1226                            }
1227                            else {
1228                                    bindPortletId = true;
1229    
1230                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_2);
1231                            }
1232    
1233                            if (orderByComparator != null) {
1234                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1235                                            orderByComparator);
1236                            }
1237                            else
1238                             if (pagination) {
1239                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1240                            }
1241    
1242                            String sql = query.toString();
1243    
1244                            Session session = null;
1245    
1246                            try {
1247                                    session = openSession();
1248    
1249                                    Query q = session.createQuery(sql);
1250    
1251                                    QueryPos qPos = QueryPos.getInstance(q);
1252    
1253                                    qPos.add(ownerType);
1254    
1255                                    if (bindPortletId) {
1256                                            qPos.add(portletId);
1257                                    }
1258    
1259                                    if (!pagination) {
1260                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1261                                                            getDialect(), start, end, false);
1262    
1263                                            Collections.sort(list);
1264    
1265                                            list = Collections.unmodifiableList(list);
1266                                    }
1267                                    else {
1268                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1269                                                            getDialect(), start, end);
1270                                    }
1271    
1272                                    cacheResult(list);
1273    
1274                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1275                            }
1276                            catch (Exception e) {
1277                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1278    
1279                                    throw processException(e);
1280                            }
1281                            finally {
1282                                    closeSession(session);
1283                            }
1284                    }
1285    
1286                    return list;
1287            }
1288    
1289            /**
1290             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and portletId = &#63;.
1291             *
1292             * @param ownerType the owner type
1293             * @param portletId the portlet ID
1294             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1295             * @return the first matching portlet preferences
1296             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1297             */
1298            @Override
1299            public PortletPreferences findByO_P_First(int ownerType, String portletId,
1300                    OrderByComparator<PortletPreferences> orderByComparator)
1301                    throws NoSuchPortletPreferencesException {
1302                    PortletPreferences portletPreferences = fetchByO_P_First(ownerType,
1303                                    portletId, orderByComparator);
1304    
1305                    if (portletPreferences != null) {
1306                            return portletPreferences;
1307                    }
1308    
1309                    StringBundler msg = new StringBundler(6);
1310    
1311                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1312    
1313                    msg.append("ownerType=");
1314                    msg.append(ownerType);
1315    
1316                    msg.append(", portletId=");
1317                    msg.append(portletId);
1318    
1319                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1320    
1321                    throw new NoSuchPortletPreferencesException(msg.toString());
1322            }
1323    
1324            /**
1325             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and portletId = &#63;.
1326             *
1327             * @param ownerType the owner type
1328             * @param portletId the portlet ID
1329             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1330             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1331             */
1332            @Override
1333            public PortletPreferences fetchByO_P_First(int ownerType, String portletId,
1334                    OrderByComparator<PortletPreferences> orderByComparator) {
1335                    List<PortletPreferences> list = findByO_P(ownerType, portletId, 0, 1,
1336                                    orderByComparator);
1337    
1338                    if (!list.isEmpty()) {
1339                            return list.get(0);
1340                    }
1341    
1342                    return null;
1343            }
1344    
1345            /**
1346             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and portletId = &#63;.
1347             *
1348             * @param ownerType the owner type
1349             * @param portletId the portlet ID
1350             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1351             * @return the last matching portlet preferences
1352             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1353             */
1354            @Override
1355            public PortletPreferences findByO_P_Last(int ownerType, String portletId,
1356                    OrderByComparator<PortletPreferences> orderByComparator)
1357                    throws NoSuchPortletPreferencesException {
1358                    PortletPreferences portletPreferences = fetchByO_P_Last(ownerType,
1359                                    portletId, orderByComparator);
1360    
1361                    if (portletPreferences != null) {
1362                            return portletPreferences;
1363                    }
1364    
1365                    StringBundler msg = new StringBundler(6);
1366    
1367                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1368    
1369                    msg.append("ownerType=");
1370                    msg.append(ownerType);
1371    
1372                    msg.append(", portletId=");
1373                    msg.append(portletId);
1374    
1375                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1376    
1377                    throw new NoSuchPortletPreferencesException(msg.toString());
1378            }
1379    
1380            /**
1381             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and portletId = &#63;.
1382             *
1383             * @param ownerType the owner type
1384             * @param portletId the portlet ID
1385             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1386             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1387             */
1388            @Override
1389            public PortletPreferences fetchByO_P_Last(int ownerType, String portletId,
1390                    OrderByComparator<PortletPreferences> orderByComparator) {
1391                    int count = countByO_P(ownerType, portletId);
1392    
1393                    if (count == 0) {
1394                            return null;
1395                    }
1396    
1397                    List<PortletPreferences> list = findByO_P(ownerType, portletId,
1398                                    count - 1, count, orderByComparator);
1399    
1400                    if (!list.isEmpty()) {
1401                            return list.get(0);
1402                    }
1403    
1404                    return null;
1405            }
1406    
1407            /**
1408             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerType = &#63; and portletId = &#63;.
1409             *
1410             * @param portletPreferencesId the primary key of the current portlet preferences
1411             * @param ownerType the owner type
1412             * @param portletId the portlet ID
1413             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1414             * @return the previous, current, and next portlet preferences
1415             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
1416             */
1417            @Override
1418            public PortletPreferences[] findByO_P_PrevAndNext(
1419                    long portletPreferencesId, int ownerType, String portletId,
1420                    OrderByComparator<PortletPreferences> orderByComparator)
1421                    throws NoSuchPortletPreferencesException {
1422                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1423    
1424                    Session session = null;
1425    
1426                    try {
1427                            session = openSession();
1428    
1429                            PortletPreferences[] array = new PortletPreferencesImpl[3];
1430    
1431                            array[0] = getByO_P_PrevAndNext(session, portletPreferences,
1432                                            ownerType, portletId, orderByComparator, true);
1433    
1434                            array[1] = portletPreferences;
1435    
1436                            array[2] = getByO_P_PrevAndNext(session, portletPreferences,
1437                                            ownerType, portletId, orderByComparator, false);
1438    
1439                            return array;
1440                    }
1441                    catch (Exception e) {
1442                            throw processException(e);
1443                    }
1444                    finally {
1445                            closeSession(session);
1446                    }
1447            }
1448    
1449            protected PortletPreferences getByO_P_PrevAndNext(Session session,
1450                    PortletPreferences portletPreferences, int ownerType, String portletId,
1451                    OrderByComparator<PortletPreferences> orderByComparator,
1452                    boolean previous) {
1453                    StringBundler query = null;
1454    
1455                    if (orderByComparator != null) {
1456                            query = new StringBundler(6 +
1457                                            (orderByComparator.getOrderByFields().length * 6));
1458                    }
1459                    else {
1460                            query = new StringBundler(3);
1461                    }
1462    
1463                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1464    
1465                    query.append(_FINDER_COLUMN_O_P_OWNERTYPE_2);
1466    
1467                    boolean bindPortletId = false;
1468    
1469                    if (portletId == null) {
1470                            query.append(_FINDER_COLUMN_O_P_PORTLETID_1);
1471                    }
1472                    else if (portletId.equals(StringPool.BLANK)) {
1473                            query.append(_FINDER_COLUMN_O_P_PORTLETID_3);
1474                    }
1475                    else {
1476                            bindPortletId = true;
1477    
1478                            query.append(_FINDER_COLUMN_O_P_PORTLETID_2);
1479                    }
1480    
1481                    if (orderByComparator != null) {
1482                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1483    
1484                            if (orderByConditionFields.length > 0) {
1485                                    query.append(WHERE_AND);
1486                            }
1487    
1488                            for (int i = 0; i < orderByConditionFields.length; i++) {
1489                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1490                                    query.append(orderByConditionFields[i]);
1491    
1492                                    if ((i + 1) < orderByConditionFields.length) {
1493                                            if (orderByComparator.isAscending() ^ previous) {
1494                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1495                                            }
1496                                            else {
1497                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1498                                            }
1499                                    }
1500                                    else {
1501                                            if (orderByComparator.isAscending() ^ previous) {
1502                                                    query.append(WHERE_GREATER_THAN);
1503                                            }
1504                                            else {
1505                                                    query.append(WHERE_LESSER_THAN);
1506                                            }
1507                                    }
1508                            }
1509    
1510                            query.append(ORDER_BY_CLAUSE);
1511    
1512                            String[] orderByFields = orderByComparator.getOrderByFields();
1513    
1514                            for (int i = 0; i < orderByFields.length; i++) {
1515                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1516                                    query.append(orderByFields[i]);
1517    
1518                                    if ((i + 1) < orderByFields.length) {
1519                                            if (orderByComparator.isAscending() ^ previous) {
1520                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1521                                            }
1522                                            else {
1523                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1524                                            }
1525                                    }
1526                                    else {
1527                                            if (orderByComparator.isAscending() ^ previous) {
1528                                                    query.append(ORDER_BY_ASC);
1529                                            }
1530                                            else {
1531                                                    query.append(ORDER_BY_DESC);
1532                                            }
1533                                    }
1534                            }
1535                    }
1536                    else {
1537                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1538                    }
1539    
1540                    String sql = query.toString();
1541    
1542                    Query q = session.createQuery(sql);
1543    
1544                    q.setFirstResult(0);
1545                    q.setMaxResults(2);
1546    
1547                    QueryPos qPos = QueryPos.getInstance(q);
1548    
1549                    qPos.add(ownerType);
1550    
1551                    if (bindPortletId) {
1552                            qPos.add(portletId);
1553                    }
1554    
1555                    if (orderByComparator != null) {
1556                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
1557    
1558                            for (Object value : values) {
1559                                    qPos.add(value);
1560                            }
1561                    }
1562    
1563                    List<PortletPreferences> list = q.list();
1564    
1565                    if (list.size() == 2) {
1566                            return list.get(1);
1567                    }
1568                    else {
1569                            return null;
1570                    }
1571            }
1572    
1573            /**
1574             * Removes all the portlet preferenceses where ownerType = &#63; and portletId = &#63; from the database.
1575             *
1576             * @param ownerType the owner type
1577             * @param portletId the portlet ID
1578             */
1579            @Override
1580            public void removeByO_P(int ownerType, String portletId) {
1581                    for (PortletPreferences portletPreferences : findByO_P(ownerType,
1582                                    portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1583                            remove(portletPreferences);
1584                    }
1585            }
1586    
1587            /**
1588             * Returns the number of portlet preferenceses where ownerType = &#63; and portletId = &#63;.
1589             *
1590             * @param ownerType the owner type
1591             * @param portletId the portlet ID
1592             * @return the number of matching portlet preferenceses
1593             */
1594            @Override
1595            public int countByO_P(int ownerType, String portletId) {
1596                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_P;
1597    
1598                    Object[] finderArgs = new Object[] { ownerType, portletId };
1599    
1600                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1601                                    this);
1602    
1603                    if (count == null) {
1604                            StringBundler query = new StringBundler(3);
1605    
1606                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1607    
1608                            query.append(_FINDER_COLUMN_O_P_OWNERTYPE_2);
1609    
1610                            boolean bindPortletId = false;
1611    
1612                            if (portletId == null) {
1613                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_1);
1614                            }
1615                            else if (portletId.equals(StringPool.BLANK)) {
1616                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_3);
1617                            }
1618                            else {
1619                                    bindPortletId = true;
1620    
1621                                    query.append(_FINDER_COLUMN_O_P_PORTLETID_2);
1622                            }
1623    
1624                            String sql = query.toString();
1625    
1626                            Session session = null;
1627    
1628                            try {
1629                                    session = openSession();
1630    
1631                                    Query q = session.createQuery(sql);
1632    
1633                                    QueryPos qPos = QueryPos.getInstance(q);
1634    
1635                                    qPos.add(ownerType);
1636    
1637                                    if (bindPortletId) {
1638                                            qPos.add(portletId);
1639                                    }
1640    
1641                                    count = (Long)q.uniqueResult();
1642    
1643                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1644                            }
1645                            catch (Exception e) {
1646                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1647    
1648                                    throw processException(e);
1649                            }
1650                            finally {
1651                                    closeSession(session);
1652                            }
1653                    }
1654    
1655                    return count.intValue();
1656            }
1657    
1658            private static final String _FINDER_COLUMN_O_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
1659            private static final String _FINDER_COLUMN_O_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1660            private static final String _FINDER_COLUMN_O_P_PORTLETID_2 = "portletPreferences.portletId = ?";
1661            private static final String _FINDER_COLUMN_O_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
1662            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1663                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1664                            PortletPreferencesImpl.class,
1665                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByP_P",
1666                            new String[] {
1667                                    Long.class.getName(), String.class.getName(),
1668                                    
1669                            Integer.class.getName(), Integer.class.getName(),
1670                                    OrderByComparator.class.getName()
1671                            });
1672            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1673                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1674                            PortletPreferencesImpl.class,
1675                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByP_P",
1676                            new String[] { Long.class.getName(), String.class.getName() },
1677                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
1678                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
1679            public static final FinderPath FINDER_PATH_COUNT_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1680                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
1681                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByP_P",
1682                            new String[] { Long.class.getName(), String.class.getName() });
1683    
1684            /**
1685             * Returns all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1686             *
1687             * @param plid the plid
1688             * @param portletId the portlet ID
1689             * @return the matching portlet preferenceses
1690             */
1691            @Override
1692            public List<PortletPreferences> findByP_P(long plid, String portletId) {
1693                    return findByP_P(plid, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1694                            null);
1695            }
1696    
1697            /**
1698             * Returns a range of all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1699             *
1700             * <p>
1701             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
1702             * </p>
1703             *
1704             * @param plid the plid
1705             * @param portletId the portlet ID
1706             * @param start the lower bound of the range of portlet preferenceses
1707             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1708             * @return the range of matching portlet preferenceses
1709             */
1710            @Override
1711            public List<PortletPreferences> findByP_P(long plid, String portletId,
1712                    int start, int end) {
1713                    return findByP_P(plid, portletId, start, end, null);
1714            }
1715    
1716            /**
1717             * Returns an ordered range of all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1718             *
1719             * <p>
1720             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
1721             * </p>
1722             *
1723             * @param plid the plid
1724             * @param portletId the portlet ID
1725             * @param start the lower bound of the range of portlet preferenceses
1726             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1727             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1728             * @return the ordered range of matching portlet preferenceses
1729             */
1730            @Override
1731            public List<PortletPreferences> findByP_P(long plid, String portletId,
1732                    int start, int end,
1733                    OrderByComparator<PortletPreferences> orderByComparator) {
1734                    boolean pagination = true;
1735                    FinderPath finderPath = null;
1736                    Object[] finderArgs = null;
1737    
1738                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1739                                    (orderByComparator == null)) {
1740                            pagination = false;
1741                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P;
1742                            finderArgs = new Object[] { plid, portletId };
1743                    }
1744                    else {
1745                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_P_P;
1746                            finderArgs = new Object[] {
1747                                            plid, portletId,
1748                                            
1749                                            start, end, orderByComparator
1750                                    };
1751                    }
1752    
1753                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
1754                                    finderArgs, this);
1755    
1756                    if ((list != null) && !list.isEmpty()) {
1757                            for (PortletPreferences portletPreferences : list) {
1758                                    if ((plid != portletPreferences.getPlid()) ||
1759                                                    !Validator.equals(portletId,
1760                                                            portletPreferences.getPortletId())) {
1761                                            list = null;
1762    
1763                                            break;
1764                                    }
1765                            }
1766                    }
1767    
1768                    if (list == null) {
1769                            StringBundler query = null;
1770    
1771                            if (orderByComparator != null) {
1772                                    query = new StringBundler(4 +
1773                                                    (orderByComparator.getOrderByFields().length * 3));
1774                            }
1775                            else {
1776                                    query = new StringBundler(4);
1777                            }
1778    
1779                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1780    
1781                            query.append(_FINDER_COLUMN_P_P_PLID_2);
1782    
1783                            boolean bindPortletId = false;
1784    
1785                            if (portletId == null) {
1786                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
1787                            }
1788                            else if (portletId.equals(StringPool.BLANK)) {
1789                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
1790                            }
1791                            else {
1792                                    bindPortletId = true;
1793    
1794                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
1795                            }
1796    
1797                            if (orderByComparator != null) {
1798                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1799                                            orderByComparator);
1800                            }
1801                            else
1802                             if (pagination) {
1803                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1804                            }
1805    
1806                            String sql = query.toString();
1807    
1808                            Session session = null;
1809    
1810                            try {
1811                                    session = openSession();
1812    
1813                                    Query q = session.createQuery(sql);
1814    
1815                                    QueryPos qPos = QueryPos.getInstance(q);
1816    
1817                                    qPos.add(plid);
1818    
1819                                    if (bindPortletId) {
1820                                            qPos.add(portletId);
1821                                    }
1822    
1823                                    if (!pagination) {
1824                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1825                                                            getDialect(), start, end, false);
1826    
1827                                            Collections.sort(list);
1828    
1829                                            list = Collections.unmodifiableList(list);
1830                                    }
1831                                    else {
1832                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1833                                                            getDialect(), start, end);
1834                                    }
1835    
1836                                    cacheResult(list);
1837    
1838                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1839                            }
1840                            catch (Exception e) {
1841                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1842    
1843                                    throw processException(e);
1844                            }
1845                            finally {
1846                                    closeSession(session);
1847                            }
1848                    }
1849    
1850                    return list;
1851            }
1852    
1853            /**
1854             * Returns the first portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1855             *
1856             * @param plid the plid
1857             * @param portletId the portlet ID
1858             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1859             * @return the first matching portlet preferences
1860             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1861             */
1862            @Override
1863            public PortletPreferences findByP_P_First(long plid, String portletId,
1864                    OrderByComparator<PortletPreferences> orderByComparator)
1865                    throws NoSuchPortletPreferencesException {
1866                    PortletPreferences portletPreferences = fetchByP_P_First(plid,
1867                                    portletId, orderByComparator);
1868    
1869                    if (portletPreferences != null) {
1870                            return portletPreferences;
1871                    }
1872    
1873                    StringBundler msg = new StringBundler(6);
1874    
1875                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1876    
1877                    msg.append("plid=");
1878                    msg.append(plid);
1879    
1880                    msg.append(", portletId=");
1881                    msg.append(portletId);
1882    
1883                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1884    
1885                    throw new NoSuchPortletPreferencesException(msg.toString());
1886            }
1887    
1888            /**
1889             * Returns the first portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1890             *
1891             * @param plid the plid
1892             * @param portletId the portlet ID
1893             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1894             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1895             */
1896            @Override
1897            public PortletPreferences fetchByP_P_First(long plid, String portletId,
1898                    OrderByComparator<PortletPreferences> orderByComparator) {
1899                    List<PortletPreferences> list = findByP_P(plid, portletId, 0, 1,
1900                                    orderByComparator);
1901    
1902                    if (!list.isEmpty()) {
1903                            return list.get(0);
1904                    }
1905    
1906                    return null;
1907            }
1908    
1909            /**
1910             * Returns the last portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1911             *
1912             * @param plid the plid
1913             * @param portletId the portlet ID
1914             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1915             * @return the last matching portlet preferences
1916             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1917             */
1918            @Override
1919            public PortletPreferences findByP_P_Last(long plid, String portletId,
1920                    OrderByComparator<PortletPreferences> orderByComparator)
1921                    throws NoSuchPortletPreferencesException {
1922                    PortletPreferences portletPreferences = fetchByP_P_Last(plid,
1923                                    portletId, orderByComparator);
1924    
1925                    if (portletPreferences != null) {
1926                            return portletPreferences;
1927                    }
1928    
1929                    StringBundler msg = new StringBundler(6);
1930    
1931                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1932    
1933                    msg.append("plid=");
1934                    msg.append(plid);
1935    
1936                    msg.append(", portletId=");
1937                    msg.append(portletId);
1938    
1939                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1940    
1941                    throw new NoSuchPortletPreferencesException(msg.toString());
1942            }
1943    
1944            /**
1945             * Returns the last portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1946             *
1947             * @param plid the plid
1948             * @param portletId the portlet ID
1949             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1950             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1951             */
1952            @Override
1953            public PortletPreferences fetchByP_P_Last(long plid, String portletId,
1954                    OrderByComparator<PortletPreferences> orderByComparator) {
1955                    int count = countByP_P(plid, portletId);
1956    
1957                    if (count == 0) {
1958                            return null;
1959                    }
1960    
1961                    List<PortletPreferences> list = findByP_P(plid, portletId, count - 1,
1962                                    count, orderByComparator);
1963    
1964                    if (!list.isEmpty()) {
1965                            return list.get(0);
1966                    }
1967    
1968                    return null;
1969            }
1970    
1971            /**
1972             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1973             *
1974             * @param portletPreferencesId the primary key of the current portlet preferences
1975             * @param plid the plid
1976             * @param portletId the portlet ID
1977             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1978             * @return the previous, current, and next portlet preferences
1979             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
1980             */
1981            @Override
1982            public PortletPreferences[] findByP_P_PrevAndNext(
1983                    long portletPreferencesId, long plid, String portletId,
1984                    OrderByComparator<PortletPreferences> orderByComparator)
1985                    throws NoSuchPortletPreferencesException {
1986                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1987    
1988                    Session session = null;
1989    
1990                    try {
1991                            session = openSession();
1992    
1993                            PortletPreferences[] array = new PortletPreferencesImpl[3];
1994    
1995                            array[0] = getByP_P_PrevAndNext(session, portletPreferences, plid,
1996                                            portletId, orderByComparator, true);
1997    
1998                            array[1] = portletPreferences;
1999    
2000                            array[2] = getByP_P_PrevAndNext(session, portletPreferences, plid,
2001                                            portletId, orderByComparator, false);
2002    
2003                            return array;
2004                    }
2005                    catch (Exception e) {
2006                            throw processException(e);
2007                    }
2008                    finally {
2009                            closeSession(session);
2010                    }
2011            }
2012    
2013            protected PortletPreferences getByP_P_PrevAndNext(Session session,
2014                    PortletPreferences portletPreferences, long plid, String portletId,
2015                    OrderByComparator<PortletPreferences> orderByComparator,
2016                    boolean previous) {
2017                    StringBundler query = null;
2018    
2019                    if (orderByComparator != null) {
2020                            query = new StringBundler(6 +
2021                                            (orderByComparator.getOrderByFields().length * 6));
2022                    }
2023                    else {
2024                            query = new StringBundler(3);
2025                    }
2026    
2027                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2028    
2029                    query.append(_FINDER_COLUMN_P_P_PLID_2);
2030    
2031                    boolean bindPortletId = false;
2032    
2033                    if (portletId == null) {
2034                            query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
2035                    }
2036                    else if (portletId.equals(StringPool.BLANK)) {
2037                            query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
2038                    }
2039                    else {
2040                            bindPortletId = true;
2041    
2042                            query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
2043                    }
2044    
2045                    if (orderByComparator != null) {
2046                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2047    
2048                            if (orderByConditionFields.length > 0) {
2049                                    query.append(WHERE_AND);
2050                            }
2051    
2052                            for (int i = 0; i < orderByConditionFields.length; i++) {
2053                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2054                                    query.append(orderByConditionFields[i]);
2055    
2056                                    if ((i + 1) < orderByConditionFields.length) {
2057                                            if (orderByComparator.isAscending() ^ previous) {
2058                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2059                                            }
2060                                            else {
2061                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2062                                            }
2063                                    }
2064                                    else {
2065                                            if (orderByComparator.isAscending() ^ previous) {
2066                                                    query.append(WHERE_GREATER_THAN);
2067                                            }
2068                                            else {
2069                                                    query.append(WHERE_LESSER_THAN);
2070                                            }
2071                                    }
2072                            }
2073    
2074                            query.append(ORDER_BY_CLAUSE);
2075    
2076                            String[] orderByFields = orderByComparator.getOrderByFields();
2077    
2078                            for (int i = 0; i < orderByFields.length; i++) {
2079                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2080                                    query.append(orderByFields[i]);
2081    
2082                                    if ((i + 1) < orderByFields.length) {
2083                                            if (orderByComparator.isAscending() ^ previous) {
2084                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2085                                            }
2086                                            else {
2087                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2088                                            }
2089                                    }
2090                                    else {
2091                                            if (orderByComparator.isAscending() ^ previous) {
2092                                                    query.append(ORDER_BY_ASC);
2093                                            }
2094                                            else {
2095                                                    query.append(ORDER_BY_DESC);
2096                                            }
2097                                    }
2098                            }
2099                    }
2100                    else {
2101                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2102                    }
2103    
2104                    String sql = query.toString();
2105    
2106                    Query q = session.createQuery(sql);
2107    
2108                    q.setFirstResult(0);
2109                    q.setMaxResults(2);
2110    
2111                    QueryPos qPos = QueryPos.getInstance(q);
2112    
2113                    qPos.add(plid);
2114    
2115                    if (bindPortletId) {
2116                            qPos.add(portletId);
2117                    }
2118    
2119                    if (orderByComparator != null) {
2120                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
2121    
2122                            for (Object value : values) {
2123                                    qPos.add(value);
2124                            }
2125                    }
2126    
2127                    List<PortletPreferences> list = q.list();
2128    
2129                    if (list.size() == 2) {
2130                            return list.get(1);
2131                    }
2132                    else {
2133                            return null;
2134                    }
2135            }
2136    
2137            /**
2138             * Removes all the portlet preferenceses where plid = &#63; and portletId = &#63; from the database.
2139             *
2140             * @param plid the plid
2141             * @param portletId the portlet ID
2142             */
2143            @Override
2144            public void removeByP_P(long plid, String portletId) {
2145                    for (PortletPreferences portletPreferences : findByP_P(plid, portletId,
2146                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2147                            remove(portletPreferences);
2148                    }
2149            }
2150    
2151            /**
2152             * Returns the number of portlet preferenceses where plid = &#63; and portletId = &#63;.
2153             *
2154             * @param plid the plid
2155             * @param portletId the portlet ID
2156             * @return the number of matching portlet preferenceses
2157             */
2158            @Override
2159            public int countByP_P(long plid, String portletId) {
2160                    FinderPath finderPath = FINDER_PATH_COUNT_BY_P_P;
2161    
2162                    Object[] finderArgs = new Object[] { plid, portletId };
2163    
2164                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2165                                    this);
2166    
2167                    if (count == null) {
2168                            StringBundler query = new StringBundler(3);
2169    
2170                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
2171    
2172                            query.append(_FINDER_COLUMN_P_P_PLID_2);
2173    
2174                            boolean bindPortletId = false;
2175    
2176                            if (portletId == null) {
2177                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
2178                            }
2179                            else if (portletId.equals(StringPool.BLANK)) {
2180                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
2181                            }
2182                            else {
2183                                    bindPortletId = true;
2184    
2185                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
2186                            }
2187    
2188                            String sql = query.toString();
2189    
2190                            Session session = null;
2191    
2192                            try {
2193                                    session = openSession();
2194    
2195                                    Query q = session.createQuery(sql);
2196    
2197                                    QueryPos qPos = QueryPos.getInstance(q);
2198    
2199                                    qPos.add(plid);
2200    
2201                                    if (bindPortletId) {
2202                                            qPos.add(portletId);
2203                                    }
2204    
2205                                    count = (Long)q.uniqueResult();
2206    
2207                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2208                            }
2209                            catch (Exception e) {
2210                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2211    
2212                                    throw processException(e);
2213                            }
2214                            finally {
2215                                    closeSession(session);
2216                            }
2217                    }
2218    
2219                    return count.intValue();
2220            }
2221    
2222            private static final String _FINDER_COLUMN_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
2223            private static final String _FINDER_COLUMN_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
2224            private static final String _FINDER_COLUMN_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
2225            private static final String _FINDER_COLUMN_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
2226            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2227                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2228                            PortletPreferencesImpl.class,
2229                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_O_P",
2230                            new String[] {
2231                                    Long.class.getName(), Integer.class.getName(),
2232                                    Long.class.getName(),
2233                                    
2234                            Integer.class.getName(), Integer.class.getName(),
2235                                    OrderByComparator.class.getName()
2236                            });
2237            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2238                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2239                            PortletPreferencesImpl.class,
2240                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_O_P",
2241                            new String[] {
2242                                    Long.class.getName(), Integer.class.getName(),
2243                                    Long.class.getName()
2244                            },
2245                            PortletPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
2246                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
2247                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK);
2248            public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2249                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
2250                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O_P",
2251                            new String[] {
2252                                    Long.class.getName(), Integer.class.getName(),
2253                                    Long.class.getName()
2254                            });
2255    
2256            /**
2257             * Returns all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2258             *
2259             * @param ownerId the owner ID
2260             * @param ownerType the owner type
2261             * @param plid the plid
2262             * @return the matching portlet preferenceses
2263             */
2264            @Override
2265            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
2266                    long plid) {
2267                    return findByO_O_P(ownerId, ownerType, plid, QueryUtil.ALL_POS,
2268                            QueryUtil.ALL_POS, null);
2269            }
2270    
2271            /**
2272             * Returns a range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2273             *
2274             * <p>
2275             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
2276             * </p>
2277             *
2278             * @param ownerId the owner ID
2279             * @param ownerType the owner type
2280             * @param plid the plid
2281             * @param start the lower bound of the range of portlet preferenceses
2282             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2283             * @return the range of matching portlet preferenceses
2284             */
2285            @Override
2286            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
2287                    long plid, int start, int end) {
2288                    return findByO_O_P(ownerId, ownerType, plid, start, end, null);
2289            }
2290    
2291            /**
2292             * Returns an ordered range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2293             *
2294             * <p>
2295             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
2296             * </p>
2297             *
2298             * @param ownerId the owner ID
2299             * @param ownerType the owner type
2300             * @param plid the plid
2301             * @param start the lower bound of the range of portlet preferenceses
2302             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2303             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2304             * @return the ordered range of matching portlet preferenceses
2305             */
2306            @Override
2307            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
2308                    long plid, int start, int end,
2309                    OrderByComparator<PortletPreferences> orderByComparator) {
2310                    boolean pagination = true;
2311                    FinderPath finderPath = null;
2312                    Object[] finderArgs = null;
2313    
2314                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2315                                    (orderByComparator == null)) {
2316                            pagination = false;
2317                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P;
2318                            finderArgs = new Object[] { ownerId, ownerType, plid };
2319                    }
2320                    else {
2321                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_P;
2322                            finderArgs = new Object[] {
2323                                            ownerId, ownerType, plid,
2324                                            
2325                                            start, end, orderByComparator
2326                                    };
2327                    }
2328    
2329                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
2330                                    finderArgs, this);
2331    
2332                    if ((list != null) && !list.isEmpty()) {
2333                            for (PortletPreferences portletPreferences : list) {
2334                                    if ((ownerId != portletPreferences.getOwnerId()) ||
2335                                                    (ownerType != portletPreferences.getOwnerType()) ||
2336                                                    (plid != portletPreferences.getPlid())) {
2337                                            list = null;
2338    
2339                                            break;
2340                                    }
2341                            }
2342                    }
2343    
2344                    if (list == null) {
2345                            StringBundler query = null;
2346    
2347                            if (orderByComparator != null) {
2348                                    query = new StringBundler(5 +
2349                                                    (orderByComparator.getOrderByFields().length * 3));
2350                            }
2351                            else {
2352                                    query = new StringBundler(5);
2353                            }
2354    
2355                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2356    
2357                            query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
2358    
2359                            query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
2360    
2361                            query.append(_FINDER_COLUMN_O_O_P_PLID_2);
2362    
2363                            if (orderByComparator != null) {
2364                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2365                                            orderByComparator);
2366                            }
2367                            else
2368                             if (pagination) {
2369                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2370                            }
2371    
2372                            String sql = query.toString();
2373    
2374                            Session session = null;
2375    
2376                            try {
2377                                    session = openSession();
2378    
2379                                    Query q = session.createQuery(sql);
2380    
2381                                    QueryPos qPos = QueryPos.getInstance(q);
2382    
2383                                    qPos.add(ownerId);
2384    
2385                                    qPos.add(ownerType);
2386    
2387                                    qPos.add(plid);
2388    
2389                                    if (!pagination) {
2390                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2391                                                            getDialect(), start, end, false);
2392    
2393                                            Collections.sort(list);
2394    
2395                                            list = Collections.unmodifiableList(list);
2396                                    }
2397                                    else {
2398                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2399                                                            getDialect(), start, end);
2400                                    }
2401    
2402                                    cacheResult(list);
2403    
2404                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2405                            }
2406                            catch (Exception e) {
2407                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2408    
2409                                    throw processException(e);
2410                            }
2411                            finally {
2412                                    closeSession(session);
2413                            }
2414                    }
2415    
2416                    return list;
2417            }
2418    
2419            /**
2420             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2421             *
2422             * @param ownerId the owner ID
2423             * @param ownerType the owner type
2424             * @param plid the plid
2425             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2426             * @return the first matching portlet preferences
2427             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
2428             */
2429            @Override
2430            public PortletPreferences findByO_O_P_First(long ownerId, int ownerType,
2431                    long plid, OrderByComparator<PortletPreferences> orderByComparator)
2432                    throws NoSuchPortletPreferencesException {
2433                    PortletPreferences portletPreferences = fetchByO_O_P_First(ownerId,
2434                                    ownerType, plid, orderByComparator);
2435    
2436                    if (portletPreferences != null) {
2437                            return portletPreferences;
2438                    }
2439    
2440                    StringBundler msg = new StringBundler(8);
2441    
2442                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2443    
2444                    msg.append("ownerId=");
2445                    msg.append(ownerId);
2446    
2447                    msg.append(", ownerType=");
2448                    msg.append(ownerType);
2449    
2450                    msg.append(", plid=");
2451                    msg.append(plid);
2452    
2453                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2454    
2455                    throw new NoSuchPortletPreferencesException(msg.toString());
2456            }
2457    
2458            /**
2459             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2460             *
2461             * @param ownerId the owner ID
2462             * @param ownerType the owner type
2463             * @param plid the plid
2464             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2465             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2466             */
2467            @Override
2468            public PortletPreferences fetchByO_O_P_First(long ownerId, int ownerType,
2469                    long plid, OrderByComparator<PortletPreferences> orderByComparator) {
2470                    List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
2471                                    0, 1, orderByComparator);
2472    
2473                    if (!list.isEmpty()) {
2474                            return list.get(0);
2475                    }
2476    
2477                    return null;
2478            }
2479    
2480            /**
2481             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2482             *
2483             * @param ownerId the owner ID
2484             * @param ownerType the owner type
2485             * @param plid the plid
2486             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2487             * @return the last matching portlet preferences
2488             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
2489             */
2490            @Override
2491            public PortletPreferences findByO_O_P_Last(long ownerId, int ownerType,
2492                    long plid, OrderByComparator<PortletPreferences> orderByComparator)
2493                    throws NoSuchPortletPreferencesException {
2494                    PortletPreferences portletPreferences = fetchByO_O_P_Last(ownerId,
2495                                    ownerType, plid, orderByComparator);
2496    
2497                    if (portletPreferences != null) {
2498                            return portletPreferences;
2499                    }
2500    
2501                    StringBundler msg = new StringBundler(8);
2502    
2503                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2504    
2505                    msg.append("ownerId=");
2506                    msg.append(ownerId);
2507    
2508                    msg.append(", ownerType=");
2509                    msg.append(ownerType);
2510    
2511                    msg.append(", plid=");
2512                    msg.append(plid);
2513    
2514                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2515    
2516                    throw new NoSuchPortletPreferencesException(msg.toString());
2517            }
2518    
2519            /**
2520             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2521             *
2522             * @param ownerId the owner ID
2523             * @param ownerType the owner type
2524             * @param plid the plid
2525             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2526             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2527             */
2528            @Override
2529            public PortletPreferences fetchByO_O_P_Last(long ownerId, int ownerType,
2530                    long plid, OrderByComparator<PortletPreferences> orderByComparator) {
2531                    int count = countByO_O_P(ownerId, ownerType, plid);
2532    
2533                    if (count == 0) {
2534                            return null;
2535                    }
2536    
2537                    List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
2538                                    count - 1, count, orderByComparator);
2539    
2540                    if (!list.isEmpty()) {
2541                            return list.get(0);
2542                    }
2543    
2544                    return null;
2545            }
2546    
2547            /**
2548             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2549             *
2550             * @param portletPreferencesId the primary key of the current portlet preferences
2551             * @param ownerId the owner ID
2552             * @param ownerType the owner type
2553             * @param plid the plid
2554             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2555             * @return the previous, current, and next portlet preferences
2556             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
2557             */
2558            @Override
2559            public PortletPreferences[] findByO_O_P_PrevAndNext(
2560                    long portletPreferencesId, long ownerId, int ownerType, long plid,
2561                    OrderByComparator<PortletPreferences> orderByComparator)
2562                    throws NoSuchPortletPreferencesException {
2563                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
2564    
2565                    Session session = null;
2566    
2567                    try {
2568                            session = openSession();
2569    
2570                            PortletPreferences[] array = new PortletPreferencesImpl[3];
2571    
2572                            array[0] = getByO_O_P_PrevAndNext(session, portletPreferences,
2573                                            ownerId, ownerType, plid, orderByComparator, true);
2574    
2575                            array[1] = portletPreferences;
2576    
2577                            array[2] = getByO_O_P_PrevAndNext(session, portletPreferences,
2578                                            ownerId, ownerType, plid, orderByComparator, false);
2579    
2580                            return array;
2581                    }
2582                    catch (Exception e) {
2583                            throw processException(e);
2584                    }
2585                    finally {
2586                            closeSession(session);
2587                    }
2588            }
2589    
2590            protected PortletPreferences getByO_O_P_PrevAndNext(Session session,
2591                    PortletPreferences portletPreferences, long ownerId, int ownerType,
2592                    long plid, OrderByComparator<PortletPreferences> orderByComparator,
2593                    boolean previous) {
2594                    StringBundler query = null;
2595    
2596                    if (orderByComparator != null) {
2597                            query = new StringBundler(6 +
2598                                            (orderByComparator.getOrderByFields().length * 6));
2599                    }
2600                    else {
2601                            query = new StringBundler(3);
2602                    }
2603    
2604                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2605    
2606                    query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
2607    
2608                    query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
2609    
2610                    query.append(_FINDER_COLUMN_O_O_P_PLID_2);
2611    
2612                    if (orderByComparator != null) {
2613                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2614    
2615                            if (orderByConditionFields.length > 0) {
2616                                    query.append(WHERE_AND);
2617                            }
2618    
2619                            for (int i = 0; i < orderByConditionFields.length; i++) {
2620                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2621                                    query.append(orderByConditionFields[i]);
2622    
2623                                    if ((i + 1) < orderByConditionFields.length) {
2624                                            if (orderByComparator.isAscending() ^ previous) {
2625                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2626                                            }
2627                                            else {
2628                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2629                                            }
2630                                    }
2631                                    else {
2632                                            if (orderByComparator.isAscending() ^ previous) {
2633                                                    query.append(WHERE_GREATER_THAN);
2634                                            }
2635                                            else {
2636                                                    query.append(WHERE_LESSER_THAN);
2637                                            }
2638                                    }
2639                            }
2640    
2641                            query.append(ORDER_BY_CLAUSE);
2642    
2643                            String[] orderByFields = orderByComparator.getOrderByFields();
2644    
2645                            for (int i = 0; i < orderByFields.length; i++) {
2646                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2647                                    query.append(orderByFields[i]);
2648    
2649                                    if ((i + 1) < orderByFields.length) {
2650                                            if (orderByComparator.isAscending() ^ previous) {
2651                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2652                                            }
2653                                            else {
2654                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2655                                            }
2656                                    }
2657                                    else {
2658                                            if (orderByComparator.isAscending() ^ previous) {
2659                                                    query.append(ORDER_BY_ASC);
2660                                            }
2661                                            else {
2662                                                    query.append(ORDER_BY_DESC);
2663                                            }
2664                                    }
2665                            }
2666                    }
2667                    else {
2668                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2669                    }
2670    
2671                    String sql = query.toString();
2672    
2673                    Query q = session.createQuery(sql);
2674    
2675                    q.setFirstResult(0);
2676                    q.setMaxResults(2);
2677    
2678                    QueryPos qPos = QueryPos.getInstance(q);
2679    
2680                    qPos.add(ownerId);
2681    
2682                    qPos.add(ownerType);
2683    
2684                    qPos.add(plid);
2685    
2686                    if (orderByComparator != null) {
2687                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
2688    
2689                            for (Object value : values) {
2690                                    qPos.add(value);
2691                            }
2692                    }
2693    
2694                    List<PortletPreferences> list = q.list();
2695    
2696                    if (list.size() == 2) {
2697                            return list.get(1);
2698                    }
2699                    else {
2700                            return null;
2701                    }
2702            }
2703    
2704            /**
2705             * Removes all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63; from the database.
2706             *
2707             * @param ownerId the owner ID
2708             * @param ownerType the owner type
2709             * @param plid the plid
2710             */
2711            @Override
2712            public void removeByO_O_P(long ownerId, int ownerType, long plid) {
2713                    for (PortletPreferences portletPreferences : findByO_O_P(ownerId,
2714                                    ownerType, plid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2715                            remove(portletPreferences);
2716                    }
2717            }
2718    
2719            /**
2720             * Returns the number of portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2721             *
2722             * @param ownerId the owner ID
2723             * @param ownerType the owner type
2724             * @param plid the plid
2725             * @return the number of matching portlet preferenceses
2726             */
2727            @Override
2728            public int countByO_O_P(long ownerId, int ownerType, long plid) {
2729                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O_P;
2730    
2731                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid };
2732    
2733                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2734                                    this);
2735    
2736                    if (count == null) {
2737                            StringBundler query = new StringBundler(4);
2738    
2739                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
2740    
2741                            query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
2742    
2743                            query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
2744    
2745                            query.append(_FINDER_COLUMN_O_O_P_PLID_2);
2746    
2747                            String sql = query.toString();
2748    
2749                            Session session = null;
2750    
2751                            try {
2752                                    session = openSession();
2753    
2754                                    Query q = session.createQuery(sql);
2755    
2756                                    QueryPos qPos = QueryPos.getInstance(q);
2757    
2758                                    qPos.add(ownerId);
2759    
2760                                    qPos.add(ownerType);
2761    
2762                                    qPos.add(plid);
2763    
2764                                    count = (Long)q.uniqueResult();
2765    
2766                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2767                            }
2768                            catch (Exception e) {
2769                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2770    
2771                                    throw processException(e);
2772                            }
2773                            finally {
2774                                    closeSession(session);
2775                            }
2776                    }
2777    
2778                    return count.intValue();
2779            }
2780    
2781            private static final String _FINDER_COLUMN_O_O_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
2782            private static final String _FINDER_COLUMN_O_O_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
2783            private static final String _FINDER_COLUMN_O_O_P_PLID_2 = "portletPreferences.plid = ?";
2784            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_PI = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2785                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2786                            PortletPreferencesImpl.class,
2787                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_O_PI",
2788                            new String[] {
2789                                    Long.class.getName(), Integer.class.getName(),
2790                                    String.class.getName(),
2791                                    
2792                            Integer.class.getName(), Integer.class.getName(),
2793                                    OrderByComparator.class.getName()
2794                            });
2795            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_PI =
2796                    new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2797                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2798                            PortletPreferencesImpl.class,
2799                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_O_PI",
2800                            new String[] {
2801                                    Long.class.getName(), Integer.class.getName(),
2802                                    String.class.getName()
2803                            },
2804                            PortletPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
2805                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
2806                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
2807            public static final FinderPath FINDER_PATH_COUNT_BY_O_O_PI = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2808                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
2809                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O_PI",
2810                            new String[] {
2811                                    Long.class.getName(), Integer.class.getName(),
2812                                    String.class.getName()
2813                            });
2814    
2815            /**
2816             * Returns all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
2817             *
2818             * @param ownerId the owner ID
2819             * @param ownerType the owner type
2820             * @param portletId the portlet ID
2821             * @return the matching portlet preferenceses
2822             */
2823            @Override
2824            public List<PortletPreferences> findByO_O_PI(long ownerId, int ownerType,
2825                    String portletId) {
2826                    return findByO_O_PI(ownerId, ownerType, portletId, QueryUtil.ALL_POS,
2827                            QueryUtil.ALL_POS, null);
2828            }
2829    
2830            /**
2831             * Returns a range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
2832             *
2833             * <p>
2834             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
2835             * </p>
2836             *
2837             * @param ownerId the owner ID
2838             * @param ownerType the owner type
2839             * @param portletId the portlet ID
2840             * @param start the lower bound of the range of portlet preferenceses
2841             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2842             * @return the range of matching portlet preferenceses
2843             */
2844            @Override
2845            public List<PortletPreferences> findByO_O_PI(long ownerId, int ownerType,
2846                    String portletId, int start, int end) {
2847                    return findByO_O_PI(ownerId, ownerType, portletId, start, end, null);
2848            }
2849    
2850            /**
2851             * Returns an ordered range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
2852             *
2853             * <p>
2854             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
2855             * </p>
2856             *
2857             * @param ownerId the owner ID
2858             * @param ownerType the owner type
2859             * @param portletId the portlet ID
2860             * @param start the lower bound of the range of portlet preferenceses
2861             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2862             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2863             * @return the ordered range of matching portlet preferenceses
2864             */
2865            @Override
2866            public List<PortletPreferences> findByO_O_PI(long ownerId, int ownerType,
2867                    String portletId, int start, int end,
2868                    OrderByComparator<PortletPreferences> orderByComparator) {
2869                    boolean pagination = true;
2870                    FinderPath finderPath = null;
2871                    Object[] finderArgs = null;
2872    
2873                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2874                                    (orderByComparator == null)) {
2875                            pagination = false;
2876                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_PI;
2877                            finderArgs = new Object[] { ownerId, ownerType, portletId };
2878                    }
2879                    else {
2880                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_PI;
2881                            finderArgs = new Object[] {
2882                                            ownerId, ownerType, portletId,
2883                                            
2884                                            start, end, orderByComparator
2885                                    };
2886                    }
2887    
2888                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
2889                                    finderArgs, this);
2890    
2891                    if ((list != null) && !list.isEmpty()) {
2892                            for (PortletPreferences portletPreferences : list) {
2893                                    if ((ownerId != portletPreferences.getOwnerId()) ||
2894                                                    (ownerType != portletPreferences.getOwnerType()) ||
2895                                                    !Validator.equals(portletId,
2896                                                            portletPreferences.getPortletId())) {
2897                                            list = null;
2898    
2899                                            break;
2900                                    }
2901                            }
2902                    }
2903    
2904                    if (list == null) {
2905                            StringBundler query = null;
2906    
2907                            if (orderByComparator != null) {
2908                                    query = new StringBundler(5 +
2909                                                    (orderByComparator.getOrderByFields().length * 3));
2910                            }
2911                            else {
2912                                    query = new StringBundler(5);
2913                            }
2914    
2915                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2916    
2917                            query.append(_FINDER_COLUMN_O_O_PI_OWNERID_2);
2918    
2919                            query.append(_FINDER_COLUMN_O_O_PI_OWNERTYPE_2);
2920    
2921                            boolean bindPortletId = false;
2922    
2923                            if (portletId == null) {
2924                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_1);
2925                            }
2926                            else if (portletId.equals(StringPool.BLANK)) {
2927                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_3);
2928                            }
2929                            else {
2930                                    bindPortletId = true;
2931    
2932                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_2);
2933                            }
2934    
2935                            if (orderByComparator != null) {
2936                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2937                                            orderByComparator);
2938                            }
2939                            else
2940                             if (pagination) {
2941                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2942                            }
2943    
2944                            String sql = query.toString();
2945    
2946                            Session session = null;
2947    
2948                            try {
2949                                    session = openSession();
2950    
2951                                    Query q = session.createQuery(sql);
2952    
2953                                    QueryPos qPos = QueryPos.getInstance(q);
2954    
2955                                    qPos.add(ownerId);
2956    
2957                                    qPos.add(ownerType);
2958    
2959                                    if (bindPortletId) {
2960                                            qPos.add(portletId);
2961                                    }
2962    
2963                                    if (!pagination) {
2964                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2965                                                            getDialect(), start, end, false);
2966    
2967                                            Collections.sort(list);
2968    
2969                                            list = Collections.unmodifiableList(list);
2970                                    }
2971                                    else {
2972                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2973                                                            getDialect(), start, end);
2974                                    }
2975    
2976                                    cacheResult(list);
2977    
2978                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2979                            }
2980                            catch (Exception e) {
2981                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2982    
2983                                    throw processException(e);
2984                            }
2985                            finally {
2986                                    closeSession(session);
2987                            }
2988                    }
2989    
2990                    return list;
2991            }
2992    
2993            /**
2994             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
2995             *
2996             * @param ownerId the owner ID
2997             * @param ownerType the owner type
2998             * @param portletId the portlet ID
2999             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3000             * @return the first matching portlet preferences
3001             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
3002             */
3003            @Override
3004            public PortletPreferences findByO_O_PI_First(long ownerId, int ownerType,
3005                    String portletId,
3006                    OrderByComparator<PortletPreferences> orderByComparator)
3007                    throws NoSuchPortletPreferencesException {
3008                    PortletPreferences portletPreferences = fetchByO_O_PI_First(ownerId,
3009                                    ownerType, portletId, orderByComparator);
3010    
3011                    if (portletPreferences != null) {
3012                            return portletPreferences;
3013                    }
3014    
3015                    StringBundler msg = new StringBundler(8);
3016    
3017                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3018    
3019                    msg.append("ownerId=");
3020                    msg.append(ownerId);
3021    
3022                    msg.append(", ownerType=");
3023                    msg.append(ownerType);
3024    
3025                    msg.append(", portletId=");
3026                    msg.append(portletId);
3027    
3028                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3029    
3030                    throw new NoSuchPortletPreferencesException(msg.toString());
3031            }
3032    
3033            /**
3034             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
3035             *
3036             * @param ownerId the owner ID
3037             * @param ownerType the owner type
3038             * @param portletId the portlet ID
3039             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3040             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
3041             */
3042            @Override
3043            public PortletPreferences fetchByO_O_PI_First(long ownerId, int ownerType,
3044                    String portletId,
3045                    OrderByComparator<PortletPreferences> orderByComparator) {
3046                    List<PortletPreferences> list = findByO_O_PI(ownerId, ownerType,
3047                                    portletId, 0, 1, orderByComparator);
3048    
3049                    if (!list.isEmpty()) {
3050                            return list.get(0);
3051                    }
3052    
3053                    return null;
3054            }
3055    
3056            /**
3057             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
3058             *
3059             * @param ownerId the owner ID
3060             * @param ownerType the owner type
3061             * @param portletId the portlet ID
3062             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3063             * @return the last matching portlet preferences
3064             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
3065             */
3066            @Override
3067            public PortletPreferences findByO_O_PI_Last(long ownerId, int ownerType,
3068                    String portletId,
3069                    OrderByComparator<PortletPreferences> orderByComparator)
3070                    throws NoSuchPortletPreferencesException {
3071                    PortletPreferences portletPreferences = fetchByO_O_PI_Last(ownerId,
3072                                    ownerType, portletId, orderByComparator);
3073    
3074                    if (portletPreferences != null) {
3075                            return portletPreferences;
3076                    }
3077    
3078                    StringBundler msg = new StringBundler(8);
3079    
3080                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3081    
3082                    msg.append("ownerId=");
3083                    msg.append(ownerId);
3084    
3085                    msg.append(", ownerType=");
3086                    msg.append(ownerType);
3087    
3088                    msg.append(", portletId=");
3089                    msg.append(portletId);
3090    
3091                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3092    
3093                    throw new NoSuchPortletPreferencesException(msg.toString());
3094            }
3095    
3096            /**
3097             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
3098             *
3099             * @param ownerId the owner ID
3100             * @param ownerType the owner type
3101             * @param portletId the portlet ID
3102             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3103             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
3104             */
3105            @Override
3106            public PortletPreferences fetchByO_O_PI_Last(long ownerId, int ownerType,
3107                    String portletId,
3108                    OrderByComparator<PortletPreferences> orderByComparator) {
3109                    int count = countByO_O_PI(ownerId, ownerType, portletId);
3110    
3111                    if (count == 0) {
3112                            return null;
3113                    }
3114    
3115                    List<PortletPreferences> list = findByO_O_PI(ownerId, ownerType,
3116                                    portletId, count - 1, count, orderByComparator);
3117    
3118                    if (!list.isEmpty()) {
3119                            return list.get(0);
3120                    }
3121    
3122                    return null;
3123            }
3124    
3125            /**
3126             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
3127             *
3128             * @param portletPreferencesId the primary key of the current portlet preferences
3129             * @param ownerId the owner ID
3130             * @param ownerType the owner type
3131             * @param portletId the portlet ID
3132             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3133             * @return the previous, current, and next portlet preferences
3134             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
3135             */
3136            @Override
3137            public PortletPreferences[] findByO_O_PI_PrevAndNext(
3138                    long portletPreferencesId, long ownerId, int ownerType,
3139                    String portletId,
3140                    OrderByComparator<PortletPreferences> orderByComparator)
3141                    throws NoSuchPortletPreferencesException {
3142                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
3143    
3144                    Session session = null;
3145    
3146                    try {
3147                            session = openSession();
3148    
3149                            PortletPreferences[] array = new PortletPreferencesImpl[3];
3150    
3151                            array[0] = getByO_O_PI_PrevAndNext(session, portletPreferences,
3152                                            ownerId, ownerType, portletId, orderByComparator, true);
3153    
3154                            array[1] = portletPreferences;
3155    
3156                            array[2] = getByO_O_PI_PrevAndNext(session, portletPreferences,
3157                                            ownerId, ownerType, portletId, orderByComparator, false);
3158    
3159                            return array;
3160                    }
3161                    catch (Exception e) {
3162                            throw processException(e);
3163                    }
3164                    finally {
3165                            closeSession(session);
3166                    }
3167            }
3168    
3169            protected PortletPreferences getByO_O_PI_PrevAndNext(Session session,
3170                    PortletPreferences portletPreferences, long ownerId, int ownerType,
3171                    String portletId,
3172                    OrderByComparator<PortletPreferences> orderByComparator,
3173                    boolean previous) {
3174                    StringBundler query = null;
3175    
3176                    if (orderByComparator != null) {
3177                            query = new StringBundler(6 +
3178                                            (orderByComparator.getOrderByFields().length * 6));
3179                    }
3180                    else {
3181                            query = new StringBundler(3);
3182                    }
3183    
3184                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
3185    
3186                    query.append(_FINDER_COLUMN_O_O_PI_OWNERID_2);
3187    
3188                    query.append(_FINDER_COLUMN_O_O_PI_OWNERTYPE_2);
3189    
3190                    boolean bindPortletId = false;
3191    
3192                    if (portletId == null) {
3193                            query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_1);
3194                    }
3195                    else if (portletId.equals(StringPool.BLANK)) {
3196                            query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_3);
3197                    }
3198                    else {
3199                            bindPortletId = true;
3200    
3201                            query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_2);
3202                    }
3203    
3204                    if (orderByComparator != null) {
3205                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3206    
3207                            if (orderByConditionFields.length > 0) {
3208                                    query.append(WHERE_AND);
3209                            }
3210    
3211                            for (int i = 0; i < orderByConditionFields.length; i++) {
3212                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3213                                    query.append(orderByConditionFields[i]);
3214    
3215                                    if ((i + 1) < orderByConditionFields.length) {
3216                                            if (orderByComparator.isAscending() ^ previous) {
3217                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3218                                            }
3219                                            else {
3220                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3221                                            }
3222                                    }
3223                                    else {
3224                                            if (orderByComparator.isAscending() ^ previous) {
3225                                                    query.append(WHERE_GREATER_THAN);
3226                                            }
3227                                            else {
3228                                                    query.append(WHERE_LESSER_THAN);
3229                                            }
3230                                    }
3231                            }
3232    
3233                            query.append(ORDER_BY_CLAUSE);
3234    
3235                            String[] orderByFields = orderByComparator.getOrderByFields();
3236    
3237                            for (int i = 0; i < orderByFields.length; i++) {
3238                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3239                                    query.append(orderByFields[i]);
3240    
3241                                    if ((i + 1) < orderByFields.length) {
3242                                            if (orderByComparator.isAscending() ^ previous) {
3243                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3244                                            }
3245                                            else {
3246                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3247                                            }
3248                                    }
3249                                    else {
3250                                            if (orderByComparator.isAscending() ^ previous) {
3251                                                    query.append(ORDER_BY_ASC);
3252                                            }
3253                                            else {
3254                                                    query.append(ORDER_BY_DESC);
3255                                            }
3256                                    }
3257                            }
3258                    }
3259                    else {
3260                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
3261                    }
3262    
3263                    String sql = query.toString();
3264    
3265                    Query q = session.createQuery(sql);
3266    
3267                    q.setFirstResult(0);
3268                    q.setMaxResults(2);
3269    
3270                    QueryPos qPos = QueryPos.getInstance(q);
3271    
3272                    qPos.add(ownerId);
3273    
3274                    qPos.add(ownerType);
3275    
3276                    if (bindPortletId) {
3277                            qPos.add(portletId);
3278                    }
3279    
3280                    if (orderByComparator != null) {
3281                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
3282    
3283                            for (Object value : values) {
3284                                    qPos.add(value);
3285                            }
3286                    }
3287    
3288                    List<PortletPreferences> list = q.list();
3289    
3290                    if (list.size() == 2) {
3291                            return list.get(1);
3292                    }
3293                    else {
3294                            return null;
3295                    }
3296            }
3297    
3298            /**
3299             * Removes all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and portletId = &#63; from the database.
3300             *
3301             * @param ownerId the owner ID
3302             * @param ownerType the owner type
3303             * @param portletId the portlet ID
3304             */
3305            @Override
3306            public void removeByO_O_PI(long ownerId, int ownerType, String portletId) {
3307                    for (PortletPreferences portletPreferences : findByO_O_PI(ownerId,
3308                                    ownerType, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3309                            remove(portletPreferences);
3310                    }
3311            }
3312    
3313            /**
3314             * Returns the number of portlet preferenceses where ownerId = &#63; and ownerType = &#63; and portletId = &#63;.
3315             *
3316             * @param ownerId the owner ID
3317             * @param ownerType the owner type
3318             * @param portletId the portlet ID
3319             * @return the number of matching portlet preferenceses
3320             */
3321            @Override
3322            public int countByO_O_PI(long ownerId, int ownerType, String portletId) {
3323                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O_PI;
3324    
3325                    Object[] finderArgs = new Object[] { ownerId, ownerType, portletId };
3326    
3327                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3328                                    this);
3329    
3330                    if (count == null) {
3331                            StringBundler query = new StringBundler(4);
3332    
3333                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
3334    
3335                            query.append(_FINDER_COLUMN_O_O_PI_OWNERID_2);
3336    
3337                            query.append(_FINDER_COLUMN_O_O_PI_OWNERTYPE_2);
3338    
3339                            boolean bindPortletId = false;
3340    
3341                            if (portletId == null) {
3342                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_1);
3343                            }
3344                            else if (portletId.equals(StringPool.BLANK)) {
3345                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_3);
3346                            }
3347                            else {
3348                                    bindPortletId = true;
3349    
3350                                    query.append(_FINDER_COLUMN_O_O_PI_PORTLETID_2);
3351                            }
3352    
3353                            String sql = query.toString();
3354    
3355                            Session session = null;
3356    
3357                            try {
3358                                    session = openSession();
3359    
3360                                    Query q = session.createQuery(sql);
3361    
3362                                    QueryPos qPos = QueryPos.getInstance(q);
3363    
3364                                    qPos.add(ownerId);
3365    
3366                                    qPos.add(ownerType);
3367    
3368                                    if (bindPortletId) {
3369                                            qPos.add(portletId);
3370                                    }
3371    
3372                                    count = (Long)q.uniqueResult();
3373    
3374                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3375                            }
3376                            catch (Exception e) {
3377                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3378    
3379                                    throw processException(e);
3380                            }
3381                            finally {
3382                                    closeSession(session);
3383                            }
3384                    }
3385    
3386                    return count.intValue();
3387            }
3388    
3389            private static final String _FINDER_COLUMN_O_O_PI_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
3390            private static final String _FINDER_COLUMN_O_O_PI_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
3391            private static final String _FINDER_COLUMN_O_O_PI_PORTLETID_1 = "portletPreferences.portletId IS NULL";
3392            private static final String _FINDER_COLUMN_O_O_PI_PORTLETID_2 = "portletPreferences.portletId = ?";
3393            private static final String _FINDER_COLUMN_O_O_PI_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
3394            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3395                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
3396                            PortletPreferencesImpl.class,
3397                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_P_P",
3398                            new String[] {
3399                                    Integer.class.getName(), Long.class.getName(),
3400                                    String.class.getName(),
3401                                    
3402                            Integer.class.getName(), Integer.class.getName(),
3403                                    OrderByComparator.class.getName()
3404                            });
3405            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3406                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
3407                            PortletPreferencesImpl.class,
3408                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_P_P",
3409                            new String[] {
3410                                    Integer.class.getName(), Long.class.getName(),
3411                                    String.class.getName()
3412                            },
3413                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
3414                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
3415                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
3416            public static final FinderPath FINDER_PATH_COUNT_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3417                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
3418                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_P_P",
3419                            new String[] {
3420                                    Integer.class.getName(), Long.class.getName(),
3421                                    String.class.getName()
3422                            });
3423    
3424            /**
3425             * Returns all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3426             *
3427             * @param ownerType the owner type
3428             * @param plid the plid
3429             * @param portletId the portlet ID
3430             * @return the matching portlet preferenceses
3431             */
3432            @Override
3433            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
3434                    String portletId) {
3435                    return findByO_P_P(ownerType, plid, portletId, QueryUtil.ALL_POS,
3436                            QueryUtil.ALL_POS, null);
3437            }
3438    
3439            /**
3440             * Returns a range of all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3441             *
3442             * <p>
3443             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
3444             * </p>
3445             *
3446             * @param ownerType the owner type
3447             * @param plid the plid
3448             * @param portletId the portlet ID
3449             * @param start the lower bound of the range of portlet preferenceses
3450             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
3451             * @return the range of matching portlet preferenceses
3452             */
3453            @Override
3454            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
3455                    String portletId, int start, int end) {
3456                    return findByO_P_P(ownerType, plid, portletId, start, end, null);
3457            }
3458    
3459            /**
3460             * Returns an ordered range of all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3461             *
3462             * <p>
3463             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
3464             * </p>
3465             *
3466             * @param ownerType the owner type
3467             * @param plid the plid
3468             * @param portletId the portlet ID
3469             * @param start the lower bound of the range of portlet preferenceses
3470             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
3471             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3472             * @return the ordered range of matching portlet preferenceses
3473             */
3474            @Override
3475            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
3476                    String portletId, int start, int end,
3477                    OrderByComparator<PortletPreferences> orderByComparator) {
3478                    boolean pagination = true;
3479                    FinderPath finderPath = null;
3480                    Object[] finderArgs = null;
3481    
3482                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3483                                    (orderByComparator == null)) {
3484                            pagination = false;
3485                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P;
3486                            finderArgs = new Object[] { ownerType, plid, portletId };
3487                    }
3488                    else {
3489                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P_P;
3490                            finderArgs = new Object[] {
3491                                            ownerType, plid, portletId,
3492                                            
3493                                            start, end, orderByComparator
3494                                    };
3495                    }
3496    
3497                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
3498                                    finderArgs, this);
3499    
3500                    if ((list != null) && !list.isEmpty()) {
3501                            for (PortletPreferences portletPreferences : list) {
3502                                    if ((ownerType != portletPreferences.getOwnerType()) ||
3503                                                    (plid != portletPreferences.getPlid()) ||
3504                                                    !Validator.equals(portletId,
3505                                                            portletPreferences.getPortletId())) {
3506                                            list = null;
3507    
3508                                            break;
3509                                    }
3510                            }
3511                    }
3512    
3513                    if (list == null) {
3514                            StringBundler query = null;
3515    
3516                            if (orderByComparator != null) {
3517                                    query = new StringBundler(5 +
3518                                                    (orderByComparator.getOrderByFields().length * 3));
3519                            }
3520                            else {
3521                                    query = new StringBundler(5);
3522                            }
3523    
3524                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
3525    
3526                            query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
3527    
3528                            query.append(_FINDER_COLUMN_O_P_P_PLID_2);
3529    
3530                            boolean bindPortletId = false;
3531    
3532                            if (portletId == null) {
3533                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
3534                            }
3535                            else if (portletId.equals(StringPool.BLANK)) {
3536                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
3537                            }
3538                            else {
3539                                    bindPortletId = true;
3540    
3541                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
3542                            }
3543    
3544                            if (orderByComparator != null) {
3545                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3546                                            orderByComparator);
3547                            }
3548                            else
3549                             if (pagination) {
3550                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
3551                            }
3552    
3553                            String sql = query.toString();
3554    
3555                            Session session = null;
3556    
3557                            try {
3558                                    session = openSession();
3559    
3560                                    Query q = session.createQuery(sql);
3561    
3562                                    QueryPos qPos = QueryPos.getInstance(q);
3563    
3564                                    qPos.add(ownerType);
3565    
3566                                    qPos.add(plid);
3567    
3568                                    if (bindPortletId) {
3569                                            qPos.add(portletId);
3570                                    }
3571    
3572                                    if (!pagination) {
3573                                            list = (List<PortletPreferences>)QueryUtil.list(q,
3574                                                            getDialect(), start, end, false);
3575    
3576                                            Collections.sort(list);
3577    
3578                                            list = Collections.unmodifiableList(list);
3579                                    }
3580                                    else {
3581                                            list = (List<PortletPreferences>)QueryUtil.list(q,
3582                                                            getDialect(), start, end);
3583                                    }
3584    
3585                                    cacheResult(list);
3586    
3587                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3588                            }
3589                            catch (Exception e) {
3590                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3591    
3592                                    throw processException(e);
3593                            }
3594                            finally {
3595                                    closeSession(session);
3596                            }
3597                    }
3598    
3599                    return list;
3600            }
3601    
3602            /**
3603             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3604             *
3605             * @param ownerType the owner type
3606             * @param plid the plid
3607             * @param portletId the portlet ID
3608             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3609             * @return the first matching portlet preferences
3610             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
3611             */
3612            @Override
3613            public PortletPreferences findByO_P_P_First(int ownerType, long plid,
3614                    String portletId,
3615                    OrderByComparator<PortletPreferences> orderByComparator)
3616                    throws NoSuchPortletPreferencesException {
3617                    PortletPreferences portletPreferences = fetchByO_P_P_First(ownerType,
3618                                    plid, portletId, orderByComparator);
3619    
3620                    if (portletPreferences != null) {
3621                            return portletPreferences;
3622                    }
3623    
3624                    StringBundler msg = new StringBundler(8);
3625    
3626                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3627    
3628                    msg.append("ownerType=");
3629                    msg.append(ownerType);
3630    
3631                    msg.append(", plid=");
3632                    msg.append(plid);
3633    
3634                    msg.append(", portletId=");
3635                    msg.append(portletId);
3636    
3637                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3638    
3639                    throw new NoSuchPortletPreferencesException(msg.toString());
3640            }
3641    
3642            /**
3643             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3644             *
3645             * @param ownerType the owner type
3646             * @param plid the plid
3647             * @param portletId the portlet ID
3648             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3649             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
3650             */
3651            @Override
3652            public PortletPreferences fetchByO_P_P_First(int ownerType, long plid,
3653                    String portletId,
3654                    OrderByComparator<PortletPreferences> orderByComparator) {
3655                    List<PortletPreferences> list = findByO_P_P(ownerType, plid, portletId,
3656                                    0, 1, orderByComparator);
3657    
3658                    if (!list.isEmpty()) {
3659                            return list.get(0);
3660                    }
3661    
3662                    return null;
3663            }
3664    
3665            /**
3666             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3667             *
3668             * @param ownerType the owner type
3669             * @param plid the plid
3670             * @param portletId the portlet ID
3671             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3672             * @return the last matching portlet preferences
3673             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
3674             */
3675            @Override
3676            public PortletPreferences findByO_P_P_Last(int ownerType, long plid,
3677                    String portletId,
3678                    OrderByComparator<PortletPreferences> orderByComparator)
3679                    throws NoSuchPortletPreferencesException {
3680                    PortletPreferences portletPreferences = fetchByO_P_P_Last(ownerType,
3681                                    plid, portletId, orderByComparator);
3682    
3683                    if (portletPreferences != null) {
3684                            return portletPreferences;
3685                    }
3686    
3687                    StringBundler msg = new StringBundler(8);
3688    
3689                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3690    
3691                    msg.append("ownerType=");
3692                    msg.append(ownerType);
3693    
3694                    msg.append(", plid=");
3695                    msg.append(plid);
3696    
3697                    msg.append(", portletId=");
3698                    msg.append(portletId);
3699    
3700                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3701    
3702                    throw new NoSuchPortletPreferencesException(msg.toString());
3703            }
3704    
3705            /**
3706             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3707             *
3708             * @param ownerType the owner type
3709             * @param plid the plid
3710             * @param portletId the portlet ID
3711             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3712             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
3713             */
3714            @Override
3715            public PortletPreferences fetchByO_P_P_Last(int ownerType, long plid,
3716                    String portletId,
3717                    OrderByComparator<PortletPreferences> orderByComparator) {
3718                    int count = countByO_P_P(ownerType, plid, portletId);
3719    
3720                    if (count == 0) {
3721                            return null;
3722                    }
3723    
3724                    List<PortletPreferences> list = findByO_P_P(ownerType, plid, portletId,
3725                                    count - 1, count, orderByComparator);
3726    
3727                    if (!list.isEmpty()) {
3728                            return list.get(0);
3729                    }
3730    
3731                    return null;
3732            }
3733    
3734            /**
3735             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3736             *
3737             * @param portletPreferencesId the primary key of the current portlet preferences
3738             * @param ownerType the owner type
3739             * @param plid the plid
3740             * @param portletId the portlet ID
3741             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3742             * @return the previous, current, and next portlet preferences
3743             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
3744             */
3745            @Override
3746            public PortletPreferences[] findByO_P_P_PrevAndNext(
3747                    long portletPreferencesId, int ownerType, long plid, String portletId,
3748                    OrderByComparator<PortletPreferences> orderByComparator)
3749                    throws NoSuchPortletPreferencesException {
3750                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
3751    
3752                    Session session = null;
3753    
3754                    try {
3755                            session = openSession();
3756    
3757                            PortletPreferences[] array = new PortletPreferencesImpl[3];
3758    
3759                            array[0] = getByO_P_P_PrevAndNext(session, portletPreferences,
3760                                            ownerType, plid, portletId, orderByComparator, true);
3761    
3762                            array[1] = portletPreferences;
3763    
3764                            array[2] = getByO_P_P_PrevAndNext(session, portletPreferences,
3765                                            ownerType, plid, portletId, orderByComparator, false);
3766    
3767                            return array;
3768                    }
3769                    catch (Exception e) {
3770                            throw processException(e);
3771                    }
3772                    finally {
3773                            closeSession(session);
3774                    }
3775            }
3776    
3777            protected PortletPreferences getByO_P_P_PrevAndNext(Session session,
3778                    PortletPreferences portletPreferences, int ownerType, long plid,
3779                    String portletId,
3780                    OrderByComparator<PortletPreferences> orderByComparator,
3781                    boolean previous) {
3782                    StringBundler query = null;
3783    
3784                    if (orderByComparator != null) {
3785                            query = new StringBundler(6 +
3786                                            (orderByComparator.getOrderByFields().length * 6));
3787                    }
3788                    else {
3789                            query = new StringBundler(3);
3790                    }
3791    
3792                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
3793    
3794                    query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
3795    
3796                    query.append(_FINDER_COLUMN_O_P_P_PLID_2);
3797    
3798                    boolean bindPortletId = false;
3799    
3800                    if (portletId == null) {
3801                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
3802                    }
3803                    else if (portletId.equals(StringPool.BLANK)) {
3804                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
3805                    }
3806                    else {
3807                            bindPortletId = true;
3808    
3809                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
3810                    }
3811    
3812                    if (orderByComparator != null) {
3813                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3814    
3815                            if (orderByConditionFields.length > 0) {
3816                                    query.append(WHERE_AND);
3817                            }
3818    
3819                            for (int i = 0; i < orderByConditionFields.length; i++) {
3820                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3821                                    query.append(orderByConditionFields[i]);
3822    
3823                                    if ((i + 1) < orderByConditionFields.length) {
3824                                            if (orderByComparator.isAscending() ^ previous) {
3825                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3826                                            }
3827                                            else {
3828                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3829                                            }
3830                                    }
3831                                    else {
3832                                            if (orderByComparator.isAscending() ^ previous) {
3833                                                    query.append(WHERE_GREATER_THAN);
3834                                            }
3835                                            else {
3836                                                    query.append(WHERE_LESSER_THAN);
3837                                            }
3838                                    }
3839                            }
3840    
3841                            query.append(ORDER_BY_CLAUSE);
3842    
3843                            String[] orderByFields = orderByComparator.getOrderByFields();
3844    
3845                            for (int i = 0; i < orderByFields.length; i++) {
3846                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3847                                    query.append(orderByFields[i]);
3848    
3849                                    if ((i + 1) < orderByFields.length) {
3850                                            if (orderByComparator.isAscending() ^ previous) {
3851                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3852                                            }
3853                                            else {
3854                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3855                                            }
3856                                    }
3857                                    else {
3858                                            if (orderByComparator.isAscending() ^ previous) {
3859                                                    query.append(ORDER_BY_ASC);
3860                                            }
3861                                            else {
3862                                                    query.append(ORDER_BY_DESC);
3863                                            }
3864                                    }
3865                            }
3866                    }
3867                    else {
3868                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
3869                    }
3870    
3871                    String sql = query.toString();
3872    
3873                    Query q = session.createQuery(sql);
3874    
3875                    q.setFirstResult(0);
3876                    q.setMaxResults(2);
3877    
3878                    QueryPos qPos = QueryPos.getInstance(q);
3879    
3880                    qPos.add(ownerType);
3881    
3882                    qPos.add(plid);
3883    
3884                    if (bindPortletId) {
3885                            qPos.add(portletId);
3886                    }
3887    
3888                    if (orderByComparator != null) {
3889                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
3890    
3891                            for (Object value : values) {
3892                                    qPos.add(value);
3893                            }
3894                    }
3895    
3896                    List<PortletPreferences> list = q.list();
3897    
3898                    if (list.size() == 2) {
3899                            return list.get(1);
3900                    }
3901                    else {
3902                            return null;
3903                    }
3904            }
3905    
3906            /**
3907             * Removes all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63; from the database.
3908             *
3909             * @param ownerType the owner type
3910             * @param plid the plid
3911             * @param portletId the portlet ID
3912             */
3913            @Override
3914            public void removeByO_P_P(int ownerType, long plid, String portletId) {
3915                    for (PortletPreferences portletPreferences : findByO_P_P(ownerType,
3916                                    plid, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3917                            remove(portletPreferences);
3918                    }
3919            }
3920    
3921            /**
3922             * Returns the number of portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
3923             *
3924             * @param ownerType the owner type
3925             * @param plid the plid
3926             * @param portletId the portlet ID
3927             * @return the number of matching portlet preferenceses
3928             */
3929            @Override
3930            public int countByO_P_P(int ownerType, long plid, String portletId) {
3931                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_P_P;
3932    
3933                    Object[] finderArgs = new Object[] { ownerType, plid, portletId };
3934    
3935                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3936                                    this);
3937    
3938                    if (count == null) {
3939                            StringBundler query = new StringBundler(4);
3940    
3941                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
3942    
3943                            query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
3944    
3945                            query.append(_FINDER_COLUMN_O_P_P_PLID_2);
3946    
3947                            boolean bindPortletId = false;
3948    
3949                            if (portletId == null) {
3950                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
3951                            }
3952                            else if (portletId.equals(StringPool.BLANK)) {
3953                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
3954                            }
3955                            else {
3956                                    bindPortletId = true;
3957    
3958                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
3959                            }
3960    
3961                            String sql = query.toString();
3962    
3963                            Session session = null;
3964    
3965                            try {
3966                                    session = openSession();
3967    
3968                                    Query q = session.createQuery(sql);
3969    
3970                                    QueryPos qPos = QueryPos.getInstance(q);
3971    
3972                                    qPos.add(ownerType);
3973    
3974                                    qPos.add(plid);
3975    
3976                                    if (bindPortletId) {
3977                                            qPos.add(portletId);
3978                                    }
3979    
3980                                    count = (Long)q.uniqueResult();
3981    
3982                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3983                            }
3984                            catch (Exception e) {
3985                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3986    
3987                                    throw processException(e);
3988                            }
3989                            finally {
3990                                    closeSession(session);
3991                            }
3992                    }
3993    
3994                    return count.intValue();
3995            }
3996    
3997            private static final String _FINDER_COLUMN_O_P_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
3998            private static final String _FINDER_COLUMN_O_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
3999            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
4000            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
4001            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
4002            public static final FinderPath FINDER_PATH_FETCH_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4003                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
4004                            PortletPreferencesImpl.class, FINDER_CLASS_NAME_ENTITY,
4005                            "fetchByO_O_P_P",
4006                            new String[] {
4007                                    Long.class.getName(), Integer.class.getName(),
4008                                    Long.class.getName(), String.class.getName()
4009                            },
4010                            PortletPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
4011                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
4012                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
4013                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
4014            public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4015                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
4016                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O_P_P",
4017                            new String[] {
4018                                    Long.class.getName(), Integer.class.getName(),
4019                                    Long.class.getName(), String.class.getName()
4020                            });
4021    
4022            /**
4023             * Returns the portlet preferences where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63; or throws a {@link com.liferay.portal.NoSuchPortletPreferencesException} if it could not be found.
4024             *
4025             * @param ownerId the owner ID
4026             * @param ownerType the owner type
4027             * @param plid the plid
4028             * @param portletId the portlet ID
4029             * @return the matching portlet preferences
4030             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
4031             */
4032            @Override
4033            public PortletPreferences findByO_O_P_P(long ownerId, int ownerType,
4034                    long plid, String portletId) throws NoSuchPortletPreferencesException {
4035                    PortletPreferences portletPreferences = fetchByO_O_P_P(ownerId,
4036                                    ownerType, plid, portletId);
4037    
4038                    if (portletPreferences == null) {
4039                            StringBundler msg = new StringBundler(10);
4040    
4041                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4042    
4043                            msg.append("ownerId=");
4044                            msg.append(ownerId);
4045    
4046                            msg.append(", ownerType=");
4047                            msg.append(ownerType);
4048    
4049                            msg.append(", plid=");
4050                            msg.append(plid);
4051    
4052                            msg.append(", portletId=");
4053                            msg.append(portletId);
4054    
4055                            msg.append(StringPool.CLOSE_CURLY_BRACE);
4056    
4057                            if (_log.isWarnEnabled()) {
4058                                    _log.warn(msg.toString());
4059                            }
4060    
4061                            throw new NoSuchPortletPreferencesException(msg.toString());
4062                    }
4063    
4064                    return portletPreferences;
4065            }
4066    
4067            /**
4068             * Returns the portlet preferences where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
4069             *
4070             * @param ownerId the owner ID
4071             * @param ownerType the owner type
4072             * @param plid the plid
4073             * @param portletId the portlet ID
4074             * @return the matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
4075             */
4076            @Override
4077            public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
4078                    long plid, String portletId) {
4079                    return fetchByO_O_P_P(ownerId, ownerType, plid, portletId, true);
4080            }
4081    
4082            /**
4083             * Returns the portlet preferences where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
4084             *
4085             * @param ownerId the owner ID
4086             * @param ownerType the owner type
4087             * @param plid the plid
4088             * @param portletId the portlet ID
4089             * @param retrieveFromCache whether to use the finder cache
4090             * @return the matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
4091             */
4092            @Override
4093            public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
4094                    long plid, String portletId, boolean retrieveFromCache) {
4095                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid, portletId };
4096    
4097                    Object result = null;
4098    
4099                    if (retrieveFromCache) {
4100                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_O_O_P_P,
4101                                            finderArgs, this);
4102                    }
4103    
4104                    if (result instanceof PortletPreferences) {
4105                            PortletPreferences portletPreferences = (PortletPreferences)result;
4106    
4107                            if ((ownerId != portletPreferences.getOwnerId()) ||
4108                                            (ownerType != portletPreferences.getOwnerType()) ||
4109                                            (plid != portletPreferences.getPlid()) ||
4110                                            !Validator.equals(portletId,
4111                                                    portletPreferences.getPortletId())) {
4112                                    result = null;
4113                            }
4114                    }
4115    
4116                    if (result == null) {
4117                            StringBundler query = new StringBundler(6);
4118    
4119                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
4120    
4121                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
4122    
4123                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
4124    
4125                            query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
4126    
4127                            boolean bindPortletId = false;
4128    
4129                            if (portletId == null) {
4130                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
4131                            }
4132                            else if (portletId.equals(StringPool.BLANK)) {
4133                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
4134                            }
4135                            else {
4136                                    bindPortletId = true;
4137    
4138                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
4139                            }
4140    
4141                            String sql = query.toString();
4142    
4143                            Session session = null;
4144    
4145                            try {
4146                                    session = openSession();
4147    
4148                                    Query q = session.createQuery(sql);
4149    
4150                                    QueryPos qPos = QueryPos.getInstance(q);
4151    
4152                                    qPos.add(ownerId);
4153    
4154                                    qPos.add(ownerType);
4155    
4156                                    qPos.add(plid);
4157    
4158                                    if (bindPortletId) {
4159                                            qPos.add(portletId);
4160                                    }
4161    
4162                                    List<PortletPreferences> list = q.list();
4163    
4164                                    if (list.isEmpty()) {
4165                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
4166                                                    finderArgs, list);
4167                                    }
4168                                    else {
4169                                            PortletPreferences portletPreferences = list.get(0);
4170    
4171                                            result = portletPreferences;
4172    
4173                                            cacheResult(portletPreferences);
4174    
4175                                            if ((portletPreferences.getOwnerId() != ownerId) ||
4176                                                            (portletPreferences.getOwnerType() != ownerType) ||
4177                                                            (portletPreferences.getPlid() != plid) ||
4178                                                            (portletPreferences.getPortletId() == null) ||
4179                                                            !portletPreferences.getPortletId().equals(portletId)) {
4180                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
4181                                                            finderArgs, portletPreferences);
4182                                            }
4183                                    }
4184                            }
4185                            catch (Exception e) {
4186                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P,
4187                                            finderArgs);
4188    
4189                                    throw processException(e);
4190                            }
4191                            finally {
4192                                    closeSession(session);
4193                            }
4194                    }
4195    
4196                    if (result instanceof List<?>) {
4197                            return null;
4198                    }
4199                    else {
4200                            return (PortletPreferences)result;
4201                    }
4202            }
4203    
4204            /**
4205             * Removes the portlet preferences where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63; from the database.
4206             *
4207             * @param ownerId the owner ID
4208             * @param ownerType the owner type
4209             * @param plid the plid
4210             * @param portletId the portlet ID
4211             * @return the portlet preferences that was removed
4212             */
4213            @Override
4214            public PortletPreferences removeByO_O_P_P(long ownerId, int ownerType,
4215                    long plid, String portletId) throws NoSuchPortletPreferencesException {
4216                    PortletPreferences portletPreferences = findByO_O_P_P(ownerId,
4217                                    ownerType, plid, portletId);
4218    
4219                    return remove(portletPreferences);
4220            }
4221    
4222            /**
4223             * Returns the number of portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63;.
4224             *
4225             * @param ownerId the owner ID
4226             * @param ownerType the owner type
4227             * @param plid the plid
4228             * @param portletId the portlet ID
4229             * @return the number of matching portlet preferenceses
4230             */
4231            @Override
4232            public int countByO_O_P_P(long ownerId, int ownerType, long plid,
4233                    String portletId) {
4234                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O_P_P;
4235    
4236                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid, portletId };
4237    
4238                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
4239                                    this);
4240    
4241                    if (count == null) {
4242                            StringBundler query = new StringBundler(5);
4243    
4244                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
4245    
4246                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
4247    
4248                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
4249    
4250                            query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
4251    
4252                            boolean bindPortletId = false;
4253    
4254                            if (portletId == null) {
4255                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
4256                            }
4257                            else if (portletId.equals(StringPool.BLANK)) {
4258                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
4259                            }
4260                            else {
4261                                    bindPortletId = true;
4262    
4263                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
4264                            }
4265    
4266                            String sql = query.toString();
4267    
4268                            Session session = null;
4269    
4270                            try {
4271                                    session = openSession();
4272    
4273                                    Query q = session.createQuery(sql);
4274    
4275                                    QueryPos qPos = QueryPos.getInstance(q);
4276    
4277                                    qPos.add(ownerId);
4278    
4279                                    qPos.add(ownerType);
4280    
4281                                    qPos.add(plid);
4282    
4283                                    if (bindPortletId) {
4284                                            qPos.add(portletId);
4285                                    }
4286    
4287                                    count = (Long)q.uniqueResult();
4288    
4289                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
4290                            }
4291                            catch (Exception e) {
4292                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4293    
4294                                    throw processException(e);
4295                            }
4296                            finally {
4297                                    closeSession(session);
4298                            }
4299                    }
4300    
4301                    return count.intValue();
4302            }
4303    
4304            private static final String _FINDER_COLUMN_O_O_P_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
4305            private static final String _FINDER_COLUMN_O_O_P_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
4306            private static final String _FINDER_COLUMN_O_O_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
4307            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
4308            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
4309            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = '')";
4310    
4311            public PortletPreferencesPersistenceImpl() {
4312                    setModelClass(PortletPreferences.class);
4313            }
4314    
4315            /**
4316             * Caches the portlet preferences in the entity cache if it is enabled.
4317             *
4318             * @param portletPreferences the portlet preferences
4319             */
4320            @Override
4321            public void cacheResult(PortletPreferences portletPreferences) {
4322                    EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4323                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
4324                            portletPreferences);
4325    
4326                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
4327                            new Object[] {
4328                                    portletPreferences.getOwnerId(),
4329                                    portletPreferences.getOwnerType(), portletPreferences.getPlid(),
4330                                    portletPreferences.getPortletId()
4331                            }, portletPreferences);
4332    
4333                    portletPreferences.resetOriginalValues();
4334            }
4335    
4336            /**
4337             * Caches the portlet preferenceses in the entity cache if it is enabled.
4338             *
4339             * @param portletPreferenceses the portlet preferenceses
4340             */
4341            @Override
4342            public void cacheResult(List<PortletPreferences> portletPreferenceses) {
4343                    for (PortletPreferences portletPreferences : portletPreferenceses) {
4344                            if (EntityCacheUtil.getResult(
4345                                                    PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4346                                                    PortletPreferencesImpl.class,
4347                                                    portletPreferences.getPrimaryKey()) == null) {
4348                                    cacheResult(portletPreferences);
4349                            }
4350                            else {
4351                                    portletPreferences.resetOriginalValues();
4352                            }
4353                    }
4354            }
4355    
4356            /**
4357             * Clears the cache for all portlet preferenceses.
4358             *
4359             * <p>
4360             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4361             * </p>
4362             */
4363            @Override
4364            public void clearCache() {
4365                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
4366                            CacheRegistryUtil.clear(PortletPreferencesImpl.class.getName());
4367                    }
4368    
4369                    EntityCacheUtil.clearCache(PortletPreferencesImpl.class);
4370    
4371                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
4372                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4373                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4374            }
4375    
4376            /**
4377             * Clears the cache for the portlet preferences.
4378             *
4379             * <p>
4380             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4381             * </p>
4382             */
4383            @Override
4384            public void clearCache(PortletPreferences portletPreferences) {
4385                    EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4386                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
4387    
4388                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4389                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4390    
4391                    clearUniqueFindersCache(portletPreferences);
4392            }
4393    
4394            @Override
4395            public void clearCache(List<PortletPreferences> portletPreferenceses) {
4396                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4397                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4398    
4399                    for (PortletPreferences portletPreferences : portletPreferenceses) {
4400                            EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4401                                    PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
4402    
4403                            clearUniqueFindersCache(portletPreferences);
4404                    }
4405            }
4406    
4407            protected void cacheUniqueFindersCache(
4408                    PortletPreferences portletPreferences) {
4409                    if (portletPreferences.isNew()) {
4410                            Object[] args = new Object[] {
4411                                            portletPreferences.getOwnerId(),
4412                                            portletPreferences.getOwnerType(),
4413                                            portletPreferences.getPlid(),
4414                                            portletPreferences.getPortletId()
4415                                    };
4416    
4417                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P_P, args,
4418                                    Long.valueOf(1));
4419                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P, args,
4420                                    portletPreferences);
4421                    }
4422                    else {
4423                            PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
4424    
4425                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4426                                            FINDER_PATH_FETCH_BY_O_O_P_P.getColumnBitmask()) != 0) {
4427                                    Object[] args = new Object[] {
4428                                                    portletPreferences.getOwnerId(),
4429                                                    portletPreferences.getOwnerType(),
4430                                                    portletPreferences.getPlid(),
4431                                                    portletPreferences.getPortletId()
4432                                            };
4433    
4434                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P_P, args,
4435                                            Long.valueOf(1));
4436                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P, args,
4437                                            portletPreferences);
4438                            }
4439                    }
4440            }
4441    
4442            protected void clearUniqueFindersCache(
4443                    PortletPreferences portletPreferences) {
4444                    PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
4445    
4446                    Object[] args = new Object[] {
4447                                    portletPreferences.getOwnerId(),
4448                                    portletPreferences.getOwnerType(), portletPreferences.getPlid(),
4449                                    portletPreferences.getPortletId()
4450                            };
4451    
4452                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P_P, args);
4453                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P, args);
4454    
4455                    if ((portletPreferencesModelImpl.getColumnBitmask() &
4456                                    FINDER_PATH_FETCH_BY_O_O_P_P.getColumnBitmask()) != 0) {
4457                            args = new Object[] {
4458                                            portletPreferencesModelImpl.getOriginalOwnerId(),
4459                                            portletPreferencesModelImpl.getOriginalOwnerType(),
4460                                            portletPreferencesModelImpl.getOriginalPlid(),
4461                                            portletPreferencesModelImpl.getOriginalPortletId()
4462                                    };
4463    
4464                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P_P, args);
4465                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P, args);
4466                    }
4467            }
4468    
4469            /**
4470             * Creates a new portlet preferences with the primary key. Does not add the portlet preferences to the database.
4471             *
4472             * @param portletPreferencesId the primary key for the new portlet preferences
4473             * @return the new portlet preferences
4474             */
4475            @Override
4476            public PortletPreferences create(long portletPreferencesId) {
4477                    PortletPreferences portletPreferences = new PortletPreferencesImpl();
4478    
4479                    portletPreferences.setNew(true);
4480                    portletPreferences.setPrimaryKey(portletPreferencesId);
4481    
4482                    return portletPreferences;
4483            }
4484    
4485            /**
4486             * Removes the portlet preferences with the primary key from the database. Also notifies the appropriate model listeners.
4487             *
4488             * @param portletPreferencesId the primary key of the portlet preferences
4489             * @return the portlet preferences that was removed
4490             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
4491             */
4492            @Override
4493            public PortletPreferences remove(long portletPreferencesId)
4494                    throws NoSuchPortletPreferencesException {
4495                    return remove((Serializable)portletPreferencesId);
4496            }
4497    
4498            /**
4499             * Removes the portlet preferences with the primary key from the database. Also notifies the appropriate model listeners.
4500             *
4501             * @param primaryKey the primary key of the portlet preferences
4502             * @return the portlet preferences that was removed
4503             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
4504             */
4505            @Override
4506            public PortletPreferences remove(Serializable primaryKey)
4507                    throws NoSuchPortletPreferencesException {
4508                    Session session = null;
4509    
4510                    try {
4511                            session = openSession();
4512    
4513                            PortletPreferences portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
4514                                            primaryKey);
4515    
4516                            if (portletPreferences == null) {
4517                                    if (_log.isWarnEnabled()) {
4518                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4519                                    }
4520    
4521                                    throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4522                                            primaryKey);
4523                            }
4524    
4525                            return remove(portletPreferences);
4526                    }
4527                    catch (NoSuchPortletPreferencesException nsee) {
4528                            throw nsee;
4529                    }
4530                    catch (Exception e) {
4531                            throw processException(e);
4532                    }
4533                    finally {
4534                            closeSession(session);
4535                    }
4536            }
4537    
4538            @Override
4539            protected PortletPreferences removeImpl(
4540                    PortletPreferences portletPreferences) {
4541                    portletPreferences = toUnwrappedModel(portletPreferences);
4542    
4543                    Session session = null;
4544    
4545                    try {
4546                            session = openSession();
4547    
4548                            if (!session.contains(portletPreferences)) {
4549                                    portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
4550                                                    portletPreferences.getPrimaryKeyObj());
4551                            }
4552    
4553                            if (portletPreferences != null) {
4554                                    session.delete(portletPreferences);
4555                            }
4556                    }
4557                    catch (Exception e) {
4558                            throw processException(e);
4559                    }
4560                    finally {
4561                            closeSession(session);
4562                    }
4563    
4564                    if (portletPreferences != null) {
4565                            clearCache(portletPreferences);
4566                    }
4567    
4568                    return portletPreferences;
4569            }
4570    
4571            @Override
4572            public PortletPreferences updateImpl(
4573                    com.liferay.portal.model.PortletPreferences portletPreferences) {
4574                    portletPreferences = toUnwrappedModel(portletPreferences);
4575    
4576                    boolean isNew = portletPreferences.isNew();
4577    
4578                    PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
4579    
4580                    Session session = null;
4581    
4582                    try {
4583                            session = openSession();
4584    
4585                            if (portletPreferences.isNew()) {
4586                                    session.save(portletPreferences);
4587    
4588                                    portletPreferences.setNew(false);
4589                            }
4590                            else {
4591                                    session.merge(portletPreferences);
4592                            }
4593                    }
4594                    catch (Exception e) {
4595                            throw processException(e);
4596                    }
4597                    finally {
4598                            closeSession(session);
4599                    }
4600    
4601                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4602    
4603                    if (isNew || !PortletPreferencesModelImpl.COLUMN_BITMASK_ENABLED) {
4604                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4605                    }
4606    
4607                    else {
4608                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4609                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID.getColumnBitmask()) != 0) {
4610                                    Object[] args = new Object[] {
4611                                                    portletPreferencesModelImpl.getOriginalPlid()
4612                                            };
4613    
4614                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PLID, args);
4615                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID,
4616                                            args);
4617    
4618                                    args = new Object[] { portletPreferencesModelImpl.getPlid() };
4619    
4620                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PLID, args);
4621                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID,
4622                                            args);
4623                            }
4624    
4625                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4626                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID.getColumnBitmask()) != 0) {
4627                                    Object[] args = new Object[] {
4628                                                    portletPreferencesModelImpl.getOriginalPortletId()
4629                                            };
4630    
4631                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PORTLETID,
4632                                            args);
4633                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID,
4634                                            args);
4635    
4636                                    args = new Object[] { portletPreferencesModelImpl.getPortletId() };
4637    
4638                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PORTLETID,
4639                                            args);
4640                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID,
4641                                            args);
4642                            }
4643    
4644                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4645                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P.getColumnBitmask()) != 0) {
4646                                    Object[] args = new Object[] {
4647                                                    portletPreferencesModelImpl.getOriginalOwnerType(),
4648                                                    portletPreferencesModelImpl.getOriginalPortletId()
4649                                            };
4650    
4651                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P, args);
4652                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P,
4653                                            args);
4654    
4655                                    args = new Object[] {
4656                                                    portletPreferencesModelImpl.getOwnerType(),
4657                                                    portletPreferencesModelImpl.getPortletId()
4658                                            };
4659    
4660                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P, args);
4661                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P,
4662                                            args);
4663                            }
4664    
4665                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4666                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P.getColumnBitmask()) != 0) {
4667                                    Object[] args = new Object[] {
4668                                                    portletPreferencesModelImpl.getOriginalPlid(),
4669                                                    portletPreferencesModelImpl.getOriginalPortletId()
4670                                            };
4671    
4672                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_P_P, args);
4673                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P,
4674                                            args);
4675    
4676                                    args = new Object[] {
4677                                                    portletPreferencesModelImpl.getPlid(),
4678                                                    portletPreferencesModelImpl.getPortletId()
4679                                            };
4680    
4681                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_P_P, args);
4682                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P,
4683                                            args);
4684                            }
4685    
4686                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4687                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P.getColumnBitmask()) != 0) {
4688                                    Object[] args = new Object[] {
4689                                                    portletPreferencesModelImpl.getOriginalOwnerId(),
4690                                                    portletPreferencesModelImpl.getOriginalOwnerType(),
4691                                                    portletPreferencesModelImpl.getOriginalPlid()
4692                                            };
4693    
4694                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P, args);
4695                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P,
4696                                            args);
4697    
4698                                    args = new Object[] {
4699                                                    portletPreferencesModelImpl.getOwnerId(),
4700                                                    portletPreferencesModelImpl.getOwnerType(),
4701                                                    portletPreferencesModelImpl.getPlid()
4702                                            };
4703    
4704                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P, args);
4705                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P,
4706                                            args);
4707                            }
4708    
4709                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4710                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_PI.getColumnBitmask()) != 0) {
4711                                    Object[] args = new Object[] {
4712                                                    portletPreferencesModelImpl.getOriginalOwnerId(),
4713                                                    portletPreferencesModelImpl.getOriginalOwnerType(),
4714                                                    portletPreferencesModelImpl.getOriginalPortletId()
4715                                            };
4716    
4717                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_PI, args);
4718                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_PI,
4719                                            args);
4720    
4721                                    args = new Object[] {
4722                                                    portletPreferencesModelImpl.getOwnerId(),
4723                                                    portletPreferencesModelImpl.getOwnerType(),
4724                                                    portletPreferencesModelImpl.getPortletId()
4725                                            };
4726    
4727                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_PI, args);
4728                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_PI,
4729                                            args);
4730                            }
4731    
4732                            if ((portletPreferencesModelImpl.getColumnBitmask() &
4733                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P.getColumnBitmask()) != 0) {
4734                                    Object[] args = new Object[] {
4735                                                    portletPreferencesModelImpl.getOriginalOwnerType(),
4736                                                    portletPreferencesModelImpl.getOriginalPlid(),
4737                                                    portletPreferencesModelImpl.getOriginalPortletId()
4738                                            };
4739    
4740                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P_P, args);
4741                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P,
4742                                            args);
4743    
4744                                    args = new Object[] {
4745                                                    portletPreferencesModelImpl.getOwnerType(),
4746                                                    portletPreferencesModelImpl.getPlid(),
4747                                                    portletPreferencesModelImpl.getPortletId()
4748                                            };
4749    
4750                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P_P, args);
4751                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P,
4752                                            args);
4753                            }
4754                    }
4755    
4756                    EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4757                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
4758                            portletPreferences, false);
4759    
4760                    clearUniqueFindersCache(portletPreferences);
4761                    cacheUniqueFindersCache(portletPreferences);
4762    
4763                    portletPreferences.resetOriginalValues();
4764    
4765                    return portletPreferences;
4766            }
4767    
4768            protected PortletPreferences toUnwrappedModel(
4769                    PortletPreferences portletPreferences) {
4770                    if (portletPreferences instanceof PortletPreferencesImpl) {
4771                            return portletPreferences;
4772                    }
4773    
4774                    PortletPreferencesImpl portletPreferencesImpl = new PortletPreferencesImpl();
4775    
4776                    portletPreferencesImpl.setNew(portletPreferences.isNew());
4777                    portletPreferencesImpl.setPrimaryKey(portletPreferences.getPrimaryKey());
4778    
4779                    portletPreferencesImpl.setMvccVersion(portletPreferences.getMvccVersion());
4780                    portletPreferencesImpl.setPortletPreferencesId(portletPreferences.getPortletPreferencesId());
4781                    portletPreferencesImpl.setOwnerId(portletPreferences.getOwnerId());
4782                    portletPreferencesImpl.setOwnerType(portletPreferences.getOwnerType());
4783                    portletPreferencesImpl.setPlid(portletPreferences.getPlid());
4784                    portletPreferencesImpl.setPortletId(portletPreferences.getPortletId());
4785                    portletPreferencesImpl.setPreferences(portletPreferences.getPreferences());
4786    
4787                    return portletPreferencesImpl;
4788            }
4789    
4790            /**
4791             * Returns the portlet preferences with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
4792             *
4793             * @param primaryKey the primary key of the portlet preferences
4794             * @return the portlet preferences
4795             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
4796             */
4797            @Override
4798            public PortletPreferences findByPrimaryKey(Serializable primaryKey)
4799                    throws NoSuchPortletPreferencesException {
4800                    PortletPreferences portletPreferences = fetchByPrimaryKey(primaryKey);
4801    
4802                    if (portletPreferences == null) {
4803                            if (_log.isWarnEnabled()) {
4804                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4805                            }
4806    
4807                            throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4808                                    primaryKey);
4809                    }
4810    
4811                    return portletPreferences;
4812            }
4813    
4814            /**
4815             * Returns the portlet preferences with the primary key or throws a {@link com.liferay.portal.NoSuchPortletPreferencesException} if it could not be found.
4816             *
4817             * @param portletPreferencesId the primary key of the portlet preferences
4818             * @return the portlet preferences
4819             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
4820             */
4821            @Override
4822            public PortletPreferences findByPrimaryKey(long portletPreferencesId)
4823                    throws NoSuchPortletPreferencesException {
4824                    return findByPrimaryKey((Serializable)portletPreferencesId);
4825            }
4826    
4827            /**
4828             * Returns the portlet preferences with the primary key or returns <code>null</code> if it could not be found.
4829             *
4830             * @param primaryKey the primary key of the portlet preferences
4831             * @return the portlet preferences, or <code>null</code> if a portlet preferences with the primary key could not be found
4832             */
4833            @Override
4834            public PortletPreferences fetchByPrimaryKey(Serializable primaryKey) {
4835                    PortletPreferences portletPreferences = (PortletPreferences)EntityCacheUtil.getResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4836                                    PortletPreferencesImpl.class, primaryKey);
4837    
4838                    if (portletPreferences == _nullPortletPreferences) {
4839                            return null;
4840                    }
4841    
4842                    if (portletPreferences == null) {
4843                            Session session = null;
4844    
4845                            try {
4846                                    session = openSession();
4847    
4848                                    portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
4849                                                    primaryKey);
4850    
4851                                    if (portletPreferences != null) {
4852                                            cacheResult(portletPreferences);
4853                                    }
4854                                    else {
4855                                            EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4856                                                    PortletPreferencesImpl.class, primaryKey,
4857                                                    _nullPortletPreferences);
4858                                    }
4859                            }
4860                            catch (Exception e) {
4861                                    EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4862                                            PortletPreferencesImpl.class, primaryKey);
4863    
4864                                    throw processException(e);
4865                            }
4866                            finally {
4867                                    closeSession(session);
4868                            }
4869                    }
4870    
4871                    return portletPreferences;
4872            }
4873    
4874            /**
4875             * Returns the portlet preferences with the primary key or returns <code>null</code> if it could not be found.
4876             *
4877             * @param portletPreferencesId the primary key of the portlet preferences
4878             * @return the portlet preferences, or <code>null</code> if a portlet preferences with the primary key could not be found
4879             */
4880            @Override
4881            public PortletPreferences fetchByPrimaryKey(long portletPreferencesId) {
4882                    return fetchByPrimaryKey((Serializable)portletPreferencesId);
4883            }
4884    
4885            @Override
4886            public Map<Serializable, PortletPreferences> fetchByPrimaryKeys(
4887                    Set<Serializable> primaryKeys) {
4888                    if (primaryKeys.isEmpty()) {
4889                            return Collections.emptyMap();
4890                    }
4891    
4892                    Map<Serializable, PortletPreferences> map = new HashMap<Serializable, PortletPreferences>();
4893    
4894                    if (primaryKeys.size() == 1) {
4895                            Iterator<Serializable> iterator = primaryKeys.iterator();
4896    
4897                            Serializable primaryKey = iterator.next();
4898    
4899                            PortletPreferences portletPreferences = fetchByPrimaryKey(primaryKey);
4900    
4901                            if (portletPreferences != null) {
4902                                    map.put(primaryKey, portletPreferences);
4903                            }
4904    
4905                            return map;
4906                    }
4907    
4908                    Set<Serializable> uncachedPrimaryKeys = null;
4909    
4910                    for (Serializable primaryKey : primaryKeys) {
4911                            PortletPreferences portletPreferences = (PortletPreferences)EntityCacheUtil.getResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4912                                            PortletPreferencesImpl.class, primaryKey);
4913    
4914                            if (portletPreferences == null) {
4915                                    if (uncachedPrimaryKeys == null) {
4916                                            uncachedPrimaryKeys = new HashSet<Serializable>();
4917                                    }
4918    
4919                                    uncachedPrimaryKeys.add(primaryKey);
4920                            }
4921                            else {
4922                                    map.put(primaryKey, portletPreferences);
4923                            }
4924                    }
4925    
4926                    if (uncachedPrimaryKeys == null) {
4927                            return map;
4928                    }
4929    
4930                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
4931                                    1);
4932    
4933                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE_PKS_IN);
4934    
4935                    for (Serializable primaryKey : uncachedPrimaryKeys) {
4936                            query.append(String.valueOf(primaryKey));
4937    
4938                            query.append(StringPool.COMMA);
4939                    }
4940    
4941                    query.setIndex(query.index() - 1);
4942    
4943                    query.append(StringPool.CLOSE_PARENTHESIS);
4944    
4945                    String sql = query.toString();
4946    
4947                    Session session = null;
4948    
4949                    try {
4950                            session = openSession();
4951    
4952                            Query q = session.createQuery(sql);
4953    
4954                            for (PortletPreferences portletPreferences : (List<PortletPreferences>)q.list()) {
4955                                    map.put(portletPreferences.getPrimaryKeyObj(),
4956                                            portletPreferences);
4957    
4958                                    cacheResult(portletPreferences);
4959    
4960                                    uncachedPrimaryKeys.remove(portletPreferences.getPrimaryKeyObj());
4961                            }
4962    
4963                            for (Serializable primaryKey : uncachedPrimaryKeys) {
4964                                    EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
4965                                            PortletPreferencesImpl.class, primaryKey,
4966                                            _nullPortletPreferences);
4967                            }
4968                    }
4969                    catch (Exception e) {
4970                            throw processException(e);
4971                    }
4972                    finally {
4973                            closeSession(session);
4974                    }
4975    
4976                    return map;
4977            }
4978    
4979            /**
4980             * Returns all the portlet preferenceses.
4981             *
4982             * @return the portlet preferenceses
4983             */
4984            @Override
4985            public List<PortletPreferences> findAll() {
4986                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4987            }
4988    
4989            /**
4990             * Returns a range of all the portlet preferenceses.
4991             *
4992             * <p>
4993             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
4994             * </p>
4995             *
4996             * @param start the lower bound of the range of portlet preferenceses
4997             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
4998             * @return the range of portlet preferenceses
4999             */
5000            @Override
5001            public List<PortletPreferences> findAll(int start, int end) {
5002                    return findAll(start, end, null);
5003            }
5004    
5005            /**
5006             * Returns an ordered range of all the portlet preferenceses.
5007             *
5008             * <p>
5009             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.PortletPreferencesModelImpl}. 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.
5010             * </p>
5011             *
5012             * @param start the lower bound of the range of portlet preferenceses
5013             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
5014             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5015             * @return the ordered range of portlet preferenceses
5016             */
5017            @Override
5018            public List<PortletPreferences> findAll(int start, int end,
5019                    OrderByComparator<PortletPreferences> orderByComparator) {
5020                    boolean pagination = true;
5021                    FinderPath finderPath = null;
5022                    Object[] finderArgs = null;
5023    
5024                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
5025                                    (orderByComparator == null)) {
5026                            pagination = false;
5027                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
5028                            finderArgs = FINDER_ARGS_EMPTY;
5029                    }
5030                    else {
5031                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
5032                            finderArgs = new Object[] { start, end, orderByComparator };
5033                    }
5034    
5035                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
5036                                    finderArgs, this);
5037    
5038                    if (list == null) {
5039                            StringBundler query = null;
5040                            String sql = null;
5041    
5042                            if (orderByComparator != null) {
5043                                    query = new StringBundler(2 +
5044                                                    (orderByComparator.getOrderByFields().length * 3));
5045    
5046                                    query.append(_SQL_SELECT_PORTLETPREFERENCES);
5047    
5048                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5049                                            orderByComparator);
5050    
5051                                    sql = query.toString();
5052                            }
5053                            else {
5054                                    sql = _SQL_SELECT_PORTLETPREFERENCES;
5055    
5056                                    if (pagination) {
5057                                            sql = sql.concat(PortletPreferencesModelImpl.ORDER_BY_JPQL);
5058                                    }
5059                            }
5060    
5061                            Session session = null;
5062    
5063                            try {
5064                                    session = openSession();
5065    
5066                                    Query q = session.createQuery(sql);
5067    
5068                                    if (!pagination) {
5069                                            list = (List<PortletPreferences>)QueryUtil.list(q,
5070                                                            getDialect(), start, end, false);
5071    
5072                                            Collections.sort(list);
5073    
5074                                            list = Collections.unmodifiableList(list);
5075                                    }
5076                                    else {
5077                                            list = (List<PortletPreferences>)QueryUtil.list(q,
5078                                                            getDialect(), start, end);
5079                                    }
5080    
5081                                    cacheResult(list);
5082    
5083                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
5084                            }
5085                            catch (Exception e) {
5086                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
5087    
5088                                    throw processException(e);
5089                            }
5090                            finally {
5091                                    closeSession(session);
5092                            }
5093                    }
5094    
5095                    return list;
5096            }
5097    
5098            /**
5099             * Removes all the portlet preferenceses from the database.
5100             *
5101             */
5102            @Override
5103            public void removeAll() {
5104                    for (PortletPreferences portletPreferences : findAll()) {
5105                            remove(portletPreferences);
5106                    }
5107            }
5108    
5109            /**
5110             * Returns the number of portlet preferenceses.
5111             *
5112             * @return the number of portlet preferenceses
5113             */
5114            @Override
5115            public int countAll() {
5116                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
5117                                    FINDER_ARGS_EMPTY, this);
5118    
5119                    if (count == null) {
5120                            Session session = null;
5121    
5122                            try {
5123                                    session = openSession();
5124    
5125                                    Query q = session.createQuery(_SQL_COUNT_PORTLETPREFERENCES);
5126    
5127                                    count = (Long)q.uniqueResult();
5128    
5129                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
5130                                            FINDER_ARGS_EMPTY, count);
5131                            }
5132                            catch (Exception e) {
5133                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
5134                                            FINDER_ARGS_EMPTY);
5135    
5136                                    throw processException(e);
5137                            }
5138                            finally {
5139                                    closeSession(session);
5140                            }
5141                    }
5142    
5143                    return count.intValue();
5144            }
5145    
5146            /**
5147             * Initializes the portlet preferences persistence.
5148             */
5149            public void afterPropertiesSet() {
5150            }
5151    
5152            public void destroy() {
5153                    EntityCacheUtil.removeCache(PortletPreferencesImpl.class.getName());
5154                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
5155                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5156                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5157            }
5158    
5159            private static final String _SQL_SELECT_PORTLETPREFERENCES = "SELECT portletPreferences FROM PortletPreferences portletPreferences";
5160            private static final String _SQL_SELECT_PORTLETPREFERENCES_WHERE_PKS_IN = "SELECT portletPreferences FROM PortletPreferences portletPreferences WHERE portletPreferencesId IN (";
5161            private static final String _SQL_SELECT_PORTLETPREFERENCES_WHERE = "SELECT portletPreferences FROM PortletPreferences portletPreferences WHERE ";
5162            private static final String _SQL_COUNT_PORTLETPREFERENCES = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences";
5163            private static final String _SQL_COUNT_PORTLETPREFERENCES_WHERE = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences WHERE ";
5164            private static final String _ORDER_BY_ENTITY_ALIAS = "portletPreferences.";
5165            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PortletPreferences exists with the primary key ";
5166            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PortletPreferences exists with the key {";
5167            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
5168            private static final Log _log = LogFactoryUtil.getLog(PortletPreferencesPersistenceImpl.class);
5169            private static final PortletPreferences _nullPortletPreferences = new PortletPreferencesImpl() {
5170                            @Override
5171                            public Object clone() {
5172                                    return this;
5173                            }
5174    
5175                            @Override
5176                            public CacheModel<PortletPreferences> toCacheModel() {
5177                                    return _nullPortletPreferencesCacheModel;
5178                            }
5179                    };
5180    
5181            private static final CacheModel<PortletPreferences> _nullPortletPreferencesCacheModel =
5182                    new NullCacheModel();
5183    
5184            private static class NullCacheModel implements CacheModel<PortletPreferences>,
5185                    MVCCModel {
5186                    @Override
5187                    public long getMvccVersion() {
5188                            return -1;
5189                    }
5190    
5191                    @Override
5192                    public void setMvccVersion(long mvccVersion) {
5193                    }
5194    
5195                    @Override
5196                    public PortletPreferences toEntityModel() {
5197                            return _nullPortletPreferences;
5198                    }
5199            }
5200    }