1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchLayoutSetException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.annotation.BeanReference;
28  import com.liferay.portal.kernel.cache.CacheRegistry;
29  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32  import com.liferay.portal.kernel.dao.orm.FinderPath;
33  import com.liferay.portal.kernel.dao.orm.Query;
34  import com.liferay.portal.kernel.dao.orm.QueryPos;
35  import com.liferay.portal.kernel.dao.orm.QueryUtil;
36  import com.liferay.portal.kernel.dao.orm.Session;
37  import com.liferay.portal.kernel.log.Log;
38  import com.liferay.portal.kernel.log.LogFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.StringPool;
42  import com.liferay.portal.kernel.util.StringUtil;
43  import com.liferay.portal.model.LayoutSet;
44  import com.liferay.portal.model.ModelListener;
45  import com.liferay.portal.model.impl.LayoutSetImpl;
46  import com.liferay.portal.model.impl.LayoutSetModelImpl;
47  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  /**
54   * <a href="LayoutSetPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class LayoutSetPersistenceImpl extends BasePersistenceImpl
60      implements LayoutSetPersistence {
61      public static final String FINDER_CLASS_NAME_ENTITY = LayoutSetImpl.class.getName();
62      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
63          ".List";
64      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
65              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
66              "findByGroupId", new String[] { Long.class.getName() });
67      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
68              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
69              "findByGroupId",
70              new String[] {
71                  Long.class.getName(),
72                  
73              "java.lang.Integer", "java.lang.Integer",
74                  "com.liferay.portal.kernel.util.OrderByComparator"
75              });
76      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
77              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
78              "countByGroupId", new String[] { Long.class.getName() });
79      public static final FinderPath FINDER_PATH_FETCH_BY_VIRTUALHOST = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
80              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
81              "fetchByVirtualHost", new String[] { String.class.getName() });
82      public static final FinderPath FINDER_PATH_COUNT_BY_VIRTUALHOST = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
83              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
84              "countByVirtualHost", new String[] { String.class.getName() });
85      public static final FinderPath FINDER_PATH_FETCH_BY_G_P = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
86              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
87              "fetchByG_P",
88              new String[] { Long.class.getName(), Boolean.class.getName() });
89      public static final FinderPath FINDER_PATH_COUNT_BY_G_P = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
90              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
91              "countByG_P",
92              new String[] { Long.class.getName(), Boolean.class.getName() });
93      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
94              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
95              "findAll", new String[0]);
96      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
97              LayoutSetModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
98              "countAll", new String[0]);
99  
100     public void cacheResult(LayoutSet layoutSet) {
101         EntityCacheUtil.putResult(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
102             LayoutSetImpl.class, layoutSet.getPrimaryKey(), layoutSet);
103 
104         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
105             new Object[] { layoutSet.getVirtualHost() }, layoutSet);
106 
107         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_P,
108             new Object[] {
109                 new Long(layoutSet.getGroupId()),
110                 Boolean.valueOf(layoutSet.getPrivateLayout())
111             }, layoutSet);
112     }
113 
114     public void cacheResult(List<LayoutSet> layoutSets) {
115         for (LayoutSet layoutSet : layoutSets) {
116             if (EntityCacheUtil.getResult(
117                         LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
118                         LayoutSetImpl.class, layoutSet.getPrimaryKey(), this) == null) {
119                 cacheResult(layoutSet);
120             }
121         }
122     }
123 
124     public void clearCache() {
125         CacheRegistry.clear(LayoutSetImpl.class.getName());
126         EntityCacheUtil.clearCache(LayoutSetImpl.class.getName());
127         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
128         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
129     }
130 
131     public LayoutSet create(long layoutSetId) {
132         LayoutSet layoutSet = new LayoutSetImpl();
133 
134         layoutSet.setNew(true);
135         layoutSet.setPrimaryKey(layoutSetId);
136 
137         return layoutSet;
138     }
139 
140     public LayoutSet remove(long layoutSetId)
141         throws NoSuchLayoutSetException, SystemException {
142         Session session = null;
143 
144         try {
145             session = openSession();
146 
147             LayoutSet layoutSet = (LayoutSet)session.get(LayoutSetImpl.class,
148                     new Long(layoutSetId));
149 
150             if (layoutSet == null) {
151                 if (_log.isWarnEnabled()) {
152                     _log.warn("No LayoutSet exists with the primary key " +
153                         layoutSetId);
154                 }
155 
156                 throw new NoSuchLayoutSetException(
157                     "No LayoutSet exists with the primary key " + layoutSetId);
158             }
159 
160             return remove(layoutSet);
161         }
162         catch (NoSuchLayoutSetException nsee) {
163             throw nsee;
164         }
165         catch (Exception e) {
166             throw processException(e);
167         }
168         finally {
169             closeSession(session);
170         }
171     }
172 
173     public LayoutSet remove(LayoutSet layoutSet) throws SystemException {
174         for (ModelListener<LayoutSet> listener : listeners) {
175             listener.onBeforeRemove(layoutSet);
176         }
177 
178         layoutSet = removeImpl(layoutSet);
179 
180         for (ModelListener<LayoutSet> listener : listeners) {
181             listener.onAfterRemove(layoutSet);
182         }
183 
184         return layoutSet;
185     }
186 
187     protected LayoutSet removeImpl(LayoutSet layoutSet)
188         throws SystemException {
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             if (layoutSet.isCachedModel() || BatchSessionUtil.isEnabled()) {
195                 Object staleObject = session.get(LayoutSetImpl.class,
196                         layoutSet.getPrimaryKeyObj());
197 
198                 if (staleObject != null) {
199                     session.evict(staleObject);
200                 }
201             }
202 
203             session.delete(layoutSet);
204 
205             session.flush();
206         }
207         catch (Exception e) {
208             throw processException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213 
214         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
215 
216         LayoutSetModelImpl layoutSetModelImpl = (LayoutSetModelImpl)layoutSet;
217 
218         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
219             new Object[] { layoutSetModelImpl.getOriginalVirtualHost() });
220 
221         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_P,
222             new Object[] {
223                 new Long(layoutSetModelImpl.getOriginalGroupId()),
224                 Boolean.valueOf(layoutSetModelImpl.getOriginalPrivateLayout())
225             });
226 
227         EntityCacheUtil.removeResult(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
228             LayoutSetImpl.class, layoutSet.getPrimaryKey());
229 
230         return layoutSet;
231     }
232 
233     /**
234      * @deprecated Use <code>update(LayoutSet layoutSet, boolean merge)</code>.
235      */
236     public LayoutSet update(LayoutSet layoutSet) throws SystemException {
237         if (_log.isWarnEnabled()) {
238             _log.warn(
239                 "Using the deprecated update(LayoutSet layoutSet) method. Use update(LayoutSet layoutSet, boolean merge) instead.");
240         }
241 
242         return update(layoutSet, false);
243     }
244 
245     /**
246      * Add, update, or merge, the entity. This method also calls the model
247      * listeners to trigger the proper events associated with adding, deleting,
248      * or updating an entity.
249      *
250      * @param        layoutSet the entity to add, update, or merge
251      * @param        merge boolean value for whether to merge the entity. The
252      *                default value is false. Setting merge to true is more
253      *                expensive and should only be true when layoutSet is
254      *                transient. See LEP-5473 for a detailed discussion of this
255      *                method.
256      * @return        true if the portlet can be displayed via Ajax
257      */
258     public LayoutSet update(LayoutSet layoutSet, boolean merge)
259         throws SystemException {
260         boolean isNew = layoutSet.isNew();
261 
262         for (ModelListener<LayoutSet> listener : listeners) {
263             if (isNew) {
264                 listener.onBeforeCreate(layoutSet);
265             }
266             else {
267                 listener.onBeforeUpdate(layoutSet);
268             }
269         }
270 
271         layoutSet = updateImpl(layoutSet, merge);
272 
273         for (ModelListener<LayoutSet> listener : listeners) {
274             if (isNew) {
275                 listener.onAfterCreate(layoutSet);
276             }
277             else {
278                 listener.onAfterUpdate(layoutSet);
279             }
280         }
281 
282         return layoutSet;
283     }
284 
285     public LayoutSet updateImpl(com.liferay.portal.model.LayoutSet layoutSet,
286         boolean merge) throws SystemException {
287         boolean isNew = layoutSet.isNew();
288 
289         LayoutSetModelImpl layoutSetModelImpl = (LayoutSetModelImpl)layoutSet;
290 
291         Session session = null;
292 
293         try {
294             session = openSession();
295 
296             BatchSessionUtil.update(session, layoutSet, merge);
297 
298             layoutSet.setNew(false);
299         }
300         catch (Exception e) {
301             throw processException(e);
302         }
303         finally {
304             closeSession(session);
305         }
306 
307         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
308 
309         EntityCacheUtil.putResult(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
310             LayoutSetImpl.class, layoutSet.getPrimaryKey(), layoutSet);
311 
312         if (!isNew &&
313                 (!layoutSet.getVirtualHost()
314                                .equals(layoutSetModelImpl.getOriginalVirtualHost()))) {
315             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
316                 new Object[] { layoutSetModelImpl.getOriginalVirtualHost() });
317         }
318 
319         if (isNew ||
320                 (!layoutSet.getVirtualHost()
321                                .equals(layoutSetModelImpl.getOriginalVirtualHost()))) {
322             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
323                 new Object[] { layoutSet.getVirtualHost() }, layoutSet);
324         }
325 
326         if (!isNew &&
327                 ((layoutSet.getGroupId() != layoutSetModelImpl.getOriginalGroupId()) ||
328                 (layoutSet.getPrivateLayout() != layoutSetModelImpl.getOriginalPrivateLayout()))) {
329             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_P,
330                 new Object[] {
331                     new Long(layoutSetModelImpl.getOriginalGroupId()),
332                     Boolean.valueOf(
333                         layoutSetModelImpl.getOriginalPrivateLayout())
334                 });
335         }
336 
337         if (isNew ||
338                 ((layoutSet.getGroupId() != layoutSetModelImpl.getOriginalGroupId()) ||
339                 (layoutSet.getPrivateLayout() != layoutSetModelImpl.getOriginalPrivateLayout()))) {
340             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_P,
341                 new Object[] {
342                     new Long(layoutSet.getGroupId()),
343                     Boolean.valueOf(layoutSet.getPrivateLayout())
344                 }, layoutSet);
345         }
346 
347         return layoutSet;
348     }
349 
350     public LayoutSet findByPrimaryKey(long layoutSetId)
351         throws NoSuchLayoutSetException, SystemException {
352         LayoutSet layoutSet = fetchByPrimaryKey(layoutSetId);
353 
354         if (layoutSet == null) {
355             if (_log.isWarnEnabled()) {
356                 _log.warn("No LayoutSet exists with the primary key " +
357                     layoutSetId);
358             }
359 
360             throw new NoSuchLayoutSetException(
361                 "No LayoutSet exists with the primary key " + layoutSetId);
362         }
363 
364         return layoutSet;
365     }
366 
367     public LayoutSet fetchByPrimaryKey(long layoutSetId)
368         throws SystemException {
369         LayoutSet layoutSet = (LayoutSet)EntityCacheUtil.getResult(LayoutSetModelImpl.ENTITY_CACHE_ENABLED,
370                 LayoutSetImpl.class, layoutSetId, this);
371 
372         if (layoutSet == null) {
373             Session session = null;
374 
375             try {
376                 session = openSession();
377 
378                 layoutSet = (LayoutSet)session.get(LayoutSetImpl.class,
379                         new Long(layoutSetId));
380             }
381             catch (Exception e) {
382                 throw processException(e);
383             }
384             finally {
385                 if (layoutSet != null) {
386                     cacheResult(layoutSet);
387                 }
388 
389                 closeSession(session);
390             }
391         }
392 
393         return layoutSet;
394     }
395 
396     public List<LayoutSet> findByGroupId(long groupId)
397         throws SystemException {
398         Object[] finderArgs = new Object[] { new Long(groupId) };
399 
400         List<LayoutSet> list = (List<LayoutSet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
401                 finderArgs, this);
402 
403         if (list == null) {
404             Session session = null;
405 
406             try {
407                 session = openSession();
408 
409                 StringBuilder query = new StringBuilder();
410 
411                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
412 
413                 query.append("groupId = ?");
414 
415                 query.append(" ");
416 
417                 Query q = session.createQuery(query.toString());
418 
419                 QueryPos qPos = QueryPos.getInstance(q);
420 
421                 qPos.add(groupId);
422 
423                 list = q.list();
424             }
425             catch (Exception e) {
426                 throw processException(e);
427             }
428             finally {
429                 if (list == null) {
430                     list = new ArrayList<LayoutSet>();
431                 }
432 
433                 cacheResult(list);
434 
435                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
436                     finderArgs, list);
437 
438                 closeSession(session);
439             }
440         }
441 
442         return list;
443     }
444 
445     public List<LayoutSet> findByGroupId(long groupId, int start, int end)
446         throws SystemException {
447         return findByGroupId(groupId, start, end, null);
448     }
449 
450     public List<LayoutSet> findByGroupId(long groupId, int start, int end,
451         OrderByComparator obc) throws SystemException {
452         Object[] finderArgs = new Object[] {
453                 new Long(groupId),
454                 
455                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
456             };
457 
458         List<LayoutSet> list = (List<LayoutSet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
459                 finderArgs, this);
460 
461         if (list == null) {
462             Session session = null;
463 
464             try {
465                 session = openSession();
466 
467                 StringBuilder query = new StringBuilder();
468 
469                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
470 
471                 query.append("groupId = ?");
472 
473                 query.append(" ");
474 
475                 if (obc != null) {
476                     query.append("ORDER BY ");
477                     query.append(obc.getOrderBy());
478                 }
479 
480                 Query q = session.createQuery(query.toString());
481 
482                 QueryPos qPos = QueryPos.getInstance(q);
483 
484                 qPos.add(groupId);
485 
486                 list = (List<LayoutSet>)QueryUtil.list(q, getDialect(), start,
487                         end);
488             }
489             catch (Exception e) {
490                 throw processException(e);
491             }
492             finally {
493                 if (list == null) {
494                     list = new ArrayList<LayoutSet>();
495                 }
496 
497                 cacheResult(list);
498 
499                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
500                     finderArgs, list);
501 
502                 closeSession(session);
503             }
504         }
505 
506         return list;
507     }
508 
509     public LayoutSet findByGroupId_First(long groupId, OrderByComparator obc)
510         throws NoSuchLayoutSetException, SystemException {
511         List<LayoutSet> list = findByGroupId(groupId, 0, 1, obc);
512 
513         if (list.isEmpty()) {
514             StringBuilder msg = new StringBuilder();
515 
516             msg.append("No LayoutSet exists with the key {");
517 
518             msg.append("groupId=" + groupId);
519 
520             msg.append(StringPool.CLOSE_CURLY_BRACE);
521 
522             throw new NoSuchLayoutSetException(msg.toString());
523         }
524         else {
525             return list.get(0);
526         }
527     }
528 
529     public LayoutSet findByGroupId_Last(long groupId, OrderByComparator obc)
530         throws NoSuchLayoutSetException, SystemException {
531         int count = countByGroupId(groupId);
532 
533         List<LayoutSet> list = findByGroupId(groupId, count - 1, count, obc);
534 
535         if (list.isEmpty()) {
536             StringBuilder msg = new StringBuilder();
537 
538             msg.append("No LayoutSet exists with the key {");
539 
540             msg.append("groupId=" + groupId);
541 
542             msg.append(StringPool.CLOSE_CURLY_BRACE);
543 
544             throw new NoSuchLayoutSetException(msg.toString());
545         }
546         else {
547             return list.get(0);
548         }
549     }
550 
551     public LayoutSet[] findByGroupId_PrevAndNext(long layoutSetId,
552         long groupId, OrderByComparator obc)
553         throws NoSuchLayoutSetException, SystemException {
554         LayoutSet layoutSet = findByPrimaryKey(layoutSetId);
555 
556         int count = countByGroupId(groupId);
557 
558         Session session = null;
559 
560         try {
561             session = openSession();
562 
563             StringBuilder query = new StringBuilder();
564 
565             query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
566 
567             query.append("groupId = ?");
568 
569             query.append(" ");
570 
571             if (obc != null) {
572                 query.append("ORDER BY ");
573                 query.append(obc.getOrderBy());
574             }
575 
576             Query q = session.createQuery(query.toString());
577 
578             QueryPos qPos = QueryPos.getInstance(q);
579 
580             qPos.add(groupId);
581 
582             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
583                     layoutSet);
584 
585             LayoutSet[] array = new LayoutSetImpl[3];
586 
587             array[0] = (LayoutSet)objArray[0];
588             array[1] = (LayoutSet)objArray[1];
589             array[2] = (LayoutSet)objArray[2];
590 
591             return array;
592         }
593         catch (Exception e) {
594             throw processException(e);
595         }
596         finally {
597             closeSession(session);
598         }
599     }
600 
601     public LayoutSet findByVirtualHost(String virtualHost)
602         throws NoSuchLayoutSetException, SystemException {
603         LayoutSet layoutSet = fetchByVirtualHost(virtualHost);
604 
605         if (layoutSet == null) {
606             StringBuilder msg = new StringBuilder();
607 
608             msg.append("No LayoutSet exists with the key {");
609 
610             msg.append("virtualHost=" + virtualHost);
611 
612             msg.append(StringPool.CLOSE_CURLY_BRACE);
613 
614             if (_log.isWarnEnabled()) {
615                 _log.warn(msg.toString());
616             }
617 
618             throw new NoSuchLayoutSetException(msg.toString());
619         }
620 
621         return layoutSet;
622     }
623 
624     public LayoutSet fetchByVirtualHost(String virtualHost)
625         throws SystemException {
626         return fetchByVirtualHost(virtualHost, true);
627     }
628 
629     public LayoutSet fetchByVirtualHost(String virtualHost,
630         boolean retrieveFromCache) throws SystemException {
631         Object[] finderArgs = new Object[] { virtualHost };
632 
633         Object result = null;
634 
635         if (retrieveFromCache) {
636             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
637                     finderArgs, this);
638         }
639 
640         if (result == null) {
641             Session session = null;
642 
643             try {
644                 session = openSession();
645 
646                 StringBuilder query = new StringBuilder();
647 
648                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
649 
650                 if (virtualHost == null) {
651                     query.append("virtualHost IS NULL");
652                 }
653                 else {
654                     query.append("virtualHost = ?");
655                 }
656 
657                 query.append(" ");
658 
659                 Query q = session.createQuery(query.toString());
660 
661                 QueryPos qPos = QueryPos.getInstance(q);
662 
663                 if (virtualHost != null) {
664                     qPos.add(virtualHost);
665                 }
666 
667                 List<LayoutSet> list = q.list();
668 
669                 result = list;
670 
671                 LayoutSet layoutSet = null;
672 
673                 if (list.isEmpty()) {
674                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
675                         finderArgs, list);
676                 }
677                 else {
678                     layoutSet = list.get(0);
679 
680                     cacheResult(layoutSet);
681 
682                     if ((layoutSet.getVirtualHost() == null) ||
683                             !layoutSet.getVirtualHost().equals(virtualHost)) {
684                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
685                             finderArgs, list);
686                     }
687                 }
688 
689                 return layoutSet;
690             }
691             catch (Exception e) {
692                 throw processException(e);
693             }
694             finally {
695                 if (result == null) {
696                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_VIRTUALHOST,
697                         finderArgs, new ArrayList<LayoutSet>());
698                 }
699 
700                 closeSession(session);
701             }
702         }
703         else {
704             if (result instanceof List) {
705                 return null;
706             }
707             else {
708                 return (LayoutSet)result;
709             }
710         }
711     }
712 
713     public LayoutSet findByG_P(long groupId, boolean privateLayout)
714         throws NoSuchLayoutSetException, SystemException {
715         LayoutSet layoutSet = fetchByG_P(groupId, privateLayout);
716 
717         if (layoutSet == null) {
718             StringBuilder msg = new StringBuilder();
719 
720             msg.append("No LayoutSet exists with the key {");
721 
722             msg.append("groupId=" + groupId);
723 
724             msg.append(", ");
725             msg.append("privateLayout=" + privateLayout);
726 
727             msg.append(StringPool.CLOSE_CURLY_BRACE);
728 
729             if (_log.isWarnEnabled()) {
730                 _log.warn(msg.toString());
731             }
732 
733             throw new NoSuchLayoutSetException(msg.toString());
734         }
735 
736         return layoutSet;
737     }
738 
739     public LayoutSet fetchByG_P(long groupId, boolean privateLayout)
740         throws SystemException {
741         return fetchByG_P(groupId, privateLayout, true);
742     }
743 
744     public LayoutSet fetchByG_P(long groupId, boolean privateLayout,
745         boolean retrieveFromCache) throws SystemException {
746         Object[] finderArgs = new Object[] {
747                 new Long(groupId), Boolean.valueOf(privateLayout)
748             };
749 
750         Object result = null;
751 
752         if (retrieveFromCache) {
753             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_P,
754                     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 WHERE ");
766 
767                 query.append("groupId = ?");
768 
769                 query.append(" AND ");
770 
771                 query.append("privateLayout = ?");
772 
773                 query.append(" ");
774 
775                 Query q = session.createQuery(query.toString());
776 
777                 QueryPos qPos = QueryPos.getInstance(q);
778 
779                 qPos.add(groupId);
780 
781                 qPos.add(privateLayout);
782 
783                 List<LayoutSet> list = q.list();
784 
785                 result = list;
786 
787                 LayoutSet layoutSet = null;
788 
789                 if (list.isEmpty()) {
790                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_P,
791                         finderArgs, list);
792                 }
793                 else {
794                     layoutSet = list.get(0);
795 
796                     cacheResult(layoutSet);
797 
798                     if ((layoutSet.getGroupId() != groupId) ||
799                             (layoutSet.getPrivateLayout() != privateLayout)) {
800                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_P,
801                             finderArgs, list);
802                     }
803                 }
804 
805                 return layoutSet;
806             }
807             catch (Exception e) {
808                 throw processException(e);
809             }
810             finally {
811                 if (result == null) {
812                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_P,
813                         finderArgs, new ArrayList<LayoutSet>());
814                 }
815 
816                 closeSession(session);
817             }
818         }
819         else {
820             if (result instanceof List) {
821                 return null;
822             }
823             else {
824                 return (LayoutSet)result;
825             }
826         }
827     }
828 
829     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
830         throws SystemException {
831         Session session = null;
832 
833         try {
834             session = openSession();
835 
836             dynamicQuery.compile(session);
837 
838             return dynamicQuery.list();
839         }
840         catch (Exception e) {
841             throw processException(e);
842         }
843         finally {
844             closeSession(session);
845         }
846     }
847 
848     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
849         int start, int end) throws SystemException {
850         Session session = null;
851 
852         try {
853             session = openSession();
854 
855             dynamicQuery.setLimit(start, end);
856 
857             dynamicQuery.compile(session);
858 
859             return dynamicQuery.list();
860         }
861         catch (Exception e) {
862             throw processException(e);
863         }
864         finally {
865             closeSession(session);
866         }
867     }
868 
869     public List<LayoutSet> findAll() throws SystemException {
870         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
871     }
872 
873     public List<LayoutSet> findAll(int start, int end)
874         throws SystemException {
875         return findAll(start, end, null);
876     }
877 
878     public List<LayoutSet> findAll(int start, int end, OrderByComparator obc)
879         throws SystemException {
880         Object[] finderArgs = new Object[] {
881                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
882             };
883 
884         List<LayoutSet> list = (List<LayoutSet>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
885                 finderArgs, this);
886 
887         if (list == null) {
888             Session session = null;
889 
890             try {
891                 session = openSession();
892 
893                 StringBuilder query = new StringBuilder();
894 
895                 query.append("FROM com.liferay.portal.model.LayoutSet ");
896 
897                 if (obc != null) {
898                     query.append("ORDER BY ");
899                     query.append(obc.getOrderBy());
900                 }
901 
902                 Query q = session.createQuery(query.toString());
903 
904                 if (obc == null) {
905                     list = (List<LayoutSet>)QueryUtil.list(q, getDialect(),
906                             start, end, false);
907 
908                     Collections.sort(list);
909                 }
910                 else {
911                     list = (List<LayoutSet>)QueryUtil.list(q, getDialect(),
912                             start, end);
913                 }
914             }
915             catch (Exception e) {
916                 throw processException(e);
917             }
918             finally {
919                 if (list == null) {
920                     list = new ArrayList<LayoutSet>();
921                 }
922 
923                 cacheResult(list);
924 
925                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
926 
927                 closeSession(session);
928             }
929         }
930 
931         return list;
932     }
933 
934     public void removeByGroupId(long groupId) throws SystemException {
935         for (LayoutSet layoutSet : findByGroupId(groupId)) {
936             remove(layoutSet);
937         }
938     }
939 
940     public void removeByVirtualHost(String virtualHost)
941         throws NoSuchLayoutSetException, SystemException {
942         LayoutSet layoutSet = findByVirtualHost(virtualHost);
943 
944         remove(layoutSet);
945     }
946 
947     public void removeByG_P(long groupId, boolean privateLayout)
948         throws NoSuchLayoutSetException, SystemException {
949         LayoutSet layoutSet = findByG_P(groupId, privateLayout);
950 
951         remove(layoutSet);
952     }
953 
954     public void removeAll() throws SystemException {
955         for (LayoutSet layoutSet : findAll()) {
956             remove(layoutSet);
957         }
958     }
959 
960     public int countByGroupId(long groupId) throws SystemException {
961         Object[] finderArgs = new Object[] { new Long(groupId) };
962 
963         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
964                 finderArgs, this);
965 
966         if (count == null) {
967             Session session = null;
968 
969             try {
970                 session = openSession();
971 
972                 StringBuilder query = new StringBuilder();
973 
974                 query.append("SELECT COUNT(*) ");
975                 query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
976 
977                 query.append("groupId = ?");
978 
979                 query.append(" ");
980 
981                 Query q = session.createQuery(query.toString());
982 
983                 QueryPos qPos = QueryPos.getInstance(q);
984 
985                 qPos.add(groupId);
986 
987                 count = (Long)q.uniqueResult();
988             }
989             catch (Exception e) {
990                 throw processException(e);
991             }
992             finally {
993                 if (count == null) {
994                     count = Long.valueOf(0);
995                 }
996 
997                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
998                     finderArgs, count);
999 
1000                closeSession(session);
1001            }
1002        }
1003
1004        return count.intValue();
1005    }
1006
1007    public int countByVirtualHost(String virtualHost) throws SystemException {
1008        Object[] finderArgs = new Object[] { virtualHost };
1009
1010        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_VIRTUALHOST,
1011                finderArgs, this);
1012
1013        if (count == null) {
1014            Session session = null;
1015
1016            try {
1017                session = openSession();
1018
1019                StringBuilder query = new StringBuilder();
1020
1021                query.append("SELECT COUNT(*) ");
1022                query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
1023
1024                if (virtualHost == null) {
1025                    query.append("virtualHost IS NULL");
1026                }
1027                else {
1028                    query.append("virtualHost = ?");
1029                }
1030
1031                query.append(" ");
1032
1033                Query q = session.createQuery(query.toString());
1034
1035                QueryPos qPos = QueryPos.getInstance(q);
1036
1037                if (virtualHost != null) {
1038                    qPos.add(virtualHost);
1039                }
1040
1041                count = (Long)q.uniqueResult();
1042            }
1043            catch (Exception e) {
1044                throw processException(e);
1045            }
1046            finally {
1047                if (count == null) {
1048                    count = Long.valueOf(0);
1049                }
1050
1051                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_VIRTUALHOST,
1052                    finderArgs, count);
1053
1054                closeSession(session);
1055            }
1056        }
1057
1058        return count.intValue();
1059    }
1060
1061    public int countByG_P(long groupId, boolean privateLayout)
1062        throws SystemException {
1063        Object[] finderArgs = new Object[] {
1064                new Long(groupId), Boolean.valueOf(privateLayout)
1065            };
1066
1067        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_P,
1068                finderArgs, this);
1069
1070        if (count == null) {
1071            Session session = null;
1072
1073            try {
1074                session = openSession();
1075
1076                StringBuilder query = new StringBuilder();
1077
1078                query.append("SELECT COUNT(*) ");
1079                query.append("FROM com.liferay.portal.model.LayoutSet WHERE ");
1080
1081                query.append("groupId = ?");
1082
1083                query.append(" AND ");
1084
1085                query.append("privateLayout = ?");
1086
1087                query.append(" ");
1088
1089                Query q = session.createQuery(query.toString());
1090
1091                QueryPos qPos = QueryPos.getInstance(q);
1092
1093                qPos.add(groupId);
1094
1095                qPos.add(privateLayout);
1096
1097                count = (Long)q.uniqueResult();
1098            }
1099            catch (Exception e) {
1100                throw processException(e);
1101            }
1102            finally {
1103                if (count == null) {
1104                    count = Long.valueOf(0);
1105                }
1106
1107                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_P, finderArgs,
1108                    count);
1109
1110                closeSession(session);
1111            }
1112        }
1113
1114        return count.intValue();
1115    }
1116
1117    public int countAll() throws SystemException {
1118        Object[] finderArgs = new Object[0];
1119
1120        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1121                finderArgs, this);
1122
1123        if (count == null) {
1124            Session session = null;
1125
1126            try {
1127                session = openSession();
1128
1129                Query q = session.createQuery(
1130                        "SELECT COUNT(*) FROM com.liferay.portal.model.LayoutSet");
1131
1132                count = (Long)q.uniqueResult();
1133            }
1134            catch (Exception e) {
1135                throw processException(e);
1136            }
1137            finally {
1138                if (count == null) {
1139                    count = Long.valueOf(0);
1140                }
1141
1142                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1143                    count);
1144
1145                closeSession(session);
1146            }
1147        }
1148
1149        return count.intValue();
1150    }
1151
1152    public void afterPropertiesSet() {
1153        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1154                    com.liferay.portal.util.PropsUtil.get(
1155                        "value.object.listener.com.liferay.portal.model.LayoutSet")));
1156
1157        if (listenerClassNames.length > 0) {
1158            try {
1159                List<ModelListener<LayoutSet>> listenersList = new ArrayList<ModelListener<LayoutSet>>();
1160
1161                for (String listenerClassName : listenerClassNames) {
1162                    listenersList.add((ModelListener<LayoutSet>)Class.forName(
1163                            listenerClassName).newInstance());
1164                }
1165
1166                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1167            }
1168            catch (Exception e) {
1169                _log.error(e);
1170            }
1171        }
1172    }
1173
1174    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
1175    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
1176    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
1177    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
1178    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
1179    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
1180    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
1181    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
1182    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
1183    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
1184    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
1185    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
1186    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
1187    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
1188    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
1189    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1190    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1191    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1192    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1193    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1194    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1195    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1196    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1197    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1198    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1199    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1200    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1201    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1202    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1203    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1204    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1205    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1206    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1207    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1208    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1209    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1210    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1211    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1212    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1213    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1214    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1215    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1216    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1217    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1218    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1219    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1220    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1221    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1222    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1223    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1224    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1225    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1226    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1227    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1228    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1229    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1230    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1231    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1232    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1233    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1234    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1235    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1236    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1237    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1238    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1239    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1240    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1241    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1242    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1243    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1244    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1245    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1246    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1247    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1248    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1249    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1250    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1251    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1252    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1253    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1254    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1255    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1256    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1257    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1258    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1259    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1260    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1261    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1262    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1263    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1264    private static Log _log = LogFactoryUtil.getLog(LayoutSetPersistenceImpl.class);
1265}