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