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