1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchPortletItemException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.bean.InitializingBean;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.ListUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.PortletItem;
41  import com.liferay.portal.model.impl.PortletItemImpl;
42  import com.liferay.portal.model.impl.PortletItemModelImpl;
43  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * <a href="PortletItemPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class PortletItemPersistenceImpl extends BasePersistenceImpl
60      implements PortletItemPersistence, InitializingBean {
61      public PortletItem create(long portletItemId) {
62          PortletItem portletItem = new PortletItemImpl();
63  
64          portletItem.setNew(true);
65          portletItem.setPrimaryKey(portletItemId);
66  
67          return portletItem;
68      }
69  
70      public PortletItem remove(long portletItemId)
71          throws NoSuchPortletItemException, SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              PortletItem portletItem = (PortletItem)session.get(PortletItemImpl.class,
78                      new Long(portletItemId));
79  
80              if (portletItem == null) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn("No PortletItem exists with the primary key " +
83                          portletItemId);
84                  }
85  
86                  throw new NoSuchPortletItemException(
87                      "No PortletItem exists with the primary key " +
88                      portletItemId);
89              }
90  
91              return remove(portletItem);
92          }
93          catch (NoSuchPortletItemException nsee) {
94              throw nsee;
95          }
96          catch (Exception e) {
97              throw processException(e);
98          }
99          finally {
100             closeSession(session);
101         }
102     }
103 
104     public PortletItem remove(PortletItem portletItem)
105         throws SystemException {
106         if (_listeners.length > 0) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(portletItem);
109             }
110         }
111 
112         portletItem = removeImpl(portletItem);
113 
114         if (_listeners.length > 0) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(portletItem);
117             }
118         }
119 
120         return portletItem;
121     }
122 
123     protected PortletItem removeImpl(PortletItem portletItem)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(portletItem);
131 
132             session.flush();
133 
134             return portletItem;
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCacheUtil.clearCache(PortletItem.class.getName());
143         }
144     }
145 
146     /**
147      * @deprecated Use <code>update(PortletItem portletItem, boolean merge)</code>.
148      */
149     public PortletItem update(PortletItem portletItem)
150         throws SystemException {
151         if (_log.isWarnEnabled()) {
152             _log.warn(
153                 "Using the deprecated update(PortletItem portletItem) method. Use update(PortletItem portletItem, boolean merge) instead.");
154         }
155 
156         return update(portletItem, false);
157     }
158 
159     /**
160      * Add, update, or merge, the entity. This method also calls the model
161      * listeners to trigger the proper events associated with adding, deleting,
162      * or updating an entity.
163      *
164      * @param        portletItem the entity to add, update, or merge
165      * @param        merge boolean value for whether to merge the entity. The
166      *                default value is false. Setting merge to true is more
167      *                expensive and should only be true when portletItem is
168      *                transient. See LEP-5473 for a detailed discussion of this
169      *                method.
170      * @return        true if the portlet can be displayed via Ajax
171      */
172     public PortletItem update(PortletItem portletItem, boolean merge)
173         throws SystemException {
174         boolean isNew = portletItem.isNew();
175 
176         if (_listeners.length > 0) {
177             for (ModelListener listener : _listeners) {
178                 if (isNew) {
179                     listener.onBeforeCreate(portletItem);
180                 }
181                 else {
182                     listener.onBeforeUpdate(portletItem);
183                 }
184             }
185         }
186 
187         portletItem = updateImpl(portletItem, merge);
188 
189         if (_listeners.length > 0) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onAfterCreate(portletItem);
193                 }
194                 else {
195                     listener.onAfterUpdate(portletItem);
196                 }
197             }
198         }
199 
200         return portletItem;
201     }
202 
203     public PortletItem updateImpl(
204         com.liferay.portal.model.PortletItem portletItem, boolean merge)
205         throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             if (merge) {
212                 session.merge(portletItem);
213             }
214             else {
215                 if (portletItem.isNew()) {
216                     session.save(portletItem);
217                 }
218             }
219 
220             session.flush();
221 
222             portletItem.setNew(false);
223 
224             return portletItem;
225         }
226         catch (Exception e) {
227             throw processException(e);
228         }
229         finally {
230             closeSession(session);
231 
232             FinderCacheUtil.clearCache(PortletItem.class.getName());
233         }
234     }
235 
236     public PortletItem findByPrimaryKey(long portletItemId)
237         throws NoSuchPortletItemException, SystemException {
238         PortletItem portletItem = fetchByPrimaryKey(portletItemId);
239 
240         if (portletItem == null) {
241             if (_log.isWarnEnabled()) {
242                 _log.warn("No PortletItem exists with the primary key " +
243                     portletItemId);
244             }
245 
246             throw new NoSuchPortletItemException(
247                 "No PortletItem exists with the primary key " + portletItemId);
248         }
249 
250         return portletItem;
251     }
252 
253     public PortletItem fetchByPrimaryKey(long portletItemId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (PortletItem)session.get(PortletItemImpl.class,
261                 new Long(portletItemId));
262         }
263         catch (Exception e) {
264             throw processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<PortletItem> findByG_C(long groupId, long classNameId)
272         throws SystemException {
273         boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
274         String finderClassName = PortletItem.class.getName();
275         String finderMethodName = "findByG_C";
276         String[] finderParams = new String[] {
277                 Long.class.getName(), Long.class.getName()
278             };
279         Object[] finderArgs = new Object[] {
280                 new Long(groupId), new Long(classNameId)
281             };
282 
283         Object result = null;
284 
285         if (finderClassNameCacheEnabled) {
286             result = FinderCacheUtil.getResult(finderClassName,
287                     finderMethodName, finderParams, finderArgs, this);
288         }
289 
290         if (result == null) {
291             Session session = null;
292 
293             try {
294                 session = openSession();
295 
296                 StringBuilder query = new StringBuilder();
297 
298                 query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
299 
300                 query.append("groupId = ?");
301 
302                 query.append(" AND ");
303 
304                 query.append("classNameId = ?");
305 
306                 query.append(" ");
307 
308                 Query q = session.createQuery(query.toString());
309 
310                 QueryPos qPos = QueryPos.getInstance(q);
311 
312                 qPos.add(groupId);
313 
314                 qPos.add(classNameId);
315 
316                 List<PortletItem> list = q.list();
317 
318                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
319                     finderClassName, finderMethodName, finderParams,
320                     finderArgs, list);
321 
322                 return list;
323             }
324             catch (Exception e) {
325                 throw processException(e);
326             }
327             finally {
328                 closeSession(session);
329             }
330         }
331         else {
332             return (List<PortletItem>)result;
333         }
334     }
335 
336     public List<PortletItem> findByG_C(long groupId, long classNameId,
337         int start, int end) throws SystemException {
338         return findByG_C(groupId, classNameId, start, end, null);
339     }
340 
341     public List<PortletItem> findByG_C(long groupId, long classNameId,
342         int start, int end, OrderByComparator obc) throws SystemException {
343         boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
344         String finderClassName = PortletItem.class.getName();
345         String finderMethodName = "findByG_C";
346         String[] finderParams = new String[] {
347                 Long.class.getName(), Long.class.getName(),
348                 
349                 "java.lang.Integer", "java.lang.Integer",
350                 "com.liferay.portal.kernel.util.OrderByComparator"
351             };
352         Object[] finderArgs = new Object[] {
353                 new Long(groupId), new Long(classNameId),
354                 
355                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
356             };
357 
358         Object result = null;
359 
360         if (finderClassNameCacheEnabled) {
361             result = FinderCacheUtil.getResult(finderClassName,
362                     finderMethodName, finderParams, finderArgs, this);
363         }
364 
365         if (result == null) {
366             Session session = null;
367 
368             try {
369                 session = openSession();
370 
371                 StringBuilder query = new StringBuilder();
372 
373                 query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
374 
375                 query.append("groupId = ?");
376 
377                 query.append(" AND ");
378 
379                 query.append("classNameId = ?");
380 
381                 query.append(" ");
382 
383                 if (obc != null) {
384                     query.append("ORDER BY ");
385                     query.append(obc.getOrderBy());
386                 }
387 
388                 Query q = session.createQuery(query.toString());
389 
390                 QueryPos qPos = QueryPos.getInstance(q);
391 
392                 qPos.add(groupId);
393 
394                 qPos.add(classNameId);
395 
396                 List<PortletItem> list = (List<PortletItem>)QueryUtil.list(q,
397                         getDialect(), start, end);
398 
399                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
400                     finderClassName, finderMethodName, finderParams,
401                     finderArgs, list);
402 
403                 return list;
404             }
405             catch (Exception e) {
406                 throw processException(e);
407             }
408             finally {
409                 closeSession(session);
410             }
411         }
412         else {
413             return (List<PortletItem>)result;
414         }
415     }
416 
417     public PortletItem findByG_C_First(long groupId, long classNameId,
418         OrderByComparator obc)
419         throws NoSuchPortletItemException, SystemException {
420         List<PortletItem> list = findByG_C(groupId, classNameId, 0, 1, obc);
421 
422         if (list.size() == 0) {
423             StringBuilder msg = new StringBuilder();
424 
425             msg.append("No PortletItem exists with the key {");
426 
427             msg.append("groupId=" + groupId);
428 
429             msg.append(", ");
430             msg.append("classNameId=" + classNameId);
431 
432             msg.append(StringPool.CLOSE_CURLY_BRACE);
433 
434             throw new NoSuchPortletItemException(msg.toString());
435         }
436         else {
437             return list.get(0);
438         }
439     }
440 
441     public PortletItem findByG_C_Last(long groupId, long classNameId,
442         OrderByComparator obc)
443         throws NoSuchPortletItemException, SystemException {
444         int count = countByG_C(groupId, classNameId);
445 
446         List<PortletItem> list = findByG_C(groupId, classNameId, count - 1,
447                 count, obc);
448 
449         if (list.size() == 0) {
450             StringBuilder msg = new StringBuilder();
451 
452             msg.append("No PortletItem exists with the key {");
453 
454             msg.append("groupId=" + groupId);
455 
456             msg.append(", ");
457             msg.append("classNameId=" + classNameId);
458 
459             msg.append(StringPool.CLOSE_CURLY_BRACE);
460 
461             throw new NoSuchPortletItemException(msg.toString());
462         }
463         else {
464             return list.get(0);
465         }
466     }
467 
468     public PortletItem[] findByG_C_PrevAndNext(long portletItemId,
469         long groupId, long classNameId, OrderByComparator obc)
470         throws NoSuchPortletItemException, SystemException {
471         PortletItem portletItem = findByPrimaryKey(portletItemId);
472 
473         int count = countByG_C(groupId, classNameId);
474 
475         Session session = null;
476 
477         try {
478             session = openSession();
479 
480             StringBuilder query = new StringBuilder();
481 
482             query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
483 
484             query.append("groupId = ?");
485 
486             query.append(" AND ");
487 
488             query.append("classNameId = ?");
489 
490             query.append(" ");
491 
492             if (obc != null) {
493                 query.append("ORDER BY ");
494                 query.append(obc.getOrderBy());
495             }
496 
497             Query q = session.createQuery(query.toString());
498 
499             QueryPos qPos = QueryPos.getInstance(q);
500 
501             qPos.add(groupId);
502 
503             qPos.add(classNameId);
504 
505             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
506                     portletItem);
507 
508             PortletItem[] array = new PortletItemImpl[3];
509 
510             array[0] = (PortletItem)objArray[0];
511             array[1] = (PortletItem)objArray[1];
512             array[2] = (PortletItem)objArray[2];
513 
514             return array;
515         }
516         catch (Exception e) {
517             throw processException(e);
518         }
519         finally {
520             closeSession(session);
521         }
522     }
523 
524     public List<PortletItem> findByG_P_C(long groupId, String portletId,
525         long classNameId) throws SystemException {
526         boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
527         String finderClassName = PortletItem.class.getName();
528         String finderMethodName = "findByG_P_C";
529         String[] finderParams = new String[] {
530                 Long.class.getName(), String.class.getName(),
531                 Long.class.getName()
532             };
533         Object[] finderArgs = new Object[] {
534                 new Long(groupId),
535                 
536                 portletId, new Long(classNameId)
537             };
538 
539         Object result = null;
540 
541         if (finderClassNameCacheEnabled) {
542             result = FinderCacheUtil.getResult(finderClassName,
543                     finderMethodName, finderParams, finderArgs, this);
544         }
545 
546         if (result == null) {
547             Session session = null;
548 
549             try {
550                 session = openSession();
551 
552                 StringBuilder query = new StringBuilder();
553 
554                 query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
555 
556                 query.append("groupId = ?");
557 
558                 query.append(" AND ");
559 
560                 if (portletId == null) {
561                     query.append("portletId IS NULL");
562                 }
563                 else {
564                     query.append("portletId = ?");
565                 }
566 
567                 query.append(" AND ");
568 
569                 query.append("classNameId = ?");
570 
571                 query.append(" ");
572 
573                 Query q = session.createQuery(query.toString());
574 
575                 QueryPos qPos = QueryPos.getInstance(q);
576 
577                 qPos.add(groupId);
578 
579                 if (portletId != null) {
580                     qPos.add(portletId);
581                 }
582 
583                 qPos.add(classNameId);
584 
585                 List<PortletItem> list = q.list();
586 
587                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
588                     finderClassName, finderMethodName, finderParams,
589                     finderArgs, list);
590 
591                 return list;
592             }
593             catch (Exception e) {
594                 throw processException(e);
595             }
596             finally {
597                 closeSession(session);
598             }
599         }
600         else {
601             return (List<PortletItem>)result;
602         }
603     }
604 
605     public List<PortletItem> findByG_P_C(long groupId, String portletId,
606         long classNameId, int start, int end) throws SystemException {
607         return findByG_P_C(groupId, portletId, classNameId, start, end, null);
608     }
609 
610     public List<PortletItem> findByG_P_C(long groupId, String portletId,
611         long classNameId, int start, int end, OrderByComparator obc)
612         throws SystemException {
613         boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
614         String finderClassName = PortletItem.class.getName();
615         String finderMethodName = "findByG_P_C";
616         String[] finderParams = new String[] {
617                 Long.class.getName(), String.class.getName(),
618                 Long.class.getName(),
619                 
620                 "java.lang.Integer", "java.lang.Integer",
621                 "com.liferay.portal.kernel.util.OrderByComparator"
622             };
623         Object[] finderArgs = new Object[] {
624                 new Long(groupId),
625                 
626                 portletId, new Long(classNameId),
627                 
628                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
629             };
630 
631         Object result = null;
632 
633         if (finderClassNameCacheEnabled) {
634             result = FinderCacheUtil.getResult(finderClassName,
635                     finderMethodName, finderParams, finderArgs, this);
636         }
637 
638         if (result == null) {
639             Session session = null;
640 
641             try {
642                 session = openSession();
643 
644                 StringBuilder query = new StringBuilder();
645 
646                 query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
647 
648                 query.append("groupId = ?");
649 
650                 query.append(" AND ");
651 
652                 if (portletId == null) {
653                     query.append("portletId IS NULL");
654                 }
655                 else {
656                     query.append("portletId = ?");
657                 }
658 
659                 query.append(" AND ");
660 
661                 query.append("classNameId = ?");
662 
663                 query.append(" ");
664 
665                 if (obc != null) {
666                     query.append("ORDER BY ");
667                     query.append(obc.getOrderBy());
668                 }
669 
670                 Query q = session.createQuery(query.toString());
671 
672                 QueryPos qPos = QueryPos.getInstance(q);
673 
674                 qPos.add(groupId);
675 
676                 if (portletId != null) {
677                     qPos.add(portletId);
678                 }
679 
680                 qPos.add(classNameId);
681 
682                 List<PortletItem> list = (List<PortletItem>)QueryUtil.list(q,
683                         getDialect(), start, end);
684 
685                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
686                     finderClassName, finderMethodName, finderParams,
687                     finderArgs, list);
688 
689                 return list;
690             }
691             catch (Exception e) {
692                 throw processException(e);
693             }
694             finally {
695                 closeSession(session);
696             }
697         }
698         else {
699             return (List<PortletItem>)result;
700         }
701     }
702 
703     public PortletItem findByG_P_C_First(long groupId, String portletId,
704         long classNameId, OrderByComparator obc)
705         throws NoSuchPortletItemException, SystemException {
706         List<PortletItem> list = findByG_P_C(groupId, portletId, classNameId,
707                 0, 1, obc);
708 
709         if (list.size() == 0) {
710             StringBuilder msg = new StringBuilder();
711 
712             msg.append("No PortletItem exists with the key {");
713 
714             msg.append("groupId=" + groupId);
715 
716             msg.append(", ");
717             msg.append("portletId=" + portletId);
718 
719             msg.append(", ");
720             msg.append("classNameId=" + classNameId);
721 
722             msg.append(StringPool.CLOSE_CURLY_BRACE);
723 
724             throw new NoSuchPortletItemException(msg.toString());
725         }
726         else {
727             return list.get(0);
728         }
729     }
730 
731     public PortletItem findByG_P_C_Last(long groupId, String portletId,
732         long classNameId, OrderByComparator obc)
733         throws NoSuchPortletItemException, SystemException {
734         int count = countByG_P_C(groupId, portletId, classNameId);
735 
736         List<PortletItem> list = findByG_P_C(groupId, portletId, classNameId,
737                 count - 1, count, obc);
738 
739         if (list.size() == 0) {
740             StringBuilder msg = new StringBuilder();
741 
742             msg.append("No PortletItem exists with the key {");
743 
744             msg.append("groupId=" + groupId);
745 
746             msg.append(", ");
747             msg.append("portletId=" + portletId);
748 
749             msg.append(", ");
750             msg.append("classNameId=" + classNameId);
751 
752             msg.append(StringPool.CLOSE_CURLY_BRACE);
753 
754             throw new NoSuchPortletItemException(msg.toString());
755         }
756         else {
757             return list.get(0);
758         }
759     }
760 
761     public PortletItem[] findByG_P_C_PrevAndNext(long portletItemId,
762         long groupId, String portletId, long classNameId, OrderByComparator obc)
763         throws NoSuchPortletItemException, SystemException {
764         PortletItem portletItem = findByPrimaryKey(portletItemId);
765 
766         int count = countByG_P_C(groupId, portletId, classNameId);
767 
768         Session session = null;
769 
770         try {
771             session = openSession();
772 
773             StringBuilder query = new StringBuilder();
774 
775             query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
776 
777             query.append("groupId = ?");
778 
779             query.append(" AND ");
780 
781             if (portletId == null) {
782                 query.append("portletId IS NULL");
783             }
784             else {
785                 query.append("portletId = ?");
786             }
787 
788             query.append(" AND ");
789 
790             query.append("classNameId = ?");
791 
792             query.append(" ");
793 
794             if (obc != null) {
795                 query.append("ORDER BY ");
796                 query.append(obc.getOrderBy());
797             }
798 
799             Query q = session.createQuery(query.toString());
800 
801             QueryPos qPos = QueryPos.getInstance(q);
802 
803             qPos.add(groupId);
804 
805             if (portletId != null) {
806                 qPos.add(portletId);
807             }
808 
809             qPos.add(classNameId);
810 
811             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
812                     portletItem);
813 
814             PortletItem[] array = new PortletItemImpl[3];
815 
816             array[0] = (PortletItem)objArray[0];
817             array[1] = (PortletItem)objArray[1];
818             array[2] = (PortletItem)objArray[2];
819 
820             return array;
821         }
822         catch (Exception e) {
823             throw processException(e);
824         }
825         finally {
826             closeSession(session);
827         }
828     }
829 
830     public PortletItem findByG_N_P_C(long groupId, String name,
831         String portletId, long classNameId)
832         throws NoSuchPortletItemException, SystemException {
833         PortletItem portletItem = fetchByG_N_P_C(groupId, name, portletId,
834                 classNameId);
835 
836         if (portletItem == null) {
837             StringBuilder msg = new StringBuilder();
838 
839             msg.append("No PortletItem exists with the key {");
840 
841             msg.append("groupId=" + groupId);
842 
843             msg.append(", ");
844             msg.append("name=" + name);
845 
846             msg.append(", ");
847             msg.append("portletId=" + portletId);
848 
849             msg.append(", ");
850             msg.append("classNameId=" + classNameId);
851 
852             msg.append(StringPool.CLOSE_CURLY_BRACE);
853 
854             if (_log.isWarnEnabled()) {
855                 _log.warn(msg.toString());
856             }
857 
858             throw new NoSuchPortletItemException(msg.toString());
859         }
860 
861         return portletItem;
862     }
863 
864     public PortletItem fetchByG_N_P_C(long groupId, String name,
865         String portletId, long classNameId) throws SystemException {
866         boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
867         String finderClassName = PortletItem.class.getName();
868         String finderMethodName = "fetchByG_N_P_C";
869         String[] finderParams = new String[] {
870                 Long.class.getName(), String.class.getName(),
871                 String.class.getName(), Long.class.getName()
872             };
873         Object[] finderArgs = new Object[] {
874                 new Long(groupId),
875                 
876                 name,
877                 
878                 portletId, new Long(classNameId)
879             };
880 
881         Object result = null;
882 
883         if (finderClassNameCacheEnabled) {
884             result = FinderCacheUtil.getResult(finderClassName,
885                     finderMethodName, finderParams, finderArgs, this);
886         }
887 
888         if (result == null) {
889             Session session = null;
890 
891             try {
892                 session = openSession();
893 
894                 StringBuilder query = new StringBuilder();
895 
896                 query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
897 
898                 query.append("groupId = ?");
899 
900                 query.append(" AND ");
901 
902                 if (name == null) {
903                     query.append("name IS NULL");
904                 }
905                 else {
906                     query.append("lower(name) = ?");
907                 }
908 
909                 query.append(" AND ");
910 
911                 if (portletId == null) {
912                     query.append("portletId IS NULL");
913                 }
914                 else {
915                     query.append("portletId = ?");
916                 }
917 
918                 query.append(" AND ");
919 
920                 query.append("classNameId = ?");
921 
922                 query.append(" ");
923 
924                 Query q = session.createQuery(query.toString());
925 
926                 QueryPos qPos = QueryPos.getInstance(q);
927 
928                 qPos.add(groupId);
929 
930                 if (name != null) {
931                     qPos.add(name);
932                 }
933 
934                 if (portletId != null) {
935                     qPos.add(portletId);
936                 }
937 
938                 qPos.add(classNameId);
939 
940                 List<PortletItem> list = q.list();
941 
942                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
943                     finderClassName, finderMethodName, finderParams,
944                     finderArgs, list);
945 
946                 if (list.size() == 0) {
947                     return null;
948                 }
949                 else {
950                     return list.get(0);
951                 }
952             }
953             catch (Exception e) {
954                 throw processException(e);
955             }
956             finally {
957                 closeSession(session);
958             }
959         }
960         else {
961             List<PortletItem> list = (List<PortletItem>)result;
962 
963             if (list.size() == 0) {
964                 return null;
965             }
966             else {
967                 return list.get(0);
968             }
969         }
970     }
971 
972     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
973         throws SystemException {
974         Session session = null;
975 
976         try {
977             session = openSession();
978 
979             dynamicQuery.compile(session);
980 
981             return dynamicQuery.list();
982         }
983         catch (Exception e) {
984             throw processException(e);
985         }
986         finally {
987             closeSession(session);
988         }
989     }
990 
991     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
992         int start, int end) throws SystemException {
993         Session session = null;
994 
995         try {
996             session = openSession();
997 
998             dynamicQuery.setLimit(start, end);
999 
1000            dynamicQuery.compile(session);
1001
1002            return dynamicQuery.list();
1003        }
1004        catch (Exception e) {
1005            throw processException(e);
1006        }
1007        finally {
1008            closeSession(session);
1009        }
1010    }
1011
1012    public List<PortletItem> findAll() throws SystemException {
1013        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1014    }
1015
1016    public List<PortletItem> findAll(int start, int end)
1017        throws SystemException {
1018        return findAll(start, end, null);
1019    }
1020
1021    public List<PortletItem> findAll(int start, int end, OrderByComparator obc)
1022        throws SystemException {
1023        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1024        String finderClassName = PortletItem.class.getName();
1025        String finderMethodName = "findAll";
1026        String[] finderParams = new String[] {
1027                "java.lang.Integer", "java.lang.Integer",
1028                "com.liferay.portal.kernel.util.OrderByComparator"
1029            };
1030        Object[] finderArgs = new Object[] {
1031                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1032            };
1033
1034        Object result = null;
1035
1036        if (finderClassNameCacheEnabled) {
1037            result = FinderCacheUtil.getResult(finderClassName,
1038                    finderMethodName, finderParams, finderArgs, this);
1039        }
1040
1041        if (result == null) {
1042            Session session = null;
1043
1044            try {
1045                session = openSession();
1046
1047                StringBuilder query = new StringBuilder();
1048
1049                query.append("FROM com.liferay.portal.model.PortletItem ");
1050
1051                if (obc != null) {
1052                    query.append("ORDER BY ");
1053                    query.append(obc.getOrderBy());
1054                }
1055
1056                Query q = session.createQuery(query.toString());
1057
1058                List<PortletItem> list = (List<PortletItem>)QueryUtil.list(q,
1059                        getDialect(), start, end);
1060
1061                if (obc == null) {
1062                    Collections.sort(list);
1063                }
1064
1065                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1066                    finderClassName, finderMethodName, finderParams,
1067                    finderArgs, list);
1068
1069                return list;
1070            }
1071            catch (Exception e) {
1072                throw processException(e);
1073            }
1074            finally {
1075                closeSession(session);
1076            }
1077        }
1078        else {
1079            return (List<PortletItem>)result;
1080        }
1081    }
1082
1083    public void removeByG_C(long groupId, long classNameId)
1084        throws SystemException {
1085        for (PortletItem portletItem : findByG_C(groupId, classNameId)) {
1086            remove(portletItem);
1087        }
1088    }
1089
1090    public void removeByG_P_C(long groupId, String portletId, long classNameId)
1091        throws SystemException {
1092        for (PortletItem portletItem : findByG_P_C(groupId, portletId,
1093                classNameId)) {
1094            remove(portletItem);
1095        }
1096    }
1097
1098    public void removeByG_N_P_C(long groupId, String name, String portletId,
1099        long classNameId) throws NoSuchPortletItemException, SystemException {
1100        PortletItem portletItem = findByG_N_P_C(groupId, name, portletId,
1101                classNameId);
1102
1103        remove(portletItem);
1104    }
1105
1106    public void removeAll() throws SystemException {
1107        for (PortletItem portletItem : findAll()) {
1108            remove(portletItem);
1109        }
1110    }
1111
1112    public int countByG_C(long groupId, long classNameId)
1113        throws SystemException {
1114        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1115        String finderClassName = PortletItem.class.getName();
1116        String finderMethodName = "countByG_C";
1117        String[] finderParams = new String[] {
1118                Long.class.getName(), Long.class.getName()
1119            };
1120        Object[] finderArgs = new Object[] {
1121                new Long(groupId), new Long(classNameId)
1122            };
1123
1124        Object result = null;
1125
1126        if (finderClassNameCacheEnabled) {
1127            result = FinderCacheUtil.getResult(finderClassName,
1128                    finderMethodName, finderParams, finderArgs, this);
1129        }
1130
1131        if (result == null) {
1132            Session session = null;
1133
1134            try {
1135                session = openSession();
1136
1137                StringBuilder query = new StringBuilder();
1138
1139                query.append("SELECT COUNT(*) ");
1140                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1141
1142                query.append("groupId = ?");
1143
1144                query.append(" AND ");
1145
1146                query.append("classNameId = ?");
1147
1148                query.append(" ");
1149
1150                Query q = session.createQuery(query.toString());
1151
1152                QueryPos qPos = QueryPos.getInstance(q);
1153
1154                qPos.add(groupId);
1155
1156                qPos.add(classNameId);
1157
1158                Long count = null;
1159
1160                Iterator<Long> itr = q.list().iterator();
1161
1162                if (itr.hasNext()) {
1163                    count = itr.next();
1164                }
1165
1166                if (count == null) {
1167                    count = new Long(0);
1168                }
1169
1170                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1171                    finderClassName, finderMethodName, finderParams,
1172                    finderArgs, count);
1173
1174                return count.intValue();
1175            }
1176            catch (Exception e) {
1177                throw processException(e);
1178            }
1179            finally {
1180                closeSession(session);
1181            }
1182        }
1183        else {
1184            return ((Long)result).intValue();
1185        }
1186    }
1187
1188    public int countByG_P_C(long groupId, String portletId, long classNameId)
1189        throws SystemException {
1190        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1191        String finderClassName = PortletItem.class.getName();
1192        String finderMethodName = "countByG_P_C";
1193        String[] finderParams = new String[] {
1194                Long.class.getName(), String.class.getName(),
1195                Long.class.getName()
1196            };
1197        Object[] finderArgs = new Object[] {
1198                new Long(groupId),
1199                
1200                portletId, new Long(classNameId)
1201            };
1202
1203        Object result = null;
1204
1205        if (finderClassNameCacheEnabled) {
1206            result = FinderCacheUtil.getResult(finderClassName,
1207                    finderMethodName, finderParams, finderArgs, this);
1208        }
1209
1210        if (result == null) {
1211            Session session = null;
1212
1213            try {
1214                session = openSession();
1215
1216                StringBuilder query = new StringBuilder();
1217
1218                query.append("SELECT COUNT(*) ");
1219                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1220
1221                query.append("groupId = ?");
1222
1223                query.append(" AND ");
1224
1225                if (portletId == null) {
1226                    query.append("portletId IS NULL");
1227                }
1228                else {
1229                    query.append("portletId = ?");
1230                }
1231
1232                query.append(" AND ");
1233
1234                query.append("classNameId = ?");
1235
1236                query.append(" ");
1237
1238                Query q = session.createQuery(query.toString());
1239
1240                QueryPos qPos = QueryPos.getInstance(q);
1241
1242                qPos.add(groupId);
1243
1244                if (portletId != null) {
1245                    qPos.add(portletId);
1246                }
1247
1248                qPos.add(classNameId);
1249
1250                Long count = null;
1251
1252                Iterator<Long> itr = q.list().iterator();
1253
1254                if (itr.hasNext()) {
1255                    count = itr.next();
1256                }
1257
1258                if (count == null) {
1259                    count = new Long(0);
1260                }
1261
1262                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1263                    finderClassName, finderMethodName, finderParams,
1264                    finderArgs, count);
1265
1266                return count.intValue();
1267            }
1268            catch (Exception e) {
1269                throw processException(e);
1270            }
1271            finally {
1272                closeSession(session);
1273            }
1274        }
1275        else {
1276            return ((Long)result).intValue();
1277        }
1278    }
1279
1280    public int countByG_N_P_C(long groupId, String name, String portletId,
1281        long classNameId) throws SystemException {
1282        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1283        String finderClassName = PortletItem.class.getName();
1284        String finderMethodName = "countByG_N_P_C";
1285        String[] finderParams = new String[] {
1286                Long.class.getName(), String.class.getName(),
1287                String.class.getName(), Long.class.getName()
1288            };
1289        Object[] finderArgs = new Object[] {
1290                new Long(groupId),
1291                
1292                name,
1293                
1294                portletId, new Long(classNameId)
1295            };
1296
1297        Object result = null;
1298
1299        if (finderClassNameCacheEnabled) {
1300            result = FinderCacheUtil.getResult(finderClassName,
1301                    finderMethodName, finderParams, finderArgs, this);
1302        }
1303
1304        if (result == null) {
1305            Session session = null;
1306
1307            try {
1308                session = openSession();
1309
1310                StringBuilder query = new StringBuilder();
1311
1312                query.append("SELECT COUNT(*) ");
1313                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1314
1315                query.append("groupId = ?");
1316
1317                query.append(" AND ");
1318
1319                if (name == null) {
1320                    query.append("name IS NULL");
1321                }
1322                else {
1323                    query.append("lower(name) = ?");
1324                }
1325
1326                query.append(" AND ");
1327
1328                if (portletId == null) {
1329                    query.append("portletId IS NULL");
1330                }
1331                else {
1332                    query.append("portletId = ?");
1333                }
1334
1335                query.append(" AND ");
1336
1337                query.append("classNameId = ?");
1338
1339                query.append(" ");
1340
1341                Query q = session.createQuery(query.toString());
1342
1343                QueryPos qPos = QueryPos.getInstance(q);
1344
1345                qPos.add(groupId);
1346
1347                if (name != null) {
1348                    qPos.add(name);
1349                }
1350
1351                if (portletId != null) {
1352                    qPos.add(portletId);
1353                }
1354
1355                qPos.add(classNameId);
1356
1357                Long count = null;
1358
1359                Iterator<Long> itr = q.list().iterator();
1360
1361                if (itr.hasNext()) {
1362                    count = itr.next();
1363                }
1364
1365                if (count == null) {
1366                    count = new Long(0);
1367                }
1368
1369                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1370                    finderClassName, finderMethodName, finderParams,
1371                    finderArgs, count);
1372
1373                return count.intValue();
1374            }
1375            catch (Exception e) {
1376                throw processException(e);
1377            }
1378            finally {
1379                closeSession(session);
1380            }
1381        }
1382        else {
1383            return ((Long)result).intValue();
1384        }
1385    }
1386
1387    public int countAll() throws SystemException {
1388        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1389        String finderClassName = PortletItem.class.getName();
1390        String finderMethodName = "countAll";
1391        String[] finderParams = new String[] {  };
1392        Object[] finderArgs = new Object[] {  };
1393
1394        Object result = null;
1395
1396        if (finderClassNameCacheEnabled) {
1397            result = FinderCacheUtil.getResult(finderClassName,
1398                    finderMethodName, finderParams, finderArgs, this);
1399        }
1400
1401        if (result == null) {
1402            Session session = null;
1403
1404            try {
1405                session = openSession();
1406
1407                Query q = session.createQuery(
1408                        "SELECT COUNT(*) FROM com.liferay.portal.model.PortletItem");
1409
1410                Long count = null;
1411
1412                Iterator<Long> itr = q.list().iterator();
1413
1414                if (itr.hasNext()) {
1415                    count = itr.next();
1416                }
1417
1418                if (count == null) {
1419                    count = new Long(0);
1420                }
1421
1422                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1423                    finderClassName, finderMethodName, finderParams,
1424                    finderArgs, count);
1425
1426                return count.intValue();
1427            }
1428            catch (Exception e) {
1429                throw processException(e);
1430            }
1431            finally {
1432                closeSession(session);
1433            }
1434        }
1435        else {
1436            return ((Long)result).intValue();
1437        }
1438    }
1439
1440    public void registerListener(ModelListener listener) {
1441        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1442
1443        listeners.add(listener);
1444
1445        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1446    }
1447
1448    public void unregisterListener(ModelListener listener) {
1449        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1450
1451        listeners.remove(listener);
1452
1453        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1454    }
1455
1456    public void afterPropertiesSet() {
1457        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1458                    com.liferay.portal.util.PropsUtil.get(
1459                        "value.object.listener.com.liferay.portal.model.PortletItem")));
1460
1461        if (listenerClassNames.length > 0) {
1462            try {
1463                List<ModelListener> listeners = new ArrayList<ModelListener>();
1464
1465                for (String listenerClassName : listenerClassNames) {
1466                    listeners.add((ModelListener)Class.forName(
1467                            listenerClassName).newInstance());
1468                }
1469
1470                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1471            }
1472            catch (Exception e) {
1473                _log.error(e);
1474            }
1475        }
1476    }
1477
1478    private static Log _log = LogFactory.getLog(PortletItemPersistenceImpl.class);
1479    private ModelListener[] _listeners = new ModelListener[0];
1480}