1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.NoSuchPortletPreferencesException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.exception.SystemException;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.InstanceFactory;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringBundler;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.model.PortletPreferences;
40  import com.liferay.portal.model.impl.PortletPreferencesImpl;
41  import com.liferay.portal.model.impl.PortletPreferencesModelImpl;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import java.io.Serializable;
45  
46  import java.util.ArrayList;
47  import java.util.Collections;
48  import java.util.List;
49  
50  /**
51   * <a href="PortletPreferencesPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * <p>
54   * ServiceBuilder generated this class. Modifications in this class will be
55   * overwritten the next time is generated.
56   * </p>
57   *
58   * @author    Brian Wing Shun Chan
59   * @see       PortletPreferencesPersistence
60   * @see       PortletPreferencesUtil
61   * @generated
62   */
63  public class PortletPreferencesPersistenceImpl extends BasePersistenceImpl<PortletPreferences>
64      implements PortletPreferencesPersistence {
65      public static final String FINDER_CLASS_NAME_ENTITY = PortletPreferencesImpl.class.getName();
66      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
67          ".List";
68      public static final FinderPath FINDER_PATH_FIND_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
69              PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
70              FINDER_CLASS_NAME_LIST, "findByPlid",
71              new String[] {
72                  Long.class.getName(),
73                  
74              "java.lang.Integer", "java.lang.Integer",
75                  "com.liferay.portal.kernel.util.OrderByComparator"
76              });
77      public static final FinderPath FINDER_PATH_COUNT_BY_PLID = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
78              PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
79              FINDER_CLASS_NAME_LIST, "countByPlid",
80              new String[] { Long.class.getName() });
81      public static final FinderPath FINDER_PATH_FIND_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
82              PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
83              FINDER_CLASS_NAME_LIST, "findByP_P",
84              new String[] {
85                  Long.class.getName(), String.class.getName(),
86                  
87              "java.lang.Integer", "java.lang.Integer",
88                  "com.liferay.portal.kernel.util.OrderByComparator"
89              });
90      public static final FinderPath FINDER_PATH_COUNT_BY_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
91              PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_LIST, "countByP_P",
93              new String[] { Long.class.getName(), String.class.getName() });
94      public static final FinderPath FINDER_PATH_FIND_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
95              PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
96              FINDER_CLASS_NAME_LIST, "findByO_O_P",
97              new String[] {
98                  Long.class.getName(), Integer.class.getName(),
99                  Long.class.getName(),
100                 
101             "java.lang.Integer", "java.lang.Integer",
102                 "com.liferay.portal.kernel.util.OrderByComparator"
103             });
104     public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
105             PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
106             FINDER_CLASS_NAME_LIST, "countByO_O_P",
107             new String[] {
108                 Long.class.getName(), Integer.class.getName(),
109                 Long.class.getName()
110             });
111     public static final FinderPath FINDER_PATH_FETCH_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
112             PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
113             FINDER_CLASS_NAME_ENTITY, "fetchByO_O_P_P",
114             new String[] {
115                 Long.class.getName(), Integer.class.getName(),
116                 Long.class.getName(), String.class.getName()
117             });
118     public static final FinderPath FINDER_PATH_COUNT_BY_O_O_P_P = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
119             PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
120             FINDER_CLASS_NAME_LIST, "countByO_O_P_P",
121             new String[] {
122                 Long.class.getName(), Integer.class.getName(),
123                 Long.class.getName(), String.class.getName()
124             });
125     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
126             PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
127             FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
128     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
129             PortletPreferencesModelImpl.FINDER_CACHE_ENABLED,
130             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
131 
132     public void cacheResult(PortletPreferences portletPreferences) {
133         EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
134             PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
135             portletPreferences);
136 
137         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
138             new Object[] {
139                 new Long(portletPreferences.getOwnerId()),
140                 new Integer(portletPreferences.getOwnerType()),
141                 new Long(portletPreferences.getPlid()),
142                 
143             portletPreferences.getPortletId()
144             }, portletPreferences);
145     }
146 
147     public void cacheResult(List<PortletPreferences> portletPreferenceses) {
148         for (PortletPreferences portletPreferences : portletPreferenceses) {
149             if (EntityCacheUtil.getResult(
150                         PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
151                         PortletPreferencesImpl.class,
152                         portletPreferences.getPrimaryKey(), this) == null) {
153                 cacheResult(portletPreferences);
154             }
155         }
156     }
157 
158     public void clearCache() {
159         CacheRegistry.clear(PortletPreferencesImpl.class.getName());
160         EntityCacheUtil.clearCache(PortletPreferencesImpl.class.getName());
161         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
162         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
163     }
164 
165     public void clearCache(PortletPreferences portletPreferences) {
166         EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
167             PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
168 
169         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P,
170             new Object[] {
171                 new Long(portletPreferences.getOwnerId()),
172                 new Integer(portletPreferences.getOwnerType()),
173                 new Long(portletPreferences.getPlid()),
174                 
175             portletPreferences.getPortletId()
176             });
177     }
178 
179     public PortletPreferences create(long portletPreferencesId) {
180         PortletPreferences portletPreferences = new PortletPreferencesImpl();
181 
182         portletPreferences.setNew(true);
183         portletPreferences.setPrimaryKey(portletPreferencesId);
184 
185         return portletPreferences;
186     }
187 
188     public PortletPreferences remove(Serializable primaryKey)
189         throws NoSuchModelException, SystemException {
190         return remove(((Long)primaryKey).longValue());
191     }
192 
193     public PortletPreferences remove(long portletPreferencesId)
194         throws NoSuchPortletPreferencesException, SystemException {
195         Session session = null;
196 
197         try {
198             session = openSession();
199 
200             PortletPreferences portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
201                     new Long(portletPreferencesId));
202 
203             if (portletPreferences == null) {
204                 if (_log.isWarnEnabled()) {
205                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
206                         portletPreferencesId);
207                 }
208 
209                 throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
210                     portletPreferencesId);
211             }
212 
213             return remove(portletPreferences);
214         }
215         catch (NoSuchPortletPreferencesException nsee) {
216             throw nsee;
217         }
218         catch (Exception e) {
219             throw processException(e);
220         }
221         finally {
222             closeSession(session);
223         }
224     }
225 
226     public PortletPreferences remove(PortletPreferences portletPreferences)
227         throws SystemException {
228         for (ModelListener<PortletPreferences> listener : listeners) {
229             listener.onBeforeRemove(portletPreferences);
230         }
231 
232         portletPreferences = removeImpl(portletPreferences);
233 
234         for (ModelListener<PortletPreferences> listener : listeners) {
235             listener.onAfterRemove(portletPreferences);
236         }
237 
238         return portletPreferences;
239     }
240 
241     protected PortletPreferences removeImpl(
242         PortletPreferences portletPreferences) throws SystemException {
243         portletPreferences = toUnwrappedModel(portletPreferences);
244 
245         Session session = null;
246 
247         try {
248             session = openSession();
249 
250             if (portletPreferences.isCachedModel() ||
251                     BatchSessionUtil.isEnabled()) {
252                 Object staleObject = session.get(PortletPreferencesImpl.class,
253                         portletPreferences.getPrimaryKeyObj());
254 
255                 if (staleObject != null) {
256                     session.evict(staleObject);
257                 }
258             }
259 
260             session.delete(portletPreferences);
261 
262             session.flush();
263         }
264         catch (Exception e) {
265             throw processException(e);
266         }
267         finally {
268             closeSession(session);
269         }
270 
271         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
272 
273         PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
274 
275         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P,
276             new Object[] {
277                 new Long(portletPreferencesModelImpl.getOriginalOwnerId()),
278                 new Integer(portletPreferencesModelImpl.getOriginalOwnerType()),
279                 new Long(portletPreferencesModelImpl.getOriginalPlid()),
280                 
281             portletPreferencesModelImpl.getOriginalPortletId()
282             });
283 
284         EntityCacheUtil.removeResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
285             PortletPreferencesImpl.class, portletPreferences.getPrimaryKey());
286 
287         return portletPreferences;
288     }
289 
290     public PortletPreferences updateImpl(
291         com.liferay.portal.model.PortletPreferences portletPreferences,
292         boolean merge) throws SystemException {
293         portletPreferences = toUnwrappedModel(portletPreferences);
294 
295         boolean isNew = portletPreferences.isNew();
296 
297         PortletPreferencesModelImpl portletPreferencesModelImpl = (PortletPreferencesModelImpl)portletPreferences;
298 
299         Session session = null;
300 
301         try {
302             session = openSession();
303 
304             BatchSessionUtil.update(session, portletPreferences, merge);
305 
306             portletPreferences.setNew(false);
307         }
308         catch (Exception e) {
309             throw processException(e);
310         }
311         finally {
312             closeSession(session);
313         }
314 
315         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
316 
317         EntityCacheUtil.putResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
318             PortletPreferencesImpl.class, portletPreferences.getPrimaryKey(),
319             portletPreferences);
320 
321         if (!isNew &&
322                 ((portletPreferences.getOwnerId() != portletPreferencesModelImpl.getOriginalOwnerId()) ||
323                 (portletPreferences.getOwnerType() != portletPreferencesModelImpl.getOriginalOwnerType()) ||
324                 (portletPreferences.getPlid() != portletPreferencesModelImpl.getOriginalPlid()) ||
325                 !Validator.equals(portletPreferences.getPortletId(),
326                     portletPreferencesModelImpl.getOriginalPortletId()))) {
327             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_O_O_P_P,
328                 new Object[] {
329                     new Long(portletPreferencesModelImpl.getOriginalOwnerId()),
330                     new Integer(portletPreferencesModelImpl.getOriginalOwnerType()),
331                     new Long(portletPreferencesModelImpl.getOriginalPlid()),
332                     
333                 portletPreferencesModelImpl.getOriginalPortletId()
334                 });
335         }
336 
337         if (isNew ||
338                 ((portletPreferences.getOwnerId() != portletPreferencesModelImpl.getOriginalOwnerId()) ||
339                 (portletPreferences.getOwnerType() != portletPreferencesModelImpl.getOriginalOwnerType()) ||
340                 (portletPreferences.getPlid() != portletPreferencesModelImpl.getOriginalPlid()) ||
341                 !Validator.equals(portletPreferences.getPortletId(),
342                     portletPreferencesModelImpl.getOriginalPortletId()))) {
343             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
344                 new Object[] {
345                     new Long(portletPreferences.getOwnerId()),
346                     new Integer(portletPreferences.getOwnerType()),
347                     new Long(portletPreferences.getPlid()),
348                     
349                 portletPreferences.getPortletId()
350                 }, portletPreferences);
351         }
352 
353         return portletPreferences;
354     }
355 
356     protected PortletPreferences toUnwrappedModel(
357         PortletPreferences portletPreferences) {
358         if (portletPreferences instanceof PortletPreferencesImpl) {
359             return portletPreferences;
360         }
361 
362         PortletPreferencesImpl portletPreferencesImpl = new PortletPreferencesImpl();
363 
364         portletPreferencesImpl.setNew(portletPreferences.isNew());
365         portletPreferencesImpl.setPrimaryKey(portletPreferences.getPrimaryKey());
366 
367         portletPreferencesImpl.setPortletPreferencesId(portletPreferences.getPortletPreferencesId());
368         portletPreferencesImpl.setOwnerId(portletPreferences.getOwnerId());
369         portletPreferencesImpl.setOwnerType(portletPreferences.getOwnerType());
370         portletPreferencesImpl.setPlid(portletPreferences.getPlid());
371         portletPreferencesImpl.setPortletId(portletPreferences.getPortletId());
372         portletPreferencesImpl.setPreferences(portletPreferences.getPreferences());
373 
374         return portletPreferencesImpl;
375     }
376 
377     public PortletPreferences findByPrimaryKey(Serializable primaryKey)
378         throws NoSuchModelException, SystemException {
379         return findByPrimaryKey(((Long)primaryKey).longValue());
380     }
381 
382     public PortletPreferences findByPrimaryKey(long portletPreferencesId)
383         throws NoSuchPortletPreferencesException, SystemException {
384         PortletPreferences portletPreferences = fetchByPrimaryKey(portletPreferencesId);
385 
386         if (portletPreferences == null) {
387             if (_log.isWarnEnabled()) {
388                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
389                     portletPreferencesId);
390             }
391 
392             throw new NoSuchPortletPreferencesException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
393                 portletPreferencesId);
394         }
395 
396         return portletPreferences;
397     }
398 
399     public PortletPreferences fetchByPrimaryKey(Serializable primaryKey)
400         throws SystemException {
401         return fetchByPrimaryKey(((Long)primaryKey).longValue());
402     }
403 
404     public PortletPreferences fetchByPrimaryKey(long portletPreferencesId)
405         throws SystemException {
406         PortletPreferences portletPreferences = (PortletPreferences)EntityCacheUtil.getResult(PortletPreferencesModelImpl.ENTITY_CACHE_ENABLED,
407                 PortletPreferencesImpl.class, portletPreferencesId, this);
408 
409         if (portletPreferences == null) {
410             Session session = null;
411 
412             try {
413                 session = openSession();
414 
415                 portletPreferences = (PortletPreferences)session.get(PortletPreferencesImpl.class,
416                         new Long(portletPreferencesId));
417             }
418             catch (Exception e) {
419                 throw processException(e);
420             }
421             finally {
422                 if (portletPreferences != null) {
423                     cacheResult(portletPreferences);
424                 }
425 
426                 closeSession(session);
427             }
428         }
429 
430         return portletPreferences;
431     }
432 
433     public List<PortletPreferences> findByPlid(long plid)
434         throws SystemException {
435         return findByPlid(plid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
436     }
437 
438     public List<PortletPreferences> findByPlid(long plid, int start, int end)
439         throws SystemException {
440         return findByPlid(plid, start, end, null);
441     }
442 
443     public List<PortletPreferences> findByPlid(long plid, int start, int end,
444         OrderByComparator orderByComparator) throws SystemException {
445         Object[] finderArgs = new Object[] {
446                 new Long(plid),
447                 
448                 String.valueOf(start), String.valueOf(end),
449                 String.valueOf(orderByComparator)
450             };
451 
452         List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_PLID,
453                 finderArgs, this);
454 
455         if (list == null) {
456             Session session = null;
457 
458             try {
459                 session = openSession();
460 
461                 StringBundler query = null;
462 
463                 if (orderByComparator != null) {
464                     query = new StringBundler(3 +
465                             (orderByComparator.getOrderByFields().length * 3));
466                 }
467                 else {
468                     query = new StringBundler(2);
469                 }
470 
471                 query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
472 
473                 query.append(_FINDER_COLUMN_PLID_PLID_2);
474 
475                 if (orderByComparator != null) {
476                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
477                         orderByComparator);
478                 }
479 
480                 String sql = query.toString();
481 
482                 Query q = session.createQuery(sql);
483 
484                 QueryPos qPos = QueryPos.getInstance(q);
485 
486                 qPos.add(plid);
487 
488                 list = (List<PortletPreferences>)QueryUtil.list(q,
489                         getDialect(), start, end);
490             }
491             catch (Exception e) {
492                 throw processException(e);
493             }
494             finally {
495                 if (list == null) {
496                     list = new ArrayList<PortletPreferences>();
497                 }
498 
499                 cacheResult(list);
500 
501                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_PLID, finderArgs,
502                     list);
503 
504                 closeSession(session);
505             }
506         }
507 
508         return list;
509     }
510 
511     public PortletPreferences findByPlid_First(long plid,
512         OrderByComparator orderByComparator)
513         throws NoSuchPortletPreferencesException, SystemException {
514         List<PortletPreferences> list = findByPlid(plid, 0, 1, orderByComparator);
515 
516         if (list.isEmpty()) {
517             StringBundler msg = new StringBundler(4);
518 
519             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
520 
521             msg.append("plid=");
522             msg.append(plid);
523 
524             msg.append(StringPool.CLOSE_CURLY_BRACE);
525 
526             throw new NoSuchPortletPreferencesException(msg.toString());
527         }
528         else {
529             return list.get(0);
530         }
531     }
532 
533     public PortletPreferences findByPlid_Last(long plid,
534         OrderByComparator orderByComparator)
535         throws NoSuchPortletPreferencesException, SystemException {
536         int count = countByPlid(plid);
537 
538         List<PortletPreferences> list = findByPlid(plid, count - 1, count,
539                 orderByComparator);
540 
541         if (list.isEmpty()) {
542             StringBundler msg = new StringBundler(4);
543 
544             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
545 
546             msg.append("plid=");
547             msg.append(plid);
548 
549             msg.append(StringPool.CLOSE_CURLY_BRACE);
550 
551             throw new NoSuchPortletPreferencesException(msg.toString());
552         }
553         else {
554             return list.get(0);
555         }
556     }
557 
558     public PortletPreferences[] findByPlid_PrevAndNext(
559         long portletPreferencesId, long plid,
560         OrderByComparator orderByComparator)
561         throws NoSuchPortletPreferencesException, SystemException {
562         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
563 
564         Session session = null;
565 
566         try {
567             session = openSession();
568 
569             PortletPreferences[] array = new PortletPreferencesImpl[3];
570 
571             array[0] = getByPlid_PrevAndNext(session, portletPreferences, plid,
572                     orderByComparator, true);
573 
574             array[1] = portletPreferences;
575 
576             array[2] = getByPlid_PrevAndNext(session, portletPreferences, plid,
577                     orderByComparator, false);
578 
579             return array;
580         }
581         catch (Exception e) {
582             throw processException(e);
583         }
584         finally {
585             closeSession(session);
586         }
587     }
588 
589     protected PortletPreferences getByPlid_PrevAndNext(Session session,
590         PortletPreferences portletPreferences, long plid,
591         OrderByComparator orderByComparator, boolean previous) {
592         StringBundler query = null;
593 
594         if (orderByComparator != null) {
595             query = new StringBundler(6 +
596                     (orderByComparator.getOrderByFields().length * 6));
597         }
598         else {
599             query = new StringBundler(3);
600         }
601 
602         query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
603 
604         query.append(_FINDER_COLUMN_PLID_PLID_2);
605 
606         if (orderByComparator != null) {
607             String[] orderByFields = orderByComparator.getOrderByFields();
608 
609             if (orderByFields.length > 0) {
610                 query.append(WHERE_AND);
611             }
612 
613             for (int i = 0; i < orderByFields.length; i++) {
614                 query.append(_ORDER_BY_ENTITY_ALIAS);
615                 query.append(orderByFields[i]);
616 
617                 if ((i + 1) < orderByFields.length) {
618                     if (orderByComparator.isAscending() ^ previous) {
619                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
620                     }
621                     else {
622                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
623                     }
624                 }
625                 else {
626                     if (orderByComparator.isAscending() ^ previous) {
627                         query.append(WHERE_GREATER_THAN);
628                     }
629                     else {
630                         query.append(WHERE_LESSER_THAN);
631                     }
632                 }
633             }
634 
635             query.append(ORDER_BY_CLAUSE);
636 
637             for (int i = 0; i < orderByFields.length; i++) {
638                 query.append(_ORDER_BY_ENTITY_ALIAS);
639                 query.append(orderByFields[i]);
640 
641                 if ((i + 1) < orderByFields.length) {
642                     if (orderByComparator.isAscending() ^ previous) {
643                         query.append(ORDER_BY_ASC_HAS_NEXT);
644                     }
645                     else {
646                         query.append(ORDER_BY_DESC_HAS_NEXT);
647                     }
648                 }
649                 else {
650                     if (orderByComparator.isAscending() ^ previous) {
651                         query.append(ORDER_BY_ASC);
652                     }
653                     else {
654                         query.append(ORDER_BY_DESC);
655                     }
656                 }
657             }
658         }
659 
660         String sql = query.toString();
661 
662         Query q = session.createQuery(sql);
663 
664         q.setFirstResult(0);
665         q.setMaxResults(2);
666 
667         QueryPos qPos = QueryPos.getInstance(q);
668 
669         qPos.add(plid);
670 
671         if (orderByComparator != null) {
672             Object[] values = orderByComparator.getOrderByValues(portletPreferences);
673 
674             for (Object value : values) {
675                 qPos.add(value);
676             }
677         }
678 
679         List<PortletPreferences> list = q.list();
680 
681         if (list.size() == 2) {
682             return list.get(1);
683         }
684         else {
685             return null;
686         }
687     }
688 
689     public List<PortletPreferences> findByP_P(long plid, String portletId)
690         throws SystemException {
691         return findByP_P(plid, portletId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
692             null);
693     }
694 
695     public List<PortletPreferences> findByP_P(long plid, String portletId,
696         int start, int end) throws SystemException {
697         return findByP_P(plid, portletId, start, end, null);
698     }
699 
700     public List<PortletPreferences> findByP_P(long plid, String portletId,
701         int start, int end, OrderByComparator orderByComparator)
702         throws SystemException {
703         Object[] finderArgs = new Object[] {
704                 new Long(plid),
705                 
706                 portletId,
707                 
708                 String.valueOf(start), String.valueOf(end),
709                 String.valueOf(orderByComparator)
710             };
711 
712         List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_P_P,
713                 finderArgs, this);
714 
715         if (list == null) {
716             Session session = null;
717 
718             try {
719                 session = openSession();
720 
721                 StringBundler query = null;
722 
723                 if (orderByComparator != null) {
724                     query = new StringBundler(4 +
725                             (orderByComparator.getOrderByFields().length * 3));
726                 }
727                 else {
728                     query = new StringBundler(3);
729                 }
730 
731                 query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
732 
733                 query.append(_FINDER_COLUMN_P_P_PLID_2);
734 
735                 if (portletId == null) {
736                     query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
737                 }
738                 else {
739                     if (portletId.equals(StringPool.BLANK)) {
740                         query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
741                     }
742                     else {
743                         query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
744                     }
745                 }
746 
747                 if (orderByComparator != null) {
748                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
749                         orderByComparator);
750                 }
751 
752                 String sql = query.toString();
753 
754                 Query q = session.createQuery(sql);
755 
756                 QueryPos qPos = QueryPos.getInstance(q);
757 
758                 qPos.add(plid);
759 
760                 if (portletId != null) {
761                     qPos.add(portletId);
762                 }
763 
764                 list = (List<PortletPreferences>)QueryUtil.list(q,
765                         getDialect(), start, end);
766             }
767             catch (Exception e) {
768                 throw processException(e);
769             }
770             finally {
771                 if (list == null) {
772                     list = new ArrayList<PortletPreferences>();
773                 }
774 
775                 cacheResult(list);
776 
777                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_P_P, finderArgs,
778                     list);
779 
780                 closeSession(session);
781             }
782         }
783 
784         return list;
785     }
786 
787     public PortletPreferences findByP_P_First(long plid, String portletId,
788         OrderByComparator orderByComparator)
789         throws NoSuchPortletPreferencesException, SystemException {
790         List<PortletPreferences> list = findByP_P(plid, portletId, 0, 1,
791                 orderByComparator);
792 
793         if (list.isEmpty()) {
794             StringBundler msg = new StringBundler(6);
795 
796             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
797 
798             msg.append("plid=");
799             msg.append(plid);
800 
801             msg.append(", portletId=");
802             msg.append(portletId);
803 
804             msg.append(StringPool.CLOSE_CURLY_BRACE);
805 
806             throw new NoSuchPortletPreferencesException(msg.toString());
807         }
808         else {
809             return list.get(0);
810         }
811     }
812 
813     public PortletPreferences findByP_P_Last(long plid, String portletId,
814         OrderByComparator orderByComparator)
815         throws NoSuchPortletPreferencesException, SystemException {
816         int count = countByP_P(plid, portletId);
817 
818         List<PortletPreferences> list = findByP_P(plid, portletId, count - 1,
819                 count, orderByComparator);
820 
821         if (list.isEmpty()) {
822             StringBundler msg = new StringBundler(6);
823 
824             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
825 
826             msg.append("plid=");
827             msg.append(plid);
828 
829             msg.append(", portletId=");
830             msg.append(portletId);
831 
832             msg.append(StringPool.CLOSE_CURLY_BRACE);
833 
834             throw new NoSuchPortletPreferencesException(msg.toString());
835         }
836         else {
837             return list.get(0);
838         }
839     }
840 
841     public PortletPreferences[] findByP_P_PrevAndNext(
842         long portletPreferencesId, long plid, String portletId,
843         OrderByComparator orderByComparator)
844         throws NoSuchPortletPreferencesException, SystemException {
845         PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
846 
847         Session session = null;
848 
849         try {
850             session = openSession();
851 
852             PortletPreferences[] array = new PortletPreferencesImpl[3];
853 
854             array[0] = getByP_P_PrevAndNext(session, portletPreferences, plid,
855                     portletId, orderByComparator, true);
856 
857             array[1] = portletPreferences;
858 
859             array[2] = getByP_P_PrevAndNext(session, portletPreferences, plid,
860                     portletId, orderByComparator, false);
861 
862             return array;
863         }
864         catch (Exception e) {
865             throw processException(e);
866         }
867         finally {
868             closeSession(session);
869         }
870     }
871 
872     protected PortletPreferences getByP_P_PrevAndNext(Session session,
873         PortletPreferences portletPreferences, long plid, String portletId,
874         OrderByComparator orderByComparator, boolean previous) {
875         StringBundler query = null;
876 
877         if (orderByComparator != null) {
878             query = new StringBundler(6 +
879                     (orderByComparator.getOrderByFields().length * 6));
880         }
881         else {
882             query = new StringBundler(3);
883         }
884 
885         query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
886 
887         query.append(_FINDER_COLUMN_P_P_PLID_2);
888 
889         if (portletId == null) {
890             query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
891         }
892         else {
893             if (portletId.equals(StringPool.BLANK)) {
894                 query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
895             }
896             else {
897                 query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
898             }
899         }
900 
901         if (orderByComparator != null) {
902             String[] orderByFields = orderByComparator.getOrderByFields();
903 
904             if (orderByFields.length > 0) {
905                 query.append(WHERE_AND);
906             }
907 
908             for (int i = 0; i < orderByFields.length; i++) {
909                 query.append(_ORDER_BY_ENTITY_ALIAS);
910                 query.append(orderByFields[i]);
911 
912                 if ((i + 1) < orderByFields.length) {
913                     if (orderByComparator.isAscending() ^ previous) {
914                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
915                     }
916                     else {
917                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
918                     }
919                 }
920                 else {
921                     if (orderByComparator.isAscending() ^ previous) {
922                         query.append(WHERE_GREATER_THAN);
923                     }
924                     else {
925                         query.append(WHERE_LESSER_THAN);
926                     }
927                 }
928             }
929 
930             query.append(ORDER_BY_CLAUSE);
931 
932             for (int i = 0; i < orderByFields.length; i++) {
933                 query.append(_ORDER_BY_ENTITY_ALIAS);
934                 query.append(orderByFields[i]);
935 
936                 if ((i + 1) < orderByFields.length) {
937                     if (orderByComparator.isAscending() ^ previous) {
938                         query.append(ORDER_BY_ASC_HAS_NEXT);
939                     }
940                     else {
941                         query.append(ORDER_BY_DESC_HAS_NEXT);
942                     }
943                 }
944                 else {
945                     if (orderByComparator.isAscending() ^ previous) {
946                         query.append(ORDER_BY_ASC);
947                     }
948                     else {
949                         query.append(ORDER_BY_DESC);
950                     }
951                 }
952             }
953         }
954 
955         String sql = query.toString();
956 
957         Query q = session.createQuery(sql);
958 
959         q.setFirstResult(0);
960         q.setMaxResults(2);
961 
962         QueryPos qPos = QueryPos.getInstance(q);
963 
964         qPos.add(plid);
965 
966         if (portletId != null) {
967             qPos.add(portletId);
968         }
969 
970         if (orderByComparator != null) {
971             Object[] values = orderByComparator.getOrderByValues(portletPreferences);
972 
973             for (Object value : values) {
974                 qPos.add(value);
975             }
976         }
977 
978         List<PortletPreferences> list = q.list();
979 
980         if (list.size() == 2) {
981             return list.get(1);
982         }
983         else {
984             return null;
985         }
986     }
987 
988     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
989         long plid) throws SystemException {
990         return findByO_O_P(ownerId, ownerType, plid, QueryUtil.ALL_POS,
991             QueryUtil.ALL_POS, null);
992     }
993 
994     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
995         long plid, int start, int end) throws SystemException {
996         return findByO_O_P(ownerId, ownerType, plid, start, end, null);
997     }
998 
999     public List<PortletPreferences> findByO_O_P(long ownerId, int ownerType,
1000        long plid, int start, int end, OrderByComparator orderByComparator)
1001        throws SystemException {
1002        Object[] finderArgs = new Object[] {
1003                new Long(ownerId), new Integer(ownerType), new Long(plid),
1004                
1005                String.valueOf(start), String.valueOf(end),
1006                String.valueOf(orderByComparator)
1007            };
1008
1009        List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_O_O_P,
1010                finderArgs, this);
1011
1012        if (list == null) {
1013            Session session = null;
1014
1015            try {
1016                session = openSession();
1017
1018                StringBundler query = null;
1019
1020                if (orderByComparator != null) {
1021                    query = new StringBundler(5 +
1022                            (orderByComparator.getOrderByFields().length * 3));
1023                }
1024                else {
1025                    query = new StringBundler(4);
1026                }
1027
1028                query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1029
1030                query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
1031
1032                query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
1033
1034                query.append(_FINDER_COLUMN_O_O_P_PLID_2);
1035
1036                if (orderByComparator != null) {
1037                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1038                        orderByComparator);
1039                }
1040
1041                String sql = query.toString();
1042
1043                Query q = session.createQuery(sql);
1044
1045                QueryPos qPos = QueryPos.getInstance(q);
1046
1047                qPos.add(ownerId);
1048
1049                qPos.add(ownerType);
1050
1051                qPos.add(plid);
1052
1053                list = (List<PortletPreferences>)QueryUtil.list(q,
1054                        getDialect(), start, end);
1055            }
1056            catch (Exception e) {
1057                throw processException(e);
1058            }
1059            finally {
1060                if (list == null) {
1061                    list = new ArrayList<PortletPreferences>();
1062                }
1063
1064                cacheResult(list);
1065
1066                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_O_O_P,
1067                    finderArgs, list);
1068
1069                closeSession(session);
1070            }
1071        }
1072
1073        return list;
1074    }
1075
1076    public PortletPreferences findByO_O_P_First(long ownerId, int ownerType,
1077        long plid, OrderByComparator orderByComparator)
1078        throws NoSuchPortletPreferencesException, SystemException {
1079        List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
1080                0, 1, orderByComparator);
1081
1082        if (list.isEmpty()) {
1083            StringBundler msg = new StringBundler(8);
1084
1085            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1086
1087            msg.append("ownerId=");
1088            msg.append(ownerId);
1089
1090            msg.append(", ownerType=");
1091            msg.append(ownerType);
1092
1093            msg.append(", plid=");
1094            msg.append(plid);
1095
1096            msg.append(StringPool.CLOSE_CURLY_BRACE);
1097
1098            throw new NoSuchPortletPreferencesException(msg.toString());
1099        }
1100        else {
1101            return list.get(0);
1102        }
1103    }
1104
1105    public PortletPreferences findByO_O_P_Last(long ownerId, int ownerType,
1106        long plid, OrderByComparator orderByComparator)
1107        throws NoSuchPortletPreferencesException, SystemException {
1108        int count = countByO_O_P(ownerId, ownerType, plid);
1109
1110        List<PortletPreferences> list = findByO_O_P(ownerId, ownerType, plid,
1111                count - 1, count, orderByComparator);
1112
1113        if (list.isEmpty()) {
1114            StringBundler msg = new StringBundler(8);
1115
1116            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1117
1118            msg.append("ownerId=");
1119            msg.append(ownerId);
1120
1121            msg.append(", ownerType=");
1122            msg.append(ownerType);
1123
1124            msg.append(", plid=");
1125            msg.append(plid);
1126
1127            msg.append(StringPool.CLOSE_CURLY_BRACE);
1128
1129            throw new NoSuchPortletPreferencesException(msg.toString());
1130        }
1131        else {
1132            return list.get(0);
1133        }
1134    }
1135
1136    public PortletPreferences[] findByO_O_P_PrevAndNext(
1137        long portletPreferencesId, long ownerId, int ownerType, long plid,
1138        OrderByComparator orderByComparator)
1139        throws NoSuchPortletPreferencesException, SystemException {
1140        PortletPreferences portletPreferences = findByPrimaryKey(portletPreferencesId);
1141
1142        Session session = null;
1143
1144        try {
1145            session = openSession();
1146
1147            PortletPreferences[] array = new PortletPreferencesImpl[3];
1148
1149            array[0] = getByO_O_P_PrevAndNext(session, portletPreferences,
1150                    ownerId, ownerType, plid, orderByComparator, true);
1151
1152            array[1] = portletPreferences;
1153
1154            array[2] = getByO_O_P_PrevAndNext(session, portletPreferences,
1155                    ownerId, ownerType, plid, orderByComparator, false);
1156
1157            return array;
1158        }
1159        catch (Exception e) {
1160            throw processException(e);
1161        }
1162        finally {
1163            closeSession(session);
1164        }
1165    }
1166
1167    protected PortletPreferences getByO_O_P_PrevAndNext(Session session,
1168        PortletPreferences portletPreferences, long ownerId, int ownerType,
1169        long plid, OrderByComparator orderByComparator, boolean previous) {
1170        StringBundler query = null;
1171
1172        if (orderByComparator != null) {
1173            query = new StringBundler(6 +
1174                    (orderByComparator.getOrderByFields().length * 6));
1175        }
1176        else {
1177            query = new StringBundler(3);
1178        }
1179
1180        query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1181
1182        query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
1183
1184        query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
1185
1186        query.append(_FINDER_COLUMN_O_O_P_PLID_2);
1187
1188        if (orderByComparator != null) {
1189            String[] orderByFields = orderByComparator.getOrderByFields();
1190
1191            if (orderByFields.length > 0) {
1192                query.append(WHERE_AND);
1193            }
1194
1195            for (int i = 0; i < orderByFields.length; i++) {
1196                query.append(_ORDER_BY_ENTITY_ALIAS);
1197                query.append(orderByFields[i]);
1198
1199                if ((i + 1) < orderByFields.length) {
1200                    if (orderByComparator.isAscending() ^ previous) {
1201                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1202                    }
1203                    else {
1204                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1205                    }
1206                }
1207                else {
1208                    if (orderByComparator.isAscending() ^ previous) {
1209                        query.append(WHERE_GREATER_THAN);
1210                    }
1211                    else {
1212                        query.append(WHERE_LESSER_THAN);
1213                    }
1214                }
1215            }
1216
1217            query.append(ORDER_BY_CLAUSE);
1218
1219            for (int i = 0; i < orderByFields.length; i++) {
1220                query.append(_ORDER_BY_ENTITY_ALIAS);
1221                query.append(orderByFields[i]);
1222
1223                if ((i + 1) < orderByFields.length) {
1224                    if (orderByComparator.isAscending() ^ previous) {
1225                        query.append(ORDER_BY_ASC_HAS_NEXT);
1226                    }
1227                    else {
1228                        query.append(ORDER_BY_DESC_HAS_NEXT);
1229                    }
1230                }
1231                else {
1232                    if (orderByComparator.isAscending() ^ previous) {
1233                        query.append(ORDER_BY_ASC);
1234                    }
1235                    else {
1236                        query.append(ORDER_BY_DESC);
1237                    }
1238                }
1239            }
1240        }
1241
1242        String sql = query.toString();
1243
1244        Query q = session.createQuery(sql);
1245
1246        q.setFirstResult(0);
1247        q.setMaxResults(2);
1248
1249        QueryPos qPos = QueryPos.getInstance(q);
1250
1251        qPos.add(ownerId);
1252
1253        qPos.add(ownerType);
1254
1255        qPos.add(plid);
1256
1257        if (orderByComparator != null) {
1258            Object[] values = orderByComparator.getOrderByValues(portletPreferences);
1259
1260            for (Object value : values) {
1261                qPos.add(value);
1262            }
1263        }
1264
1265        List<PortletPreferences> list = q.list();
1266
1267        if (list.size() == 2) {
1268            return list.get(1);
1269        }
1270        else {
1271            return null;
1272        }
1273    }
1274
1275    public PortletPreferences findByO_O_P_P(long ownerId, int ownerType,
1276        long plid, String portletId)
1277        throws NoSuchPortletPreferencesException, SystemException {
1278        PortletPreferences portletPreferences = fetchByO_O_P_P(ownerId,
1279                ownerType, plid, portletId);
1280
1281        if (portletPreferences == null) {
1282            StringBundler msg = new StringBundler(10);
1283
1284            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1285
1286            msg.append("ownerId=");
1287            msg.append(ownerId);
1288
1289            msg.append(", ownerType=");
1290            msg.append(ownerType);
1291
1292            msg.append(", plid=");
1293            msg.append(plid);
1294
1295            msg.append(", portletId=");
1296            msg.append(portletId);
1297
1298            msg.append(StringPool.CLOSE_CURLY_BRACE);
1299
1300            if (_log.isWarnEnabled()) {
1301                _log.warn(msg.toString());
1302            }
1303
1304            throw new NoSuchPortletPreferencesException(msg.toString());
1305        }
1306
1307        return portletPreferences;
1308    }
1309
1310    public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
1311        long plid, String portletId) throws SystemException {
1312        return fetchByO_O_P_P(ownerId, ownerType, plid, portletId, true);
1313    }
1314
1315    public PortletPreferences fetchByO_O_P_P(long ownerId, int ownerType,
1316        long plid, String portletId, boolean retrieveFromCache)
1317        throws SystemException {
1318        Object[] finderArgs = new Object[] {
1319                new Long(ownerId), new Integer(ownerType), new Long(plid),
1320                
1321                portletId
1322            };
1323
1324        Object result = null;
1325
1326        if (retrieveFromCache) {
1327            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_O_O_P_P,
1328                    finderArgs, this);
1329        }
1330
1331        if (result == null) {
1332            Session session = null;
1333
1334            try {
1335                session = openSession();
1336
1337                StringBundler query = new StringBundler(5);
1338
1339                query.append(_SQL_SELECT_PORTLETPREFERENCES_WHERE);
1340
1341                query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
1342
1343                query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
1344
1345                query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
1346
1347                if (portletId == null) {
1348                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
1349                }
1350                else {
1351                    if (portletId.equals(StringPool.BLANK)) {
1352                        query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
1353                    }
1354                    else {
1355                        query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
1356                    }
1357                }
1358
1359                String sql = query.toString();
1360
1361                Query q = session.createQuery(sql);
1362
1363                QueryPos qPos = QueryPos.getInstance(q);
1364
1365                qPos.add(ownerId);
1366
1367                qPos.add(ownerType);
1368
1369                qPos.add(plid);
1370
1371                if (portletId != null) {
1372                    qPos.add(portletId);
1373                }
1374
1375                List<PortletPreferences> list = q.list();
1376
1377                result = list;
1378
1379                PortletPreferences portletPreferences = null;
1380
1381                if (list.isEmpty()) {
1382                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
1383                        finderArgs, list);
1384                }
1385                else {
1386                    portletPreferences = list.get(0);
1387
1388                    cacheResult(portletPreferences);
1389
1390                    if ((portletPreferences.getOwnerId() != ownerId) ||
1391                            (portletPreferences.getOwnerType() != ownerType) ||
1392                            (portletPreferences.getPlid() != plid) ||
1393                            (portletPreferences.getPortletId() == null) ||
1394                            !portletPreferences.getPortletId().equals(portletId)) {
1395                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
1396                            finderArgs, portletPreferences);
1397                    }
1398                }
1399
1400                return portletPreferences;
1401            }
1402            catch (Exception e) {
1403                throw processException(e);
1404            }
1405            finally {
1406                if (result == null) {
1407                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_O_O_P_P,
1408                        finderArgs, new ArrayList<PortletPreferences>());
1409                }
1410
1411                closeSession(session);
1412            }
1413        }
1414        else {
1415            if (result instanceof List<?>) {
1416                return null;
1417            }
1418            else {
1419                return (PortletPreferences)result;
1420            }
1421        }
1422    }
1423
1424    public List<PortletPreferences> findAll() throws SystemException {
1425        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1426    }
1427
1428    public List<PortletPreferences> findAll(int start, int end)
1429        throws SystemException {
1430        return findAll(start, end, null);
1431    }
1432
1433    public List<PortletPreferences> findAll(int start, int end,
1434        OrderByComparator orderByComparator) throws SystemException {
1435        Object[] finderArgs = new Object[] {
1436                String.valueOf(start), String.valueOf(end),
1437                String.valueOf(orderByComparator)
1438            };
1439
1440        List<PortletPreferences> list = (List<PortletPreferences>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1441                finderArgs, this);
1442
1443        if (list == null) {
1444            Session session = null;
1445
1446            try {
1447                session = openSession();
1448
1449                StringBundler query = null;
1450                String sql = null;
1451
1452                if (orderByComparator != null) {
1453                    query = new StringBundler(2 +
1454                            (orderByComparator.getOrderByFields().length * 3));
1455
1456                    query.append(_SQL_SELECT_PORTLETPREFERENCES);
1457
1458                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1459                        orderByComparator);
1460
1461                    sql = query.toString();
1462                }
1463
1464                sql = _SQL_SELECT_PORTLETPREFERENCES;
1465
1466                Query q = session.createQuery(sql);
1467
1468                if (orderByComparator == null) {
1469                    list = (List<PortletPreferences>)QueryUtil.list(q,
1470                            getDialect(), start, end, false);
1471
1472                    Collections.sort(list);
1473                }
1474                else {
1475                    list = (List<PortletPreferences>)QueryUtil.list(q,
1476                            getDialect(), start, end);
1477                }
1478            }
1479            catch (Exception e) {
1480                throw processException(e);
1481            }
1482            finally {
1483                if (list == null) {
1484                    list = new ArrayList<PortletPreferences>();
1485                }
1486
1487                cacheResult(list);
1488
1489                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1490
1491                closeSession(session);
1492            }
1493        }
1494
1495        return list;
1496    }
1497
1498    public void removeByPlid(long plid) throws SystemException {
1499        for (PortletPreferences portletPreferences : findByPlid(plid)) {
1500            remove(portletPreferences);
1501        }
1502    }
1503
1504    public void removeByP_P(long plid, String portletId)
1505        throws SystemException {
1506        for (PortletPreferences portletPreferences : findByP_P(plid, portletId)) {
1507            remove(portletPreferences);
1508        }
1509    }
1510
1511    public void removeByO_O_P(long ownerId, int ownerType, long plid)
1512        throws SystemException {
1513        for (PortletPreferences portletPreferences : findByO_O_P(ownerId,
1514                ownerType, plid)) {
1515            remove(portletPreferences);
1516        }
1517    }
1518
1519    public void removeByO_O_P_P(long ownerId, int ownerType, long plid,
1520        String portletId)
1521        throws NoSuchPortletPreferencesException, SystemException {
1522        PortletPreferences portletPreferences = findByO_O_P_P(ownerId,
1523                ownerType, plid, portletId);
1524
1525        remove(portletPreferences);
1526    }
1527
1528    public void removeAll() throws SystemException {
1529        for (PortletPreferences portletPreferences : findAll()) {
1530            remove(portletPreferences);
1531        }
1532    }
1533
1534    public int countByPlid(long plid) throws SystemException {
1535        Object[] finderArgs = new Object[] { new Long(plid) };
1536
1537        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_PLID,
1538                finderArgs, this);
1539
1540        if (count == null) {
1541            Session session = null;
1542
1543            try {
1544                session = openSession();
1545
1546                StringBundler query = new StringBundler(2);
1547
1548                query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1549
1550                query.append(_FINDER_COLUMN_PLID_PLID_2);
1551
1552                String sql = query.toString();
1553
1554                Query q = session.createQuery(sql);
1555
1556                QueryPos qPos = QueryPos.getInstance(q);
1557
1558                qPos.add(plid);
1559
1560                count = (Long)q.uniqueResult();
1561            }
1562            catch (Exception e) {
1563                throw processException(e);
1564            }
1565            finally {
1566                if (count == null) {
1567                    count = Long.valueOf(0);
1568                }
1569
1570                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_PLID,
1571                    finderArgs, count);
1572
1573                closeSession(session);
1574            }
1575        }
1576
1577        return count.intValue();
1578    }
1579
1580    public int countByP_P(long plid, String portletId)
1581        throws SystemException {
1582        Object[] finderArgs = new Object[] { new Long(plid), portletId };
1583
1584        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_P_P,
1585                finderArgs, this);
1586
1587        if (count == null) {
1588            Session session = null;
1589
1590            try {
1591                session = openSession();
1592
1593                StringBundler query = new StringBundler(3);
1594
1595                query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1596
1597                query.append(_FINDER_COLUMN_P_P_PLID_2);
1598
1599                if (portletId == null) {
1600                    query.append(_FINDER_COLUMN_P_P_PORTLETID_1);
1601                }
1602                else {
1603                    if (portletId.equals(StringPool.BLANK)) {
1604                        query.append(_FINDER_COLUMN_P_P_PORTLETID_3);
1605                    }
1606                    else {
1607                        query.append(_FINDER_COLUMN_P_P_PORTLETID_2);
1608                    }
1609                }
1610
1611                String sql = query.toString();
1612
1613                Query q = session.createQuery(sql);
1614
1615                QueryPos qPos = QueryPos.getInstance(q);
1616
1617                qPos.add(plid);
1618
1619                if (portletId != null) {
1620                    qPos.add(portletId);
1621                }
1622
1623                count = (Long)q.uniqueResult();
1624            }
1625            catch (Exception e) {
1626                throw processException(e);
1627            }
1628            finally {
1629                if (count == null) {
1630                    count = Long.valueOf(0);
1631                }
1632
1633                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_P_P, finderArgs,
1634                    count);
1635
1636                closeSession(session);
1637            }
1638        }
1639
1640        return count.intValue();
1641    }
1642
1643    public int countByO_O_P(long ownerId, int ownerType, long plid)
1644        throws SystemException {
1645        Object[] finderArgs = new Object[] {
1646                new Long(ownerId), new Integer(ownerType), new Long(plid)
1647            };
1648
1649        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_O_O_P,
1650                finderArgs, this);
1651
1652        if (count == null) {
1653            Session session = null;
1654
1655            try {
1656                session = openSession();
1657
1658                StringBundler query = new StringBundler(4);
1659
1660                query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1661
1662                query.append(_FINDER_COLUMN_O_O_P_OWNERID_2);
1663
1664                query.append(_FINDER_COLUMN_O_O_P_OWNERTYPE_2);
1665
1666                query.append(_FINDER_COLUMN_O_O_P_PLID_2);
1667
1668                String sql = query.toString();
1669
1670                Query q = session.createQuery(sql);
1671
1672                QueryPos qPos = QueryPos.getInstance(q);
1673
1674                qPos.add(ownerId);
1675
1676                qPos.add(ownerType);
1677
1678                qPos.add(plid);
1679
1680                count = (Long)q.uniqueResult();
1681            }
1682            catch (Exception e) {
1683                throw processException(e);
1684            }
1685            finally {
1686                if (count == null) {
1687                    count = Long.valueOf(0);
1688                }
1689
1690                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P,
1691                    finderArgs, count);
1692
1693                closeSession(session);
1694            }
1695        }
1696
1697        return count.intValue();
1698    }
1699
1700    public int countByO_O_P_P(long ownerId, int ownerType, long plid,
1701        String portletId) throws SystemException {
1702        Object[] finderArgs = new Object[] {
1703                new Long(ownerId), new Integer(ownerType), new Long(plid),
1704                
1705                portletId
1706            };
1707
1708        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_O_O_P_P,
1709                finderArgs, this);
1710
1711        if (count == null) {
1712            Session session = null;
1713
1714            try {
1715                session = openSession();
1716
1717                StringBundler query = new StringBundler(5);
1718
1719                query.append(_SQL_COUNT_PORTLETPREFERENCES_WHERE);
1720
1721                query.append(_FINDER_COLUMN_O_O_P_P_OWNERID_2);
1722
1723                query.append(_FINDER_COLUMN_O_O_P_P_OWNERTYPE_2);
1724
1725                query.append(_FINDER_COLUMN_O_O_P_P_PLID_2);
1726
1727                if (portletId == null) {
1728                    query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_1);
1729                }
1730                else {
1731                    if (portletId.equals(StringPool.BLANK)) {
1732                        query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_3);
1733                    }
1734                    else {
1735                        query.append(_FINDER_COLUMN_O_O_P_P_PORTLETID_2);
1736                    }
1737                }
1738
1739                String sql = query.toString();
1740
1741                Query q = session.createQuery(sql);
1742
1743                QueryPos qPos = QueryPos.getInstance(q);
1744
1745                qPos.add(ownerId);
1746
1747                qPos.add(ownerType);
1748
1749                qPos.add(plid);
1750
1751                if (portletId != null) {
1752                    qPos.add(portletId);
1753                }
1754
1755                count = (Long)q.uniqueResult();
1756            }
1757            catch (Exception e) {
1758                throw processException(e);
1759            }
1760            finally {
1761                if (count == null) {
1762                    count = Long.valueOf(0);
1763                }
1764
1765                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_O_O_P_P,
1766                    finderArgs, count);
1767
1768                closeSession(session);
1769            }
1770        }
1771
1772        return count.intValue();
1773    }
1774
1775    public int countAll() throws SystemException {
1776        Object[] finderArgs = new Object[0];
1777
1778        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1779                finderArgs, this);
1780
1781        if (count == null) {
1782            Session session = null;
1783
1784            try {
1785                session = openSession();
1786
1787                Query q = session.createQuery(_SQL_COUNT_PORTLETPREFERENCES);
1788
1789                count = (Long)q.uniqueResult();
1790            }
1791            catch (Exception e) {
1792                throw processException(e);
1793            }
1794            finally {
1795                if (count == null) {
1796                    count = Long.valueOf(0);
1797                }
1798
1799                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1800                    count);
1801
1802                closeSession(session);
1803            }
1804        }
1805
1806        return count.intValue();
1807    }
1808
1809    public void afterPropertiesSet() {
1810        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1811                    com.liferay.portal.util.PropsUtil.get(
1812                        "value.object.listener.com.liferay.portal.model.PortletPreferences")));
1813
1814        if (listenerClassNames.length > 0) {
1815            try {
1816                List<ModelListener<PortletPreferences>> listenersList = new ArrayList<ModelListener<PortletPreferences>>();
1817
1818                for (String listenerClassName : listenerClassNames) {
1819                    listenersList.add((ModelListener<PortletPreferences>)InstanceFactory.newInstance(
1820                            listenerClassName));
1821                }
1822
1823                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1824            }
1825            catch (Exception e) {
1826                _log.error(e);
1827            }
1828        }
1829    }
1830
1831    @BeanReference(type = AccountPersistence.class)
1832    protected AccountPersistence accountPersistence;
1833    @BeanReference(type = AddressPersistence.class)
1834    protected AddressPersistence addressPersistence;
1835    @BeanReference(type = BrowserTrackerPersistence.class)
1836    protected BrowserTrackerPersistence browserTrackerPersistence;
1837    @BeanReference(type = ClassNamePersistence.class)
1838    protected ClassNamePersistence classNamePersistence;
1839    @BeanReference(type = CompanyPersistence.class)
1840    protected CompanyPersistence companyPersistence;
1841    @BeanReference(type = ContactPersistence.class)
1842    protected ContactPersistence contactPersistence;
1843    @BeanReference(type = CountryPersistence.class)
1844    protected CountryPersistence countryPersistence;
1845    @BeanReference(type = EmailAddressPersistence.class)
1846    protected EmailAddressPersistence emailAddressPersistence;
1847    @BeanReference(type = GroupPersistence.class)
1848    protected GroupPersistence groupPersistence;
1849    @BeanReference(type = ImagePersistence.class)
1850    protected ImagePersistence imagePersistence;
1851    @BeanReference(type = LayoutPersistence.class)
1852    protected LayoutPersistence layoutPersistence;
1853    @BeanReference(type = LayoutPrototypePersistence.class)
1854    protected LayoutPrototypePersistence layoutPrototypePersistence;
1855    @BeanReference(type = LayoutSetPersistence.class)
1856    protected LayoutSetPersistence layoutSetPersistence;
1857    @BeanReference(type = LayoutSetPrototypePersistence.class)
1858    protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1859    @BeanReference(type = ListTypePersistence.class)
1860    protected ListTypePersistence listTypePersistence;
1861    @BeanReference(type = LockPersistence.class)
1862    protected LockPersistence lockPersistence;
1863    @BeanReference(type = MembershipRequestPersistence.class)
1864    protected MembershipRequestPersistence membershipRequestPersistence;
1865    @BeanReference(type = OrganizationPersistence.class)
1866    protected OrganizationPersistence organizationPersistence;
1867    @BeanReference(type = OrgGroupPermissionPersistence.class)
1868    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1869    @BeanReference(type = OrgGroupRolePersistence.class)
1870    protected OrgGroupRolePersistence orgGroupRolePersistence;
1871    @BeanReference(type = OrgLaborPersistence.class)
1872    protected OrgLaborPersistence orgLaborPersistence;
1873    @BeanReference(type = PasswordPolicyPersistence.class)
1874    protected PasswordPolicyPersistence passwordPolicyPersistence;
1875    @BeanReference(type = PasswordPolicyRelPersistence.class)
1876    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1877    @BeanReference(type = PasswordTrackerPersistence.class)
1878    protected PasswordTrackerPersistence passwordTrackerPersistence;
1879    @BeanReference(type = PermissionPersistence.class)
1880    protected PermissionPersistence permissionPersistence;
1881    @BeanReference(type = PhonePersistence.class)
1882    protected PhonePersistence phonePersistence;
1883    @BeanReference(type = PluginSettingPersistence.class)
1884    protected PluginSettingPersistence pluginSettingPersistence;
1885    @BeanReference(type = PortletPersistence.class)
1886    protected PortletPersistence portletPersistence;
1887    @BeanReference(type = PortletItemPersistence.class)
1888    protected PortletItemPersistence portletItemPersistence;
1889    @BeanReference(type = PortletPreferencesPersistence.class)
1890    protected PortletPreferencesPersistence portletPreferencesPersistence;
1891    @BeanReference(type = RegionPersistence.class)
1892    protected RegionPersistence regionPersistence;
1893    @BeanReference(type = ReleasePersistence.class)
1894    protected ReleasePersistence releasePersistence;
1895    @BeanReference(type = ResourcePersistence.class)
1896    protected ResourcePersistence resourcePersistence;
1897    @BeanReference(type = ResourceActionPersistence.class)
1898    protected ResourceActionPersistence resourceActionPersistence;
1899    @BeanReference(type = ResourceCodePersistence.class)
1900    protected ResourceCodePersistence resourceCodePersistence;
1901    @BeanReference(type = ResourcePermissionPersistence.class)
1902    protected ResourcePermissionPersistence resourcePermissionPersistence;
1903    @BeanReference(type = RolePersistence.class)
1904    protected RolePersistence rolePersistence;
1905    @BeanReference(type = ServiceComponentPersistence.class)
1906    protected ServiceComponentPersistence serviceComponentPersistence;
1907    @BeanReference(type = ShardPersistence.class)
1908    protected ShardPersistence shardPersistence;
1909    @BeanReference(type = SubscriptionPersistence.class)
1910    protected SubscriptionPersistence subscriptionPersistence;
1911    @BeanReference(type = TicketPersistence.class)
1912    protected TicketPersistence ticketPersistence;
1913    @BeanReference(type = TeamPersistence.class)
1914    protected TeamPersistence teamPersistence;
1915    @BeanReference(type = UserPersistence.class)
1916    protected UserPersistence userPersistence;
1917    @BeanReference(type = UserGroupPersistence.class)
1918    protected UserGroupPersistence userGroupPersistence;
1919    @BeanReference(type = UserGroupGroupRolePersistence.class)
1920    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1921    @BeanReference(type = UserGroupRolePersistence.class)
1922    protected UserGroupRolePersistence userGroupRolePersistence;
1923    @BeanReference(type = UserIdMapperPersistence.class)
1924    protected UserIdMapperPersistence userIdMapperPersistence;
1925    @BeanReference(type = UserTrackerPersistence.class)
1926    protected UserTrackerPersistence userTrackerPersistence;
1927    @BeanReference(type = UserTrackerPathPersistence.class)
1928    protected UserTrackerPathPersistence userTrackerPathPersistence;
1929    @BeanReference(type = WebDAVPropsPersistence.class)
1930    protected WebDAVPropsPersistence webDAVPropsPersistence;
1931    @BeanReference(type = WebsitePersistence.class)
1932    protected WebsitePersistence websitePersistence;
1933    @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1934    protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1935    @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1936    protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1937    private static final String _SQL_SELECT_PORTLETPREFERENCES = "SELECT portletPreferences FROM PortletPreferences portletPreferences";
1938    private static final String _SQL_SELECT_PORTLETPREFERENCES_WHERE = "SELECT portletPreferences FROM PortletPreferences portletPreferences WHERE ";
1939    private static final String _SQL_COUNT_PORTLETPREFERENCES = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences";
1940    private static final String _SQL_COUNT_PORTLETPREFERENCES_WHERE = "SELECT COUNT(portletPreferences) FROM PortletPreferences portletPreferences WHERE ";
1941    private static final String _FINDER_COLUMN_PLID_PLID_2 = "portletPreferences.plid = ?";
1942    private static final String _FINDER_COLUMN_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
1943    private static final String _FINDER_COLUMN_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1944    private static final String _FINDER_COLUMN_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
1945    private static final String _FINDER_COLUMN_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
1946    private static final String _FINDER_COLUMN_O_O_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
1947    private static final String _FINDER_COLUMN_O_O_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
1948    private static final String _FINDER_COLUMN_O_O_P_PLID_2 = "portletPreferences.plid = ?";
1949    private static final String _FINDER_COLUMN_O_O_P_P_OWNERID_2 = "portletPreferences.ownerId = ? AND ";
1950    private static final String _FINDER_COLUMN_O_O_P_P_OWNERTYPE_2 = "portletPreferences.ownerType = ? AND ";
1951    private static final String _FINDER_COLUMN_O_O_P_P_PLID_2 = "portletPreferences.plid = ? AND ";
1952    private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_1 = "portletPreferences.portletId IS NULL";
1953    private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_2 = "portletPreferences.portletId = ?";
1954    private static final String _FINDER_COLUMN_O_O_P_P_PORTLETID_3 = "(portletPreferences.portletId IS NULL OR portletPreferences.portletId = ?)";
1955    private static final String _ORDER_BY_ENTITY_ALIAS = "portletPreferences.";
1956    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No PortletPreferences exists with the primary key ";
1957    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No PortletPreferences exists with the key {";
1958    private static Log _log = LogFactoryUtil.getLog(PortletPreferencesPersistenceImpl.class);
1959}