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