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