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