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.NoSuchLayoutSetException;
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.LayoutSet;
35  import com.liferay.portal.model.ModelListener;
36  import com.liferay.portal.model.impl.LayoutSetImpl;
37  import com.liferay.portal.model.impl.LayoutSetModelImpl;
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="LayoutSetPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class LayoutSetPersistenceImpl extends BasePersistence
62      implements LayoutSetPersistence {
63      public LayoutSet create(long layoutSetId) {
64          LayoutSet layoutSet = new LayoutSetImpl();
65  
66          layoutSet.setNew(true);
67          layoutSet.setPrimaryKey(layoutSetId);
68  
69          return layoutSet;
70      }
71  
72      public LayoutSet remove(long layoutSetId)
73          throws NoSuchLayoutSetException, SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              LayoutSet layoutSet = (LayoutSet)session.get(LayoutSetImpl.class,
80                      new Long(layoutSetId));
81  
82              if (layoutSet == null) {
83                  if (_log.isWarnEnabled()) {
84                      _log.warn("No LayoutSet exists with the primary key " +
85                          layoutSetId);
86                  }
87  
88                  throw new NoSuchLayoutSetException(
89                      "No LayoutSet exists with the primary key " + layoutSetId);
90              }
91  
92              return remove(layoutSet);
93          }
94          catch (NoSuchLayoutSetException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw HibernateUtil.processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public LayoutSet remove(LayoutSet layoutSet) throws SystemException {
106         if (_listeners != null) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(layoutSet);
109             }
110         }
111 
112         layoutSet = removeImpl(layoutSet);
113 
114         if (_listeners != null) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(layoutSet);
117             }
118         }
119 
120         return layoutSet;
121     }
122 
123     protected LayoutSet removeImpl(LayoutSet layoutSet)
124         throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             session.delete(layoutSet);
131 
132             session.flush();
133 
134             return layoutSet;
135         }
136         catch (Exception e) {
137             throw HibernateUtil.processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCache.clearCache(LayoutSet.class.getName());
143         }
144     }
145 
146     /**
147      * @deprecated Use <code>update(LayoutSet layoutSet, boolean merge)</code>.
148      */
149     public LayoutSet update(LayoutSet layoutSet) throws SystemException {
150         if (_log.isWarnEnabled()) {
151             _log.warn(
152                 "Using the deprecated update(LayoutSet layoutSet) method. Use update(LayoutSet layoutSet, boolean merge) instead.");
153         }
154 
155         return update(layoutSet, false);
156     }
157 
158     /**
159      * Add, update, or merge, the entity. This method also calls the model
160      * listeners to trigger the proper events associated with adding, deleting,
161      * or updating an entity.
162      *
163      * @param        layoutSet the entity to add, update, or merge
164      * @param        merge boolean value for whether to merge the entity. The
165      *                default value is false. Setting merge to true is more
166      *                expensive and should only be true when layoutSet is
167      *                transient. See LEP-5473 for a detailed discussion of this
168      *                method.
169      * @return        true if the portlet can be displayed via Ajax
170      */
171     public LayoutSet update(LayoutSet layoutSet, boolean merge)
172         throws SystemException {
173         boolean isNew = layoutSet.isNew();
174 
175         if (_listeners != null) {
176             for (ModelListener listener : _listeners) {
177                 if (isNew) {
178                     listener.onBeforeCreate(layoutSet);
179                 }
180                 else {
181                     listener.onBeforeUpdate(layoutSet);
182                 }
183             }
184         }
185 
186         layoutSet = updateImpl(layoutSet, merge);
187 
188         if (_listeners != null) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onAfterCreate(layoutSet);
192                 }
193                 else {
194                     listener.onAfterUpdate(layoutSet);
195                 }
196             }
197         }
198 
199         return layoutSet;
200     }
201 
202     public LayoutSet updateImpl(com.liferay.portal.model.LayoutSet layoutSet,
203         boolean merge) throws SystemException {
204         Session session = null;
205 
206         try {
207             session = openSession();
208 
209             if (merge) {
210                 session.merge(layoutSet);
211             }
212             else {
213                 if (layoutSet.isNew()) {
214                     session.save(layoutSet);
215                 }
216             }
217 
218             session.flush();
219 
220             layoutSet.setNew(false);
221 
222             return layoutSet;
223         }
224         catch (Exception e) {
225             throw HibernateUtil.processException(e);
226         }
227         finally {
228             closeSession(session);
229 
230             FinderCache.clearCache(LayoutSet.class.getName());
231         }
232     }
233 
234     public LayoutSet findByPrimaryKey(long layoutSetId)
235         throws NoSuchLayoutSetException, SystemException {
236         LayoutSet layoutSet = fetchByPrimaryKey(layoutSetId);
237 
238         if (layoutSet == null) {
239             if (_log.isWarnEnabled()) {
240                 _log.warn("No LayoutSet exists with the primary key " +
241                     layoutSetId);
242             }
243 
244             throw new NoSuchLayoutSetException(
245                 "No LayoutSet exists with the primary key " + layoutSetId);
246         }
247 
248         return layoutSet;
249     }
250 
251     public LayoutSet fetchByPrimaryKey(long layoutSetId)
252         throws SystemException {
253         Session session = null;
254 
255         try {
256             session = openSession();
257 
258             return (LayoutSet)session.get(LayoutSetImpl.class,
259                 new Long(layoutSetId));
260         }
261         catch (Exception e) {
262             throw HibernateUtil.processException(e);
263         }
264         finally {
265             closeSession(session);
266         }
267     }
268 
269     public List<LayoutSet> findByGroupId(long groupId)
270         throws SystemException {
271         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
272         String finderClassName = LayoutSet.class.getName();
273         String finderMethodName = "findByGroupId";
274         String[] finderParams = new String[] { Long.class.getName() };
275         Object[] finderArgs = new Object[] { new Long(groupId) };
276 
277         Object result = null;
278 
279         if (finderClassNameCacheEnabled) {
280             result = FinderCache.getResult(finderClassName, finderMethodName,
281                     finderParams, finderArgs, getSessionFactory());
282         }
283 
284         if (result == null) {
285             Session session = null;
286 
287             try {
288                 session = openSession();
289 
290                 StringMaker query = new StringMaker();
291 
292                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
293 
294                 query.append("groupId = ?");
295 
296                 query.append(" ");
297 
298                 Query q = session.createQuery(query.toString());
299 
300                 int queryPos = 0;
301 
302                 q.setLong(queryPos++, groupId);
303 
304                 List<LayoutSet> list = q.list();
305 
306                 FinderCache.putResult(finderClassNameCacheEnabled,
307                     finderClassName, finderMethodName, finderParams,
308                     finderArgs, list);
309 
310                 return list;
311             }
312             catch (Exception e) {
313                 throw HibernateUtil.processException(e);
314             }
315             finally {
316                 closeSession(session);
317             }
318         }
319         else {
320             return (List<LayoutSet>)result;
321         }
322     }
323 
324     public List<LayoutSet> findByGroupId(long groupId, int begin, int end)
325         throws SystemException {
326         return findByGroupId(groupId, begin, end, null);
327     }
328 
329     public List<LayoutSet> findByGroupId(long groupId, int begin, int end,
330         OrderByComparator obc) throws SystemException {
331         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
332         String finderClassName = LayoutSet.class.getName();
333         String finderMethodName = "findByGroupId";
334         String[] finderParams = new String[] {
335                 Long.class.getName(),
336                 
337                 "java.lang.Integer", "java.lang.Integer",
338                 "com.liferay.portal.kernel.util.OrderByComparator"
339             };
340         Object[] finderArgs = new Object[] {
341                 new Long(groupId),
342                 
343                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
344             };
345 
346         Object result = null;
347 
348         if (finderClassNameCacheEnabled) {
349             result = FinderCache.getResult(finderClassName, finderMethodName,
350                     finderParams, finderArgs, getSessionFactory());
351         }
352 
353         if (result == null) {
354             Session session = null;
355 
356             try {
357                 session = openSession();
358 
359                 StringMaker query = new StringMaker();
360 
361                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
362 
363                 query.append("groupId = ?");
364 
365                 query.append(" ");
366 
367                 if (obc != null) {
368                     query.append("ORDER BY ");
369                     query.append(obc.getOrderBy());
370                 }
371 
372                 Query q = session.createQuery(query.toString());
373 
374                 int queryPos = 0;
375 
376                 q.setLong(queryPos++, groupId);
377 
378                 List<LayoutSet> list = (List<LayoutSet>)QueryUtil.list(q,
379                         getDialect(), begin, end);
380 
381                 FinderCache.putResult(finderClassNameCacheEnabled,
382                     finderClassName, finderMethodName, finderParams,
383                     finderArgs, list);
384 
385                 return list;
386             }
387             catch (Exception e) {
388                 throw HibernateUtil.processException(e);
389             }
390             finally {
391                 closeSession(session);
392             }
393         }
394         else {
395             return (List<LayoutSet>)result;
396         }
397     }
398 
399     public LayoutSet findByGroupId_First(long groupId, OrderByComparator obc)
400         throws NoSuchLayoutSetException, SystemException {
401         List<LayoutSet> list = findByGroupId(groupId, 0, 1, obc);
402 
403         if (list.size() == 0) {
404             StringMaker msg = new StringMaker();
405 
406             msg.append("No LayoutSet exists with the key {");
407 
408             msg.append("groupId=" + groupId);
409 
410             msg.append(StringPool.CLOSE_CURLY_BRACE);
411 
412             throw new NoSuchLayoutSetException(msg.toString());
413         }
414         else {
415             return list.get(0);
416         }
417     }
418 
419     public LayoutSet findByGroupId_Last(long groupId, OrderByComparator obc)
420         throws NoSuchLayoutSetException, SystemException {
421         int count = countByGroupId(groupId);
422 
423         List<LayoutSet> list = findByGroupId(groupId, count - 1, count, obc);
424 
425         if (list.size() == 0) {
426             StringMaker msg = new StringMaker();
427 
428             msg.append("No LayoutSet exists with the key {");
429 
430             msg.append("groupId=" + groupId);
431 
432             msg.append(StringPool.CLOSE_CURLY_BRACE);
433 
434             throw new NoSuchLayoutSetException(msg.toString());
435         }
436         else {
437             return list.get(0);
438         }
439     }
440 
441     public LayoutSet[] findByGroupId_PrevAndNext(long layoutSetId,
442         long groupId, OrderByComparator obc)
443         throws NoSuchLayoutSetException, SystemException {
444         LayoutSet layoutSet = findByPrimaryKey(layoutSetId);
445 
446         int count = countByGroupId(groupId);
447 
448         Session session = null;
449 
450         try {
451             session = openSession();
452 
453             StringMaker query = new StringMaker();
454 
455             query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
456 
457             query.append("groupId = ?");
458 
459             query.append(" ");
460 
461             if (obc != null) {
462                 query.append("ORDER BY ");
463                 query.append(obc.getOrderBy());
464             }
465 
466             Query q = session.createQuery(query.toString());
467 
468             int queryPos = 0;
469 
470             q.setLong(queryPos++, groupId);
471 
472             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
473                     layoutSet);
474 
475             LayoutSet[] array = new LayoutSetImpl[3];
476 
477             array[0] = (LayoutSet)objArray[0];
478             array[1] = (LayoutSet)objArray[1];
479             array[2] = (LayoutSet)objArray[2];
480 
481             return array;
482         }
483         catch (Exception e) {
484             throw HibernateUtil.processException(e);
485         }
486         finally {
487             closeSession(session);
488         }
489     }
490 
491     public LayoutSet findByVirtualHost(String virtualHost)
492         throws NoSuchLayoutSetException, SystemException {
493         LayoutSet layoutSet = fetchByVirtualHost(virtualHost);
494 
495         if (layoutSet == null) {
496             StringMaker msg = new StringMaker();
497 
498             msg.append("No LayoutSet exists with the key {");
499 
500             msg.append("virtualHost=" + virtualHost);
501 
502             msg.append(StringPool.CLOSE_CURLY_BRACE);
503 
504             if (_log.isWarnEnabled()) {
505                 _log.warn(msg.toString());
506             }
507 
508             throw new NoSuchLayoutSetException(msg.toString());
509         }
510 
511         return layoutSet;
512     }
513 
514     public LayoutSet fetchByVirtualHost(String virtualHost)
515         throws SystemException {
516         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
517         String finderClassName = LayoutSet.class.getName();
518         String finderMethodName = "fetchByVirtualHost";
519         String[] finderParams = new String[] { String.class.getName() };
520         Object[] finderArgs = new Object[] { virtualHost };
521 
522         Object result = null;
523 
524         if (finderClassNameCacheEnabled) {
525             result = FinderCache.getResult(finderClassName, finderMethodName,
526                     finderParams, finderArgs, getSessionFactory());
527         }
528 
529         if (result == null) {
530             Session session = null;
531 
532             try {
533                 session = openSession();
534 
535                 StringMaker query = new StringMaker();
536 
537                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
538 
539                 if (virtualHost == null) {
540                     query.append("virtualHost IS NULL");
541                 }
542                 else {
543                     query.append("virtualHost = ?");
544                 }
545 
546                 query.append(" ");
547 
548                 Query q = session.createQuery(query.toString());
549 
550                 int queryPos = 0;
551 
552                 if (virtualHost != null) {
553                     q.setString(queryPos++, virtualHost);
554                 }
555 
556                 List<LayoutSet> list = q.list();
557 
558                 FinderCache.putResult(finderClassNameCacheEnabled,
559                     finderClassName, finderMethodName, finderParams,
560                     finderArgs, list);
561 
562                 if (list.size() == 0) {
563                     return null;
564                 }
565                 else {
566                     return list.get(0);
567                 }
568             }
569             catch (Exception e) {
570                 throw HibernateUtil.processException(e);
571             }
572             finally {
573                 closeSession(session);
574             }
575         }
576         else {
577             List<LayoutSet> list = (List<LayoutSet>)result;
578 
579             if (list.size() == 0) {
580                 return null;
581             }
582             else {
583                 return list.get(0);
584             }
585         }
586     }
587 
588     public LayoutSet findByG_P(long groupId, boolean privateLayout)
589         throws NoSuchLayoutSetException, SystemException {
590         LayoutSet layoutSet = fetchByG_P(groupId, privateLayout);
591 
592         if (layoutSet == null) {
593             StringMaker msg = new StringMaker();
594 
595             msg.append("No LayoutSet exists with the key {");
596 
597             msg.append("groupId=" + groupId);
598 
599             msg.append(", ");
600             msg.append("privateLayout=" + privateLayout);
601 
602             msg.append(StringPool.CLOSE_CURLY_BRACE);
603 
604             if (_log.isWarnEnabled()) {
605                 _log.warn(msg.toString());
606             }
607 
608             throw new NoSuchLayoutSetException(msg.toString());
609         }
610 
611         return layoutSet;
612     }
613 
614     public LayoutSet fetchByG_P(long groupId, boolean privateLayout)
615         throws SystemException {
616         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
617         String finderClassName = LayoutSet.class.getName();
618         String finderMethodName = "fetchByG_P";
619         String[] finderParams = new String[] {
620                 Long.class.getName(), Boolean.class.getName()
621             };
622         Object[] finderArgs = new Object[] {
623                 new Long(groupId), Boolean.valueOf(privateLayout)
624             };
625 
626         Object result = null;
627 
628         if (finderClassNameCacheEnabled) {
629             result = FinderCache.getResult(finderClassName, finderMethodName,
630                     finderParams, finderArgs, getSessionFactory());
631         }
632 
633         if (result == null) {
634             Session session = null;
635 
636             try {
637                 session = openSession();
638 
639                 StringMaker query = new StringMaker();
640 
641                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
642 
643                 query.append("groupId = ?");
644 
645                 query.append(" AND ");
646 
647                 query.append("privateLayout = ?");
648 
649                 query.append(" ");
650 
651                 Query q = session.createQuery(query.toString());
652 
653                 int queryPos = 0;
654 
655                 q.setLong(queryPos++, groupId);
656 
657                 q.setBoolean(queryPos++, privateLayout);
658 
659                 List<LayoutSet> list = q.list();
660 
661                 FinderCache.putResult(finderClassNameCacheEnabled,
662                     finderClassName, finderMethodName, finderParams,
663                     finderArgs, list);
664 
665                 if (list.size() == 0) {
666                     return null;
667                 }
668                 else {
669                     return list.get(0);
670                 }
671             }
672             catch (Exception e) {
673                 throw HibernateUtil.processException(e);
674             }
675             finally {
676                 closeSession(session);
677             }
678         }
679         else {
680             List<LayoutSet> list = (List<LayoutSet>)result;
681 
682             if (list.size() == 0) {
683                 return null;
684             }
685             else {
686                 return list.get(0);
687             }
688         }
689     }
690 
691     public List<LayoutSet> findWithDynamicQuery(
692         DynamicQueryInitializer queryInitializer) throws SystemException {
693         Session session = null;
694 
695         try {
696             session = openSession();
697 
698             DynamicQuery query = queryInitializer.initialize(session);
699 
700             return query.list();
701         }
702         catch (Exception e) {
703             throw HibernateUtil.processException(e);
704         }
705         finally {
706             closeSession(session);
707         }
708     }
709 
710     public List<LayoutSet> findWithDynamicQuery(
711         DynamicQueryInitializer queryInitializer, int begin, int end)
712         throws SystemException {
713         Session session = null;
714 
715         try {
716             session = openSession();
717 
718             DynamicQuery query = queryInitializer.initialize(session);
719 
720             query.setLimit(begin, end);
721 
722             return query.list();
723         }
724         catch (Exception e) {
725             throw HibernateUtil.processException(e);
726         }
727         finally {
728             closeSession(session);
729         }
730     }
731 
732     public List<LayoutSet> findAll() throws SystemException {
733         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
734     }
735 
736     public List<LayoutSet> findAll(int begin, int end)
737         throws SystemException {
738         return findAll(begin, end, null);
739     }
740 
741     public List<LayoutSet> findAll(int begin, int end, OrderByComparator obc)
742         throws SystemException {
743         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
744         String finderClassName = LayoutSet.class.getName();
745         String finderMethodName = "findAll";
746         String[] finderParams = new String[] {
747                 "java.lang.Integer", "java.lang.Integer",
748                 "com.liferay.portal.kernel.util.OrderByComparator"
749             };
750         Object[] finderArgs = new Object[] {
751                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
752             };
753 
754         Object result = null;
755 
756         if (finderClassNameCacheEnabled) {
757             result = FinderCache.getResult(finderClassName, finderMethodName,
758                     finderParams, finderArgs, getSessionFactory());
759         }
760 
761         if (result == null) {
762             Session session = null;
763 
764             try {
765                 session = openSession();
766 
767                 StringMaker query = new StringMaker();
768 
769                 query.append("FROM com.liferay.portal.model.LayoutSet ");
770 
771                 if (obc != null) {
772                     query.append("ORDER BY ");
773                     query.append(obc.getOrderBy());
774                 }
775 
776                 Query q = session.createQuery(query.toString());
777 
778                 List<LayoutSet> list = (List<LayoutSet>)QueryUtil.list(q,
779                         getDialect(), begin, end);
780 
781                 if (obc == null) {
782                     Collections.sort(list);
783                 }
784 
785                 FinderCache.putResult(finderClassNameCacheEnabled,
786                     finderClassName, finderMethodName, finderParams,
787                     finderArgs, list);
788 
789                 return list;
790             }
791             catch (Exception e) {
792                 throw HibernateUtil.processException(e);
793             }
794             finally {
795                 closeSession(session);
796             }
797         }
798         else {
799             return (List<LayoutSet>)result;
800         }
801     }
802 
803     public void removeByGroupId(long groupId) throws SystemException {
804         for (LayoutSet layoutSet : findByGroupId(groupId)) {
805             remove(layoutSet);
806         }
807     }
808 
809     public void removeByVirtualHost(String virtualHost)
810         throws NoSuchLayoutSetException, SystemException {
811         LayoutSet layoutSet = findByVirtualHost(virtualHost);
812 
813         remove(layoutSet);
814     }
815 
816     public void removeByG_P(long groupId, boolean privateLayout)
817         throws NoSuchLayoutSetException, SystemException {
818         LayoutSet layoutSet = findByG_P(groupId, privateLayout);
819 
820         remove(layoutSet);
821     }
822 
823     public void removeAll() throws SystemException {
824         for (LayoutSet layoutSet : findAll()) {
825             remove(layoutSet);
826         }
827     }
828 
829     public int countByGroupId(long groupId) throws SystemException {
830         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
831         String finderClassName = LayoutSet.class.getName();
832         String finderMethodName = "countByGroupId";
833         String[] finderParams = new String[] { Long.class.getName() };
834         Object[] finderArgs = new Object[] { new Long(groupId) };
835 
836         Object result = null;
837 
838         if (finderClassNameCacheEnabled) {
839             result = FinderCache.getResult(finderClassName, finderMethodName,
840                     finderParams, finderArgs, getSessionFactory());
841         }
842 
843         if (result == null) {
844             Session session = null;
845 
846             try {
847                 session = openSession();
848 
849                 StringMaker query = new StringMaker();
850 
851                 query.append("SELECT COUNT(*) ");
852                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
853 
854                 query.append("groupId = ?");
855 
856                 query.append(" ");
857 
858                 Query q = session.createQuery(query.toString());
859 
860                 int queryPos = 0;
861 
862                 q.setLong(queryPos++, groupId);
863 
864                 Long count = null;
865 
866                 Iterator<Long> itr = q.list().iterator();
867 
868                 if (itr.hasNext()) {
869                     count = itr.next();
870                 }
871 
872                 if (count == null) {
873                     count = new Long(0);
874                 }
875 
876                 FinderCache.putResult(finderClassNameCacheEnabled,
877                     finderClassName, finderMethodName, finderParams,
878                     finderArgs, count);
879 
880                 return count.intValue();
881             }
882             catch (Exception e) {
883                 throw HibernateUtil.processException(e);
884             }
885             finally {
886                 closeSession(session);
887             }
888         }
889         else {
890             return ((Long)result).intValue();
891         }
892     }
893 
894     public int countByVirtualHost(String virtualHost) throws SystemException {
895         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
896         String finderClassName = LayoutSet.class.getName();
897         String finderMethodName = "countByVirtualHost";
898         String[] finderParams = new String[] { String.class.getName() };
899         Object[] finderArgs = new Object[] { virtualHost };
900 
901         Object result = null;
902 
903         if (finderClassNameCacheEnabled) {
904             result = FinderCache.getResult(finderClassName, finderMethodName,
905                     finderParams, finderArgs, getSessionFactory());
906         }
907 
908         if (result == null) {
909             Session session = null;
910 
911             try {
912                 session = openSession();
913 
914                 StringMaker query = new StringMaker();
915 
916                 query.append("SELECT COUNT(*) ");
917                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
918 
919                 if (virtualHost == null) {
920                     query.append("virtualHost IS NULL");
921                 }
922                 else {
923                     query.append("virtualHost = ?");
924                 }
925 
926                 query.append(" ");
927 
928                 Query q = session.createQuery(query.toString());
929 
930                 int queryPos = 0;
931 
932                 if (virtualHost != null) {
933                     q.setString(queryPos++, virtualHost);
934                 }
935 
936                 Long count = null;
937 
938                 Iterator<Long> itr = q.list().iterator();
939 
940                 if (itr.hasNext()) {
941                     count = itr.next();
942                 }
943 
944                 if (count == null) {
945                     count = new Long(0);
946                 }
947 
948                 FinderCache.putResult(finderClassNameCacheEnabled,
949                     finderClassName, finderMethodName, finderParams,
950                     finderArgs, count);
951 
952                 return count.intValue();
953             }
954             catch (Exception e) {
955                 throw HibernateUtil.processException(e);
956             }
957             finally {
958                 closeSession(session);
959             }
960         }
961         else {
962             return ((Long)result).intValue();
963         }
964     }
965 
966     public int countByG_P(long groupId, boolean privateLayout)
967         throws SystemException {
968         boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
969         String finderClassName = LayoutSet.class.getName();
970         String finderMethodName = "countByG_P";
971         String[] finderParams = new String[] {
972                 Long.class.getName(), Boolean.class.getName()
973             };
974         Object[] finderArgs = new Object[] {
975                 new Long(groupId), Boolean.valueOf(privateLayout)
976             };
977 
978         Object result = null;
979 
980         if (finderClassNameCacheEnabled) {
981             result = FinderCache.getResult(finderClassName, finderMethodName,
982                     finderParams, finderArgs, getSessionFactory());
983         }
984 
985         if (result == null) {
986             Session session = null;
987 
988             try {
989                 session = openSession();
990 
991                 StringMaker query = new StringMaker();
992 
993                 query.append("SELECT COUNT(*) ");
994                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
995 
996                 query.append("groupId = ?");
997 
998                 query.append(" AND ");
999 
1000                query.append("privateLayout = ?");
1001
1002                query.append(" ");
1003
1004                Query q = session.createQuery(query.toString());
1005
1006                int queryPos = 0;
1007
1008                q.setLong(queryPos++, groupId);
1009
1010                q.setBoolean(queryPos++, privateLayout);
1011
1012                Long count = null;
1013
1014                Iterator<Long> itr = q.list().iterator();
1015
1016                if (itr.hasNext()) {
1017                    count = itr.next();
1018                }
1019
1020                if (count == null) {
1021                    count = new Long(0);
1022                }
1023
1024                FinderCache.putResult(finderClassNameCacheEnabled,
1025                    finderClassName, finderMethodName, finderParams,
1026                    finderArgs, count);
1027
1028                return count.intValue();
1029            }
1030            catch (Exception e) {
1031                throw HibernateUtil.processException(e);
1032            }
1033            finally {
1034                closeSession(session);
1035            }
1036        }
1037        else {
1038            return ((Long)result).intValue();
1039        }
1040    }
1041
1042    public int countAll() throws SystemException {
1043        boolean finderClassNameCacheEnabled = LayoutSetModelImpl.CACHE_ENABLED;
1044        String finderClassName = LayoutSet.class.getName();
1045        String finderMethodName = "countAll";
1046        String[] finderParams = new String[] {  };
1047        Object[] finderArgs = new Object[] {  };
1048
1049        Object result = null;
1050
1051        if (finderClassNameCacheEnabled) {
1052            result = FinderCache.getResult(finderClassName, finderMethodName,
1053                    finderParams, finderArgs, getSessionFactory());
1054        }
1055
1056        if (result == null) {
1057            Session session = null;
1058
1059            try {
1060                session = openSession();
1061
1062                Query q = session.createQuery(
1063                        "SELECT COUNT(*) FROM com.liferay.portal.model.LayoutSet");
1064
1065                Long count = null;
1066
1067                Iterator<Long> itr = q.list().iterator();
1068
1069                if (itr.hasNext()) {
1070                    count = itr.next();
1071                }
1072
1073                if (count == null) {
1074                    count = new Long(0);
1075                }
1076
1077                FinderCache.putResult(finderClassNameCacheEnabled,
1078                    finderClassName, finderMethodName, finderParams,
1079                    finderArgs, count);
1080
1081                return count.intValue();
1082            }
1083            catch (Exception e) {
1084                throw HibernateUtil.processException(e);
1085            }
1086            finally {
1087                closeSession(session);
1088            }
1089        }
1090        else {
1091            return ((Long)result).intValue();
1092        }
1093    }
1094
1095    protected void initDao() {
1096        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1097                    PropsUtil.get(
1098                        "value.object.listener.com.liferay.portal.model.LayoutSet")));
1099
1100        if (listenerClassNames.length > 0) {
1101            try {
1102                List<ModelListener> listeners = new ArrayList<ModelListener>();
1103
1104                for (String listenerClassName : listenerClassNames) {
1105                    listeners.add((ModelListener)Class.forName(
1106                            listenerClassName).newInstance());
1107                }
1108
1109                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1110            }
1111            catch (Exception e) {
1112                _log.error(e);
1113            }
1114        }
1115    }
1116
1117    private static Log _log = LogFactory.getLog(LayoutSetPersistenceImpl.class);
1118    private ModelListener[] _listeners;
1119}