001    /**
002     * Copyright (c) 2000-2012 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;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.NoSuchPortletPreferencesException;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.UnmodifiableList;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.CacheModel;
039    import com.liferay.portal.model.ModelListener;
040    import com.liferay.portal.model.PortletPreferences;
041    import com.liferay.portal.model.impl.PortletPreferencesImpl;
042    import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
043    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.List;
050    
051    /**
052     * The persistence implementation for the 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    public class PortletPreferencesPersistenceImpl extends BasePersistenceImpl<PortletPreferences>
064            implements PortletPreferencesPersistence {
065            /*
066             * NOTE FOR DEVELOPERS:
067             *
068             * 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.
069             */
070            public static final String FINDER_CLASS_NAME_ENTITY = PortletPreferencesImpl.class.getName();
071            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List1";
073            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List2";
075            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
076                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
077                            PortletPreferencesImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
080                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
081                            PortletPreferencesImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
084                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
087                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
088                            PortletPreferencesImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPlid",
090                            new String[] {
091                                    Long.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
097                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
098                            PortletPreferencesImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPlid",
100                            new String[] { Long.class.getName() },
101                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
103                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPlid",
105                            new String[] { Long.class.getName() });
106    
107            /**
108             * Returns all the portlet preferenceses where plid = &#63;.
109             *
110             * @param plid the plid
111             * @return the matching portlet preferenceses
112             * @throws SystemException if a system exception occurred
113             */
114            public List<PortletPreferences> findByPlid(long plid)
115                    throws SystemException {
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             * @throws SystemException if a system exception occurred
131             */
132            public List<PortletPreferences> findByPlid(long plid, int start, int end)
133                    throws SystemException {
134                    return findByPlid(plid, start, end, null);
135            }
136    
137            /**
138             * Returns an ordered range of all the portlet preferenceses where plid = &#63;.
139             *
140             * <p>
141             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.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.
142             * </p>
143             *
144             * @param plid the plid
145             * @param start the lower bound of the range of portlet preferenceses
146             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
147             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
148             * @return the ordered range of matching portlet preferenceses
149             * @throws SystemException if a system exception occurred
150             */
151            public List<PortletPreferences> findByPlid(long plid, int start, int end,
152                    OrderByComparator orderByComparator) throws SystemException {
153                    boolean pagination = true;
154                    FinderPath finderPath = null;
155                    Object[] finderArgs = null;
156    
157                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
158                                    (orderByComparator == null)) {
159                            pagination = false;
160                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID;
161                            finderArgs = new Object[] { plid };
162                    }
163                    else {
164                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PLID;
165                            finderArgs = new Object[] { plid, start, end, orderByComparator };
166                    }
167    
168                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
169                                    finderArgs, this);
170    
171                    if ((list != null) && !list.isEmpty()) {
172                            for (PortletPreferences portletPreferences : list) {
173                                    if ((plid != portletPreferences.getPlid())) {
174                                            list = null;
175    
176                                            break;
177                                    }
178                            }
179                    }
180    
181                    if (list == null) {
182                            StringBundler query = null;
183    
184                            if (orderByComparator != null) {
185                                    query = new StringBundler(3 +
186                                                    (orderByComparator.getOrderByFields().length * 3));
187                            }
188                            else {
189                                    query = new StringBundler(3);
190                            }
191    
192                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
193    
194                            query.append(_FINDER_COLUMN_PLID_PLID_2);
195    
196                            if (orderByComparator != null) {
197                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
198                                            orderByComparator);
199                            }
200                            else
201                             if (pagination) {
202                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
203                            }
204    
205                            String sql = query.toString();
206    
207                            Session session = null;
208    
209                            try {
210                                    session = openSession();
211    
212                                    Query q = session.createQuery(sql);
213    
214                                    QueryPos qPos = QueryPos.getInstance(q);
215    
216                                    qPos.add(plid);
217    
218                                    if (!pagination) {
219                                            list = (List<PortletPreferences>)QueryUtil.list(q,
220                                                            getDialect(), start, end, false);
221    
222                                            Collections.sort(list);
223    
224                                            list = new UnmodifiableList<PortletPreferences>(list);
225                                    }
226                                    else {
227                                            list = (List<PortletPreferences>)QueryUtil.list(q,
228                                                            getDialect(), start, end);
229                                    }
230    
231                                    cacheResult(list);
232    
233                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
234                            }
235                            catch (Exception e) {
236                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
237    
238                                    throw processException(e);
239                            }
240                            finally {
241                                    closeSession(session);
242                            }
243                    }
244    
245                    return list;
246            }
247    
248            /**
249             * Returns the first portlet preferences in the ordered set where plid = &#63;.
250             *
251             * @param plid the plid
252             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
253             * @return the first matching portlet preferences
254             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
255             * @throws SystemException if a system exception occurred
256             */
257            public PortletPreferences findByPlid_First(long plid,
258                    OrderByComparator orderByComparator)
259                    throws NoSuchPortletPreferencesException, SystemException {
260                    PortletPreferences portletPreferences = fetchByPlid_First(plid,
261                                    orderByComparator);
262    
263                    if (portletPreferences != null) {
264                            return portletPreferences;
265                    }
266    
267                    StringBundler msg = new StringBundler(4);
268    
269                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
270    
271                    msg.append("plid=");
272                    msg.append(plid);
273    
274                    msg.append(StringPool.CLOSE_CURLY_BRACE);
275    
276                    throw new NoSuchPortletPreferencesException(msg.toString());
277            }
278    
279            /**
280             * Returns the first portlet preferences in the ordered set where plid = &#63;.
281             *
282             * @param plid the plid
283             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
284             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
285             * @throws SystemException if a system exception occurred
286             */
287            public PortletPreferences fetchByPlid_First(long plid,
288                    OrderByComparator orderByComparator) throws SystemException {
289                    List<PortletPreferences> list = findByPlid(plid, 0, 1, orderByComparator);
290    
291                    if (!list.isEmpty()) {
292                            return list.get(0);
293                    }
294    
295                    return null;
296            }
297    
298            /**
299             * Returns the last portlet preferences in the ordered set where plid = &#63;.
300             *
301             * @param plid the plid
302             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
303             * @return the last matching portlet preferences
304             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
305             * @throws SystemException if a system exception occurred
306             */
307            public PortletPreferences findByPlid_Last(long plid,
308                    OrderByComparator orderByComparator)
309                    throws NoSuchPortletPreferencesException, SystemException {
310                    PortletPreferences portletPreferences = fetchByPlid_Last(plid,
311                                    orderByComparator);
312    
313                    if (portletPreferences != null) {
314                            return portletPreferences;
315                    }
316    
317                    StringBundler msg = new StringBundler(4);
318    
319                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
320    
321                    msg.append("plid=");
322                    msg.append(plid);
323    
324                    msg.append(StringPool.CLOSE_CURLY_BRACE);
325    
326                    throw new NoSuchPortletPreferencesException(msg.toString());
327            }
328    
329            /**
330             * Returns the last portlet preferences in the ordered set where plid = &#63;.
331             *
332             * @param plid the plid
333             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
334             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
335             * @throws SystemException if a system exception occurred
336             */
337            public PortletPreferences fetchByPlid_Last(long plid,
338                    OrderByComparator orderByComparator) throws SystemException {
339                    int count = countByPlid(plid);
340    
341                    List<PortletPreferences> list = findByPlid(plid, count - 1, count,
342                                    orderByComparator);
343    
344                    if (!list.isEmpty()) {
345                            return list.get(0);
346                    }
347    
348                    return null;
349            }
350    
351            /**
352             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where plid = &#63;.
353             *
354             * @param portletPreferencesId the primary key of the current portlet preferences
355             * @param plid the plid
356             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
357             * @return the previous, current, and next portlet preferences
358             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
359             * @throws SystemException if a system exception occurred
360             */
361            public PortletPreferences[] findByPlid_PrevAndNext(
362                    long portletPreferencesId, long plid,
363                    OrderByComparator orderByComparator)
364                    throws NoSuchPortletPreferencesException, SystemException {
365                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
366    
367                    Session session = null;
368    
369                    try {
370                            session = openSession();
371    
372                            PortletPreferences[] array = new PortletPreferencesImpl[3];
373    
374                            array[0] = getByPlid_PrevAndNext(session, portletPreferences, plid,
375                                            orderByComparator, true);
376    
377                            array[1] = portletPreferences;
378    
379                            array[2] = getByPlid_PrevAndNext(session, portletPreferences, plid,
380                                            orderByComparator, false);
381    
382                            return array;
383                    }
384                    catch (Exception e) {
385                            throw processException(e);
386                    }
387                    finally {
388                            closeSession(session);
389                    }
390            }
391    
392            protected PortletPreferences getByPlid_PrevAndNext(Session session,
393                    PortletPreferences portletPreferences, long plid,
394                    OrderByComparator orderByComparator, boolean previous) {
395                    StringBundler query = null;
396    
397                    if (orderByComparator != null) {
398                            query = new StringBundler(6 +
399                                            (orderByComparator.getOrderByFields().length * 6));
400                    }
401                    else {
402                            query = new StringBundler(3);
403                    }
404    
405                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
406    
407                    query.append(_FINDER_COLUMN_PLID_PLID_2);
408    
409                    if (orderByComparator != null) {
410                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
411    
412                            if (orderByConditionFields.length > 0) {
413                                    query.append(WHERE_AND);
414                            }
415    
416                            for (int i = 0; i < orderByConditionFields.length; i++) {
417                                    query.append(_ORDER_BY_ENTITY_ALIAS);
418                                    query.append(orderByConditionFields[i]);
419    
420                                    if ((i + 1) < orderByConditionFields.length) {
421                                            if (orderByComparator.isAscending() ^ previous) {
422                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
423                                            }
424                                            else {
425                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
426                                            }
427                                    }
428                                    else {
429                                            if (orderByComparator.isAscending() ^ previous) {
430                                                    query.append(WHERE_GREATER_THAN);
431                                            }
432                                            else {
433                                                    query.append(WHERE_LESSER_THAN);
434                                            }
435                                    }
436                            }
437    
438                            query.append(ORDER_BY_CLAUSE);
439    
440                            String[] orderByFields = orderByComparator.getOrderByFields();
441    
442                            for (int i = 0; i < orderByFields.length; i++) {
443                                    query.append(_ORDER_BY_ENTITY_ALIAS);
444                                    query.append(orderByFields[i]);
445    
446                                    if ((i + 1) < orderByFields.length) {
447                                            if (orderByComparator.isAscending() ^ previous) {
448                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
449                                            }
450                                            else {
451                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
452                                            }
453                                    }
454                                    else {
455                                            if (orderByComparator.isAscending() ^ previous) {
456                                                    query.append(ORDER_BY_ASC);
457                                            }
458                                            else {
459                                                    query.append(ORDER_BY_DESC);
460                                            }
461                                    }
462                            }
463                    }
464                    else {
465                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
466                    }
467    
468                    String sql = query.toString();
469    
470                    Query q = session.createQuery(sql);
471    
472                    q.setFirstResult(0);
473                    q.setMaxResults(2);
474    
475                    QueryPos qPos = QueryPos.getInstance(q);
476    
477                    qPos.add(plid);
478    
479                    if (orderByComparator != null) {
480                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
481    
482                            for (Object value : values) {
483                                    qPos.add(value);
484                            }
485                    }
486    
487                    List<PortletPreferences> list = q.list();
488    
489                    if (list.size() == 2) {
490                            return list.get(1);
491                    }
492                    else {
493                            return null;
494                    }
495            }
496    
497            /**
498             * Removes all the portlet preferenceses where plid = &#63; from the database.
499             *
500             * @param plid the plid
501             * @throws SystemException if a system exception occurred
502             */
503            public void removeByPlid(long plid) throws SystemException {
504                    for (PortletPreferences portletPreferences : findByPlid(plid,
505                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
506                            remove(portletPreferences);
507                    }
508            }
509    
510            /**
511             * Returns the number of portlet preferenceses where plid = &#63;.
512             *
513             * @param plid the plid
514             * @return the number of matching portlet preferenceses
515             * @throws SystemException if a system exception occurred
516             */
517            public int countByPlid(long plid) throws SystemException {
518                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PLID;
519    
520                    Object[] finderArgs = new Object[] { plid };
521    
522                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
523                                    this);
524    
525                    if (count == null) {
526                            StringBundler query = new StringBundler(2);
527    
528                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
529    
530                            query.append(_FINDER_COLUMN_PLID_PLID_2);
531    
532                            String sql = query.toString();
533    
534                            Session session = null;
535    
536                            try {
537                                    session = openSession();
538    
539                                    Query q = session.createQuery(sql);
540    
541                                    QueryPos qPos = QueryPos.getInstance(q);
542    
543                                    qPos.add(plid);
544    
545                                    count = (Long)q.uniqueResult();
546    
547                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
548                            }
549                            catch (Exception e) {
550                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
551    
552                                    throw processException(e);
553                            }
554                            finally {
555                                    closeSession(session);
556                            }
557                    }
558    
559                    return count.intValue();
560            }
561    
562            private static final String _FINDER_COLUMN_PLID_PLID_2 = "portletPreferences.plid = ?";
563            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PORTLETID =
564                    new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
565                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
566                            PortletPreferencesImpl.class,
567                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPortletId",
568                            new String[] {
569                                    String.class.getName(),
570                                    
571                            Integer.class.getName(), Integer.class.getName(),
572                                    OrderByComparator.class.getName()
573                            });
574            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID =
575                    new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
576                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
577                            PortletPreferencesImpl.class,
578                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPortletId",
579                            new String[] { String.class.getName() },
580                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
581            public static final FinderPath FINDER_PATH_COUNT_BY_PORTLETID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
582                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
583                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPortletId",
584                            new String[] { String.class.getName() });
585    
586            /**
587             * Returns all the portlet preferenceses where portletId = &#63;.
588             *
589             * @param portletId the portlet ID
590             * @return the matching portlet preferenceses
591             * @throws SystemException if a system exception occurred
592             */
593            public List<PortletPreferences> findByPortletId(String portletId)
594                    throws SystemException {
595                    return findByPortletId(portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
596                            null);
597            }
598    
599            /**
600             * Returns a range of all the portlet preferenceses where portletId = &#63;.
601             *
602             * <p>
603             * 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.
604             * </p>
605             *
606             * @param portletId the portlet ID
607             * @param start the lower bound of the range of portlet preferenceses
608             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
609             * @return the range of matching portlet preferenceses
610             * @throws SystemException if a system exception occurred
611             */
612            public List<PortletPreferences> findByPortletId(String portletId,
613                    int start, int end) throws SystemException {
614                    return findByPortletId(portletId, start, end, null);
615            }
616    
617            /**
618             * Returns an ordered range of all the portlet preferenceses where portletId = &#63;.
619             *
620             * <p>
621             * 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.
622             * </p>
623             *
624             * @param portletId the portlet ID
625             * @param start the lower bound of the range of portlet preferenceses
626             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
627             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
628             * @return the ordered range of matching portlet preferenceses
629             * @throws SystemException if a system exception occurred
630             */
631            public List<PortletPreferences> findByPortletId(String portletId,
632                    int start, int end, OrderByComparator orderByComparator)
633                    throws SystemException {
634                    boolean pagination = true;
635                    FinderPath finderPath = null;
636                    Object[] finderArgs = null;
637    
638                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
639                                    (orderByComparator == null)) {
640                            pagination = false;
641                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID;
642                            finderArgs = new Object[] { portletId };
643                    }
644                    else {
645                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PORTLETID;
646                            finderArgs = new Object[] { portletId, start, end, orderByComparator };
647                    }
648    
649                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
650                                    finderArgs, this);
651    
652                    if ((list != null) && !list.isEmpty()) {
653                            for (PortletPreferences portletPreferences : list) {
654                                    if (!Validator.equals(portletId,
655                                                            portletPreferences.getPortletId())) {
656                                            list = null;
657    
658                                            break;
659                                    }
660                            }
661                    }
662    
663                    if (list == null) {
664                            StringBundler query = null;
665    
666                            if (orderByComparator != null) {
667                                    query = new StringBundler(3 +
668                                                    (orderByComparator.getOrderByFields().length * 3));
669                            }
670                            else {
671                                    query = new StringBundler(3);
672                            }
673    
674                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
675    
676                            if (portletId == null) {
677                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
678                            }
679                            else {
680                                    if (portletId.equals(StringPool.BLANK)) {
681                                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
682                                    }
683                                    else {
684                                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
685                                    }
686                            }
687    
688                            if (orderByComparator != null) {
689                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
690                                            orderByComparator);
691                            }
692                            else
693                             if (pagination) {
694                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
695                            }
696    
697                            String sql = query.toString();
698    
699                            Session session = null;
700    
701                            try {
702                                    session = openSession();
703    
704                                    Query q = session.createQuery(sql);
705    
706                                    QueryPos qPos = QueryPos.getInstance(q);
707    
708                                    if (portletId != null) {
709                                            qPos.add(portletId);
710                                    }
711    
712                                    if (!pagination) {
713                                            list = (List<PortletPreferences>)QueryUtil.list(q,
714                                                            getDialect(), start, end, false);
715    
716                                            Collections.sort(list);
717    
718                                            list = new UnmodifiableList<PortletPreferences>(list);
719                                    }
720                                    else {
721                                            list = (List<PortletPreferences>)QueryUtil.list(q,
722                                                            getDialect(), start, end);
723                                    }
724    
725                                    cacheResult(list);
726    
727                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
728                            }
729                            catch (Exception e) {
730                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
731    
732                                    throw processException(e);
733                            }
734                            finally {
735                                    closeSession(session);
736                            }
737                    }
738    
739                    return list;
740            }
741    
742            /**
743             * Returns the first portlet preferences in the ordered set where portletId = &#63;.
744             *
745             * @param portletId the portlet ID
746             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
747             * @return the first matching portlet preferences
748             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
749             * @throws SystemException if a system exception occurred
750             */
751            public PortletPreferences findByPortletId_First(String portletId,
752                    OrderByComparator orderByComparator)
753                    throws NoSuchPortletPreferencesException, SystemException {
754                    PortletPreferences portletPreferences = fetchByPortletId_First(portletId,
755                                    orderByComparator);
756    
757                    if (portletPreferences != null) {
758                            return portletPreferences;
759                    }
760    
761                    StringBundler msg = new StringBundler(4);
762    
763                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
764    
765                    msg.append("portletId=");
766                    msg.append(portletId);
767    
768                    msg.append(StringPool.CLOSE_CURLY_BRACE);
769    
770                    throw new NoSuchPortletPreferencesException(msg.toString());
771            }
772    
773            /**
774             * Returns the first portlet preferences in the ordered set where portletId = &#63;.
775             *
776             * @param portletId the portlet ID
777             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
778             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
779             * @throws SystemException if a system exception occurred
780             */
781            public PortletPreferences fetchByPortletId_First(String portletId,
782                    OrderByComparator orderByComparator) throws SystemException {
783                    List<PortletPreferences> list = findByPortletId(portletId, 0, 1,
784                                    orderByComparator);
785    
786                    if (!list.isEmpty()) {
787                            return list.get(0);
788                    }
789    
790                    return null;
791            }
792    
793            /**
794             * Returns the last portlet preferences in the ordered set where portletId = &#63;.
795             *
796             * @param portletId the portlet ID
797             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
798             * @return the last matching portlet preferences
799             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
800             * @throws SystemException if a system exception occurred
801             */
802            public PortletPreferences findByPortletId_Last(String portletId,
803                    OrderByComparator orderByComparator)
804                    throws NoSuchPortletPreferencesException, SystemException {
805                    PortletPreferences portletPreferences = fetchByPortletId_Last(portletId,
806                                    orderByComparator);
807    
808                    if (portletPreferences != null) {
809                            return portletPreferences;
810                    }
811    
812                    StringBundler msg = new StringBundler(4);
813    
814                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
815    
816                    msg.append("portletId=");
817                    msg.append(portletId);
818    
819                    msg.append(StringPool.CLOSE_CURLY_BRACE);
820    
821                    throw new NoSuchPortletPreferencesException(msg.toString());
822            }
823    
824            /**
825             * Returns the last portlet preferences in the ordered set where portletId = &#63;.
826             *
827             * @param portletId the portlet ID
828             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
829             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
830             * @throws SystemException if a system exception occurred
831             */
832            public PortletPreferences fetchByPortletId_Last(String portletId,
833                    OrderByComparator orderByComparator) throws SystemException {
834                    int count = countByPortletId(portletId);
835    
836                    List<PortletPreferences> list = findByPortletId(portletId, count - 1,
837                                    count, orderByComparator);
838    
839                    if (!list.isEmpty()) {
840                            return list.get(0);
841                    }
842    
843                    return null;
844            }
845    
846            /**
847             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where portletId = &#63;.
848             *
849             * @param portletPreferencesId the primary key of the current portlet preferences
850             * @param portletId the portlet ID
851             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
852             * @return the previous, current, and next portlet preferences
853             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
854             * @throws SystemException if a system exception occurred
855             */
856            public PortletPreferences[] findByPortletId_PrevAndNext(
857                    long portletPreferencesId, String portletId,
858                    OrderByComparator orderByComparator)
859                    throws NoSuchPortletPreferencesException, SystemException {
860                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
861    
862                    Session session = null;
863    
864                    try {
865                            session = openSession();
866    
867                            PortletPreferences[] array = new PortletPreferencesImpl[3];
868    
869                            array[0] = getByPortletId_PrevAndNext(session, portletPreferences,
870                                            portletId, orderByComparator, true);
871    
872                            array[1] = portletPreferences;
873    
874                            array[2] = getByPortletId_PrevAndNext(session, portletPreferences,
875                                            portletId, orderByComparator, false);
876    
877                            return array;
878                    }
879                    catch (Exception e) {
880                            throw processException(e);
881                    }
882                    finally {
883                            closeSession(session);
884                    }
885            }
886    
887            protected PortletPreferences getByPortletId_PrevAndNext(Session session,
888                    PortletPreferences portletPreferences, String portletId,
889                    OrderByComparator orderByComparator, boolean previous) {
890                    StringBundler query = null;
891    
892                    if (orderByComparator != null) {
893                            query = new StringBundler(6 +
894                                            (orderByComparator.getOrderByFields().length * 6));
895                    }
896                    else {
897                            query = new StringBundler(3);
898                    }
899    
900                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
901    
902                    if (portletId == null) {
903                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
904                    }
905                    else {
906                            if (portletId.equals(StringPool.BLANK)) {
907                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
908                            }
909                            else {
910                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
911                            }
912                    }
913    
914                    if (orderByComparator != null) {
915                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
916    
917                            if (orderByConditionFields.length > 0) {
918                                    query.append(WHERE_AND);
919                            }
920    
921                            for (int i = 0; i < orderByConditionFields.length; i++) {
922                                    query.append(_ORDER_BY_ENTITY_ALIAS);
923                                    query.append(orderByConditionFields[i]);
924    
925                                    if ((i + 1) < orderByConditionFields.length) {
926                                            if (orderByComparator.isAscending() ^ previous) {
927                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
928                                            }
929                                            else {
930                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
931                                            }
932                                    }
933                                    else {
934                                            if (orderByComparator.isAscending() ^ previous) {
935                                                    query.append(WHERE_GREATER_THAN);
936                                            }
937                                            else {
938                                                    query.append(WHERE_LESSER_THAN);
939                                            }
940                                    }
941                            }
942    
943                            query.append(ORDER_BY_CLAUSE);
944    
945                            String[] orderByFields = orderByComparator.getOrderByFields();
946    
947                            for (int i = 0; i < orderByFields.length; i++) {
948                                    query.append(_ORDER_BY_ENTITY_ALIAS);
949                                    query.append(orderByFields[i]);
950    
951                                    if ((i + 1) < orderByFields.length) {
952                                            if (orderByComparator.isAscending() ^ previous) {
953                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
954                                            }
955                                            else {
956                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
957                                            }
958                                    }
959                                    else {
960                                            if (orderByComparator.isAscending() ^ previous) {
961                                                    query.append(ORDER_BY_ASC);
962                                            }
963                                            else {
964                                                    query.append(ORDER_BY_DESC);
965                                            }
966                                    }
967                            }
968                    }
969                    else {
970                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
971                    }
972    
973                    String sql = query.toString();
974    
975                    Query q = session.createQuery(sql);
976    
977                    q.setFirstResult(0);
978                    q.setMaxResults(2);
979    
980                    QueryPos qPos = QueryPos.getInstance(q);
981    
982                    if (portletId != null) {
983                            qPos.add(portletId);
984                    }
985    
986                    if (orderByComparator != null) {
987                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
988    
989                            for (Object value : values) {
990                                    qPos.add(value);
991                            }
992                    }
993    
994                    List<PortletPreferences> list = q.list();
995    
996                    if (list.size() == 2) {
997                            return list.get(1);
998                    }
999                    else {
1000                            return null;
1001                    }
1002            }
1003    
1004            /**
1005             * Removes all the portlet preferenceses where portletId = &#63; from the database.
1006             *
1007             * @param portletId the portlet ID
1008             * @throws SystemException if a system exception occurred
1009             */
1010            public void removeByPortletId(String portletId) throws SystemException {
1011                    for (PortletPreferences portletPreferences : findByPortletId(
1012                                    portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1013                            remove(portletPreferences);
1014                    }
1015            }
1016    
1017            /**
1018             * Returns the number of portlet preferenceses where portletId = &#63;.
1019             *
1020             * @param portletId the portlet ID
1021             * @return the number of matching portlet preferenceses
1022             * @throws SystemException if a system exception occurred
1023             */
1024            public int countByPortletId(String portletId) throws SystemException {
1025                    FinderPath finderPath = FINDER_PATH_COUNT_BY_PORTLETID;
1026    
1027                    Object[] finderArgs = new Object[] { portletId };
1028    
1029                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1030                                    this);
1031    
1032                    if (count == null) {
1033                            StringBundler query = new StringBundler(2);
1034    
1035                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1036    
1037                            if (portletId == null) {
1038                                    query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_1);
1039                            }
1040                            else {
1041                                    if (portletId.equals(StringPool.BLANK)) {
1042                                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_3);
1043                                    }
1044                                    else {
1045                                            query.append(_FINDER_COLUMN_PORTLETID_PORTLETID_2);
1046                                    }
1047                            }
1048    
1049                            String sql = query.toString();
1050    
1051                            Session session = null;
1052    
1053                            try {
1054                                    session = openSession();
1055    
1056                                    Query q = session.createQuery(sql);
1057    
1058                                    QueryPos qPos = QueryPos.getInstance(q);
1059    
1060                                    if (portletId != null) {
1061                                            qPos.add(portletId);
1062                                    }
1063    
1064                                    count = (Long)q.uniqueResult();
1065    
1066                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1067                            }
1068                            catch (Exception e) {
1069                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1070    
1071                                    throw processException(e);
1072                            }
1073                            finally {
1074                                    closeSession(session);
1075                            }
1076                    }
1077    
1078                    return count.intValue();
1079            }
1080    
1081            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1082            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_2 = "portletPreferences.portletId = ?";
1083            private static final String _FINDER_COLUMN_PORTLETID_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
1084            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1085                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1086                            PortletPreferencesImpl.class,
1087                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByP_P",
1088                            new String[] {
1089                                    Long.class.getName(), String.class.getName(),
1090                                    
1091                            Integer.class.getName(), Integer.class.getName(),
1092                                    OrderByComparator.class.getName()
1093                            });
1094            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1095                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1096                            PortletPreferencesImpl.class,
1097                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByP_P",
1098                            new String[] { Long.class.getName(), String.class.getName() },
1099                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
1100                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
1101            public static final FinderPath FINDER_PATH_COUNT_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1102                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
1103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByP_P",
1104                            new String[] { Long.class.getName(), String.class.getName() });
1105    
1106            /**
1107             * Returns all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1108             *
1109             * @param plid the plid
1110             * @param portletId the portlet ID
1111             * @return the matching portlet preferenceses
1112             * @throws SystemException if a system exception occurred
1113             */
1114            public List<PortletPreferences> findByP_P(long plid, String portletId)
1115                    throws SystemException {
1116                    return findByP_P(plid, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1117                            null);
1118            }
1119    
1120            /**
1121             * Returns a range of all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1122             *
1123             * <p>
1124             * 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.
1125             * </p>
1126             *
1127             * @param plid the plid
1128             * @param portletId the portlet ID
1129             * @param start the lower bound of the range of portlet preferenceses
1130             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1131             * @return the range of matching portlet preferenceses
1132             * @throws SystemException if a system exception occurred
1133             */
1134            public List<PortletPreferences> findByP_P(long plid, String portletId,
1135                    int start, int end) throws SystemException {
1136                    return findByP_P(plid, portletId, start, end, null);
1137            }
1138    
1139            /**
1140             * Returns an ordered range of all the portlet preferenceses where plid = &#63; and portletId = &#63;.
1141             *
1142             * <p>
1143             * 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.
1144             * </p>
1145             *
1146             * @param plid the plid
1147             * @param portletId the portlet ID
1148             * @param start the lower bound of the range of portlet preferenceses
1149             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1150             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1151             * @return the ordered range of matching portlet preferenceses
1152             * @throws SystemException if a system exception occurred
1153             */
1154            public List<PortletPreferences> findByP_P(long plid, String portletId,
1155                    int start, int end, OrderByComparator orderByComparator)
1156                    throws SystemException {
1157                    boolean pagination = true;
1158                    FinderPath finderPath = null;
1159                    Object[] finderArgs = null;
1160    
1161                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1162                                    (orderByComparator == null)) {
1163                            pagination = false;
1164                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P;
1165                            finderArgs = new Object[] { plid, portletId };
1166                    }
1167                    else {
1168                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_P_P;
1169                            finderArgs = new Object[] {
1170                                            plid, portletId,
1171                                            
1172                                            start, end, orderByComparator
1173                                    };
1174                    }
1175    
1176                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
1177                                    finderArgs, this);
1178    
1179                    if ((list != null) && !list.isEmpty()) {
1180                            for (PortletPreferences portletPreferences : list) {
1181                                    if ((plid != portletPreferences.getPlid()) ||
1182                                                    !Validator.equals(portletId,
1183                                                            portletPreferences.getPortletId())) {
1184                                            list = null;
1185    
1186                                            break;
1187                                    }
1188                            }
1189                    }
1190    
1191                    if (list == null) {
1192                            StringBundler query = null;
1193    
1194                            if (orderByComparator != null) {
1195                                    query = new StringBundler(4 +
1196                                                    (orderByComparator.getOrderByFields().length * 3));
1197                            }
1198                            else {
1199                                    query = new StringBundler(4);
1200                            }
1201    
1202                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1203    
1204                            query.append(_FINDER_COLUMN_P_P_PLID_2);
1205    
1206                            if (portletId == null) {
1207                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
1208                            }
1209                            else {
1210                                    if (portletId.equals(StringPool.BLANK)) {
1211                                            query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
1212                                    }
1213                                    else {
1214                                            query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
1215                                    }
1216                            }
1217    
1218                            if (orderByComparator != null) {
1219                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1220                                            orderByComparator);
1221                            }
1222                            else
1223                             if (pagination) {
1224                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1225                            }
1226    
1227                            String sql = query.toString();
1228    
1229                            Session session = null;
1230    
1231                            try {
1232                                    session = openSession();
1233    
1234                                    Query q = session.createQuery(sql);
1235    
1236                                    QueryPos qPos = QueryPos.getInstance(q);
1237    
1238                                    qPos.add(plid);
1239    
1240                                    if (portletId != null) {
1241                                            qPos.add(portletId);
1242                                    }
1243    
1244                                    if (!pagination) {
1245                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1246                                                            getDialect(), start, end, false);
1247    
1248                                            Collections.sort(list);
1249    
1250                                            list = new UnmodifiableList<PortletPreferences>(list);
1251                                    }
1252                                    else {
1253                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1254                                                            getDialect(), start, end);
1255                                    }
1256    
1257                                    cacheResult(list);
1258    
1259                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1260                            }
1261                            catch (Exception e) {
1262                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1263    
1264                                    throw processException(e);
1265                            }
1266                            finally {
1267                                    closeSession(session);
1268                            }
1269                    }
1270    
1271                    return list;
1272            }
1273    
1274            /**
1275             * Returns the first portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1276             *
1277             * @param plid the plid
1278             * @param portletId the portlet ID
1279             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1280             * @return the first matching portlet preferences
1281             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1282             * @throws SystemException if a system exception occurred
1283             */
1284            public PortletPreferences findByP_P_First(long plid, String portletId,
1285                    OrderByComparator orderByComparator)
1286                    throws NoSuchPortletPreferencesException, SystemException {
1287                    PortletPreferences portletPreferences = fetchByP_P_First(plid,
1288                                    portletId, orderByComparator);
1289    
1290                    if (portletPreferences != null) {
1291                            return portletPreferences;
1292                    }
1293    
1294                    StringBundler msg = new StringBundler(6);
1295    
1296                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1297    
1298                    msg.append("plid=");
1299                    msg.append(plid);
1300    
1301                    msg.append(", portletId=");
1302                    msg.append(portletId);
1303    
1304                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1305    
1306                    throw new NoSuchPortletPreferencesException(msg.toString());
1307            }
1308    
1309            /**
1310             * Returns the first portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1311             *
1312             * @param plid the plid
1313             * @param portletId the portlet ID
1314             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1315             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1316             * @throws SystemException if a system exception occurred
1317             */
1318            public PortletPreferences fetchByP_P_First(long plid, String portletId,
1319                    OrderByComparator orderByComparator) throws SystemException {
1320                    List<PortletPreferences> list = findByP_P(plid, portletId, 0, 1,
1321                                    orderByComparator);
1322    
1323                    if (!list.isEmpty()) {
1324                            return list.get(0);
1325                    }
1326    
1327                    return null;
1328            }
1329    
1330            /**
1331             * Returns the last portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1332             *
1333             * @param plid the plid
1334             * @param portletId the portlet ID
1335             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1336             * @return the last matching portlet preferences
1337             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1338             * @throws SystemException if a system exception occurred
1339             */
1340            public PortletPreferences findByP_P_Last(long plid, String portletId,
1341                    OrderByComparator orderByComparator)
1342                    throws NoSuchPortletPreferencesException, SystemException {
1343                    PortletPreferences portletPreferences = fetchByP_P_Last(plid,
1344                                    portletId, orderByComparator);
1345    
1346                    if (portletPreferences != null) {
1347                            return portletPreferences;
1348                    }
1349    
1350                    StringBundler msg = new StringBundler(6);
1351    
1352                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1353    
1354                    msg.append("plid=");
1355                    msg.append(plid);
1356    
1357                    msg.append(", portletId=");
1358                    msg.append(portletId);
1359    
1360                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1361    
1362                    throw new NoSuchPortletPreferencesException(msg.toString());
1363            }
1364    
1365            /**
1366             * Returns the last portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1367             *
1368             * @param plid the plid
1369             * @param portletId the portlet ID
1370             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1371             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1372             * @throws SystemException if a system exception occurred
1373             */
1374            public PortletPreferences fetchByP_P_Last(long plid, String portletId,
1375                    OrderByComparator orderByComparator) throws SystemException {
1376                    int count = countByP_P(plid, portletId);
1377    
1378                    List<PortletPreferences> list = findByP_P(plid, portletId, count - 1,
1379                                    count, orderByComparator);
1380    
1381                    if (!list.isEmpty()) {
1382                            return list.get(0);
1383                    }
1384    
1385                    return null;
1386            }
1387    
1388            /**
1389             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where plid = &#63; and portletId = &#63;.
1390             *
1391             * @param portletPreferencesId the primary key of the current portlet preferences
1392             * @param plid the plid
1393             * @param portletId the portlet ID
1394             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1395             * @return the previous, current, and next portlet preferences
1396             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
1397             * @throws SystemException if a system exception occurred
1398             */
1399            public PortletPreferences[] findByP_P_PrevAndNext(
1400                    long portletPreferencesId, long plid, String portletId,
1401                    OrderByComparator orderByComparator)
1402                    throws NoSuchPortletPreferencesException, SystemException {
1403                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1404    
1405                    Session session = null;
1406    
1407                    try {
1408                            session = openSession();
1409    
1410                            PortletPreferences[] array = new PortletPreferencesImpl[3];
1411    
1412                            array[0] = getByP_P_PrevAndNext(session, portletPreferences, plid,
1413                                            portletId, orderByComparator, true);
1414    
1415                            array[1] = portletPreferences;
1416    
1417                            array[2] = getByP_P_PrevAndNext(session, portletPreferences, plid,
1418                                            portletId, orderByComparator, false);
1419    
1420                            return array;
1421                    }
1422                    catch (Exception e) {
1423                            throw processException(e);
1424                    }
1425                    finally {
1426                            closeSession(session);
1427                    }
1428            }
1429    
1430            protected PortletPreferences getByP_P_PrevAndNext(Session session,
1431                    PortletPreferences portletPreferences, long plid, String portletId,
1432                    OrderByComparator orderByComparator, boolean previous) {
1433                    StringBundler query = null;
1434    
1435                    if (orderByComparator != null) {
1436                            query = new StringBundler(6 +
1437                                            (orderByComparator.getOrderByFields().length * 6));
1438                    }
1439                    else {
1440                            query = new StringBundler(3);
1441                    }
1442    
1443                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1444    
1445                    query.append(_FINDER_COLUMN_P_P_PLID_2);
1446    
1447                    if (portletId == null) {
1448                            query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
1449                    }
1450                    else {
1451                            if (portletId.equals(StringPool.BLANK)) {
1452                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
1453                            }
1454                            else {
1455                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
1456                            }
1457                    }
1458    
1459                    if (orderByComparator != null) {
1460                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1461    
1462                            if (orderByConditionFields.length > 0) {
1463                                    query.append(WHERE_AND);
1464                            }
1465    
1466                            for (int i = 0; i < orderByConditionFields.length; i++) {
1467                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1468                                    query.append(orderByConditionFields[i]);
1469    
1470                                    if ((i + 1) < orderByConditionFields.length) {
1471                                            if (orderByComparator.isAscending() ^ previous) {
1472                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1473                                            }
1474                                            else {
1475                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1476                                            }
1477                                    }
1478                                    else {
1479                                            if (orderByComparator.isAscending() ^ previous) {
1480                                                    query.append(WHERE_GREATER_THAN);
1481                                            }
1482                                            else {
1483                                                    query.append(WHERE_LESSER_THAN);
1484                                            }
1485                                    }
1486                            }
1487    
1488                            query.append(ORDER_BY_CLAUSE);
1489    
1490                            String[] orderByFields = orderByComparator.getOrderByFields();
1491    
1492                            for (int i = 0; i < orderByFields.length; i++) {
1493                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1494                                    query.append(orderByFields[i]);
1495    
1496                                    if ((i + 1) < orderByFields.length) {
1497                                            if (orderByComparator.isAscending() ^ previous) {
1498                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1499                                            }
1500                                            else {
1501                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1502                                            }
1503                                    }
1504                                    else {
1505                                            if (orderByComparator.isAscending() ^ previous) {
1506                                                    query.append(ORDER_BY_ASC);
1507                                            }
1508                                            else {
1509                                                    query.append(ORDER_BY_DESC);
1510                                            }
1511                                    }
1512                            }
1513                    }
1514                    else {
1515                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1516                    }
1517    
1518                    String sql = query.toString();
1519    
1520                    Query q = session.createQuery(sql);
1521    
1522                    q.setFirstResult(0);
1523                    q.setMaxResults(2);
1524    
1525                    QueryPos qPos = QueryPos.getInstance(q);
1526    
1527                    qPos.add(plid);
1528    
1529                    if (portletId != null) {
1530                            qPos.add(portletId);
1531                    }
1532    
1533                    if (orderByComparator != null) {
1534                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
1535    
1536                            for (Object value : values) {
1537                                    qPos.add(value);
1538                            }
1539                    }
1540    
1541                    List<PortletPreferences> list = q.list();
1542    
1543                    if (list.size() == 2) {
1544                            return list.get(1);
1545                    }
1546                    else {
1547                            return null;
1548                    }
1549            }
1550    
1551            /**
1552             * Removes all the portlet preferenceses where plid = &#63; and portletId = &#63; from the database.
1553             *
1554             * @param plid the plid
1555             * @param portletId the portlet ID
1556             * @throws SystemException if a system exception occurred
1557             */
1558            public void removeByP_P(long plid, String portletId)
1559                    throws SystemException {
1560                    for (PortletPreferences portletPreferences : findByP_P(plid, portletId,
1561                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1562                            remove(portletPreferences);
1563                    }
1564            }
1565    
1566            /**
1567             * Returns the number of portlet preferenceses where plid = &#63; and portletId = &#63;.
1568             *
1569             * @param plid the plid
1570             * @param portletId the portlet ID
1571             * @return the number of matching portlet preferenceses
1572             * @throws SystemException if a system exception occurred
1573             */
1574            public int countByP_P(long plid, String portletId)
1575                    throws SystemException {
1576                    FinderPath finderPath = FINDER_PATH_COUNT_BY_P_P;
1577    
1578                    Object[] finderArgs = new Object[] { plid, portletId };
1579    
1580                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1581                                    this);
1582    
1583                    if (count == null) {
1584                            StringBundler query = new StringBundler(3);
1585    
1586                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1587    
1588                            query.append(_FINDER_COLUMN_P_P_PLID_2);
1589    
1590                            if (portletId == null) {
1591                                    query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
1592                            }
1593                            else {
1594                                    if (portletId.equals(StringPool.BLANK)) {
1595                                            query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
1596                                    }
1597                                    else {
1598                                            query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
1599                                    }
1600                            }
1601    
1602                            String sql = query.toString();
1603    
1604                            Session session = null;
1605    
1606                            try {
1607                                    session = openSession();
1608    
1609                                    Query q = session.createQuery(sql);
1610    
1611                                    QueryPos qPos = QueryPos.getInstance(q);
1612    
1613                                    qPos.add(plid);
1614    
1615                                    if (portletId != null) {
1616                                            qPos.add(portletId);
1617                                    }
1618    
1619                                    count = (Long)q.uniqueResult();
1620    
1621                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1622                            }
1623                            catch (Exception e) {
1624                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1625    
1626                                    throw processException(e);
1627                            }
1628                            finally {
1629                                    closeSession(session);
1630                            }
1631                    }
1632    
1633                    return count.intValue();
1634            }
1635    
1636            private static final String _FINDER_COLUMN_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
1637            private static final String _FINDER_COLUMN_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1638            private static final String _FINDER_COLUMN_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
1639            private static final String _FINDER_COLUMN_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
1640            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1641                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1642                            PortletPreferencesImpl.class,
1643                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_O_P",
1644                            new String[] {
1645                                    Long.class.getName(), Integer.class.getName(),
1646                                    Long.class.getName(),
1647                                    
1648                            Integer.class.getName(), Integer.class.getName(),
1649                                    OrderByComparator.class.getName()
1650                            });
1651            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1652                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
1653                            PortletPreferencesImpl.class,
1654                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_O_P",
1655                            new String[] {
1656                                    Long.class.getName(), Integer.class.getName(),
1657                                    Long.class.getName()
1658                            },
1659                            PortletPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
1660                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
1661                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK);
1662            public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
1663                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
1664                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O_P",
1665                            new String[] {
1666                                    Long.class.getName(), Integer.class.getName(),
1667                                    Long.class.getName()
1668                            });
1669    
1670            /**
1671             * Returns all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1672             *
1673             * @param ownerId the owner ID
1674             * @param ownerType the owner type
1675             * @param plid the plid
1676             * @return the matching portlet preferenceses
1677             * @throws SystemException if a system exception occurred
1678             */
1679            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
1680                    long plid) throws SystemException {
1681                    return findByO_O_P(ownerId, ownerType, plid, QueryUtil.ALL_POS,
1682                            QueryUtil.ALL_POS, null);
1683            }
1684    
1685            /**
1686             * Returns a range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1687             *
1688             * <p>
1689             * 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.
1690             * </p>
1691             *
1692             * @param ownerId the owner ID
1693             * @param ownerType the owner type
1694             * @param plid the plid
1695             * @param start the lower bound of the range of portlet preferenceses
1696             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1697             * @return the range of matching portlet preferenceses
1698             * @throws SystemException if a system exception occurred
1699             */
1700            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
1701                    long plid, int start, int end) throws SystemException {
1702                    return findByO_O_P(ownerId, ownerType, plid, start, end, null);
1703            }
1704    
1705            /**
1706             * Returns an ordered range of all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1707             *
1708             * <p>
1709             * 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.
1710             * </p>
1711             *
1712             * @param ownerId the owner ID
1713             * @param ownerType the owner type
1714             * @param plid the plid
1715             * @param start the lower bound of the range of portlet preferenceses
1716             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
1717             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1718             * @return the ordered range of matching portlet preferenceses
1719             * @throws SystemException if a system exception occurred
1720             */
1721            public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
1722                    long plid, int start, int end, OrderByComparator orderByComparator)
1723                    throws SystemException {
1724                    boolean pagination = true;
1725                    FinderPath finderPath = null;
1726                    Object[] finderArgs = null;
1727    
1728                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1729                                    (orderByComparator == null)) {
1730                            pagination = false;
1731                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P;
1732                            finderArgs = new Object[] { ownerId, ownerType, plid };
1733                    }
1734                    else {
1735                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_O_P;
1736                            finderArgs = new Object[] {
1737                                            ownerId, ownerType, plid,
1738                                            
1739                                            start, end, orderByComparator
1740                                    };
1741                    }
1742    
1743                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
1744                                    finderArgs, this);
1745    
1746                    if ((list != null) && !list.isEmpty()) {
1747                            for (PortletPreferences portletPreferences : list) {
1748                                    if ((ownerId != portletPreferences.getOwnerId()) ||
1749                                                    (ownerType != portletPreferences.getOwnerType()) ||
1750                                                    (plid != portletPreferences.getPlid())) {
1751                                            list = null;
1752    
1753                                            break;
1754                                    }
1755                            }
1756                    }
1757    
1758                    if (list == null) {
1759                            StringBundler query = null;
1760    
1761                            if (orderByComparator != null) {
1762                                    query = new StringBundler(5 +
1763                                                    (orderByComparator.getOrderByFields().length * 3));
1764                            }
1765                            else {
1766                                    query = new StringBundler(5);
1767                            }
1768    
1769                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1770    
1771                            query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
1772    
1773                            query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
1774    
1775                            query.append(_FINDER_COLUMN_O_O_P_PLID_2);
1776    
1777                            if (orderByComparator != null) {
1778                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1779                                            orderByComparator);
1780                            }
1781                            else
1782                             if (pagination) {
1783                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
1784                            }
1785    
1786                            String sql = query.toString();
1787    
1788                            Session session = null;
1789    
1790                            try {
1791                                    session = openSession();
1792    
1793                                    Query q = session.createQuery(sql);
1794    
1795                                    QueryPos qPos = QueryPos.getInstance(q);
1796    
1797                                    qPos.add(ownerId);
1798    
1799                                    qPos.add(ownerType);
1800    
1801                                    qPos.add(plid);
1802    
1803                                    if (!pagination) {
1804                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1805                                                            getDialect(), start, end, false);
1806    
1807                                            Collections.sort(list);
1808    
1809                                            list = new UnmodifiableList<PortletPreferences>(list);
1810                                    }
1811                                    else {
1812                                            list = (List<PortletPreferences>)QueryUtil.list(q,
1813                                                            getDialect(), start, end);
1814                                    }
1815    
1816                                    cacheResult(list);
1817    
1818                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1819                            }
1820                            catch (Exception e) {
1821                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1822    
1823                                    throw processException(e);
1824                            }
1825                            finally {
1826                                    closeSession(session);
1827                            }
1828                    }
1829    
1830                    return list;
1831            }
1832    
1833            /**
1834             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1835             *
1836             * @param ownerId the owner ID
1837             * @param ownerType the owner type
1838             * @param plid the plid
1839             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1840             * @return the first matching portlet preferences
1841             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1842             * @throws SystemException if a system exception occurred
1843             */
1844            public PortletPreferences findByO_O_P_First(long ownerId, int ownerType,
1845                    long plid, OrderByComparator orderByComparator)
1846                    throws NoSuchPortletPreferencesException, SystemException {
1847                    PortletPreferences portletPreferences = fetchByO_O_P_First(ownerId,
1848                                    ownerType, plid, orderByComparator);
1849    
1850                    if (portletPreferences != null) {
1851                            return portletPreferences;
1852                    }
1853    
1854                    StringBundler msg = new StringBundler(8);
1855    
1856                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1857    
1858                    msg.append("ownerId=");
1859                    msg.append(ownerId);
1860    
1861                    msg.append(", ownerType=");
1862                    msg.append(ownerType);
1863    
1864                    msg.append(", plid=");
1865                    msg.append(plid);
1866    
1867                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1868    
1869                    throw new NoSuchPortletPreferencesException(msg.toString());
1870            }
1871    
1872            /**
1873             * Returns the first portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1874             *
1875             * @param ownerId the owner ID
1876             * @param ownerType the owner type
1877             * @param plid the plid
1878             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1879             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1880             * @throws SystemException if a system exception occurred
1881             */
1882            public PortletPreferences fetchByO_O_P_First(long ownerId, int ownerType,
1883                    long plid, OrderByComparator orderByComparator)
1884                    throws SystemException {
1885                    List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
1886                                    0, 1, orderByComparator);
1887    
1888                    if (!list.isEmpty()) {
1889                            return list.get(0);
1890                    }
1891    
1892                    return null;
1893            }
1894    
1895            /**
1896             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1897             *
1898             * @param ownerId the owner ID
1899             * @param ownerType the owner type
1900             * @param plid the plid
1901             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1902             * @return the last matching portlet preferences
1903             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
1904             * @throws SystemException if a system exception occurred
1905             */
1906            public PortletPreferences findByO_O_P_Last(long ownerId, int ownerType,
1907                    long plid, OrderByComparator orderByComparator)
1908                    throws NoSuchPortletPreferencesException, SystemException {
1909                    PortletPreferences portletPreferences = fetchByO_O_P_Last(ownerId,
1910                                    ownerType, plid, orderByComparator);
1911    
1912                    if (portletPreferences != null) {
1913                            return portletPreferences;
1914                    }
1915    
1916                    StringBundler msg = new StringBundler(8);
1917    
1918                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1919    
1920                    msg.append("ownerId=");
1921                    msg.append(ownerId);
1922    
1923                    msg.append(", ownerType=");
1924                    msg.append(ownerType);
1925    
1926                    msg.append(", plid=");
1927                    msg.append(plid);
1928    
1929                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1930    
1931                    throw new NoSuchPortletPreferencesException(msg.toString());
1932            }
1933    
1934            /**
1935             * Returns the last portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1936             *
1937             * @param ownerId the owner ID
1938             * @param ownerType the owner type
1939             * @param plid the plid
1940             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1941             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
1942             * @throws SystemException if a system exception occurred
1943             */
1944            public PortletPreferences fetchByO_O_P_Last(long ownerId, int ownerType,
1945                    long plid, OrderByComparator orderByComparator)
1946                    throws SystemException {
1947                    int count = countByO_O_P(ownerId, ownerType, plid);
1948    
1949                    List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
1950                                    count - 1, count, orderByComparator);
1951    
1952                    if (!list.isEmpty()) {
1953                            return list.get(0);
1954                    }
1955    
1956                    return null;
1957            }
1958    
1959            /**
1960             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
1961             *
1962             * @param portletPreferencesId the primary key of the current portlet preferences
1963             * @param ownerId the owner ID
1964             * @param ownerType the owner type
1965             * @param plid the plid
1966             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1967             * @return the previous, current, and next portlet preferences
1968             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
1969             * @throws SystemException if a system exception occurred
1970             */
1971            public PortletPreferences[] findByO_O_P_PrevAndNext(
1972                    long portletPreferencesId, long ownerId, int ownerType, long plid,
1973                    OrderByComparator orderByComparator)
1974                    throws NoSuchPortletPreferencesException, SystemException {
1975                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1976    
1977                    Session session = null;
1978    
1979                    try {
1980                            session = openSession();
1981    
1982                            PortletPreferences[] array = new PortletPreferencesImpl[3];
1983    
1984                            array[0] = getByO_O_P_PrevAndNext(session, portletPreferences,
1985                                            ownerId, ownerType, plid, orderByComparator, true);
1986    
1987                            array[1] = portletPreferences;
1988    
1989                            array[2] = getByO_O_P_PrevAndNext(session, portletPreferences,
1990                                            ownerId, ownerType, plid, orderByComparator, false);
1991    
1992                            return array;
1993                    }
1994                    catch (Exception e) {
1995                            throw processException(e);
1996                    }
1997                    finally {
1998                            closeSession(session);
1999                    }
2000            }
2001    
2002            protected PortletPreferences getByO_O_P_PrevAndNext(Session session,
2003                    PortletPreferences portletPreferences, long ownerId, int ownerType,
2004                    long plid, OrderByComparator orderByComparator, boolean previous) {
2005                    StringBundler query = null;
2006    
2007                    if (orderByComparator != null) {
2008                            query = new StringBundler(6 +
2009                                            (orderByComparator.getOrderByFields().length * 6));
2010                    }
2011                    else {
2012                            query = new StringBundler(3);
2013                    }
2014    
2015                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2016    
2017                    query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
2018    
2019                    query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
2020    
2021                    query.append(_FINDER_COLUMN_O_O_P_PLID_2);
2022    
2023                    if (orderByComparator != null) {
2024                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2025    
2026                            if (orderByConditionFields.length > 0) {
2027                                    query.append(WHERE_AND);
2028                            }
2029    
2030                            for (int i = 0; i < orderByConditionFields.length; i++) {
2031                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2032                                    query.append(orderByConditionFields[i]);
2033    
2034                                    if ((i + 1) < orderByConditionFields.length) {
2035                                            if (orderByComparator.isAscending() ^ previous) {
2036                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2037                                            }
2038                                            else {
2039                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2040                                            }
2041                                    }
2042                                    else {
2043                                            if (orderByComparator.isAscending() ^ previous) {
2044                                                    query.append(WHERE_GREATER_THAN);
2045                                            }
2046                                            else {
2047                                                    query.append(WHERE_LESSER_THAN);
2048                                            }
2049                                    }
2050                            }
2051    
2052                            query.append(ORDER_BY_CLAUSE);
2053    
2054                            String[] orderByFields = orderByComparator.getOrderByFields();
2055    
2056                            for (int i = 0; i < orderByFields.length; i++) {
2057                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2058                                    query.append(orderByFields[i]);
2059    
2060                                    if ((i + 1) < orderByFields.length) {
2061                                            if (orderByComparator.isAscending() ^ previous) {
2062                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2063                                            }
2064                                            else {
2065                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2066                                            }
2067                                    }
2068                                    else {
2069                                            if (orderByComparator.isAscending() ^ previous) {
2070                                                    query.append(ORDER_BY_ASC);
2071                                            }
2072                                            else {
2073                                                    query.append(ORDER_BY_DESC);
2074                                            }
2075                                    }
2076                            }
2077                    }
2078                    else {
2079                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2080                    }
2081    
2082                    String sql = query.toString();
2083    
2084                    Query q = session.createQuery(sql);
2085    
2086                    q.setFirstResult(0);
2087                    q.setMaxResults(2);
2088    
2089                    QueryPos qPos = QueryPos.getInstance(q);
2090    
2091                    qPos.add(ownerId);
2092    
2093                    qPos.add(ownerType);
2094    
2095                    qPos.add(plid);
2096    
2097                    if (orderByComparator != null) {
2098                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
2099    
2100                            for (Object value : values) {
2101                                    qPos.add(value);
2102                            }
2103                    }
2104    
2105                    List<PortletPreferences> list = q.list();
2106    
2107                    if (list.size() == 2) {
2108                            return list.get(1);
2109                    }
2110                    else {
2111                            return null;
2112                    }
2113            }
2114    
2115            /**
2116             * Removes all the portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63; from the database.
2117             *
2118             * @param ownerId the owner ID
2119             * @param ownerType the owner type
2120             * @param plid the plid
2121             * @throws SystemException if a system exception occurred
2122             */
2123            public void removeByO_O_P(long ownerId, int ownerType, long plid)
2124                    throws SystemException {
2125                    for (PortletPreferences portletPreferences : findByO_O_P(ownerId,
2126                                    ownerType, plid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2127                            remove(portletPreferences);
2128                    }
2129            }
2130    
2131            /**
2132             * Returns the number of portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63;.
2133             *
2134             * @param ownerId the owner ID
2135             * @param ownerType the owner type
2136             * @param plid the plid
2137             * @return the number of matching portlet preferenceses
2138             * @throws SystemException if a system exception occurred
2139             */
2140            public int countByO_O_P(long ownerId, int ownerType, long plid)
2141                    throws SystemException {
2142                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O_P;
2143    
2144                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid };
2145    
2146                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2147                                    this);
2148    
2149                    if (count == null) {
2150                            StringBundler query = new StringBundler(4);
2151    
2152                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
2153    
2154                            query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
2155    
2156                            query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
2157    
2158                            query.append(_FINDER_COLUMN_O_O_P_PLID_2);
2159    
2160                            String sql = query.toString();
2161    
2162                            Session session = null;
2163    
2164                            try {
2165                                    session = openSession();
2166    
2167                                    Query q = session.createQuery(sql);
2168    
2169                                    QueryPos qPos = QueryPos.getInstance(q);
2170    
2171                                    qPos.add(ownerId);
2172    
2173                                    qPos.add(ownerType);
2174    
2175                                    qPos.add(plid);
2176    
2177                                    count = (Long)q.uniqueResult();
2178    
2179                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2180                            }
2181                            catch (Exception e) {
2182                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2183    
2184                                    throw processException(e);
2185                            }
2186                            finally {
2187                                    closeSession(session);
2188                            }
2189                    }
2190    
2191                    return count.intValue();
2192            }
2193    
2194            private static final String _FINDER_COLUMN_O_O_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
2195            private static final String _FINDER_COLUMN_O_O_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
2196            private static final String _FINDER_COLUMN_O_O_P_PLID_2 = "portletPreferences.plid = ?";
2197            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2198                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2199                            PortletPreferencesImpl.class,
2200                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByO_P_P",
2201                            new String[] {
2202                                    Integer.class.getName(), Long.class.getName(),
2203                                    String.class.getName(),
2204                                    
2205                            Integer.class.getName(), Integer.class.getName(),
2206                                    OrderByComparator.class.getName()
2207                            });
2208            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2209                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2210                            PortletPreferencesImpl.class,
2211                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByO_P_P",
2212                            new String[] {
2213                                    Integer.class.getName(), Long.class.getName(),
2214                                    String.class.getName()
2215                            },
2216                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
2217                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
2218                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
2219            public static final FinderPath FINDER_PATH_COUNT_BY_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2220                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
2221                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_P_P",
2222                            new String[] {
2223                                    Integer.class.getName(), Long.class.getName(),
2224                                    String.class.getName()
2225                            });
2226    
2227            /**
2228             * Returns all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2229             *
2230             * @param ownerType the owner type
2231             * @param plid the plid
2232             * @param portletId the portlet ID
2233             * @return the matching portlet preferenceses
2234             * @throws SystemException if a system exception occurred
2235             */
2236            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
2237                    String portletId) throws SystemException {
2238                    return findByO_P_P(ownerType, plid, portletId, QueryUtil.ALL_POS,
2239                            QueryUtil.ALL_POS, null);
2240            }
2241    
2242            /**
2243             * Returns a range of all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2244             *
2245             * <p>
2246             * 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.
2247             * </p>
2248             *
2249             * @param ownerType the owner type
2250             * @param plid the plid
2251             * @param portletId the portlet ID
2252             * @param start the lower bound of the range of portlet preferenceses
2253             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2254             * @return the range of matching portlet preferenceses
2255             * @throws SystemException if a system exception occurred
2256             */
2257            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
2258                    String portletId, int start, int end) throws SystemException {
2259                    return findByO_P_P(ownerType, plid, portletId, start, end, null);
2260            }
2261    
2262            /**
2263             * Returns an ordered range of all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2264             *
2265             * <p>
2266             * 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.
2267             * </p>
2268             *
2269             * @param ownerType the owner type
2270             * @param plid the plid
2271             * @param portletId the portlet ID
2272             * @param start the lower bound of the range of portlet preferenceses
2273             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
2274             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2275             * @return the ordered range of matching portlet preferenceses
2276             * @throws SystemException if a system exception occurred
2277             */
2278            public List<PortletPreferences> findByO_P_P(int ownerType, long plid,
2279                    String portletId, int start, int end,
2280                    OrderByComparator orderByComparator) throws SystemException {
2281                    boolean pagination = true;
2282                    FinderPath finderPath = null;
2283                    Object[] finderArgs = null;
2284    
2285                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2286                                    (orderByComparator == null)) {
2287                            pagination = false;
2288                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P;
2289                            finderArgs = new Object[] { ownerType, plid, portletId };
2290                    }
2291                    else {
2292                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_O_P_P;
2293                            finderArgs = new Object[] {
2294                                            ownerType, plid, portletId,
2295                                            
2296                                            start, end, orderByComparator
2297                                    };
2298                    }
2299    
2300                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
2301                                    finderArgs, this);
2302    
2303                    if ((list != null) && !list.isEmpty()) {
2304                            for (PortletPreferences portletPreferences : list) {
2305                                    if ((ownerType != portletPreferences.getOwnerType()) ||
2306                                                    (plid != portletPreferences.getPlid()) ||
2307                                                    !Validator.equals(portletId,
2308                                                            portletPreferences.getPortletId())) {
2309                                            list = null;
2310    
2311                                            break;
2312                                    }
2313                            }
2314                    }
2315    
2316                    if (list == null) {
2317                            StringBundler query = null;
2318    
2319                            if (orderByComparator != null) {
2320                                    query = new StringBundler(5 +
2321                                                    (orderByComparator.getOrderByFields().length * 3));
2322                            }
2323                            else {
2324                                    query = new StringBundler(5);
2325                            }
2326    
2327                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2328    
2329                            query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
2330    
2331                            query.append(_FINDER_COLUMN_O_P_P_PLID_2);
2332    
2333                            if (portletId == null) {
2334                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
2335                            }
2336                            else {
2337                                    if (portletId.equals(StringPool.BLANK)) {
2338                                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
2339                                    }
2340                                    else {
2341                                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
2342                                    }
2343                            }
2344    
2345                            if (orderByComparator != null) {
2346                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2347                                            orderByComparator);
2348                            }
2349                            else
2350                             if (pagination) {
2351                                    query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2352                            }
2353    
2354                            String sql = query.toString();
2355    
2356                            Session session = null;
2357    
2358                            try {
2359                                    session = openSession();
2360    
2361                                    Query q = session.createQuery(sql);
2362    
2363                                    QueryPos qPos = QueryPos.getInstance(q);
2364    
2365                                    qPos.add(ownerType);
2366    
2367                                    qPos.add(plid);
2368    
2369                                    if (portletId != null) {
2370                                            qPos.add(portletId);
2371                                    }
2372    
2373                                    if (!pagination) {
2374                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2375                                                            getDialect(), start, end, false);
2376    
2377                                            Collections.sort(list);
2378    
2379                                            list = new UnmodifiableList<PortletPreferences>(list);
2380                                    }
2381                                    else {
2382                                            list = (List<PortletPreferences>)QueryUtil.list(q,
2383                                                            getDialect(), start, end);
2384                                    }
2385    
2386                                    cacheResult(list);
2387    
2388                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2389                            }
2390                            catch (Exception e) {
2391                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2392    
2393                                    throw processException(e);
2394                            }
2395                            finally {
2396                                    closeSession(session);
2397                            }
2398                    }
2399    
2400                    return list;
2401            }
2402    
2403            /**
2404             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2405             *
2406             * @param ownerType the owner type
2407             * @param plid the plid
2408             * @param portletId the portlet ID
2409             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2410             * @return the first matching portlet preferences
2411             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
2412             * @throws SystemException if a system exception occurred
2413             */
2414            public PortletPreferences findByO_P_P_First(int ownerType, long plid,
2415                    String portletId, OrderByComparator orderByComparator)
2416                    throws NoSuchPortletPreferencesException, SystemException {
2417                    PortletPreferences portletPreferences = fetchByO_P_P_First(ownerType,
2418                                    plid, portletId, orderByComparator);
2419    
2420                    if (portletPreferences != null) {
2421                            return portletPreferences;
2422                    }
2423    
2424                    StringBundler msg = new StringBundler(8);
2425    
2426                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2427    
2428                    msg.append("ownerType=");
2429                    msg.append(ownerType);
2430    
2431                    msg.append(", plid=");
2432                    msg.append(plid);
2433    
2434                    msg.append(", portletId=");
2435                    msg.append(portletId);
2436    
2437                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2438    
2439                    throw new NoSuchPortletPreferencesException(msg.toString());
2440            }
2441    
2442            /**
2443             * Returns the first portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2444             *
2445             * @param ownerType the owner type
2446             * @param plid the plid
2447             * @param portletId the portlet ID
2448             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2449             * @return the first matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2450             * @throws SystemException if a system exception occurred
2451             */
2452            public PortletPreferences fetchByO_P_P_First(int ownerType, long plid,
2453                    String portletId, OrderByComparator orderByComparator)
2454                    throws SystemException {
2455                    List<PortletPreferences> list = findByO_P_P(ownerType, plid, portletId,
2456                                    0, 1, orderByComparator);
2457    
2458                    if (!list.isEmpty()) {
2459                            return list.get(0);
2460                    }
2461    
2462                    return null;
2463            }
2464    
2465            /**
2466             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2467             *
2468             * @param ownerType the owner type
2469             * @param plid the plid
2470             * @param portletId the portlet ID
2471             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2472             * @return the last matching portlet preferences
2473             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
2474             * @throws SystemException if a system exception occurred
2475             */
2476            public PortletPreferences findByO_P_P_Last(int ownerType, long plid,
2477                    String portletId, OrderByComparator orderByComparator)
2478                    throws NoSuchPortletPreferencesException, SystemException {
2479                    PortletPreferences portletPreferences = fetchByO_P_P_Last(ownerType,
2480                                    plid, portletId, orderByComparator);
2481    
2482                    if (portletPreferences != null) {
2483                            return portletPreferences;
2484                    }
2485    
2486                    StringBundler msg = new StringBundler(8);
2487    
2488                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2489    
2490                    msg.append("ownerType=");
2491                    msg.append(ownerType);
2492    
2493                    msg.append(", plid=");
2494                    msg.append(plid);
2495    
2496                    msg.append(", portletId=");
2497                    msg.append(portletId);
2498    
2499                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2500    
2501                    throw new NoSuchPortletPreferencesException(msg.toString());
2502            }
2503    
2504            /**
2505             * Returns the last portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2506             *
2507             * @param ownerType the owner type
2508             * @param plid the plid
2509             * @param portletId the portlet ID
2510             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2511             * @return the last matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2512             * @throws SystemException if a system exception occurred
2513             */
2514            public PortletPreferences fetchByO_P_P_Last(int ownerType, long plid,
2515                    String portletId, OrderByComparator orderByComparator)
2516                    throws SystemException {
2517                    int count = countByO_P_P(ownerType, plid, portletId);
2518    
2519                    List<PortletPreferences> list = findByO_P_P(ownerType, plid, portletId,
2520                                    count - 1, count, orderByComparator);
2521    
2522                    if (!list.isEmpty()) {
2523                            return list.get(0);
2524                    }
2525    
2526                    return null;
2527            }
2528    
2529            /**
2530             * Returns the portlet preferenceses before and after the current portlet preferences in the ordered set where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2531             *
2532             * @param portletPreferencesId the primary key of the current portlet preferences
2533             * @param ownerType the owner type
2534             * @param plid the plid
2535             * @param portletId the portlet ID
2536             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2537             * @return the previous, current, and next portlet preferences
2538             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
2539             * @throws SystemException if a system exception occurred
2540             */
2541            public PortletPreferences[] findByO_P_P_PrevAndNext(
2542                    long portletPreferencesId, int ownerType, long plid, String portletId,
2543                    OrderByComparator orderByComparator)
2544                    throws NoSuchPortletPreferencesException, SystemException {
2545                    PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
2546    
2547                    Session session = null;
2548    
2549                    try {
2550                            session = openSession();
2551    
2552                            PortletPreferences[] array = new PortletPreferencesImpl[3];
2553    
2554                            array[0] = getByO_P_P_PrevAndNext(session, portletPreferences,
2555                                            ownerType, plid, portletId, orderByComparator, true);
2556    
2557                            array[1] = portletPreferences;
2558    
2559                            array[2] = getByO_P_P_PrevAndNext(session, portletPreferences,
2560                                            ownerType, plid, portletId, orderByComparator, false);
2561    
2562                            return array;
2563                    }
2564                    catch (Exception e) {
2565                            throw processException(e);
2566                    }
2567                    finally {
2568                            closeSession(session);
2569                    }
2570            }
2571    
2572            protected PortletPreferences getByO_P_P_PrevAndNext(Session session,
2573                    PortletPreferences portletPreferences, int ownerType, long plid,
2574                    String portletId, OrderByComparator orderByComparator, boolean previous) {
2575                    StringBundler query = null;
2576    
2577                    if (orderByComparator != null) {
2578                            query = new StringBundler(6 +
2579                                            (orderByComparator.getOrderByFields().length * 6));
2580                    }
2581                    else {
2582                            query = new StringBundler(3);
2583                    }
2584    
2585                    query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2586    
2587                    query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
2588    
2589                    query.append(_FINDER_COLUMN_O_P_P_PLID_2);
2590    
2591                    if (portletId == null) {
2592                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
2593                    }
2594                    else {
2595                            if (portletId.equals(StringPool.BLANK)) {
2596                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
2597                            }
2598                            else {
2599                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
2600                            }
2601                    }
2602    
2603                    if (orderByComparator != null) {
2604                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2605    
2606                            if (orderByConditionFields.length > 0) {
2607                                    query.append(WHERE_AND);
2608                            }
2609    
2610                            for (int i = 0; i < orderByConditionFields.length; i++) {
2611                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2612                                    query.append(orderByConditionFields[i]);
2613    
2614                                    if ((i + 1) < orderByConditionFields.length) {
2615                                            if (orderByComparator.isAscending() ^ previous) {
2616                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2617                                            }
2618                                            else {
2619                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2620                                            }
2621                                    }
2622                                    else {
2623                                            if (orderByComparator.isAscending() ^ previous) {
2624                                                    query.append(WHERE_GREATER_THAN);
2625                                            }
2626                                            else {
2627                                                    query.append(WHERE_LESSER_THAN);
2628                                            }
2629                                    }
2630                            }
2631    
2632                            query.append(ORDER_BY_CLAUSE);
2633    
2634                            String[] orderByFields = orderByComparator.getOrderByFields();
2635    
2636                            for (int i = 0; i < orderByFields.length; i++) {
2637                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2638                                    query.append(orderByFields[i]);
2639    
2640                                    if ((i + 1) < orderByFields.length) {
2641                                            if (orderByComparator.isAscending() ^ previous) {
2642                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2643                                            }
2644                                            else {
2645                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2646                                            }
2647                                    }
2648                                    else {
2649                                            if (orderByComparator.isAscending() ^ previous) {
2650                                                    query.append(ORDER_BY_ASC);
2651                                            }
2652                                            else {
2653                                                    query.append(ORDER_BY_DESC);
2654                                            }
2655                                    }
2656                            }
2657                    }
2658                    else {
2659                            query.append(PortletPreferencesModelImpl.ORDER_BY_JPQL);
2660                    }
2661    
2662                    String sql = query.toString();
2663    
2664                    Query q = session.createQuery(sql);
2665    
2666                    q.setFirstResult(0);
2667                    q.setMaxResults(2);
2668    
2669                    QueryPos qPos = QueryPos.getInstance(q);
2670    
2671                    qPos.add(ownerType);
2672    
2673                    qPos.add(plid);
2674    
2675                    if (portletId != null) {
2676                            qPos.add(portletId);
2677                    }
2678    
2679                    if (orderByComparator != null) {
2680                            Object[] values = orderByComparator.getOrderByConditionValues(portletPreferences);
2681    
2682                            for (Object value : values) {
2683                                    qPos.add(value);
2684                            }
2685                    }
2686    
2687                    List<PortletPreferences> list = q.list();
2688    
2689                    if (list.size() == 2) {
2690                            return list.get(1);
2691                    }
2692                    else {
2693                            return null;
2694                    }
2695            }
2696    
2697            /**
2698             * Removes all the portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63; from the database.
2699             *
2700             * @param ownerType the owner type
2701             * @param plid the plid
2702             * @param portletId the portlet ID
2703             * @throws SystemException if a system exception occurred
2704             */
2705            public void removeByO_P_P(int ownerType, long plid, String portletId)
2706                    throws SystemException {
2707                    for (PortletPreferences portletPreferences : findByO_P_P(ownerType,
2708                                    plid, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2709                            remove(portletPreferences);
2710                    }
2711            }
2712    
2713            /**
2714             * Returns the number of portlet preferenceses where ownerType = &#63; and plid = &#63; and portletId = &#63;.
2715             *
2716             * @param ownerType the owner type
2717             * @param plid the plid
2718             * @param portletId the portlet ID
2719             * @return the number of matching portlet preferenceses
2720             * @throws SystemException if a system exception occurred
2721             */
2722            public int countByO_P_P(int ownerType, long plid, String portletId)
2723                    throws SystemException {
2724                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_P_P;
2725    
2726                    Object[] finderArgs = new Object[] { ownerType, plid, portletId };
2727    
2728                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2729                                    this);
2730    
2731                    if (count == null) {
2732                            StringBundler query = new StringBundler(4);
2733    
2734                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
2735    
2736                            query.append(_FINDER_COLUMN_O_P_P_OWNERTYPE_2);
2737    
2738                            query.append(_FINDER_COLUMN_O_P_P_PLID_2);
2739    
2740                            if (portletId == null) {
2741                                    query.append(_FINDER_COLUMN_O_P_P_PORTLETID_1);
2742                            }
2743                            else {
2744                                    if (portletId.equals(StringPool.BLANK)) {
2745                                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_3);
2746                                    }
2747                                    else {
2748                                            query.append(_FINDER_COLUMN_O_P_P_PORTLETID_2);
2749                                    }
2750                            }
2751    
2752                            String sql = query.toString();
2753    
2754                            Session session = null;
2755    
2756                            try {
2757                                    session = openSession();
2758    
2759                                    Query q = session.createQuery(sql);
2760    
2761                                    QueryPos qPos = QueryPos.getInstance(q);
2762    
2763                                    qPos.add(ownerType);
2764    
2765                                    qPos.add(plid);
2766    
2767                                    if (portletId != null) {
2768                                            qPos.add(portletId);
2769                                    }
2770    
2771                                    count = (Long)q.uniqueResult();
2772    
2773                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2774                            }
2775                            catch (Exception e) {
2776                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2777    
2778                                    throw processException(e);
2779                            }
2780                            finally {
2781                                    closeSession(session);
2782                            }
2783                    }
2784    
2785                    return count.intValue();
2786            }
2787    
2788            private static final String _FINDER_COLUMN_O_P_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
2789            private static final String _FINDER_COLUMN_O_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
2790            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
2791            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
2792            private static final String _FINDER_COLUMN_O_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
2793            public static final FinderPath FINDER_PATH_FETCH_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2794                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
2795                            PortletPreferencesImpl.class, FINDER_CLASS_NAME_ENTITY,
2796                            "fetchByO_O_P_P",
2797                            new String[] {
2798                                    Long.class.getName(), Integer.class.getName(),
2799                                    Long.class.getName(), String.class.getName()
2800                            },
2801                            PortletPreferencesModelImpl.OWNERID_COLUMN_BITMASK |
2802                            PortletPreferencesModelImpl.OWNERTYPE_COLUMN_BITMASK |
2803                            PortletPreferencesModelImpl.PLID_COLUMN_BITMASK |
2804                            PortletPreferencesModelImpl.PORTLETID_COLUMN_BITMASK);
2805            public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
2806                            PortletPreferencesModelImpl.FINDER_CACHE_ENABLED, Long.class,
2807                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByO_O_P_P",
2808                            new String[] {
2809                                    Long.class.getName(), Integer.class.getName(),
2810                                    Long.class.getName(), String.class.getName()
2811                            });
2812    
2813            /**
2814             * 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.
2815             *
2816             * @param ownerId the owner ID
2817             * @param ownerType the owner type
2818             * @param plid the plid
2819             * @param portletId the portlet ID
2820             * @return the matching portlet preferences
2821             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a matching portlet preferences could not be found
2822             * @throws SystemException if a system exception occurred
2823             */
2824            public PortletPreferences findByO_O_P_P(long ownerId, int ownerType,
2825                    long plid, String portletId)
2826                    throws NoSuchPortletPreferencesException, SystemException {
2827                    PortletPreferences portletPreferences = fetchByO_O_P_P(ownerId,
2828                                    ownerType, plid, portletId);
2829    
2830                    if (portletPreferences == null) {
2831                            StringBundler msg = new StringBundler(10);
2832    
2833                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2834    
2835                            msg.append("ownerId=");
2836                            msg.append(ownerId);
2837    
2838                            msg.append(", ownerType=");
2839                            msg.append(ownerType);
2840    
2841                            msg.append(", plid=");
2842                            msg.append(plid);
2843    
2844                            msg.append(", portletId=");
2845                            msg.append(portletId);
2846    
2847                            msg.append(StringPool.CLOSE_CURLY_BRACE);
2848    
2849                            if (_log.isWarnEnabled()) {
2850                                    _log.warn(msg.toString());
2851                            }
2852    
2853                            throw new NoSuchPortletPreferencesException(msg.toString());
2854                    }
2855    
2856                    return portletPreferences;
2857            }
2858    
2859            /**
2860             * 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.
2861             *
2862             * @param ownerId the owner ID
2863             * @param ownerType the owner type
2864             * @param plid the plid
2865             * @param portletId the portlet ID
2866             * @return the matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2867             * @throws SystemException if a system exception occurred
2868             */
2869            public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
2870                    long plid, String portletId) throws SystemException {
2871                    return fetchByO_O_P_P(ownerId, ownerType, plid, portletId, true);
2872            }
2873    
2874            /**
2875             * 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.
2876             *
2877             * @param ownerId the owner ID
2878             * @param ownerType the owner type
2879             * @param plid the plid
2880             * @param portletId the portlet ID
2881             * @param retrieveFromCache whether to use the finder cache
2882             * @return the matching portlet preferences, or <code>null</code> if a matching portlet preferences could not be found
2883             * @throws SystemException if a system exception occurred
2884             */
2885            public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
2886                    long plid, String portletId, boolean retrieveFromCache)
2887                    throws SystemException {
2888                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid, portletId };
2889    
2890                    Object result = null;
2891    
2892                    if (retrieveFromCache) {
2893                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_O_O_P_P,
2894                                            finderArgs, this);
2895                    }
2896    
2897                    if (result instanceof PortletPreferences) {
2898                            PortletPreferences portletPreferences = (PortletPreferences)result;
2899    
2900                            if ((ownerId != portletPreferences.getOwnerId()) ||
2901                                            (ownerType != portletPreferences.getOwnerType()) ||
2902                                            (plid != portletPreferences.getPlid()) ||
2903                                            !Validator.equals(portletId,
2904                                                    portletPreferences.getPortletId())) {
2905                                    result = null;
2906                            }
2907                    }
2908    
2909                    if (result == null) {
2910                            StringBundler query = new StringBundler(6);
2911    
2912                            query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
2913    
2914                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
2915    
2916                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
2917    
2918                            query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
2919    
2920                            if (portletId == null) {
2921                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
2922                            }
2923                            else {
2924                                    if (portletId.equals(StringPool.BLANK)) {
2925                                            query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
2926                                    }
2927                                    else {
2928                                            query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
2929                                    }
2930                            }
2931    
2932                            String sql = query.toString();
2933    
2934                            Session session = null;
2935    
2936                            try {
2937                                    session = openSession();
2938    
2939                                    Query q = session.createQuery(sql);
2940    
2941                                    QueryPos qPos = QueryPos.getInstance(q);
2942    
2943                                    qPos.add(ownerId);
2944    
2945                                    qPos.add(ownerType);
2946    
2947                                    qPos.add(plid);
2948    
2949                                    if (portletId != null) {
2950                                            qPos.add(portletId);
2951                                    }
2952    
2953                                    List<PortletPreferences> list = q.list();
2954    
2955                                    if (list.isEmpty()) {
2956                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
2957                                                    finderArgs, list);
2958                                    }
2959                                    else {
2960                                            PortletPreferences portletPreferences = list.get(0);
2961    
2962                                            result = portletPreferences;
2963    
2964                                            cacheResult(portletPreferences);
2965    
2966                                            if ((portletPreferences.getOwnerId() != ownerId) ||
2967                                                            (portletPreferences.getOwnerType() != ownerType) ||
2968                                                            (portletPreferences.getPlid() != plid) ||
2969                                                            (portletPreferences.getPortletId() == null) ||
2970                                                            !portletPreferences.getPortletId().equals(portletId)) {
2971                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
2972                                                            finderArgs, portletPreferences);
2973                                            }
2974                                    }
2975                            }
2976                            catch (Exception e) {
2977                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P,
2978                                            finderArgs);
2979    
2980                                    throw processException(e);
2981                            }
2982                            finally {
2983                                    closeSession(session);
2984                            }
2985                    }
2986    
2987                    if (result instanceof List<?>) {
2988                            return null;
2989                    }
2990                    else {
2991                            return (PortletPreferences)result;
2992                    }
2993            }
2994    
2995            /**
2996             * Removes the portlet preferences where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63; from the database.
2997             *
2998             * @param ownerId the owner ID
2999             * @param ownerType the owner type
3000             * @param plid the plid
3001             * @param portletId the portlet ID
3002             * @return the portlet preferences that was removed
3003             * @throws SystemException if a system exception occurred
3004             */
3005            public PortletPreferences removeByO_O_P_P(long ownerId, int ownerType,
3006                    long plid, String portletId)
3007                    throws NoSuchPortletPreferencesException, SystemException {
3008                    PortletPreferences portletPreferences = findByO_O_P_P(ownerId,
3009                                    ownerType, plid, portletId);
3010    
3011                    return remove(portletPreferences);
3012            }
3013    
3014            /**
3015             * Returns the number of portlet preferenceses where ownerId = &#63; and ownerType = &#63; and plid = &#63; and portletId = &#63;.
3016             *
3017             * @param ownerId the owner ID
3018             * @param ownerType the owner type
3019             * @param plid the plid
3020             * @param portletId the portlet ID
3021             * @return the number of matching portlet preferenceses
3022             * @throws SystemException if a system exception occurred
3023             */
3024            public int countByO_O_P_P(long ownerId, int ownerType, long plid,
3025                    String portletId) throws SystemException {
3026                    FinderPath finderPath = FINDER_PATH_COUNT_BY_O_O_P_P;
3027    
3028                    Object[] finderArgs = new Object[] { ownerId, ownerType, plid, portletId };
3029    
3030                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3031                                    this);
3032    
3033                    if (count == null) {
3034                            StringBundler query = new StringBundler(5);
3035    
3036                            query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
3037    
3038                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
3039    
3040                            query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
3041    
3042                            query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
3043    
3044                            if (portletId == null) {
3045                                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
3046                            }
3047                            else {
3048                                    if (portletId.equals(StringPool.BLANK)) {
3049                                            query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
3050                                    }
3051                                    else {
3052                                            query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
3053                                    }
3054                            }
3055    
3056                            String sql = query.toString();
3057    
3058                            Session session = null;
3059    
3060                            try {
3061                                    session = openSession();
3062    
3063                                    Query q = session.createQuery(sql);
3064    
3065                                    QueryPos qPos = QueryPos.getInstance(q);
3066    
3067                                    qPos.add(ownerId);
3068    
3069                                    qPos.add(ownerType);
3070    
3071                                    qPos.add(plid);
3072    
3073                                    if (portletId != null) {
3074                                            qPos.add(portletId);
3075                                    }
3076    
3077                                    count = (Long)q.uniqueResult();
3078    
3079                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3080                            }
3081                            catch (Exception e) {
3082                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3083    
3084                                    throw processException(e);
3085                            }
3086                            finally {
3087                                    closeSession(session);
3088                            }
3089                    }
3090    
3091                    return count.intValue();
3092            }
3093    
3094            private static final String _FINDER_COLUMN_O_O_P_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
3095            private static final String _FINDER_COLUMN_O_O_P_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
3096            private static final String _FINDER_COLUMN_O_O_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
3097            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
3098            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
3099            private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
3100    
3101            /**
3102             * Caches the portlet preferences in the entity cache if it is enabled.
3103             *
3104             * @param portletPreferences the portlet preferences
3105             */
3106            public void cacheResult(PortletPreferences portletPreferences) {
3107                    EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3108                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
3109                            portletPreferences);
3110    
3111                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
3112                            new Object[] {
3113                                    Long.valueOf(portletPreferences.getOwnerId()),
3114                                    Integer.valueOf(portletPreferences.getOwnerType()),
3115                                    Long.valueOf(portletPreferences.getPlid()),
3116                                    
3117                            portletPreferences.getPortletId()
3118                            }, portletPreferences);
3119    
3120                    portletPreferences.resetOriginalValues();
3121            }
3122    
3123            /**
3124             * Caches the portlet preferenceses in the entity cache if it is enabled.
3125             *
3126             * @param portletPreferenceses the portlet preferenceses
3127             */
3128            public void cacheResult(List<PortletPreferences> portletPreferenceses) {
3129                    for (PortletPreferences portletPreferences : portletPreferenceses) {
3130                            if (EntityCacheUtil.getResult(
3131                                                    PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3132                                                    PortletPreferencesImpl.class,
3133                                                    portletPreferences.getPrimaryKey()) == null) {
3134                                    cacheResult(portletPreferences);
3135                            }
3136                            else {
3137                                    portletPreferences.resetOriginalValues();
3138                            }
3139                    }
3140            }
3141    
3142            /**
3143             * Clears the cache for all portlet preferenceses.
3144             *
3145             * <p>
3146             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
3147             * </p>
3148             */
3149            @Override
3150            public void clearCache() {
3151                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
3152                            CacheRegistryUtil.clear(PortletPreferencesImpl.class.getName());
3153                    }
3154    
3155                    EntityCacheUtil.clearCache(PortletPreferencesImpl.class.getName());
3156    
3157                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
3158                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3159                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3160            }
3161    
3162            /**
3163             * Clears the cache for the portlet preferences.
3164             *
3165             * <p>
3166             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
3167             * </p>
3168             */
3169            @Override
3170            public void clearCache(PortletPreferences portletPreferences) {
3171                    EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3172                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
3173    
3174                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3175                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3176    
3177                    clearUniqueFindersCache(portletPreferences);
3178            }
3179    
3180            @Override
3181            public void clearCache(List<PortletPreferences> portletPreferenceses) {
3182                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3183                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3184    
3185                    for (PortletPreferences portletPreferences : portletPreferenceses) {
3186                            EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3187                                    PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
3188    
3189                            clearUniqueFindersCache(portletPreferences);
3190                    }
3191            }
3192    
3193            protected void cacheUniqueFindersCache(
3194                    PortletPreferences portletPreferences) {
3195                    if (portletPreferences.isNew()) {
3196                            Object[] args = new Object[] {
3197                                            Long.valueOf(portletPreferences.getOwnerId()),
3198                                            Integer.valueOf(portletPreferences.getOwnerType()),
3199                                            Long.valueOf(portletPreferences.getPlid()),
3200                                            
3201                                            portletPreferences.getPortletId()
3202                                    };
3203    
3204                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P_P, args,
3205                                    Long.valueOf(1));
3206                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P, args,
3207                                    portletPreferences);
3208                    }
3209                    else {
3210                            PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
3211    
3212                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3213                                            FINDER_PATH_FETCH_BY_O_O_P_P.getColumnBitmask()) != 0) {
3214                                    Object[] args = new Object[] {
3215                                                    Long.valueOf(portletPreferences.getOwnerId()),
3216                                                    Integer.valueOf(portletPreferences.getOwnerType()),
3217                                                    Long.valueOf(portletPreferences.getPlid()),
3218                                                    
3219                                                    portletPreferences.getPortletId()
3220                                            };
3221    
3222                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P_P, args,
3223                                            Long.valueOf(1));
3224                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P, args,
3225                                            portletPreferences);
3226                            }
3227                    }
3228            }
3229    
3230            protected void clearUniqueFindersCache(
3231                    PortletPreferences portletPreferences) {
3232                    PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
3233    
3234                    Object[] args = new Object[] {
3235                                    Long.valueOf(portletPreferences.getOwnerId()),
3236                                    Integer.valueOf(portletPreferences.getOwnerType()),
3237                                    Long.valueOf(portletPreferences.getPlid()),
3238                                    
3239                                    portletPreferences.getPortletId()
3240                            };
3241    
3242                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P_P, args);
3243                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P, args);
3244    
3245                    if ((portletPreferencesModelImpl.getColumnBitmask() &
3246                                    FINDER_PATH_FETCH_BY_O_O_P_P.getColumnBitmask()) != 0) {
3247                            args = new Object[] {
3248                                            Long.valueOf(portletPreferencesModelImpl.getOriginalOwnerId()),
3249                                            Integer.valueOf(portletPreferencesModelImpl.getOriginalOwnerType()),
3250                                            Long.valueOf(portletPreferencesModelImpl.getOriginalPlid()),
3251                                            
3252                                            portletPreferencesModelImpl.getOriginalPortletId()
3253                                    };
3254    
3255                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P_P, args);
3256                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P, args);
3257                    }
3258            }
3259    
3260            /**
3261             * Creates a new portlet preferences with the primary key. Does not add the portlet preferences to the database.
3262             *
3263             * @param portletPreferencesId the primary key for the new portlet preferences
3264             * @return the new portlet preferences
3265             */
3266            public PortletPreferences create(long portletPreferencesId) {
3267                    PortletPreferences portletPreferences = new PortletPreferencesImpl();
3268    
3269                    portletPreferences.setNew(true);
3270                    portletPreferences.setPrimaryKey(portletPreferencesId);
3271    
3272                    return portletPreferences;
3273            }
3274    
3275            /**
3276             * Removes the portlet preferences with the primary key from the database. Also notifies the appropriate model listeners.
3277             *
3278             * @param portletPreferencesId the primary key of the portlet preferences
3279             * @return the portlet preferences that was removed
3280             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
3281             * @throws SystemException if a system exception occurred
3282             */
3283            public PortletPreferences remove(long portletPreferencesId)
3284                    throws NoSuchPortletPreferencesException, SystemException {
3285                    return remove(Long.valueOf(portletPreferencesId));
3286            }
3287    
3288            /**
3289             * Removes the portlet preferences with the primary key from the database. Also notifies the appropriate model listeners.
3290             *
3291             * @param primaryKey the primary key of the portlet preferences
3292             * @return the portlet preferences that was removed
3293             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
3294             * @throws SystemException if a system exception occurred
3295             */
3296            @Override
3297            public PortletPreferences remove(Serializable primaryKey)
3298                    throws NoSuchPortletPreferencesException, SystemException {
3299                    Session session = null;
3300    
3301                    try {
3302                            session = openSession();
3303    
3304                            PortletPreferences portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
3305                                            primaryKey);
3306    
3307                            if (portletPreferences == null) {
3308                                    if (_log.isWarnEnabled()) {
3309                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
3310                                    }
3311    
3312                                    throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
3313                                            primaryKey);
3314                            }
3315    
3316                            return remove(portletPreferences);
3317                    }
3318                    catch (NoSuchPortletPreferencesException nsee) {
3319                            throw nsee;
3320                    }
3321                    catch (Exception e) {
3322                            throw processException(e);
3323                    }
3324                    finally {
3325                            closeSession(session);
3326                    }
3327            }
3328    
3329            @Override
3330            protected PortletPreferences removeImpl(
3331                    PortletPreferences portletPreferences) throws SystemException {
3332                    portletPreferences = toUnwrappedModel(portletPreferences);
3333    
3334                    Session session = null;
3335    
3336                    try {
3337                            session = openSession();
3338    
3339                            if (!session.contains(portletPreferences)) {
3340                                    portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
3341                                                    portletPreferences.getPrimaryKeyObj());
3342                            }
3343    
3344                            if (portletPreferences != null) {
3345                                    session.delete(portletPreferences);
3346                            }
3347                    }
3348                    catch (Exception e) {
3349                            throw processException(e);
3350                    }
3351                    finally {
3352                            closeSession(session);
3353                    }
3354    
3355                    if (portletPreferences != null) {
3356                            clearCache(portletPreferences);
3357                    }
3358    
3359                    return portletPreferences;
3360            }
3361    
3362            @Override
3363            public PortletPreferences updateImpl(
3364                    com.liferay.portal.model.PortletPreferences portletPreferences)
3365                    throws SystemException {
3366                    portletPreferences = toUnwrappedModel(portletPreferences);
3367    
3368                    boolean isNew = portletPreferences.isNew();
3369    
3370                    PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
3371    
3372                    Session session = null;
3373    
3374                    try {
3375                            session = openSession();
3376    
3377                            if (portletPreferences.isNew()) {
3378                                    session.save(portletPreferences);
3379    
3380                                    portletPreferences.setNew(false);
3381                            }
3382                            else {
3383                                    session.merge(portletPreferences);
3384                            }
3385                    }
3386                    catch (Exception e) {
3387                            throw processException(e);
3388                    }
3389                    finally {
3390                            closeSession(session);
3391                    }
3392    
3393                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3394    
3395                    if (isNew || !PortletPreferencesModelImpl.COLUMN_BITMASK_ENABLED) {
3396                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3397                    }
3398    
3399                    else {
3400                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3401                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID.getColumnBitmask()) != 0) {
3402                                    Object[] args = new Object[] {
3403                                                    Long.valueOf(portletPreferencesModelImpl.getOriginalPlid())
3404                                            };
3405    
3406                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PLID, args);
3407                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID,
3408                                            args);
3409    
3410                                    args = new Object[] {
3411                                                    Long.valueOf(portletPreferencesModelImpl.getPlid())
3412                                            };
3413    
3414                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PLID, args);
3415                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PLID,
3416                                            args);
3417                            }
3418    
3419                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3420                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID.getColumnBitmask()) != 0) {
3421                                    Object[] args = new Object[] {
3422                                                    portletPreferencesModelImpl.getOriginalPortletId()
3423                                            };
3424    
3425                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PORTLETID,
3426                                            args);
3427                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID,
3428                                            args);
3429    
3430                                    args = new Object[] { portletPreferencesModelImpl.getPortletId() };
3431    
3432                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_PORTLETID,
3433                                            args);
3434                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORTLETID,
3435                                            args);
3436                            }
3437    
3438                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3439                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P.getColumnBitmask()) != 0) {
3440                                    Object[] args = new Object[] {
3441                                                    Long.valueOf(portletPreferencesModelImpl.getOriginalPlid()),
3442                                                    
3443                                                    portletPreferencesModelImpl.getOriginalPortletId()
3444                                            };
3445    
3446                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_P_P, args);
3447                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P,
3448                                            args);
3449    
3450                                    args = new Object[] {
3451                                                    Long.valueOf(portletPreferencesModelImpl.getPlid()),
3452                                                    
3453                                                    portletPreferencesModelImpl.getPortletId()
3454                                            };
3455    
3456                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_P_P, args);
3457                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_P_P,
3458                                            args);
3459                            }
3460    
3461                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3462                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P.getColumnBitmask()) != 0) {
3463                                    Object[] args = new Object[] {
3464                                                    Long.valueOf(portletPreferencesModelImpl.getOriginalOwnerId()),
3465                                                    Integer.valueOf(portletPreferencesModelImpl.getOriginalOwnerType()),
3466                                                    Long.valueOf(portletPreferencesModelImpl.getOriginalPlid())
3467                                            };
3468    
3469                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P, args);
3470                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P,
3471                                            args);
3472    
3473                                    args = new Object[] {
3474                                                    Long.valueOf(portletPreferencesModelImpl.getOwnerId()),
3475                                                    Integer.valueOf(portletPreferencesModelImpl.getOwnerType()),
3476                                                    Long.valueOf(portletPreferencesModelImpl.getPlid())
3477                                            };
3478    
3479                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_O_P, args);
3480                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_O_P,
3481                                            args);
3482                            }
3483    
3484                            if ((portletPreferencesModelImpl.getColumnBitmask() &
3485                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P.getColumnBitmask()) != 0) {
3486                                    Object[] args = new Object[] {
3487                                                    Integer.valueOf(portletPreferencesModelImpl.getOriginalOwnerType()),
3488                                                    Long.valueOf(portletPreferencesModelImpl.getOriginalPlid()),
3489                                                    
3490                                                    portletPreferencesModelImpl.getOriginalPortletId()
3491                                            };
3492    
3493                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P_P, args);
3494                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P,
3495                                            args);
3496    
3497                                    args = new Object[] {
3498                                                    Integer.valueOf(portletPreferencesModelImpl.getOwnerType()),
3499                                                    Long.valueOf(portletPreferencesModelImpl.getPlid()),
3500                                                    
3501                                                    portletPreferencesModelImpl.getPortletId()
3502                                            };
3503    
3504                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_O_P_P, args);
3505                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_O_P_P,
3506                                            args);
3507                            }
3508                    }
3509    
3510                    EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3511                            PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
3512                            portletPreferences);
3513    
3514                    clearUniqueFindersCache(portletPreferences);
3515                    cacheUniqueFindersCache(portletPreferences);
3516    
3517                    return portletPreferences;
3518            }
3519    
3520            protected PortletPreferences toUnwrappedModel(
3521                    PortletPreferences portletPreferences) {
3522                    if (portletPreferences instanceof PortletPreferencesImpl) {
3523                            return portletPreferences;
3524                    }
3525    
3526                    PortletPreferencesImpl portletPreferencesImpl = new PortletPreferencesImpl();
3527    
3528                    portletPreferencesImpl.setNew(portletPreferences.isNew());
3529                    portletPreferencesImpl.setPrimaryKey(portletPreferences.getPrimaryKey());
3530    
3531                    portletPreferencesImpl.setPortletPreferencesId(portletPreferences.getPortletPreferencesId());
3532                    portletPreferencesImpl.setOwnerId(portletPreferences.getOwnerId());
3533                    portletPreferencesImpl.setOwnerType(portletPreferences.getOwnerType());
3534                    portletPreferencesImpl.setPlid(portletPreferences.getPlid());
3535                    portletPreferencesImpl.setPortletId(portletPreferences.getPortletId());
3536                    portletPreferencesImpl.setPreferences(portletPreferences.getPreferences());
3537    
3538                    return portletPreferencesImpl;
3539            }
3540    
3541            /**
3542             * Returns the portlet preferences with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
3543             *
3544             * @param primaryKey the primary key of the portlet preferences
3545             * @return the portlet preferences
3546             * @throws com.liferay.portal.NoSuchModelException if a portlet preferences with the primary key could not be found
3547             * @throws SystemException if a system exception occurred
3548             */
3549            @Override
3550            public PortletPreferences findByPrimaryKey(Serializable primaryKey)
3551                    throws NoSuchModelException, SystemException {
3552                    return findByPrimaryKey(((Long)primaryKey).longValue());
3553            }
3554    
3555            /**
3556             * Returns the portlet preferences with the primary key or throws a {@link com.liferay.portal.NoSuchPortletPreferencesException} if it could not be found.
3557             *
3558             * @param portletPreferencesId the primary key of the portlet preferences
3559             * @return the portlet preferences
3560             * @throws com.liferay.portal.NoSuchPortletPreferencesException if a portlet preferences with the primary key could not be found
3561             * @throws SystemException if a system exception occurred
3562             */
3563            public PortletPreferences findByPrimaryKey(long portletPreferencesId)
3564                    throws NoSuchPortletPreferencesException, SystemException {
3565                    PortletPreferences portletPreferences = fetchByPrimaryKey(portletPreferencesId);
3566    
3567                    if (portletPreferences == null) {
3568                            if (_log.isWarnEnabled()) {
3569                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
3570                                            portletPreferencesId);
3571                            }
3572    
3573                            throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
3574                                    portletPreferencesId);
3575                    }
3576    
3577                    return portletPreferences;
3578            }
3579    
3580            /**
3581             * Returns the portlet preferences with the primary key or returns <code>null</code> if it could not be found.
3582             *
3583             * @param primaryKey the primary key of the portlet preferences
3584             * @return the portlet preferences, or <code>null</code> if a portlet preferences with the primary key could not be found
3585             * @throws SystemException if a system exception occurred
3586             */
3587            @Override
3588            public PortletPreferences fetchByPrimaryKey(Serializable primaryKey)
3589                    throws SystemException {
3590                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
3591            }
3592    
3593            /**
3594             * Returns the portlet preferences with the primary key or returns <code>null</code> if it could not be found.
3595             *
3596             * @param portletPreferencesId the primary key of the portlet preferences
3597             * @return the portlet preferences, or <code>null</code> if a portlet preferences with the primary key could not be found
3598             * @throws SystemException if a system exception occurred
3599             */
3600            public PortletPreferences fetchByPrimaryKey(long portletPreferencesId)
3601                    throws SystemException {
3602                    PortletPreferences portletPreferences = (PortletPreferences)EntityCacheUtil.getResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3603                                    PortletPreferencesImpl.class, portletPreferencesId);
3604    
3605                    if (portletPreferences == _nullPortletPreferences) {
3606                            return null;
3607                    }
3608    
3609                    if (portletPreferences == null) {
3610                            Session session = null;
3611    
3612                            try {
3613                                    session = openSession();
3614    
3615                                    portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
3616                                                    Long.valueOf(portletPreferencesId));
3617    
3618                                    if (portletPreferences != null) {
3619                                            cacheResult(portletPreferences);
3620                                    }
3621                                    else {
3622                                            EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3623                                                    PortletPreferencesImpl.class, portletPreferencesId,
3624                                                    _nullPortletPreferences);
3625                                    }
3626                            }
3627                            catch (Exception e) {
3628                                    EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
3629                                            PortletPreferencesImpl.class, portletPreferencesId);
3630    
3631                                    throw processException(e);
3632                            }
3633                            finally {
3634                                    closeSession(session);
3635                            }
3636                    }
3637    
3638                    return portletPreferences;
3639            }
3640    
3641            /**
3642             * Returns all the portlet preferenceses.
3643             *
3644             * @return the portlet preferenceses
3645             * @throws SystemException if a system exception occurred
3646             */
3647            public List<PortletPreferences> findAll() throws SystemException {
3648                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3649            }
3650    
3651            /**
3652             * Returns a range of all the portlet preferenceses.
3653             *
3654             * <p>
3655             * 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.
3656             * </p>
3657             *
3658             * @param start the lower bound of the range of portlet preferenceses
3659             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
3660             * @return the range of portlet preferenceses
3661             * @throws SystemException if a system exception occurred
3662             */
3663            public List<PortletPreferences> findAll(int start, int end)
3664                    throws SystemException {
3665                    return findAll(start, end, null);
3666            }
3667    
3668            /**
3669             * Returns an ordered range of all the portlet preferenceses.
3670             *
3671             * <p>
3672             * 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.
3673             * </p>
3674             *
3675             * @param start the lower bound of the range of portlet preferenceses
3676             * @param end the upper bound of the range of portlet preferenceses (not inclusive)
3677             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3678             * @return the ordered range of portlet preferenceses
3679             * @throws SystemException if a system exception occurred
3680             */
3681            public List<PortletPreferences> findAll(int start, int end,
3682                    OrderByComparator orderByComparator) throws SystemException {
3683                    boolean pagination = true;
3684                    FinderPath finderPath = null;
3685                    Object[] finderArgs = null;
3686    
3687                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3688                                    (orderByComparator == null)) {
3689                            pagination = false;
3690                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
3691                            finderArgs = FINDER_ARGS_EMPTY;
3692                    }
3693                    else {
3694                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
3695                            finderArgs = new Object[] { start, end, orderByComparator };
3696                    }
3697    
3698                    List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(finderPath,
3699                                    finderArgs, this);
3700    
3701                    if (list == null) {
3702                            StringBundler query = null;
3703                            String sql = null;
3704    
3705                            if (orderByComparator != null) {
3706                                    query = new StringBundler(2 +
3707                                                    (orderByComparator.getOrderByFields().length * 3));
3708    
3709                                    query.append(_SQL_SELECT_PORTLETPREFERENCES);
3710    
3711                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3712                                            orderByComparator);
3713    
3714                                    sql = query.toString();
3715                            }
3716                            else {
3717                                    sql = _SQL_SELECT_PORTLETPREFERENCES;
3718    
3719                                    if (pagination) {
3720                                            sql = sql.concat(PortletPreferencesModelImpl.ORDER_BY_JPQL);
3721                                    }
3722                            }
3723    
3724                            Session session = null;
3725    
3726                            try {
3727                                    session = openSession();
3728    
3729                                    Query q = session.createQuery(sql);
3730    
3731                                    if (!pagination) {
3732                                            list = (List<PortletPreferences>)QueryUtil.list(q,
3733                                                            getDialect(), start, end, false);
3734    
3735                                            Collections.sort(list);
3736    
3737                                            list = new UnmodifiableList<PortletPreferences>(list);
3738                                    }
3739                                    else {
3740                                            list = (List<PortletPreferences>)QueryUtil.list(q,
3741                                                            getDialect(), start, end);
3742                                    }
3743    
3744                                    cacheResult(list);
3745    
3746                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3747                            }
3748                            catch (Exception e) {
3749                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3750    
3751                                    throw processException(e);
3752                            }
3753                            finally {
3754                                    closeSession(session);
3755                            }
3756                    }
3757    
3758                    return list;
3759            }
3760    
3761            /**
3762             * Removes all the portlet preferenceses from the database.
3763             *
3764             * @throws SystemException if a system exception occurred
3765             */
3766            public void removeAll() throws SystemException {
3767                    for (PortletPreferences portletPreferences : findAll()) {
3768                            remove(portletPreferences);
3769                    }
3770            }
3771    
3772            /**
3773             * Returns the number of portlet preferenceses.
3774             *
3775             * @return the number of portlet preferenceses
3776             * @throws SystemException if a system exception occurred
3777             */
3778            public int countAll() throws SystemException {
3779                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
3780                                    FINDER_ARGS_EMPTY, this);
3781    
3782                    if (count == null) {
3783                            Session session = null;
3784    
3785                            try {
3786                                    session = openSession();
3787    
3788                                    Query q = session.createQuery(_SQL_COUNT_PORTLETPREFERENCES);
3789    
3790                                    count = (Long)q.uniqueResult();
3791    
3792                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
3793                                            FINDER_ARGS_EMPTY, count);
3794                            }
3795                            catch (Exception e) {
3796                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
3797                                            FINDER_ARGS_EMPTY);
3798    
3799                                    throw processException(e);
3800                            }
3801                            finally {
3802                                    closeSession(session);
3803                            }
3804                    }
3805    
3806                    return count.intValue();
3807            }
3808    
3809            /**
3810             * Initializes the portlet preferences persistence.
3811             */
3812            public void afterPropertiesSet() {
3813                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
3814                                            com.liferay.portal.util.PropsUtil.get(
3815                                                    "value.object.listener.com.liferay.portal.model.PortletPreferences")));
3816    
3817                    if (listenerClassNames.length > 0) {
3818                            try {
3819                                    List<ModelListener<PortletPreferences>> listenersList = new ArrayList<ModelListener<PortletPreferences>>();
3820    
3821                                    for (String listenerClassName : listenerClassNames) {
3822                                            listenersList.add((ModelListener<PortletPreferences>)InstanceFactory.newInstance(
3823                                                            listenerClassName));
3824                                    }
3825    
3826                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
3827                            }
3828                            catch (Exception e) {
3829                                    _log.error(e);
3830                            }
3831                    }
3832            }
3833    
3834            public void destroy() {
3835                    EntityCacheUtil.removeCache(PortletPreferencesImpl.class.getName());
3836                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
3837                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3838                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3839            }
3840    
3841            private static final String _SQL_SELECT_PORTLETPREFERENCES = "SELECT portletPreferences FROM PortletPreferences portletPreferences";
3842            private static final String _SQL_SELECT_PORTLETPREFERENCES_WHERE = "SELECT portletPreferences FROM PortletPreferences portletPreferences WHERE ";
3843            private static final String _SQL_COUNT_PORTLETPREFERENCES = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences";
3844            private static final String _SQL_COUNT_PORTLETPREFERENCES_WHERE = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences WHERE ";
3845            private static final String _ORDER_BY_ENTITY_ALIAS = "portletPreferences.";
3846            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PortletPreferences exists with the primary key ";
3847            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PortletPreferences exists with the key {";
3848            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
3849            private static Log _log = LogFactoryUtil.getLog(PortletPreferencesPersistenceImpl.class);
3850            private static PortletPreferences _nullPortletPreferences = new PortletPreferencesImpl() {
3851                            @Override
3852                            public Object clone() {
3853                                    return this;
3854                            }
3855    
3856                            @Override
3857                            public CacheModel<PortletPreferences> toCacheModel() {
3858                                    return _nullPortletPreferencesCacheModel;
3859                            }
3860                    };
3861    
3862            private static CacheModel<PortletPreferences> _nullPortletPreferencesCacheModel =
3863                    new CacheModel<PortletPreferences>() {
3864                            public PortletPreferences toEntityModel() {
3865                                    return _nullPortletPreferences;
3866                            }
3867                    };
3868    }