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.NoSuchPortletException;
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.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.model.Portlet;
39  import com.liferay.portal.model.impl.PortletImpl;
40  import com.liferay.portal.model.impl.PortletModelImpl;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import java.io.Serializable;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.List;
48  
49  /**
50   * <a href="PortletPersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * <p>
53   * ServiceBuilder generated this class. Modifications in this class will be
54   * overwritten the next time is generated.
55   * </p>
56   *
57   * @author    Brian Wing Shun Chan
58   * @see       PortletPersistence
59   * @see       PortletUtil
60   * @generated
61   */
62  public class PortletPersistenceImpl extends BasePersistenceImpl<Portlet>
63      implements PortletPersistence {
64      public static final String FINDER_CLASS_NAME_ENTITY = PortletImpl.class.getName();
65      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
66          ".List";
67      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
68              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
69              "findByCompanyId", new String[] { Long.class.getName() });
70      public static final FinderPath FINDER_PATH_FIND_BY_OBC_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
71              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
72              "findByCompanyId",
73              new String[] {
74                  Long.class.getName(),
75                  
76              "java.lang.Integer", "java.lang.Integer",
77                  "com.liferay.portal.kernel.util.OrderByComparator"
78              });
79      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
80              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81              "countByCompanyId", new String[] { Long.class.getName() });
82      public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
83              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
84              "fetchByC_P",
85              new String[] { Long.class.getName(), String.class.getName() });
86      public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
87              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "countByC_P",
89              new String[] { Long.class.getName(), String.class.getName() });
90      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
91              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
92              "findAll", new String[0]);
93      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
94              PortletModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
95              "countAll", new String[0]);
96  
97      public void cacheResult(Portlet portlet) {
98          EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
99              PortletImpl.class, portlet.getPrimaryKey(), portlet);
100 
101         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
102             new Object[] {
103                 new Long(portlet.getCompanyId()),
104                 
105             portlet.getPortletId()
106             }, portlet);
107     }
108 
109     public void cacheResult(List<Portlet> portlets) {
110         for (Portlet portlet : portlets) {
111             if (EntityCacheUtil.getResult(
112                         PortletModelImpl.ENTITY_CACHE_ENABLED,
113                         PortletImpl.class, portlet.getPrimaryKey(), this) == null) {
114                 cacheResult(portlet);
115             }
116         }
117     }
118 
119     public void clearCache() {
120         CacheRegistry.clear(PortletImpl.class.getName());
121         EntityCacheUtil.clearCache(PortletImpl.class.getName());
122         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
123         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
124     }
125 
126     public Portlet create(long id) {
127         Portlet portlet = new PortletImpl();
128 
129         portlet.setNew(true);
130         portlet.setPrimaryKey(id);
131 
132         return portlet;
133     }
134 
135     public Portlet remove(Serializable primaryKey)
136         throws NoSuchModelException, SystemException {
137         return remove(((Long)primaryKey).longValue());
138     }
139 
140     public Portlet remove(long id)
141         throws NoSuchPortletException, SystemException {
142         Session session = null;
143 
144         try {
145             session = openSession();
146 
147             Portlet portlet = (Portlet)session.get(PortletImpl.class,
148                     new Long(id));
149 
150             if (portlet == null) {
151                 if (_log.isWarnEnabled()) {
152                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
153                 }
154 
155                 throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
156                     id);
157             }
158 
159             return remove(portlet);
160         }
161         catch (NoSuchPortletException nsee) {
162             throw nsee;
163         }
164         catch (Exception e) {
165             throw processException(e);
166         }
167         finally {
168             closeSession(session);
169         }
170     }
171 
172     public Portlet remove(Portlet portlet) throws SystemException {
173         for (ModelListener<Portlet> listener : listeners) {
174             listener.onBeforeRemove(portlet);
175         }
176 
177         portlet = removeImpl(portlet);
178 
179         for (ModelListener<Portlet> listener : listeners) {
180             listener.onAfterRemove(portlet);
181         }
182 
183         return portlet;
184     }
185 
186     protected Portlet removeImpl(Portlet portlet) throws SystemException {
187         portlet = toUnwrappedModel(portlet);
188 
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             if (portlet.isCachedModel() || BatchSessionUtil.isEnabled()) {
195                 Object staleObject = session.get(PortletImpl.class,
196                         portlet.getPrimaryKeyObj());
197 
198                 if (staleObject != null) {
199                     session.evict(staleObject);
200                 }
201             }
202 
203             session.delete(portlet);
204 
205             session.flush();
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213 
214         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
215 
216         PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
217 
218         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
219             new Object[] {
220                 new Long(portletModelImpl.getOriginalCompanyId()),
221                 
222             portletModelImpl.getOriginalPortletId()
223             });
224 
225         EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
226             PortletImpl.class, portlet.getPrimaryKey());
227 
228         return portlet;
229     }
230 
231     public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
232         boolean merge) throws SystemException {
233         portlet = toUnwrappedModel(portlet);
234 
235         boolean isNew = portlet.isNew();
236 
237         PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
238 
239         Session session = null;
240 
241         try {
242             session = openSession();
243 
244             BatchSessionUtil.update(session, portlet, merge);
245 
246             portlet.setNew(false);
247         }
248         catch (Exception e) {
249             throw processException(e);
250         }
251         finally {
252             closeSession(session);
253         }
254 
255         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
256 
257         EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
258             PortletImpl.class, portlet.getPrimaryKey(), portlet);
259 
260         if (!isNew &&
261                 ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
262                 !Validator.equals(portlet.getPortletId(),
263                     portletModelImpl.getOriginalPortletId()))) {
264             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
265                 new Object[] {
266                     new Long(portletModelImpl.getOriginalCompanyId()),
267                     
268                 portletModelImpl.getOriginalPortletId()
269                 });
270         }
271 
272         if (isNew ||
273                 ((portlet.getCompanyId() != portletModelImpl.getOriginalCompanyId()) ||
274                 !Validator.equals(portlet.getPortletId(),
275                     portletModelImpl.getOriginalPortletId()))) {
276             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
277                 new Object[] {
278                     new Long(portlet.getCompanyId()),
279                     
280                 portlet.getPortletId()
281                 }, portlet);
282         }
283 
284         return portlet;
285     }
286 
287     protected Portlet toUnwrappedModel(Portlet portlet) {
288         if (portlet instanceof PortletImpl) {
289             return portlet;
290         }
291 
292         PortletImpl portletImpl = new PortletImpl();
293 
294         portletImpl.setNew(portlet.isNew());
295         portletImpl.setPrimaryKey(portlet.getPrimaryKey());
296 
297         portletImpl.setId(portlet.getId());
298         portletImpl.setCompanyId(portlet.getCompanyId());
299         portletImpl.setPortletId(portlet.getPortletId());
300         portletImpl.setRoles(portlet.getRoles());
301         portletImpl.setActive(portlet.isActive());
302 
303         return portletImpl;
304     }
305 
306     public Portlet findByPrimaryKey(Serializable primaryKey)
307         throws NoSuchModelException, SystemException {
308         return findByPrimaryKey(((Long)primaryKey).longValue());
309     }
310 
311     public Portlet findByPrimaryKey(long id)
312         throws NoSuchPortletException, SystemException {
313         Portlet portlet = fetchByPrimaryKey(id);
314 
315         if (portlet == null) {
316             if (_log.isWarnEnabled()) {
317                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
318             }
319 
320             throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
321                 id);
322         }
323 
324         return portlet;
325     }
326 
327     public Portlet fetchByPrimaryKey(Serializable primaryKey)
328         throws SystemException {
329         return fetchByPrimaryKey(((Long)primaryKey).longValue());
330     }
331 
332     public Portlet fetchByPrimaryKey(long id) throws SystemException {
333         Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
334                 PortletImpl.class, id, this);
335 
336         if (portlet == null) {
337             Session session = null;
338 
339             try {
340                 session = openSession();
341 
342                 portlet = (Portlet)session.get(PortletImpl.class, new Long(id));
343             }
344             catch (Exception e) {
345                 throw processException(e);
346             }
347             finally {
348                 if (portlet != null) {
349                     cacheResult(portlet);
350                 }
351 
352                 closeSession(session);
353             }
354         }
355 
356         return portlet;
357     }
358 
359     public List<Portlet> findByCompanyId(long companyId)
360         throws SystemException {
361         Object[] finderArgs = new Object[] { new Long(companyId) };
362 
363         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
364                 finderArgs, this);
365 
366         if (list == null) {
367             Session session = null;
368 
369             try {
370                 session = openSession();
371 
372                 StringBundler query = new StringBundler(2);
373 
374                 query.append(_SQL_SELECT_PORTLET_WHERE);
375 
376                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
377 
378                 String sql = query.toString();
379 
380                 Query q = session.createQuery(sql);
381 
382                 QueryPos qPos = QueryPos.getInstance(q);
383 
384                 qPos.add(companyId);
385 
386                 list = q.list();
387             }
388             catch (Exception e) {
389                 throw processException(e);
390             }
391             finally {
392                 if (list == null) {
393                     list = new ArrayList<Portlet>();
394                 }
395 
396                 cacheResult(list);
397 
398                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
399                     finderArgs, list);
400 
401                 closeSession(session);
402             }
403         }
404 
405         return list;
406     }
407 
408     public List<Portlet> findByCompanyId(long companyId, int start, int end)
409         throws SystemException {
410         return findByCompanyId(companyId, start, end, null);
411     }
412 
413     public List<Portlet> findByCompanyId(long companyId, int start, int end,
414         OrderByComparator orderByComparator) throws SystemException {
415         Object[] finderArgs = new Object[] {
416                 new Long(companyId),
417                 
418                 String.valueOf(start), String.valueOf(end),
419                 String.valueOf(orderByComparator)
420             };
421 
422         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
423                 finderArgs, this);
424 
425         if (list == null) {
426             Session session = null;
427 
428             try {
429                 session = openSession();
430 
431                 StringBundler query = null;
432 
433                 if (orderByComparator != null) {
434                     query = new StringBundler(3 +
435                             (orderByComparator.getOrderByFields().length * 3));
436                 }
437                 else {
438                     query = new StringBundler(2);
439                 }
440 
441                 query.append(_SQL_SELECT_PORTLET_WHERE);
442 
443                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
444 
445                 if (orderByComparator != null) {
446                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
447                         orderByComparator);
448                 }
449 
450                 String sql = query.toString();
451 
452                 Query q = session.createQuery(sql);
453 
454                 QueryPos qPos = QueryPos.getInstance(q);
455 
456                 qPos.add(companyId);
457 
458                 list = (List<Portlet>)QueryUtil.list(q, getDialect(), start, end);
459             }
460             catch (Exception e) {
461                 throw processException(e);
462             }
463             finally {
464                 if (list == null) {
465                     list = new ArrayList<Portlet>();
466                 }
467 
468                 cacheResult(list);
469 
470                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
471                     finderArgs, list);
472 
473                 closeSession(session);
474             }
475         }
476 
477         return list;
478     }
479 
480     public Portlet findByCompanyId_First(long companyId,
481         OrderByComparator orderByComparator)
482         throws NoSuchPortletException, SystemException {
483         List<Portlet> list = findByCompanyId(companyId, 0, 1, orderByComparator);
484 
485         if (list.isEmpty()) {
486             StringBundler msg = new StringBundler(4);
487 
488             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
489 
490             msg.append("companyId=");
491             msg.append(companyId);
492 
493             msg.append(StringPool.CLOSE_CURLY_BRACE);
494 
495             throw new NoSuchPortletException(msg.toString());
496         }
497         else {
498             return list.get(0);
499         }
500     }
501 
502     public Portlet findByCompanyId_Last(long companyId,
503         OrderByComparator orderByComparator)
504         throws NoSuchPortletException, SystemException {
505         int count = countByCompanyId(companyId);
506 
507         List<Portlet> list = findByCompanyId(companyId, count - 1, count,
508                 orderByComparator);
509 
510         if (list.isEmpty()) {
511             StringBundler msg = new StringBundler(4);
512 
513             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
514 
515             msg.append("companyId=");
516             msg.append(companyId);
517 
518             msg.append(StringPool.CLOSE_CURLY_BRACE);
519 
520             throw new NoSuchPortletException(msg.toString());
521         }
522         else {
523             return list.get(0);
524         }
525     }
526 
527     public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
528         OrderByComparator orderByComparator)
529         throws NoSuchPortletException, SystemException {
530         Portlet portlet = findByPrimaryKey(id);
531 
532         int count = countByCompanyId(companyId);
533 
534         Session session = null;
535 
536         try {
537             session = openSession();
538 
539             StringBundler query = null;
540 
541             if (orderByComparator != null) {
542                 query = new StringBundler(3 +
543                         (orderByComparator.getOrderByFields().length * 3));
544             }
545             else {
546                 query = new StringBundler(2);
547             }
548 
549             query.append(_SQL_SELECT_PORTLET_WHERE);
550 
551             query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
552 
553             if (orderByComparator != null) {
554                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
555                     orderByComparator);
556             }
557 
558             String sql = query.toString();
559 
560             Query q = session.createQuery(sql);
561 
562             QueryPos qPos = QueryPos.getInstance(q);
563 
564             qPos.add(companyId);
565 
566             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
567                     orderByComparator, portlet);
568 
569             Portlet[] array = new PortletImpl[3];
570 
571             array[0] = (Portlet)objArray[0];
572             array[1] = (Portlet)objArray[1];
573             array[2] = (Portlet)objArray[2];
574 
575             return array;
576         }
577         catch (Exception e) {
578             throw processException(e);
579         }
580         finally {
581             closeSession(session);
582         }
583     }
584 
585     public Portlet findByC_P(long companyId, String portletId)
586         throws NoSuchPortletException, SystemException {
587         Portlet portlet = fetchByC_P(companyId, portletId);
588 
589         if (portlet == null) {
590             StringBundler msg = new StringBundler(6);
591 
592             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
593 
594             msg.append("companyId=");
595             msg.append(companyId);
596 
597             msg.append(", portletId=");
598             msg.append(portletId);
599 
600             msg.append(StringPool.CLOSE_CURLY_BRACE);
601 
602             if (_log.isWarnEnabled()) {
603                 _log.warn(msg.toString());
604             }
605 
606             throw new NoSuchPortletException(msg.toString());
607         }
608 
609         return portlet;
610     }
611 
612     public Portlet fetchByC_P(long companyId, String portletId)
613         throws SystemException {
614         return fetchByC_P(companyId, portletId, true);
615     }
616 
617     public Portlet fetchByC_P(long companyId, String portletId,
618         boolean retrieveFromCache) throws SystemException {
619         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
620 
621         Object result = null;
622 
623         if (retrieveFromCache) {
624             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
625                     finderArgs, this);
626         }
627 
628         if (result == null) {
629             Session session = null;
630 
631             try {
632                 session = openSession();
633 
634                 StringBundler query = new StringBundler(3);
635 
636                 query.append(_SQL_SELECT_PORTLET_WHERE);
637 
638                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
639 
640                 if (portletId == null) {
641                     query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
642                 }
643                 else {
644                     if (portletId.equals(StringPool.BLANK)) {
645                         query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
646                     }
647                     else {
648                         query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
649                     }
650                 }
651 
652                 String sql = query.toString();
653 
654                 Query q = session.createQuery(sql);
655 
656                 QueryPos qPos = QueryPos.getInstance(q);
657 
658                 qPos.add(companyId);
659 
660                 if (portletId != null) {
661                     qPos.add(portletId);
662                 }
663 
664                 List<Portlet> list = q.list();
665 
666                 result = list;
667 
668                 Portlet portlet = null;
669 
670                 if (list.isEmpty()) {
671                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
672                         finderArgs, list);
673                 }
674                 else {
675                     portlet = list.get(0);
676 
677                     cacheResult(portlet);
678 
679                     if ((portlet.getCompanyId() != companyId) ||
680                             (portlet.getPortletId() == null) ||
681                             !portlet.getPortletId().equals(portletId)) {
682                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
683                             finderArgs, portlet);
684                     }
685                 }
686 
687                 return portlet;
688             }
689             catch (Exception e) {
690                 throw processException(e);
691             }
692             finally {
693                 if (result == null) {
694                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
695                         finderArgs, new ArrayList<Portlet>());
696                 }
697 
698                 closeSession(session);
699             }
700         }
701         else {
702             if (result instanceof List<?>) {
703                 return null;
704             }
705             else {
706                 return (Portlet)result;
707             }
708         }
709     }
710 
711     public List<Portlet> findAll() throws SystemException {
712         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
713     }
714 
715     public List<Portlet> findAll(int start, int end) throws SystemException {
716         return findAll(start, end, null);
717     }
718 
719     public List<Portlet> findAll(int start, int end,
720         OrderByComparator orderByComparator) throws SystemException {
721         Object[] finderArgs = new Object[] {
722                 String.valueOf(start), String.valueOf(end),
723                 String.valueOf(orderByComparator)
724             };
725 
726         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
727                 finderArgs, this);
728 
729         if (list == null) {
730             Session session = null;
731 
732             try {
733                 session = openSession();
734 
735                 StringBundler query = null;
736                 String sql = null;
737 
738                 if (orderByComparator != null) {
739                     query = new StringBundler(2 +
740                             (orderByComparator.getOrderByFields().length * 3));
741 
742                     query.append(_SQL_SELECT_PORTLET);
743 
744                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
745                         orderByComparator);
746 
747                     sql = query.toString();
748                 }
749 
750                 sql = _SQL_SELECT_PORTLET;
751 
752                 Query q = session.createQuery(sql);
753 
754                 if (orderByComparator == null) {
755                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
756                             start, end, false);
757 
758                     Collections.sort(list);
759                 }
760                 else {
761                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
762                             start, end);
763                 }
764             }
765             catch (Exception e) {
766                 throw processException(e);
767             }
768             finally {
769                 if (list == null) {
770                     list = new ArrayList<Portlet>();
771                 }
772 
773                 cacheResult(list);
774 
775                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
776 
777                 closeSession(session);
778             }
779         }
780 
781         return list;
782     }
783 
784     public void removeByCompanyId(long companyId) throws SystemException {
785         for (Portlet portlet : findByCompanyId(companyId)) {
786             remove(portlet);
787         }
788     }
789 
790     public void removeByC_P(long companyId, String portletId)
791         throws NoSuchPortletException, SystemException {
792         Portlet portlet = findByC_P(companyId, portletId);
793 
794         remove(portlet);
795     }
796 
797     public void removeAll() throws SystemException {
798         for (Portlet portlet : findAll()) {
799             remove(portlet);
800         }
801     }
802 
803     public int countByCompanyId(long companyId) throws SystemException {
804         Object[] finderArgs = new Object[] { new Long(companyId) };
805 
806         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
807                 finderArgs, this);
808 
809         if (count == null) {
810             Session session = null;
811 
812             try {
813                 session = openSession();
814 
815                 StringBundler query = new StringBundler(2);
816 
817                 query.append(_SQL_COUNT_PORTLET_WHERE);
818 
819                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
820 
821                 String sql = query.toString();
822 
823                 Query q = session.createQuery(sql);
824 
825                 QueryPos qPos = QueryPos.getInstance(q);
826 
827                 qPos.add(companyId);
828 
829                 count = (Long)q.uniqueResult();
830             }
831             catch (Exception e) {
832                 throw processException(e);
833             }
834             finally {
835                 if (count == null) {
836                     count = Long.valueOf(0);
837                 }
838 
839                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
840                     finderArgs, count);
841 
842                 closeSession(session);
843             }
844         }
845 
846         return count.intValue();
847     }
848 
849     public int countByC_P(long companyId, String portletId)
850         throws SystemException {
851         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
852 
853         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
854                 finderArgs, this);
855 
856         if (count == null) {
857             Session session = null;
858 
859             try {
860                 session = openSession();
861 
862                 StringBundler query = new StringBundler(3);
863 
864                 query.append(_SQL_COUNT_PORTLET_WHERE);
865 
866                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
867 
868                 if (portletId == null) {
869                     query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
870                 }
871                 else {
872                     if (portletId.equals(StringPool.BLANK)) {
873                         query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
874                     }
875                     else {
876                         query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
877                     }
878                 }
879 
880                 String sql = query.toString();
881 
882                 Query q = session.createQuery(sql);
883 
884                 QueryPos qPos = QueryPos.getInstance(q);
885 
886                 qPos.add(companyId);
887 
888                 if (portletId != null) {
889                     qPos.add(portletId);
890                 }
891 
892                 count = (Long)q.uniqueResult();
893             }
894             catch (Exception e) {
895                 throw processException(e);
896             }
897             finally {
898                 if (count == null) {
899                     count = Long.valueOf(0);
900                 }
901 
902                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
903                     count);
904 
905                 closeSession(session);
906             }
907         }
908 
909         return count.intValue();
910     }
911 
912     public int countAll() throws SystemException {
913         Object[] finderArgs = new Object[0];
914 
915         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
916                 finderArgs, this);
917 
918         if (count == null) {
919             Session session = null;
920 
921             try {
922                 session = openSession();
923 
924                 Query q = session.createQuery(_SQL_COUNT_PORTLET);
925 
926                 count = (Long)q.uniqueResult();
927             }
928             catch (Exception e) {
929                 throw processException(e);
930             }
931             finally {
932                 if (count == null) {
933                     count = Long.valueOf(0);
934                 }
935 
936                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
937                     count);
938 
939                 closeSession(session);
940             }
941         }
942 
943         return count.intValue();
944     }
945 
946     public void afterPropertiesSet() {
947         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
948                     com.liferay.portal.util.PropsUtil.get(
949                         "value.object.listener.com.liferay.portal.model.Portlet")));
950 
951         if (listenerClassNames.length > 0) {
952             try {
953                 List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
954 
955                 for (String listenerClassName : listenerClassNames) {
956                     listenersList.add((ModelListener<Portlet>)Class.forName(
957                             listenerClassName).newInstance());
958                 }
959 
960                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
961             }
962             catch (Exception e) {
963                 _log.error(e);
964             }
965         }
966     }
967 
968     @BeanReference(type = AccountPersistence.class)
969     protected AccountPersistence accountPersistence;
970     @BeanReference(type = AddressPersistence.class)
971     protected AddressPersistence addressPersistence;
972     @BeanReference(type = BrowserTrackerPersistence.class)
973     protected BrowserTrackerPersistence browserTrackerPersistence;
974     @BeanReference(type = ClassNamePersistence.class)
975     protected ClassNamePersistence classNamePersistence;
976     @BeanReference(type = CompanyPersistence.class)
977     protected CompanyPersistence companyPersistence;
978     @BeanReference(type = ContactPersistence.class)
979     protected ContactPersistence contactPersistence;
980     @BeanReference(type = CountryPersistence.class)
981     protected CountryPersistence countryPersistence;
982     @BeanReference(type = EmailAddressPersistence.class)
983     protected EmailAddressPersistence emailAddressPersistence;
984     @BeanReference(type = GroupPersistence.class)
985     protected GroupPersistence groupPersistence;
986     @BeanReference(type = ImagePersistence.class)
987     protected ImagePersistence imagePersistence;
988     @BeanReference(type = LayoutPersistence.class)
989     protected LayoutPersistence layoutPersistence;
990     @BeanReference(type = LayoutPrototypePersistence.class)
991     protected LayoutPrototypePersistence layoutPrototypePersistence;
992     @BeanReference(type = LayoutSetPersistence.class)
993     protected LayoutSetPersistence layoutSetPersistence;
994     @BeanReference(type = LayoutSetPrototypePersistence.class)
995     protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
996     @BeanReference(type = ListTypePersistence.class)
997     protected ListTypePersistence listTypePersistence;
998     @BeanReference(type = LockPersistence.class)
999     protected LockPersistence lockPersistence;
1000    @BeanReference(type = MembershipRequestPersistence.class)
1001    protected MembershipRequestPersistence membershipRequestPersistence;
1002    @BeanReference(type = OrganizationPersistence.class)
1003    protected OrganizationPersistence organizationPersistence;
1004    @BeanReference(type = OrgGroupPermissionPersistence.class)
1005    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1006    @BeanReference(type = OrgGroupRolePersistence.class)
1007    protected OrgGroupRolePersistence orgGroupRolePersistence;
1008    @BeanReference(type = OrgLaborPersistence.class)
1009    protected OrgLaborPersistence orgLaborPersistence;
1010    @BeanReference(type = PasswordPolicyPersistence.class)
1011    protected PasswordPolicyPersistence passwordPolicyPersistence;
1012    @BeanReference(type = PasswordPolicyRelPersistence.class)
1013    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1014    @BeanReference(type = PasswordTrackerPersistence.class)
1015    protected PasswordTrackerPersistence passwordTrackerPersistence;
1016    @BeanReference(type = PermissionPersistence.class)
1017    protected PermissionPersistence permissionPersistence;
1018    @BeanReference(type = PhonePersistence.class)
1019    protected PhonePersistence phonePersistence;
1020    @BeanReference(type = PluginSettingPersistence.class)
1021    protected PluginSettingPersistence pluginSettingPersistence;
1022    @BeanReference(type = PortletPersistence.class)
1023    protected PortletPersistence portletPersistence;
1024    @BeanReference(type = PortletItemPersistence.class)
1025    protected PortletItemPersistence portletItemPersistence;
1026    @BeanReference(type = PortletPreferencesPersistence.class)
1027    protected PortletPreferencesPersistence portletPreferencesPersistence;
1028    @BeanReference(type = RegionPersistence.class)
1029    protected RegionPersistence regionPersistence;
1030    @BeanReference(type = ReleasePersistence.class)
1031    protected ReleasePersistence releasePersistence;
1032    @BeanReference(type = ResourcePersistence.class)
1033    protected ResourcePersistence resourcePersistence;
1034    @BeanReference(type = ResourceActionPersistence.class)
1035    protected ResourceActionPersistence resourceActionPersistence;
1036    @BeanReference(type = ResourceCodePersistence.class)
1037    protected ResourceCodePersistence resourceCodePersistence;
1038    @BeanReference(type = ResourcePermissionPersistence.class)
1039    protected ResourcePermissionPersistence resourcePermissionPersistence;
1040    @BeanReference(type = RolePersistence.class)
1041    protected RolePersistence rolePersistence;
1042    @BeanReference(type = ServiceComponentPersistence.class)
1043    protected ServiceComponentPersistence serviceComponentPersistence;
1044    @BeanReference(type = ShardPersistence.class)
1045    protected ShardPersistence shardPersistence;
1046    @BeanReference(type = SubscriptionPersistence.class)
1047    protected SubscriptionPersistence subscriptionPersistence;
1048    @BeanReference(type = TeamPersistence.class)
1049    protected TeamPersistence teamPersistence;
1050    @BeanReference(type = UserPersistence.class)
1051    protected UserPersistence userPersistence;
1052    @BeanReference(type = UserGroupPersistence.class)
1053    protected UserGroupPersistence userGroupPersistence;
1054    @BeanReference(type = UserGroupGroupRolePersistence.class)
1055    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1056    @BeanReference(type = UserGroupRolePersistence.class)
1057    protected UserGroupRolePersistence userGroupRolePersistence;
1058    @BeanReference(type = UserIdMapperPersistence.class)
1059    protected UserIdMapperPersistence userIdMapperPersistence;
1060    @BeanReference(type = UserTrackerPersistence.class)
1061    protected UserTrackerPersistence userTrackerPersistence;
1062    @BeanReference(type = UserTrackerPathPersistence.class)
1063    protected UserTrackerPathPersistence userTrackerPathPersistence;
1064    @BeanReference(type = WebDAVPropsPersistence.class)
1065    protected WebDAVPropsPersistence webDAVPropsPersistence;
1066    @BeanReference(type = WebsitePersistence.class)
1067    protected WebsitePersistence websitePersistence;
1068    @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1069    protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1070    @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1071    protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1072    private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1073    private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1074    private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1075    private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1076    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "portlet.companyId = ?";
1077    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "portlet.companyId = ? AND ";
1078    private static final String _FINDER_COLUMN_C_P_PORTLETID_1 = "portlet.portletId IS NULL";
1079    private static final String _FINDER_COLUMN_C_P_PORTLETID_2 = "portlet.portletId = ?";
1080    private static final String _FINDER_COLUMN_C_P_PORTLETID_3 = "(portlet.portletId IS NULL OR portlet.portletId = ?)";
1081    private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1082    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1083    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1084    private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1085}