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             if (BatchSessionUtil.isEnabled()) {
130                 Object staleObject = session.get(PortletItemImpl.class,
131                         portletItem.getPrimaryKeyObj());
132 
133                 if (staleObject != null) {
134                     session.evict(staleObject);
135                 }
136             }
137 
138             session.delete(portletItem);
139 
140             session.flush();
141 
142             return portletItem;
143         }
144         catch (Exception e) {
145             throw processException(e);
146         }
147         finally {
148             closeSession(session);
149 
150             FinderCacheUtil.clearCache(PortletItem.class.getName());
151         }
152     }
153 
154     /**
155      * @deprecated Use <code>update(PortletItem portletItem, boolean merge)</code>.
156      */
157     public PortletItem update(PortletItem portletItem)
158         throws SystemException {
159         if (_log.isWarnEnabled()) {
160             _log.warn(
161                 "Using the deprecated update(PortletItem portletItem) method. Use update(PortletItem portletItem, boolean merge) instead.");
162         }
163 
164         return update(portletItem, false);
165     }
166 
167     /**
168      * Add, update, or merge, the entity. This method also calls the model
169      * listeners to trigger the proper events associated with adding, deleting,
170      * or updating an entity.
171      *
172      * @param        portletItem the entity to add, update, or merge
173      * @param        merge boolean value for whether to merge the entity. The
174      *                default value is false. Setting merge to true is more
175      *                expensive and should only be true when portletItem is
176      *                transient. See LEP-5473 for a detailed discussion of this
177      *                method.
178      * @return        true if the portlet can be displayed via Ajax
179      */
180     public PortletItem update(PortletItem portletItem, boolean merge)
181         throws SystemException {
182         boolean isNew = portletItem.isNew();
183 
184         if (_listeners.length > 0) {
185             for (ModelListener listener : _listeners) {
186                 if (isNew) {
187                     listener.onBeforeCreate(portletItem);
188                 }
189                 else {
190                     listener.onBeforeUpdate(portletItem);
191                 }
192             }
193         }
194 
195         portletItem = updateImpl(portletItem, merge);
196 
197         if (_listeners.length > 0) {
198             for (ModelListener listener : _listeners) {
199                 if (isNew) {
200                     listener.onAfterCreate(portletItem);
201                 }
202                 else {
203                     listener.onAfterUpdate(portletItem);
204                 }
205             }
206         }
207 
208         return portletItem;
209     }
210 
211     public PortletItem updateImpl(
212         com.liferay.portal.model.PortletItem portletItem, boolean merge)
213         throws SystemException {
214         Session session = null;
215 
216         try {
217             session = openSession();
218 
219             BatchSessionUtil.update(session, portletItem, merge);
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 = null;
1058
1059                if (obc == null) {
1060                    list = (List<PortletItem>)QueryUtil.list(q, getDialect(),
1061                            start, end, false);
1062
1063                    Collections.sort(list);
1064                }
1065                else {
1066                    list = (List<PortletItem>)QueryUtil.list(q, getDialect(),
1067                            start, end);
1068                }
1069
1070                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1071                    finderClassName, finderMethodName, finderParams,
1072                    finderArgs, list);
1073
1074                return list;
1075            }
1076            catch (Exception e) {
1077                throw processException(e);
1078            }
1079            finally {
1080                closeSession(session);
1081            }
1082        }
1083        else {
1084            return (List<PortletItem>)result;
1085        }
1086    }
1087
1088    public void removeByG_C(long groupId, long classNameId)
1089        throws SystemException {
1090        for (PortletItem portletItem : findByG_C(groupId, classNameId)) {
1091            remove(portletItem);
1092        }
1093    }
1094
1095    public void removeByG_P_C(long groupId, String portletId, long classNameId)
1096        throws SystemException {
1097        for (PortletItem portletItem : findByG_P_C(groupId, portletId,
1098                classNameId)) {
1099            remove(portletItem);
1100        }
1101    }
1102
1103    public void removeByG_N_P_C(long groupId, String name, String portletId,
1104        long classNameId) throws NoSuchPortletItemException, SystemException {
1105        PortletItem portletItem = findByG_N_P_C(groupId, name, portletId,
1106                classNameId);
1107
1108        remove(portletItem);
1109    }
1110
1111    public void removeAll() throws SystemException {
1112        for (PortletItem portletItem : findAll()) {
1113            remove(portletItem);
1114        }
1115    }
1116
1117    public int countByG_C(long groupId, long classNameId)
1118        throws SystemException {
1119        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1120        String finderClassName = PortletItem.class.getName();
1121        String finderMethodName = "countByG_C";
1122        String[] finderParams = new String[] {
1123                Long.class.getName(), Long.class.getName()
1124            };
1125        Object[] finderArgs = new Object[] {
1126                new Long(groupId), new Long(classNameId)
1127            };
1128
1129        Object result = null;
1130
1131        if (finderClassNameCacheEnabled) {
1132            result = FinderCacheUtil.getResult(finderClassName,
1133                    finderMethodName, finderParams, finderArgs, this);
1134        }
1135
1136        if (result == null) {
1137            Session session = null;
1138
1139            try {
1140                session = openSession();
1141
1142                StringBuilder query = new StringBuilder();
1143
1144                query.append("SELECT COUNT(*) ");
1145                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1146
1147                query.append("groupId = ?");
1148
1149                query.append(" AND ");
1150
1151                query.append("classNameId = ?");
1152
1153                query.append(" ");
1154
1155                Query q = session.createQuery(query.toString());
1156
1157                QueryPos qPos = QueryPos.getInstance(q);
1158
1159                qPos.add(groupId);
1160
1161                qPos.add(classNameId);
1162
1163                Long count = null;
1164
1165                Iterator<Long> itr = q.list().iterator();
1166
1167                if (itr.hasNext()) {
1168                    count = itr.next();
1169                }
1170
1171                if (count == null) {
1172                    count = new Long(0);
1173                }
1174
1175                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1176                    finderClassName, finderMethodName, finderParams,
1177                    finderArgs, count);
1178
1179                return count.intValue();
1180            }
1181            catch (Exception e) {
1182                throw processException(e);
1183            }
1184            finally {
1185                closeSession(session);
1186            }
1187        }
1188        else {
1189            return ((Long)result).intValue();
1190        }
1191    }
1192
1193    public int countByG_P_C(long groupId, String portletId, long classNameId)
1194        throws SystemException {
1195        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1196        String finderClassName = PortletItem.class.getName();
1197        String finderMethodName = "countByG_P_C";
1198        String[] finderParams = new String[] {
1199                Long.class.getName(), String.class.getName(),
1200                Long.class.getName()
1201            };
1202        Object[] finderArgs = new Object[] {
1203                new Long(groupId),
1204                
1205                portletId, new Long(classNameId)
1206            };
1207
1208        Object result = null;
1209
1210        if (finderClassNameCacheEnabled) {
1211            result = FinderCacheUtil.getResult(finderClassName,
1212                    finderMethodName, finderParams, finderArgs, this);
1213        }
1214
1215        if (result == null) {
1216            Session session = null;
1217
1218            try {
1219                session = openSession();
1220
1221                StringBuilder query = new StringBuilder();
1222
1223                query.append("SELECT COUNT(*) ");
1224                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1225
1226                query.append("groupId = ?");
1227
1228                query.append(" AND ");
1229
1230                if (portletId == null) {
1231                    query.append("portletId IS NULL");
1232                }
1233                else {
1234                    query.append("portletId = ?");
1235                }
1236
1237                query.append(" AND ");
1238
1239                query.append("classNameId = ?");
1240
1241                query.append(" ");
1242
1243                Query q = session.createQuery(query.toString());
1244
1245                QueryPos qPos = QueryPos.getInstance(q);
1246
1247                qPos.add(groupId);
1248
1249                if (portletId != null) {
1250                    qPos.add(portletId);
1251                }
1252
1253                qPos.add(classNameId);
1254
1255                Long count = null;
1256
1257                Iterator<Long> itr = q.list().iterator();
1258
1259                if (itr.hasNext()) {
1260                    count = itr.next();
1261                }
1262
1263                if (count == null) {
1264                    count = new Long(0);
1265                }
1266
1267                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1268                    finderClassName, finderMethodName, finderParams,
1269                    finderArgs, count);
1270
1271                return count.intValue();
1272            }
1273            catch (Exception e) {
1274                throw processException(e);
1275            }
1276            finally {
1277                closeSession(session);
1278            }
1279        }
1280        else {
1281            return ((Long)result).intValue();
1282        }
1283    }
1284
1285    public int countByG_N_P_C(long groupId, String name, String portletId,
1286        long classNameId) throws SystemException {
1287        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1288        String finderClassName = PortletItem.class.getName();
1289        String finderMethodName = "countByG_N_P_C";
1290        String[] finderParams = new String[] {
1291                Long.class.getName(), String.class.getName(),
1292                String.class.getName(), Long.class.getName()
1293            };
1294        Object[] finderArgs = new Object[] {
1295                new Long(groupId),
1296                
1297                name,
1298                
1299                portletId, new Long(classNameId)
1300            };
1301
1302        Object result = null;
1303
1304        if (finderClassNameCacheEnabled) {
1305            result = FinderCacheUtil.getResult(finderClassName,
1306                    finderMethodName, finderParams, finderArgs, this);
1307        }
1308
1309        if (result == null) {
1310            Session session = null;
1311
1312            try {
1313                session = openSession();
1314
1315                StringBuilder query = new StringBuilder();
1316
1317                query.append("SELECT COUNT(*) ");
1318                query.append("FROM com.liferay.portal.model.PortletItem WHERE ");
1319
1320                query.append("groupId = ?");
1321
1322                query.append(" AND ");
1323
1324                if (name == null) {
1325                    query.append("name IS NULL");
1326                }
1327                else {
1328                    query.append("lower(name) = ?");
1329                }
1330
1331                query.append(" AND ");
1332
1333                if (portletId == null) {
1334                    query.append("portletId IS NULL");
1335                }
1336                else {
1337                    query.append("portletId = ?");
1338                }
1339
1340                query.append(" AND ");
1341
1342                query.append("classNameId = ?");
1343
1344                query.append(" ");
1345
1346                Query q = session.createQuery(query.toString());
1347
1348                QueryPos qPos = QueryPos.getInstance(q);
1349
1350                qPos.add(groupId);
1351
1352                if (name != null) {
1353                    qPos.add(name);
1354                }
1355
1356                if (portletId != null) {
1357                    qPos.add(portletId);
1358                }
1359
1360                qPos.add(classNameId);
1361
1362                Long count = null;
1363
1364                Iterator<Long> itr = q.list().iterator();
1365
1366                if (itr.hasNext()) {
1367                    count = itr.next();
1368                }
1369
1370                if (count == null) {
1371                    count = new Long(0);
1372                }
1373
1374                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1375                    finderClassName, finderMethodName, finderParams,
1376                    finderArgs, count);
1377
1378                return count.intValue();
1379            }
1380            catch (Exception e) {
1381                throw processException(e);
1382            }
1383            finally {
1384                closeSession(session);
1385            }
1386        }
1387        else {
1388            return ((Long)result).intValue();
1389        }
1390    }
1391
1392    public int countAll() throws SystemException {
1393        boolean finderClassNameCacheEnabled = PortletItemModelImpl.CACHE_ENABLED;
1394        String finderClassName = PortletItem.class.getName();
1395        String finderMethodName = "countAll";
1396        String[] finderParams = new String[] {  };
1397        Object[] finderArgs = new Object[] {  };
1398
1399        Object result = null;
1400
1401        if (finderClassNameCacheEnabled) {
1402            result = FinderCacheUtil.getResult(finderClassName,
1403                    finderMethodName, finderParams, finderArgs, this);
1404        }
1405
1406        if (result == null) {
1407            Session session = null;
1408
1409            try {
1410                session = openSession();
1411
1412                Query q = session.createQuery(
1413                        "SELECT COUNT(*) FROM com.liferay.portal.model.PortletItem");
1414
1415                Long count = null;
1416
1417                Iterator<Long> itr = q.list().iterator();
1418
1419                if (itr.hasNext()) {
1420                    count = itr.next();
1421                }
1422
1423                if (count == null) {
1424                    count = new Long(0);
1425                }
1426
1427                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1428                    finderClassName, finderMethodName, finderParams,
1429                    finderArgs, count);
1430
1431                return count.intValue();
1432            }
1433            catch (Exception e) {
1434                throw processException(e);
1435            }
1436            finally {
1437                closeSession(session);
1438            }
1439        }
1440        else {
1441            return ((Long)result).intValue();
1442        }
1443    }
1444
1445    public void registerListener(ModelListener listener) {
1446        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1447
1448        listeners.add(listener);
1449
1450        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1451    }
1452
1453    public void unregisterListener(ModelListener listener) {
1454        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1455
1456        listeners.remove(listener);
1457
1458        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1459    }
1460
1461    public void afterPropertiesSet() {
1462        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1463                    com.liferay.portal.util.PropsUtil.get(
1464                        "value.object.listener.com.liferay.portal.model.PortletItem")));
1465
1466        if (listenerClassNames.length > 0) {
1467            try {
1468                List<ModelListener> listeners = new ArrayList<ModelListener>();
1469
1470                for (String listenerClassName : listenerClassNames) {
1471                    listeners.add((ModelListener)Class.forName(
1472                            listenerClassName).newInstance());
1473                }
1474
1475                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1476            }
1477            catch (Exception e) {
1478                _log.error(e);
1479            }
1480        }
1481    }
1482
1483    private static Log _log = LogFactory.getLog(PortletItemPersistenceImpl.class);
1484    private ModelListener[] _listeners = new ModelListener[0];
1485}