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