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