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