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