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("SELECT portlet FROM Portlet portlet WHERE ");
387 
388                 query.append("portlet.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("SELECT portlet FROM Portlet portlet WHERE ");
445 
446                 query.append("portlet.companyId = ?");
447 
448                 query.append(" ");
449 
450                 if (obc != null) {
451                     query.append("ORDER BY ");
452 
453                     String[] orderByFields = obc.getOrderByFields();
454 
455                     for (int i = 0; i < orderByFields.length; i++) {
456                         query.append("portlet.");
457                         query.append(orderByFields[i]);
458 
459                         if (obc.isAscending()) {
460                             query.append(" ASC");
461                         }
462                         else {
463                             query.append(" DESC");
464                         }
465 
466                         if ((i + 1) < orderByFields.length) {
467                             query.append(", ");
468                         }
469                     }
470                 }
471 
472                 Query q = session.createQuery(query.toString());
473 
474                 QueryPos qPos = QueryPos.getInstance(q);
475 
476                 qPos.add(companyId);
477 
478                 list = (List<Portlet>)QueryUtil.list(q, getDialect(), start, end);
479             }
480             catch (Exception e) {
481                 throw processException(e);
482             }
483             finally {
484                 if (list == null) {
485                     list = new ArrayList<Portlet>();
486                 }
487 
488                 cacheResult(list);
489 
490                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
491                     finderArgs, list);
492 
493                 closeSession(session);
494             }
495         }
496 
497         return list;
498     }
499 
500     public Portlet findByCompanyId_First(long companyId, OrderByComparator obc)
501         throws NoSuchPortletException, SystemException {
502         List<Portlet> list = findByCompanyId(companyId, 0, 1, obc);
503 
504         if (list.isEmpty()) {
505             StringBuilder msg = new StringBuilder();
506 
507             msg.append("No Portlet exists with the key {");
508 
509             msg.append("companyId=" + companyId);
510 
511             msg.append(StringPool.CLOSE_CURLY_BRACE);
512 
513             throw new NoSuchPortletException(msg.toString());
514         }
515         else {
516             return list.get(0);
517         }
518     }
519 
520     public Portlet findByCompanyId_Last(long companyId, OrderByComparator obc)
521         throws NoSuchPortletException, SystemException {
522         int count = countByCompanyId(companyId);
523 
524         List<Portlet> list = findByCompanyId(companyId, count - 1, count, obc);
525 
526         if (list.isEmpty()) {
527             StringBuilder msg = new StringBuilder();
528 
529             msg.append("No Portlet exists with the key {");
530 
531             msg.append("companyId=" + companyId);
532 
533             msg.append(StringPool.CLOSE_CURLY_BRACE);
534 
535             throw new NoSuchPortletException(msg.toString());
536         }
537         else {
538             return list.get(0);
539         }
540     }
541 
542     public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
543         OrderByComparator obc) throws NoSuchPortletException, SystemException {
544         Portlet portlet = findByPrimaryKey(id);
545 
546         int count = countByCompanyId(companyId);
547 
548         Session session = null;
549 
550         try {
551             session = openSession();
552 
553             StringBuilder query = new StringBuilder();
554 
555             query.append("SELECT portlet FROM Portlet portlet WHERE ");
556 
557             query.append("portlet.companyId = ?");
558 
559             query.append(" ");
560 
561             if (obc != null) {
562                 query.append("ORDER BY ");
563 
564                 String[] orderByFields = obc.getOrderByFields();
565 
566                 for (int i = 0; i < orderByFields.length; i++) {
567                     query.append("portlet.");
568                     query.append(orderByFields[i]);
569 
570                     if (obc.isAscending()) {
571                         query.append(" ASC");
572                     }
573                     else {
574                         query.append(" DESC");
575                     }
576 
577                     if ((i + 1) < orderByFields.length) {
578                         query.append(", ");
579                     }
580                 }
581             }
582 
583             Query q = session.createQuery(query.toString());
584 
585             QueryPos qPos = QueryPos.getInstance(q);
586 
587             qPos.add(companyId);
588 
589             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, portlet);
590 
591             Portlet[] array = new PortletImpl[3];
592 
593             array[0] = (Portlet)objArray[0];
594             array[1] = (Portlet)objArray[1];
595             array[2] = (Portlet)objArray[2];
596 
597             return array;
598         }
599         catch (Exception e) {
600             throw processException(e);
601         }
602         finally {
603             closeSession(session);
604         }
605     }
606 
607     public Portlet findByC_P(long companyId, String portletId)
608         throws NoSuchPortletException, SystemException {
609         Portlet portlet = fetchByC_P(companyId, portletId);
610 
611         if (portlet == null) {
612             StringBuilder msg = new StringBuilder();
613 
614             msg.append("No Portlet exists with the key {");
615 
616             msg.append("companyId=" + companyId);
617 
618             msg.append(", ");
619             msg.append("portletId=" + portletId);
620 
621             msg.append(StringPool.CLOSE_CURLY_BRACE);
622 
623             if (_log.isWarnEnabled()) {
624                 _log.warn(msg.toString());
625             }
626 
627             throw new NoSuchPortletException(msg.toString());
628         }
629 
630         return portlet;
631     }
632 
633     public Portlet fetchByC_P(long companyId, String portletId)
634         throws SystemException {
635         return fetchByC_P(companyId, portletId, true);
636     }
637 
638     public Portlet fetchByC_P(long companyId, String portletId,
639         boolean retrieveFromCache) throws SystemException {
640         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
641 
642         Object result = null;
643 
644         if (retrieveFromCache) {
645             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
646                     finderArgs, this);
647         }
648 
649         if (result == null) {
650             Session session = null;
651 
652             try {
653                 session = openSession();
654 
655                 StringBuilder query = new StringBuilder();
656 
657                 query.append("SELECT portlet FROM Portlet portlet WHERE ");
658 
659                 query.append("portlet.companyId = ?");
660 
661                 query.append(" AND ");
662 
663                 if (portletId == null) {
664                     query.append("portlet.portletId IS NULL");
665                 }
666                 else {
667                     query.append("portlet.portletId = ?");
668                 }
669 
670                 query.append(" ");
671 
672                 Query q = session.createQuery(query.toString());
673 
674                 QueryPos qPos = QueryPos.getInstance(q);
675 
676                 qPos.add(companyId);
677 
678                 if (portletId != null) {
679                     qPos.add(portletId);
680                 }
681 
682                 List<Portlet> list = q.list();
683 
684                 result = list;
685 
686                 Portlet portlet = null;
687 
688                 if (list.isEmpty()) {
689                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
690                         finderArgs, list);
691                 }
692                 else {
693                     portlet = list.get(0);
694 
695                     cacheResult(portlet);
696 
697                     if ((portlet.getCompanyId() != companyId) ||
698                             (portlet.getPortletId() == null) ||
699                             !portlet.getPortletId().equals(portletId)) {
700                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
701                             finderArgs, portlet);
702                     }
703                 }
704 
705                 return portlet;
706             }
707             catch (Exception e) {
708                 throw processException(e);
709             }
710             finally {
711                 if (result == null) {
712                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
713                         finderArgs, new ArrayList<Portlet>());
714                 }
715 
716                 closeSession(session);
717             }
718         }
719         else {
720             if (result instanceof List) {
721                 return null;
722             }
723             else {
724                 return (Portlet)result;
725             }
726         }
727     }
728 
729     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
730         throws SystemException {
731         Session session = null;
732 
733         try {
734             session = openSession();
735 
736             dynamicQuery.compile(session);
737 
738             return dynamicQuery.list();
739         }
740         catch (Exception e) {
741             throw processException(e);
742         }
743         finally {
744             closeSession(session);
745         }
746     }
747 
748     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
749         int start, int end) throws SystemException {
750         Session session = null;
751 
752         try {
753             session = openSession();
754 
755             dynamicQuery.setLimit(start, end);
756 
757             dynamicQuery.compile(session);
758 
759             return dynamicQuery.list();
760         }
761         catch (Exception e) {
762             throw processException(e);
763         }
764         finally {
765             closeSession(session);
766         }
767     }
768 
769     public List<Portlet> findAll() throws SystemException {
770         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
771     }
772 
773     public List<Portlet> findAll(int start, int end) throws SystemException {
774         return findAll(start, end, null);
775     }
776 
777     public List<Portlet> findAll(int start, int end, OrderByComparator obc)
778         throws SystemException {
779         Object[] finderArgs = new Object[] {
780                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
781             };
782 
783         List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
784                 finderArgs, this);
785 
786         if (list == null) {
787             Session session = null;
788 
789             try {
790                 session = openSession();
791 
792                 StringBuilder query = new StringBuilder();
793 
794                 query.append("SELECT portlet FROM Portlet portlet ");
795 
796                 if (obc != null) {
797                     query.append("ORDER BY ");
798 
799                     String[] orderByFields = obc.getOrderByFields();
800 
801                     for (int i = 0; i < orderByFields.length; i++) {
802                         query.append("portlet.");
803                         query.append(orderByFields[i]);
804 
805                         if (obc.isAscending()) {
806                             query.append(" ASC");
807                         }
808                         else {
809                             query.append(" DESC");
810                         }
811 
812                         if ((i + 1) < orderByFields.length) {
813                             query.append(", ");
814                         }
815                     }
816                 }
817 
818                 Query q = session.createQuery(query.toString());
819 
820                 if (obc == null) {
821                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
822                             start, end, false);
823 
824                     Collections.sort(list);
825                 }
826                 else {
827                     list = (List<Portlet>)QueryUtil.list(q, getDialect(),
828                             start, end);
829                 }
830             }
831             catch (Exception e) {
832                 throw processException(e);
833             }
834             finally {
835                 if (list == null) {
836                     list = new ArrayList<Portlet>();
837                 }
838 
839                 cacheResult(list);
840 
841                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
842 
843                 closeSession(session);
844             }
845         }
846 
847         return list;
848     }
849 
850     public void removeByCompanyId(long companyId) throws SystemException {
851         for (Portlet portlet : findByCompanyId(companyId)) {
852             remove(portlet);
853         }
854     }
855 
856     public void removeByC_P(long companyId, String portletId)
857         throws NoSuchPortletException, SystemException {
858         Portlet portlet = findByC_P(companyId, portletId);
859 
860         remove(portlet);
861     }
862 
863     public void removeAll() throws SystemException {
864         for (Portlet portlet : findAll()) {
865             remove(portlet);
866         }
867     }
868 
869     public int countByCompanyId(long companyId) throws SystemException {
870         Object[] finderArgs = new Object[] { new Long(companyId) };
871 
872         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
873                 finderArgs, this);
874 
875         if (count == null) {
876             Session session = null;
877 
878             try {
879                 session = openSession();
880 
881                 StringBuilder query = new StringBuilder();
882 
883                 query.append("SELECT COUNT(portlet) ");
884                 query.append("FROM Portlet portlet WHERE ");
885 
886                 query.append("portlet.companyId = ?");
887 
888                 query.append(" ");
889 
890                 Query q = session.createQuery(query.toString());
891 
892                 QueryPos qPos = QueryPos.getInstance(q);
893 
894                 qPos.add(companyId);
895 
896                 count = (Long)q.uniqueResult();
897             }
898             catch (Exception e) {
899                 throw processException(e);
900             }
901             finally {
902                 if (count == null) {
903                     count = Long.valueOf(0);
904                 }
905 
906                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
907                     finderArgs, count);
908 
909                 closeSession(session);
910             }
911         }
912 
913         return count.intValue();
914     }
915 
916     public int countByC_P(long companyId, String portletId)
917         throws SystemException {
918         Object[] finderArgs = new Object[] { new Long(companyId), portletId };
919 
920         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
921                 finderArgs, this);
922 
923         if (count == null) {
924             Session session = null;
925 
926             try {
927                 session = openSession();
928 
929                 StringBuilder query = new StringBuilder();
930 
931                 query.append("SELECT COUNT(portlet) ");
932                 query.append("FROM Portlet portlet WHERE ");
933 
934                 query.append("portlet.companyId = ?");
935 
936                 query.append(" AND ");
937 
938                 if (portletId == null) {
939                     query.append("portlet.portletId IS NULL");
940                 }
941                 else {
942                     query.append("portlet.portletId = ?");
943                 }
944 
945                 query.append(" ");
946 
947                 Query q = session.createQuery(query.toString());
948 
949                 QueryPos qPos = QueryPos.getInstance(q);
950 
951                 qPos.add(companyId);
952 
953                 if (portletId != null) {
954                     qPos.add(portletId);
955                 }
956 
957                 count = (Long)q.uniqueResult();
958             }
959             catch (Exception e) {
960                 throw processException(e);
961             }
962             finally {
963                 if (count == null) {
964                     count = Long.valueOf(0);
965                 }
966 
967                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
968                     count);
969 
970                 closeSession(session);
971             }
972         }
973 
974         return count.intValue();
975     }
976 
977     public int countAll() throws SystemException {
978         Object[] finderArgs = new Object[0];
979 
980         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
981                 finderArgs, this);
982 
983         if (count == null) {
984             Session session = null;
985 
986             try {
987                 session = openSession();
988 
989                 Query q = session.createQuery(
990                         "SELECT COUNT(portlet) FROM Portlet portlet");
991 
992                 count = (Long)q.uniqueResult();
993             }
994             catch (Exception e) {
995                 throw processException(e);
996             }
997             finally {
998                 if (count == null) {
999                     count = Long.valueOf(0);
1000                }
1001
1002                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1003                    count);
1004
1005                closeSession(session);
1006            }
1007        }
1008
1009        return count.intValue();
1010    }
1011
1012    public void afterPropertiesSet() {
1013        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1014                    com.liferay.portal.util.PropsUtil.get(
1015                        "value.object.listener.com.liferay.portal.model.Portlet")));
1016
1017        if (listenerClassNames.length > 0) {
1018            try {
1019                List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
1020
1021                for (String listenerClassName : listenerClassNames) {
1022                    listenersList.add((ModelListener<Portlet>)Class.forName(
1023                            listenerClassName).newInstance());
1024                }
1025
1026                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1027            }
1028            catch (Exception e) {
1029                _log.error(e);
1030            }
1031        }
1032    }
1033
1034    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
1035    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
1036    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
1037    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
1038    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
1039    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
1040    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
1041    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
1042    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
1043    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
1044    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
1045    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
1046    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
1047    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
1048    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
1049    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1050    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1051    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1052    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1053    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1054    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1055    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1056    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1057    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1058    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1059    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1060    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1061    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1062    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1063    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1064    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1065    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1066    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1067    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1068    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1069    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1070    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1071    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1072    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1073    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1074    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1075    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1076    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1077    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1078    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1079    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1080    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1081    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1082    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1083    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1084    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1085    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1086    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1087    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1088    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1089    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1090    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1091    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1092    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1093    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1094    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1095    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1096    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1097    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1098    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1099    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1100    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1101    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1102    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1103    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1104    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1105    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1106    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1107    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1108    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1109    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1110    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1111    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1112    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1113    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1114    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1115    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1116    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1117    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1118    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1119    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1120    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1121    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1122    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1123    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1124    private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1125}