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