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