1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
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.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.BatchSessionUtil;
40  import com.liferay.portal.service.persistence.GroupPersistence;
41  import com.liferay.portal.service.persistence.ResourcePersistence;
42  import com.liferay.portal.service.persistence.SubscriptionPersistence;
43  import com.liferay.portal.service.persistence.UserPersistence;
44  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45  
46  import com.liferay.portlet.messageboards.NoSuchCategoryException;
47  import com.liferay.portlet.messageboards.model.MBCategory;
48  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
49  import com.liferay.portlet.messageboards.model.impl.MBCategoryModelImpl;
50  import com.liferay.portlet.tags.service.persistence.TagsEntryPersistence;
51  
52  import java.io.Serializable;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.List;
57  
58  /**
59   * <a href="MBCategoryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * <p>
62   * ServiceBuilder generated this class. Modifications in this class will be
63   * overwritten the next time is generated.
64   * </p>
65   *
66   * @author    Brian Wing Shun Chan
67   * @see       MBCategoryPersistence
68   * @see       MBCategoryUtil
69   * @generated
70   */
71  public class MBCategoryPersistenceImpl extends BasePersistenceImpl<MBCategory>
72      implements MBCategoryPersistence {
73      public static final String FINDER_CLASS_NAME_ENTITY = MBCategoryImpl.class.getName();
74      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
75          ".List";
76      public static final FinderPath FINDER_PATH_FIND_BY_UUID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
77              MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
78              "findByUuid",
79              new String[] {
80                  String.class.getName(),
81                  
82              "java.lang.Integer", "java.lang.Integer",
83                  "com.liferay.portal.kernel.util.OrderByComparator"
84              });
85      public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
86              MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
87              "countByUuid", new String[] { String.class.getName() });
88      public static final FinderPath FINDER_PATH_FETCH_BY_UUID_G = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
89              MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
90              "fetchByUUID_G",
91              new String[] { String.class.getName(), Long.class.getName() });
92      public static final FinderPath FINDER_PATH_COUNT_BY_UUID_G = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
93              MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
94              "countByUUID_G",
95              new String[] { String.class.getName(), Long.class.getName() });
96      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
97              MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
98              "findByGroupId",
99              new String[] {
100                 Long.class.getName(),
101                 
102             "java.lang.Integer", "java.lang.Integer",
103                 "com.liferay.portal.kernel.util.OrderByComparator"
104             });
105     public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
106             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
107             "countByGroupId", new String[] { Long.class.getName() });
108     public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
109             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
110             "findByCompanyId",
111             new String[] {
112                 Long.class.getName(),
113                 
114             "java.lang.Integer", "java.lang.Integer",
115                 "com.liferay.portal.kernel.util.OrderByComparator"
116             });
117     public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
118             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
119             "countByCompanyId", new String[] { Long.class.getName() });
120     public static final FinderPath FINDER_PATH_FIND_BY_G_P = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
121             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
122             "findByG_P",
123             new String[] {
124                 Long.class.getName(), Long.class.getName(),
125                 
126             "java.lang.Integer", "java.lang.Integer",
127                 "com.liferay.portal.kernel.util.OrderByComparator"
128             });
129     public static final FinderPath FINDER_PATH_COUNT_BY_G_P = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
130             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
131             "countByG_P",
132             new String[] { Long.class.getName(), Long.class.getName() });
133     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
134             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
135             "findAll", new String[0]);
136     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
137             MBCategoryModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
138             "countAll", new String[0]);
139 
140     public void cacheResult(MBCategory mbCategory) {
141         EntityCacheUtil.putResult(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
142             MBCategoryImpl.class, mbCategory.getPrimaryKey(), mbCategory);
143 
144         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
145             new Object[] { mbCategory.getUuid(), new Long(
146                     mbCategory.getGroupId()) }, mbCategory);
147     }
148 
149     public void cacheResult(List<MBCategory> mbCategories) {
150         for (MBCategory mbCategory : mbCategories) {
151             if (EntityCacheUtil.getResult(
152                         MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
153                         MBCategoryImpl.class, mbCategory.getPrimaryKey(), this) == null) {
154                 cacheResult(mbCategory);
155             }
156         }
157     }
158 
159     public void clearCache() {
160         CacheRegistry.clear(MBCategoryImpl.class.getName());
161         EntityCacheUtil.clearCache(MBCategoryImpl.class.getName());
162         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
163         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
164     }
165 
166     public void clearCache(MBCategory mbCategory) {
167         EntityCacheUtil.removeResult(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
168             MBCategoryImpl.class, mbCategory.getPrimaryKey());
169 
170         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
171             new Object[] { mbCategory.getUuid(), new Long(
172                     mbCategory.getGroupId()) });
173     }
174 
175     public MBCategory create(long categoryId) {
176         MBCategory mbCategory = new MBCategoryImpl();
177 
178         mbCategory.setNew(true);
179         mbCategory.setPrimaryKey(categoryId);
180 
181         String uuid = PortalUUIDUtil.generate();
182 
183         mbCategory.setUuid(uuid);
184 
185         return mbCategory;
186     }
187 
188     public MBCategory remove(Serializable primaryKey)
189         throws NoSuchModelException, SystemException {
190         return remove(((Long)primaryKey).longValue());
191     }
192 
193     public MBCategory remove(long categoryId)
194         throws NoSuchCategoryException, SystemException {
195         Session session = null;
196 
197         try {
198             session = openSession();
199 
200             MBCategory mbCategory = (MBCategory)session.get(MBCategoryImpl.class,
201                     new Long(categoryId));
202 
203             if (mbCategory == null) {
204                 if (_log.isWarnEnabled()) {
205                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + categoryId);
206                 }
207 
208                 throw new NoSuchCategoryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
209                     categoryId);
210             }
211 
212             return remove(mbCategory);
213         }
214         catch (NoSuchCategoryException nsee) {
215             throw nsee;
216         }
217         catch (Exception e) {
218             throw processException(e);
219         }
220         finally {
221             closeSession(session);
222         }
223     }
224 
225     protected MBCategory removeImpl(MBCategory mbCategory)
226         throws SystemException {
227         mbCategory = toUnwrappedModel(mbCategory);
228 
229         Session session = null;
230 
231         try {
232             session = openSession();
233 
234             BatchSessionUtil.delete(session, mbCategory);
235         }
236         catch (Exception e) {
237             throw processException(e);
238         }
239         finally {
240             closeSession(session);
241         }
242 
243         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
244 
245         MBCategoryModelImpl mbCategoryModelImpl = (MBCategoryModelImpl)mbCategory;
246 
247         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
248             new Object[] {
249                 mbCategoryModelImpl.getOriginalUuid(),
250                 new Long(mbCategoryModelImpl.getOriginalGroupId())
251             });
252 
253         EntityCacheUtil.removeResult(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
254             MBCategoryImpl.class, mbCategory.getPrimaryKey());
255 
256         return mbCategory;
257     }
258 
259     /**
260      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
261      */
262     public MBCategory update(MBCategory mbCategory) throws SystemException {
263         if (_log.isWarnEnabled()) {
264             _log.warn(
265                 "Using the deprecated update(MBCategory mbCategory) method. Use update(MBCategory mbCategory, boolean merge) instead.");
266         }
267 
268         return update(mbCategory, false);
269     }
270 
271     public MBCategory updateImpl(
272         com.liferay.portlet.messageboards.model.MBCategory mbCategory,
273         boolean merge) throws SystemException {
274         mbCategory = toUnwrappedModel(mbCategory);
275 
276         boolean isNew = mbCategory.isNew();
277 
278         MBCategoryModelImpl mbCategoryModelImpl = (MBCategoryModelImpl)mbCategory;
279 
280         if (Validator.isNull(mbCategory.getUuid())) {
281             String uuid = PortalUUIDUtil.generate();
282 
283             mbCategory.setUuid(uuid);
284         }
285 
286         Session session = null;
287 
288         try {
289             session = openSession();
290 
291             BatchSessionUtil.update(session, mbCategory, merge);
292 
293             mbCategory.setNew(false);
294         }
295         catch (Exception e) {
296             throw processException(e);
297         }
298         finally {
299             closeSession(session);
300         }
301 
302         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
303 
304         EntityCacheUtil.putResult(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
305             MBCategoryImpl.class, mbCategory.getPrimaryKey(), mbCategory);
306 
307         if (!isNew &&
308                 (!Validator.equals(mbCategory.getUuid(),
309                     mbCategoryModelImpl.getOriginalUuid()) ||
310                 (mbCategory.getGroupId() != mbCategoryModelImpl.getOriginalGroupId()))) {
311             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_UUID_G,
312                 new Object[] {
313                     mbCategoryModelImpl.getOriginalUuid(),
314                     new Long(mbCategoryModelImpl.getOriginalGroupId())
315                 });
316         }
317 
318         if (isNew ||
319                 (!Validator.equals(mbCategory.getUuid(),
320                     mbCategoryModelImpl.getOriginalUuid()) ||
321                 (mbCategory.getGroupId() != mbCategoryModelImpl.getOriginalGroupId()))) {
322             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
323                 new Object[] {
324                     mbCategory.getUuid(), new Long(mbCategory.getGroupId())
325                 }, mbCategory);
326         }
327 
328         return mbCategory;
329     }
330 
331     protected MBCategory toUnwrappedModel(MBCategory mbCategory) {
332         if (mbCategory instanceof MBCategoryImpl) {
333             return mbCategory;
334         }
335 
336         MBCategoryImpl mbCategoryImpl = new MBCategoryImpl();
337 
338         mbCategoryImpl.setNew(mbCategory.isNew());
339         mbCategoryImpl.setPrimaryKey(mbCategory.getPrimaryKey());
340 
341         mbCategoryImpl.setUuid(mbCategory.getUuid());
342         mbCategoryImpl.setCategoryId(mbCategory.getCategoryId());
343         mbCategoryImpl.setGroupId(mbCategory.getGroupId());
344         mbCategoryImpl.setCompanyId(mbCategory.getCompanyId());
345         mbCategoryImpl.setUserId(mbCategory.getUserId());
346         mbCategoryImpl.setUserName(mbCategory.getUserName());
347         mbCategoryImpl.setCreateDate(mbCategory.getCreateDate());
348         mbCategoryImpl.setModifiedDate(mbCategory.getModifiedDate());
349         mbCategoryImpl.setParentCategoryId(mbCategory.getParentCategoryId());
350         mbCategoryImpl.setName(mbCategory.getName());
351         mbCategoryImpl.setDescription(mbCategory.getDescription());
352         mbCategoryImpl.setThreadCount(mbCategory.getThreadCount());
353         mbCategoryImpl.setMessageCount(mbCategory.getMessageCount());
354         mbCategoryImpl.setLastPostDate(mbCategory.getLastPostDate());
355 
356         return mbCategoryImpl;
357     }
358 
359     public MBCategory findByPrimaryKey(Serializable primaryKey)
360         throws NoSuchModelException, SystemException {
361         return findByPrimaryKey(((Long)primaryKey).longValue());
362     }
363 
364     public MBCategory findByPrimaryKey(long categoryId)
365         throws NoSuchCategoryException, SystemException {
366         MBCategory mbCategory = fetchByPrimaryKey(categoryId);
367 
368         if (mbCategory == null) {
369             if (_log.isWarnEnabled()) {
370                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + categoryId);
371             }
372 
373             throw new NoSuchCategoryException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
374                 categoryId);
375         }
376 
377         return mbCategory;
378     }
379 
380     public MBCategory fetchByPrimaryKey(Serializable primaryKey)
381         throws SystemException {
382         return fetchByPrimaryKey(((Long)primaryKey).longValue());
383     }
384 
385     public MBCategory fetchByPrimaryKey(long categoryId)
386         throws SystemException {
387         MBCategory mbCategory = (MBCategory)EntityCacheUtil.getResult(MBCategoryModelImpl.ENTITY_CACHE_ENABLED,
388                 MBCategoryImpl.class, categoryId, this);
389 
390         if (mbCategory == null) {
391             Session session = null;
392 
393             try {
394                 session = openSession();
395 
396                 mbCategory = (MBCategory)session.get(MBCategoryImpl.class,
397                         new Long(categoryId));
398             }
399             catch (Exception e) {
400                 throw processException(e);
401             }
402             finally {
403                 if (mbCategory != null) {
404                     cacheResult(mbCategory);
405                 }
406 
407                 closeSession(session);
408             }
409         }
410 
411         return mbCategory;
412     }
413 
414     public List<MBCategory> findByUuid(String uuid) throws SystemException {
415         return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
416     }
417 
418     public List<MBCategory> findByUuid(String uuid, int start, int end)
419         throws SystemException {
420         return findByUuid(uuid, start, end, null);
421     }
422 
423     public List<MBCategory> findByUuid(String uuid, int start, int end,
424         OrderByComparator orderByComparator) throws SystemException {
425         Object[] finderArgs = new Object[] {
426                 uuid,
427                 
428                 String.valueOf(start), String.valueOf(end),
429                 String.valueOf(orderByComparator)
430             };
431 
432         List<MBCategory> list = (List<MBCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_UUID,
433                 finderArgs, this);
434 
435         if (list == null) {
436             StringBundler query = null;
437 
438             if (orderByComparator != null) {
439                 query = new StringBundler(3 +
440                         (orderByComparator.getOrderByFields().length * 3));
441             }
442             else {
443                 query = new StringBundler(3);
444             }
445 
446             query.append(_SQL_SELECT_MBCATEGORY_WHERE);
447 
448             if (uuid == null) {
449                 query.append(_FINDER_COLUMN_UUID_UUID_1);
450             }
451             else {
452                 if (uuid.equals(StringPool.BLANK)) {
453                     query.append(_FINDER_COLUMN_UUID_UUID_3);
454                 }
455                 else {
456                     query.append(_FINDER_COLUMN_UUID_UUID_2);
457                 }
458             }
459 
460             if (orderByComparator != null) {
461                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
462                     orderByComparator);
463             }
464 
465             else {
466                 query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
467             }
468 
469             String sql = query.toString();
470 
471             Session session = null;
472 
473             try {
474                 session = openSession();
475 
476                 Query q = session.createQuery(sql);
477 
478                 QueryPos qPos = QueryPos.getInstance(q);
479 
480                 if (uuid != null) {
481                     qPos.add(uuid);
482                 }
483 
484                 list = (List<MBCategory>)QueryUtil.list(q, getDialect(), start,
485                         end);
486             }
487             catch (Exception e) {
488                 throw processException(e);
489             }
490             finally {
491                 if (list == null) {
492                     list = new ArrayList<MBCategory>();
493                 }
494 
495                 cacheResult(list);
496 
497                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_UUID, finderArgs,
498                     list);
499 
500                 closeSession(session);
501             }
502         }
503 
504         return list;
505     }
506 
507     public MBCategory findByUuid_First(String uuid,
508         OrderByComparator orderByComparator)
509         throws NoSuchCategoryException, SystemException {
510         List<MBCategory> list = findByUuid(uuid, 0, 1, orderByComparator);
511 
512         if (list.isEmpty()) {
513             StringBundler msg = new StringBundler(4);
514 
515             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
516 
517             msg.append("uuid=");
518             msg.append(uuid);
519 
520             msg.append(StringPool.CLOSE_CURLY_BRACE);
521 
522             throw new NoSuchCategoryException(msg.toString());
523         }
524         else {
525             return list.get(0);
526         }
527     }
528 
529     public MBCategory findByUuid_Last(String uuid,
530         OrderByComparator orderByComparator)
531         throws NoSuchCategoryException, SystemException {
532         int count = countByUuid(uuid);
533 
534         List<MBCategory> list = findByUuid(uuid, count - 1, count,
535                 orderByComparator);
536 
537         if (list.isEmpty()) {
538             StringBundler msg = new StringBundler(4);
539 
540             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
541 
542             msg.append("uuid=");
543             msg.append(uuid);
544 
545             msg.append(StringPool.CLOSE_CURLY_BRACE);
546 
547             throw new NoSuchCategoryException(msg.toString());
548         }
549         else {
550             return list.get(0);
551         }
552     }
553 
554     public MBCategory[] findByUuid_PrevAndNext(long categoryId, String uuid,
555         OrderByComparator orderByComparator)
556         throws NoSuchCategoryException, SystemException {
557         MBCategory mbCategory = findByPrimaryKey(categoryId);
558 
559         Session session = null;
560 
561         try {
562             session = openSession();
563 
564             MBCategory[] array = new MBCategoryImpl[3];
565 
566             array[0] = getByUuid_PrevAndNext(session, mbCategory, uuid,
567                     orderByComparator, true);
568 
569             array[1] = mbCategory;
570 
571             array[2] = getByUuid_PrevAndNext(session, mbCategory, uuid,
572                     orderByComparator, false);
573 
574             return array;
575         }
576         catch (Exception e) {
577             throw processException(e);
578         }
579         finally {
580             closeSession(session);
581         }
582     }
583 
584     protected MBCategory getByUuid_PrevAndNext(Session session,
585         MBCategory mbCategory, String uuid,
586         OrderByComparator orderByComparator, boolean previous) {
587         StringBundler query = null;
588 
589         if (orderByComparator != null) {
590             query = new StringBundler(6 +
591                     (orderByComparator.getOrderByFields().length * 6));
592         }
593         else {
594             query = new StringBundler(3);
595         }
596 
597         query.append(_SQL_SELECT_MBCATEGORY_WHERE);
598 
599         if (uuid == null) {
600             query.append(_FINDER_COLUMN_UUID_UUID_1);
601         }
602         else {
603             if (uuid.equals(StringPool.BLANK)) {
604                 query.append(_FINDER_COLUMN_UUID_UUID_3);
605             }
606             else {
607                 query.append(_FINDER_COLUMN_UUID_UUID_2);
608             }
609         }
610 
611         if (orderByComparator != null) {
612             String[] orderByFields = orderByComparator.getOrderByFields();
613 
614             if (orderByFields.length > 0) {
615                 query.append(WHERE_AND);
616             }
617 
618             for (int i = 0; i < orderByFields.length; i++) {
619                 query.append(_ORDER_BY_ENTITY_ALIAS);
620                 query.append(orderByFields[i]);
621 
622                 if ((i + 1) < orderByFields.length) {
623                     if (orderByComparator.isAscending() ^ previous) {
624                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
625                     }
626                     else {
627                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
628                     }
629                 }
630                 else {
631                     if (orderByComparator.isAscending() ^ previous) {
632                         query.append(WHERE_GREATER_THAN);
633                     }
634                     else {
635                         query.append(WHERE_LESSER_THAN);
636                     }
637                 }
638             }
639 
640             query.append(ORDER_BY_CLAUSE);
641 
642             for (int i = 0; i < orderByFields.length; i++) {
643                 query.append(_ORDER_BY_ENTITY_ALIAS);
644                 query.append(orderByFields[i]);
645 
646                 if ((i + 1) < orderByFields.length) {
647                     if (orderByComparator.isAscending() ^ previous) {
648                         query.append(ORDER_BY_ASC_HAS_NEXT);
649                     }
650                     else {
651                         query.append(ORDER_BY_DESC_HAS_NEXT);
652                     }
653                 }
654                 else {
655                     if (orderByComparator.isAscending() ^ previous) {
656                         query.append(ORDER_BY_ASC);
657                     }
658                     else {
659                         query.append(ORDER_BY_DESC);
660                     }
661                 }
662             }
663         }
664 
665         else {
666             query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
667         }
668 
669         String sql = query.toString();
670 
671         Query q = session.createQuery(sql);
672 
673         q.setFirstResult(0);
674         q.setMaxResults(2);
675 
676         QueryPos qPos = QueryPos.getInstance(q);
677 
678         if (uuid != null) {
679             qPos.add(uuid);
680         }
681 
682         if (orderByComparator != null) {
683             Object[] values = orderByComparator.getOrderByValues(mbCategory);
684 
685             for (Object value : values) {
686                 qPos.add(value);
687             }
688         }
689 
690         List<MBCategory> list = q.list();
691 
692         if (list.size() == 2) {
693             return list.get(1);
694         }
695         else {
696             return null;
697         }
698     }
699 
700     public MBCategory findByUUID_G(String uuid, long groupId)
701         throws NoSuchCategoryException, SystemException {
702         MBCategory mbCategory = fetchByUUID_G(uuid, groupId);
703 
704         if (mbCategory == null) {
705             StringBundler msg = new StringBundler(6);
706 
707             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
708 
709             msg.append("uuid=");
710             msg.append(uuid);
711 
712             msg.append(", groupId=");
713             msg.append(groupId);
714 
715             msg.append(StringPool.CLOSE_CURLY_BRACE);
716 
717             if (_log.isWarnEnabled()) {
718                 _log.warn(msg.toString());
719             }
720 
721             throw new NoSuchCategoryException(msg.toString());
722         }
723 
724         return mbCategory;
725     }
726 
727     public MBCategory fetchByUUID_G(String uuid, long groupId)
728         throws SystemException {
729         return fetchByUUID_G(uuid, groupId, true);
730     }
731 
732     public MBCategory fetchByUUID_G(String uuid, long groupId,
733         boolean retrieveFromCache) throws SystemException {
734         Object[] finderArgs = new Object[] { uuid, groupId };
735 
736         Object result = null;
737 
738         if (retrieveFromCache) {
739             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_UUID_G,
740                     finderArgs, this);
741         }
742 
743         if (result == null) {
744             StringBundler query = new StringBundler(4);
745 
746             query.append(_SQL_SELECT_MBCATEGORY_WHERE);
747 
748             if (uuid == null) {
749                 query.append(_FINDER_COLUMN_UUID_G_UUID_1);
750             }
751             else {
752                 if (uuid.equals(StringPool.BLANK)) {
753                     query.append(_FINDER_COLUMN_UUID_G_UUID_3);
754                 }
755                 else {
756                     query.append(_FINDER_COLUMN_UUID_G_UUID_2);
757                 }
758             }
759 
760             query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
761 
762             query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
763 
764             String sql = query.toString();
765 
766             Session session = null;
767 
768             try {
769                 session = openSession();
770 
771                 Query q = session.createQuery(sql);
772 
773                 QueryPos qPos = QueryPos.getInstance(q);
774 
775                 if (uuid != null) {
776                     qPos.add(uuid);
777                 }
778 
779                 qPos.add(groupId);
780 
781                 List<MBCategory> list = q.list();
782 
783                 result = list;
784 
785                 MBCategory mbCategory = null;
786 
787                 if (list.isEmpty()) {
788                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
789                         finderArgs, list);
790                 }
791                 else {
792                     mbCategory = list.get(0);
793 
794                     cacheResult(mbCategory);
795 
796                     if ((mbCategory.getUuid() == null) ||
797                             !mbCategory.getUuid().equals(uuid) ||
798                             (mbCategory.getGroupId() != groupId)) {
799                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
800                             finderArgs, mbCategory);
801                     }
802                 }
803 
804                 return mbCategory;
805             }
806             catch (Exception e) {
807                 throw processException(e);
808             }
809             finally {
810                 if (result == null) {
811                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_UUID_G,
812                         finderArgs, new ArrayList<MBCategory>());
813                 }
814 
815                 closeSession(session);
816             }
817         }
818         else {
819             if (result instanceof List<?>) {
820                 return null;
821             }
822             else {
823                 return (MBCategory)result;
824             }
825         }
826     }
827 
828     public List<MBCategory> findByGroupId(long groupId)
829         throws SystemException {
830         return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
831     }
832 
833     public List<MBCategory> findByGroupId(long groupId, int start, int end)
834         throws SystemException {
835         return findByGroupId(groupId, start, end, null);
836     }
837 
838     public List<MBCategory> findByGroupId(long groupId, int start, int end,
839         OrderByComparator orderByComparator) throws SystemException {
840         Object[] finderArgs = new Object[] {
841                 groupId,
842                 
843                 String.valueOf(start), String.valueOf(end),
844                 String.valueOf(orderByComparator)
845             };
846 
847         List<MBCategory> list = (List<MBCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
848                 finderArgs, this);
849 
850         if (list == null) {
851             StringBundler query = null;
852 
853             if (orderByComparator != null) {
854                 query = new StringBundler(3 +
855                         (orderByComparator.getOrderByFields().length * 3));
856             }
857             else {
858                 query = new StringBundler(3);
859             }
860 
861             query.append(_SQL_SELECT_MBCATEGORY_WHERE);
862 
863             query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
864 
865             if (orderByComparator != null) {
866                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
867                     orderByComparator);
868             }
869 
870             else {
871                 query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
872             }
873 
874             String sql = query.toString();
875 
876             Session session = null;
877 
878             try {
879                 session = openSession();
880 
881                 Query q = session.createQuery(sql);
882 
883                 QueryPos qPos = QueryPos.getInstance(q);
884 
885                 qPos.add(groupId);
886 
887                 list = (List<MBCategory>)QueryUtil.list(q, getDialect(), start,
888                         end);
889             }
890             catch (Exception e) {
891                 throw processException(e);
892             }
893             finally {
894                 if (list == null) {
895                     list = new ArrayList<MBCategory>();
896                 }
897 
898                 cacheResult(list);
899 
900                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
901                     finderArgs, list);
902 
903                 closeSession(session);
904             }
905         }
906 
907         return list;
908     }
909 
910     public MBCategory findByGroupId_First(long groupId,
911         OrderByComparator orderByComparator)
912         throws NoSuchCategoryException, SystemException {
913         List<MBCategory> list = findByGroupId(groupId, 0, 1, orderByComparator);
914 
915         if (list.isEmpty()) {
916             StringBundler msg = new StringBundler(4);
917 
918             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
919 
920             msg.append("groupId=");
921             msg.append(groupId);
922 
923             msg.append(StringPool.CLOSE_CURLY_BRACE);
924 
925             throw new NoSuchCategoryException(msg.toString());
926         }
927         else {
928             return list.get(0);
929         }
930     }
931 
932     public MBCategory findByGroupId_Last(long groupId,
933         OrderByComparator orderByComparator)
934         throws NoSuchCategoryException, SystemException {
935         int count = countByGroupId(groupId);
936 
937         List<MBCategory> list = findByGroupId(groupId, count - 1, count,
938                 orderByComparator);
939 
940         if (list.isEmpty()) {
941             StringBundler msg = new StringBundler(4);
942 
943             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
944 
945             msg.append("groupId=");
946             msg.append(groupId);
947 
948             msg.append(StringPool.CLOSE_CURLY_BRACE);
949 
950             throw new NoSuchCategoryException(msg.toString());
951         }
952         else {
953             return list.get(0);
954         }
955     }
956 
957     public MBCategory[] findByGroupId_PrevAndNext(long categoryId,
958         long groupId, OrderByComparator orderByComparator)
959         throws NoSuchCategoryException, SystemException {
960         MBCategory mbCategory = findByPrimaryKey(categoryId);
961 
962         Session session = null;
963 
964         try {
965             session = openSession();
966 
967             MBCategory[] array = new MBCategoryImpl[3];
968 
969             array[0] = getByGroupId_PrevAndNext(session, mbCategory, groupId,
970                     orderByComparator, true);
971 
972             array[1] = mbCategory;
973 
974             array[2] = getByGroupId_PrevAndNext(session, mbCategory, groupId,
975                     orderByComparator, false);
976 
977             return array;
978         }
979         catch (Exception e) {
980             throw processException(e);
981         }
982         finally {
983             closeSession(session);
984         }
985     }
986 
987     protected MBCategory getByGroupId_PrevAndNext(Session session,
988         MBCategory mbCategory, long groupId,
989         OrderByComparator orderByComparator, boolean previous) {
990         StringBundler query = null;
991 
992         if (orderByComparator != null) {
993             query = new StringBundler(6 +
994                     (orderByComparator.getOrderByFields().length * 6));
995         }
996         else {
997             query = new StringBundler(3);
998         }
999 
1000        query.append(_SQL_SELECT_MBCATEGORY_WHERE);
1001
1002        query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1003
1004        if (orderByComparator != null) {
1005            String[] orderByFields = orderByComparator.getOrderByFields();
1006
1007            if (orderByFields.length > 0) {
1008                query.append(WHERE_AND);
1009            }
1010
1011            for (int i = 0; i < orderByFields.length; i++) {
1012                query.append(_ORDER_BY_ENTITY_ALIAS);
1013                query.append(orderByFields[i]);
1014
1015                if ((i + 1) < orderByFields.length) {
1016                    if (orderByComparator.isAscending() ^ previous) {
1017                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1018                    }
1019                    else {
1020                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1021                    }
1022                }
1023                else {
1024                    if (orderByComparator.isAscending() ^ previous) {
1025                        query.append(WHERE_GREATER_THAN);
1026                    }
1027                    else {
1028                        query.append(WHERE_LESSER_THAN);
1029                    }
1030                }
1031            }
1032
1033            query.append(ORDER_BY_CLAUSE);
1034
1035            for (int i = 0; i < orderByFields.length; i++) {
1036                query.append(_ORDER_BY_ENTITY_ALIAS);
1037                query.append(orderByFields[i]);
1038
1039                if ((i + 1) < orderByFields.length) {
1040                    if (orderByComparator.isAscending() ^ previous) {
1041                        query.append(ORDER_BY_ASC_HAS_NEXT);
1042                    }
1043                    else {
1044                        query.append(ORDER_BY_DESC_HAS_NEXT);
1045                    }
1046                }
1047                else {
1048                    if (orderByComparator.isAscending() ^ previous) {
1049                        query.append(ORDER_BY_ASC);
1050                    }
1051                    else {
1052                        query.append(ORDER_BY_DESC);
1053                    }
1054                }
1055            }
1056        }
1057
1058        else {
1059            query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
1060        }
1061
1062        String sql = query.toString();
1063
1064        Query q = session.createQuery(sql);
1065
1066        q.setFirstResult(0);
1067        q.setMaxResults(2);
1068
1069        QueryPos qPos = QueryPos.getInstance(q);
1070
1071        qPos.add(groupId);
1072
1073        if (orderByComparator != null) {
1074            Object[] values = orderByComparator.getOrderByValues(mbCategory);
1075
1076            for (Object value : values) {
1077                qPos.add(value);
1078            }
1079        }
1080
1081        List<MBCategory> list = q.list();
1082
1083        if (list.size() == 2) {
1084            return list.get(1);
1085        }
1086        else {
1087            return null;
1088        }
1089    }
1090
1091    public List<MBCategory> findByCompanyId(long companyId)
1092        throws SystemException {
1093        return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1094            null);
1095    }
1096
1097    public List<MBCategory> findByCompanyId(long companyId, int start, int end)
1098        throws SystemException {
1099        return findByCompanyId(companyId, start, end, null);
1100    }
1101
1102    public List<MBCategory> findByCompanyId(long companyId, int start, int end,
1103        OrderByComparator orderByComparator) throws SystemException {
1104        Object[] finderArgs = new Object[] {
1105                companyId,
1106                
1107                String.valueOf(start), String.valueOf(end),
1108                String.valueOf(orderByComparator)
1109            };
1110
1111        List<MBCategory> list = (List<MBCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
1112                finderArgs, this);
1113
1114        if (list == null) {
1115            StringBundler query = null;
1116
1117            if (orderByComparator != null) {
1118                query = new StringBundler(3 +
1119                        (orderByComparator.getOrderByFields().length * 3));
1120            }
1121            else {
1122                query = new StringBundler(3);
1123            }
1124
1125            query.append(_SQL_SELECT_MBCATEGORY_WHERE);
1126
1127            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1128
1129            if (orderByComparator != null) {
1130                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1131                    orderByComparator);
1132            }
1133
1134            else {
1135                query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
1136            }
1137
1138            String sql = query.toString();
1139
1140            Session session = null;
1141
1142            try {
1143                session = openSession();
1144
1145                Query q = session.createQuery(sql);
1146
1147                QueryPos qPos = QueryPos.getInstance(q);
1148
1149                qPos.add(companyId);
1150
1151                list = (List<MBCategory>)QueryUtil.list(q, getDialect(), start,
1152                        end);
1153            }
1154            catch (Exception e) {
1155                throw processException(e);
1156            }
1157            finally {
1158                if (list == null) {
1159                    list = new ArrayList<MBCategory>();
1160                }
1161
1162                cacheResult(list);
1163
1164                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
1165                    finderArgs, list);
1166
1167                closeSession(session);
1168            }
1169        }
1170
1171        return list;
1172    }
1173
1174    public MBCategory findByCompanyId_First(long companyId,
1175        OrderByComparator orderByComparator)
1176        throws NoSuchCategoryException, SystemException {
1177        List<MBCategory> list = findByCompanyId(companyId, 0, 1,
1178                orderByComparator);
1179
1180        if (list.isEmpty()) {
1181            StringBundler msg = new StringBundler(4);
1182
1183            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1184
1185            msg.append("companyId=");
1186            msg.append(companyId);
1187
1188            msg.append(StringPool.CLOSE_CURLY_BRACE);
1189
1190            throw new NoSuchCategoryException(msg.toString());
1191        }
1192        else {
1193            return list.get(0);
1194        }
1195    }
1196
1197    public MBCategory findByCompanyId_Last(long companyId,
1198        OrderByComparator orderByComparator)
1199        throws NoSuchCategoryException, SystemException {
1200        int count = countByCompanyId(companyId);
1201
1202        List<MBCategory> list = findByCompanyId(companyId, count - 1, count,
1203                orderByComparator);
1204
1205        if (list.isEmpty()) {
1206            StringBundler msg = new StringBundler(4);
1207
1208            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1209
1210            msg.append("companyId=");
1211            msg.append(companyId);
1212
1213            msg.append(StringPool.CLOSE_CURLY_BRACE);
1214
1215            throw new NoSuchCategoryException(msg.toString());
1216        }
1217        else {
1218            return list.get(0);
1219        }
1220    }
1221
1222    public MBCategory[] findByCompanyId_PrevAndNext(long categoryId,
1223        long companyId, OrderByComparator orderByComparator)
1224        throws NoSuchCategoryException, SystemException {
1225        MBCategory mbCategory = findByPrimaryKey(categoryId);
1226
1227        Session session = null;
1228
1229        try {
1230            session = openSession();
1231
1232            MBCategory[] array = new MBCategoryImpl[3];
1233
1234            array[0] = getByCompanyId_PrevAndNext(session, mbCategory,
1235                    companyId, orderByComparator, true);
1236
1237            array[1] = mbCategory;
1238
1239            array[2] = getByCompanyId_PrevAndNext(session, mbCategory,
1240                    companyId, orderByComparator, false);
1241
1242            return array;
1243        }
1244        catch (Exception e) {
1245            throw processException(e);
1246        }
1247        finally {
1248            closeSession(session);
1249        }
1250    }
1251
1252    protected MBCategory getByCompanyId_PrevAndNext(Session session,
1253        MBCategory mbCategory, long companyId,
1254        OrderByComparator orderByComparator, boolean previous) {
1255        StringBundler query = null;
1256
1257        if (orderByComparator != null) {
1258            query = new StringBundler(6 +
1259                    (orderByComparator.getOrderByFields().length * 6));
1260        }
1261        else {
1262            query = new StringBundler(3);
1263        }
1264
1265        query.append(_SQL_SELECT_MBCATEGORY_WHERE);
1266
1267        query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1268
1269        if (orderByComparator != null) {
1270            String[] orderByFields = orderByComparator.getOrderByFields();
1271
1272            if (orderByFields.length > 0) {
1273                query.append(WHERE_AND);
1274            }
1275
1276            for (int i = 0; i < orderByFields.length; i++) {
1277                query.append(_ORDER_BY_ENTITY_ALIAS);
1278                query.append(orderByFields[i]);
1279
1280                if ((i + 1) < orderByFields.length) {
1281                    if (orderByComparator.isAscending() ^ previous) {
1282                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1283                    }
1284                    else {
1285                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1286                    }
1287                }
1288                else {
1289                    if (orderByComparator.isAscending() ^ previous) {
1290                        query.append(WHERE_GREATER_THAN);
1291                    }
1292                    else {
1293                        query.append(WHERE_LESSER_THAN);
1294                    }
1295                }
1296            }
1297
1298            query.append(ORDER_BY_CLAUSE);
1299
1300            for (int i = 0; i < orderByFields.length; i++) {
1301                query.append(_ORDER_BY_ENTITY_ALIAS);
1302                query.append(orderByFields[i]);
1303
1304                if ((i + 1) < orderByFields.length) {
1305                    if (orderByComparator.isAscending() ^ previous) {
1306                        query.append(ORDER_BY_ASC_HAS_NEXT);
1307                    }
1308                    else {
1309                        query.append(ORDER_BY_DESC_HAS_NEXT);
1310                    }
1311                }
1312                else {
1313                    if (orderByComparator.isAscending() ^ previous) {
1314                        query.append(ORDER_BY_ASC);
1315                    }
1316                    else {
1317                        query.append(ORDER_BY_DESC);
1318                    }
1319                }
1320            }
1321        }
1322
1323        else {
1324            query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
1325        }
1326
1327        String sql = query.toString();
1328
1329        Query q = session.createQuery(sql);
1330
1331        q.setFirstResult(0);
1332        q.setMaxResults(2);
1333
1334        QueryPos qPos = QueryPos.getInstance(q);
1335
1336        qPos.add(companyId);
1337
1338        if (orderByComparator != null) {
1339            Object[] values = orderByComparator.getOrderByValues(mbCategory);
1340
1341            for (Object value : values) {
1342                qPos.add(value);
1343            }
1344        }
1345
1346        List<MBCategory> list = q.list();
1347
1348        if (list.size() == 2) {
1349            return list.get(1);
1350        }
1351        else {
1352            return null;
1353        }
1354    }
1355
1356    public List<MBCategory> findByG_P(long groupId, long parentCategoryId)
1357        throws SystemException {
1358        return findByG_P(groupId, parentCategoryId, QueryUtil.ALL_POS,
1359            QueryUtil.ALL_POS, null);
1360    }
1361
1362    public List<MBCategory> findByG_P(long groupId, long parentCategoryId,
1363        int start, int end) throws SystemException {
1364        return findByG_P(groupId, parentCategoryId, start, end, null);
1365    }
1366
1367    public List<MBCategory> findByG_P(long groupId, long parentCategoryId,
1368        int start, int end, OrderByComparator orderByComparator)
1369        throws SystemException {
1370        Object[] finderArgs = new Object[] {
1371                groupId, parentCategoryId,
1372                
1373                String.valueOf(start), String.valueOf(end),
1374                String.valueOf(orderByComparator)
1375            };
1376
1377        List<MBCategory> list = (List<MBCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_P,
1378                finderArgs, this);
1379
1380        if (list == null) {
1381            StringBundler query = null;
1382
1383            if (orderByComparator != null) {
1384                query = new StringBundler(4 +
1385                        (orderByComparator.getOrderByFields().length * 3));
1386            }
1387            else {
1388                query = new StringBundler(4);
1389            }
1390
1391            query.append(_SQL_SELECT_MBCATEGORY_WHERE);
1392
1393            query.append(_FINDER_COLUMN_G_P_GROUPID_2);
1394
1395            query.append(_FINDER_COLUMN_G_P_PARENTCATEGORYID_2);
1396
1397            if (orderByComparator != null) {
1398                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1399                    orderByComparator);
1400            }
1401
1402            else {
1403                query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
1404            }
1405
1406            String sql = query.toString();
1407
1408            Session session = null;
1409
1410            try {
1411                session = openSession();
1412
1413                Query q = session.createQuery(sql);
1414
1415                QueryPos qPos = QueryPos.getInstance(q);
1416
1417                qPos.add(groupId);
1418
1419                qPos.add(parentCategoryId);
1420
1421                list = (List<MBCategory>)QueryUtil.list(q, getDialect(), start,
1422                        end);
1423            }
1424            catch (Exception e) {
1425                throw processException(e);
1426            }
1427            finally {
1428                if (list == null) {
1429                    list = new ArrayList<MBCategory>();
1430                }
1431
1432                cacheResult(list);
1433
1434                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_P, finderArgs,
1435                    list);
1436
1437                closeSession(session);
1438            }
1439        }
1440
1441        return list;
1442    }
1443
1444    public MBCategory findByG_P_First(long groupId, long parentCategoryId,
1445        OrderByComparator orderByComparator)
1446        throws NoSuchCategoryException, SystemException {
1447        List<MBCategory> list = findByG_P(groupId, parentCategoryId, 0, 1,
1448                orderByComparator);
1449
1450        if (list.isEmpty()) {
1451            StringBundler msg = new StringBundler(6);
1452
1453            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1454
1455            msg.append("groupId=");
1456            msg.append(groupId);
1457
1458            msg.append(", parentCategoryId=");
1459            msg.append(parentCategoryId);
1460
1461            msg.append(StringPool.CLOSE_CURLY_BRACE);
1462
1463            throw new NoSuchCategoryException(msg.toString());
1464        }
1465        else {
1466            return list.get(0);
1467        }
1468    }
1469
1470    public MBCategory findByG_P_Last(long groupId, long parentCategoryId,
1471        OrderByComparator orderByComparator)
1472        throws NoSuchCategoryException, SystemException {
1473        int count = countByG_P(groupId, parentCategoryId);
1474
1475        List<MBCategory> list = findByG_P(groupId, parentCategoryId, count - 1,
1476                count, orderByComparator);
1477
1478        if (list.isEmpty()) {
1479            StringBundler msg = new StringBundler(6);
1480
1481            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1482
1483            msg.append("groupId=");
1484            msg.append(groupId);
1485
1486            msg.append(", parentCategoryId=");
1487            msg.append(parentCategoryId);
1488
1489            msg.append(StringPool.CLOSE_CURLY_BRACE);
1490
1491            throw new NoSuchCategoryException(msg.toString());
1492        }
1493        else {
1494            return list.get(0);
1495        }
1496    }
1497
1498    public MBCategory[] findByG_P_PrevAndNext(long categoryId, long groupId,
1499        long parentCategoryId, OrderByComparator orderByComparator)
1500        throws NoSuchCategoryException, SystemException {
1501        MBCategory mbCategory = findByPrimaryKey(categoryId);
1502
1503        Session session = null;
1504
1505        try {
1506            session = openSession();
1507
1508            MBCategory[] array = new MBCategoryImpl[3];
1509
1510            array[0] = getByG_P_PrevAndNext(session, mbCategory, groupId,
1511                    parentCategoryId, orderByComparator, true);
1512
1513            array[1] = mbCategory;
1514
1515            array[2] = getByG_P_PrevAndNext(session, mbCategory, groupId,
1516                    parentCategoryId, orderByComparator, false);
1517
1518            return array;
1519        }
1520        catch (Exception e) {
1521            throw processException(e);
1522        }
1523        finally {
1524            closeSession(session);
1525        }
1526    }
1527
1528    protected MBCategory getByG_P_PrevAndNext(Session session,
1529        MBCategory mbCategory, long groupId, long parentCategoryId,
1530        OrderByComparator orderByComparator, boolean previous) {
1531        StringBundler query = null;
1532
1533        if (orderByComparator != null) {
1534            query = new StringBundler(6 +
1535                    (orderByComparator.getOrderByFields().length * 6));
1536        }
1537        else {
1538            query = new StringBundler(3);
1539        }
1540
1541        query.append(_SQL_SELECT_MBCATEGORY_WHERE);
1542
1543        query.append(_FINDER_COLUMN_G_P_GROUPID_2);
1544
1545        query.append(_FINDER_COLUMN_G_P_PARENTCATEGORYID_2);
1546
1547        if (orderByComparator != null) {
1548            String[] orderByFields = orderByComparator.getOrderByFields();
1549
1550            if (orderByFields.length > 0) {
1551                query.append(WHERE_AND);
1552            }
1553
1554            for (int i = 0; i < orderByFields.length; i++) {
1555                query.append(_ORDER_BY_ENTITY_ALIAS);
1556                query.append(orderByFields[i]);
1557
1558                if ((i + 1) < orderByFields.length) {
1559                    if (orderByComparator.isAscending() ^ previous) {
1560                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1561                    }
1562                    else {
1563                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1564                    }
1565                }
1566                else {
1567                    if (orderByComparator.isAscending() ^ previous) {
1568                        query.append(WHERE_GREATER_THAN);
1569                    }
1570                    else {
1571                        query.append(WHERE_LESSER_THAN);
1572                    }
1573                }
1574            }
1575
1576            query.append(ORDER_BY_CLAUSE);
1577
1578            for (int i = 0; i < orderByFields.length; i++) {
1579                query.append(_ORDER_BY_ENTITY_ALIAS);
1580                query.append(orderByFields[i]);
1581
1582                if ((i + 1) < orderByFields.length) {
1583                    if (orderByComparator.isAscending() ^ previous) {
1584                        query.append(ORDER_BY_ASC_HAS_NEXT);
1585                    }
1586                    else {
1587                        query.append(ORDER_BY_DESC_HAS_NEXT);
1588                    }
1589                }
1590                else {
1591                    if (orderByComparator.isAscending() ^ previous) {
1592                        query.append(ORDER_BY_ASC);
1593                    }
1594                    else {
1595                        query.append(ORDER_BY_DESC);
1596                    }
1597                }
1598            }
1599        }
1600
1601        else {
1602            query.append(MBCategoryModelImpl.ORDER_BY_JPQL);
1603        }
1604
1605        String sql = query.toString();
1606
1607        Query q = session.createQuery(sql);
1608
1609        q.setFirstResult(0);
1610        q.setMaxResults(2);
1611
1612        QueryPos qPos = QueryPos.getInstance(q);
1613
1614        qPos.add(groupId);
1615
1616        qPos.add(parentCategoryId);
1617
1618        if (orderByComparator != null) {
1619            Object[] values = orderByComparator.getOrderByValues(mbCategory);
1620
1621            for (Object value : values) {
1622                qPos.add(value);
1623            }
1624        }
1625
1626        List<MBCategory> list = q.list();
1627
1628        if (list.size() == 2) {
1629            return list.get(1);
1630        }
1631        else {
1632            return null;
1633        }
1634    }
1635
1636    public List<MBCategory> findAll() throws SystemException {
1637        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1638    }
1639
1640    public List<MBCategory> findAll(int start, int end)
1641        throws SystemException {
1642        return findAll(start, end, null);
1643    }
1644
1645    public List<MBCategory> findAll(int start, int end,
1646        OrderByComparator orderByComparator) throws SystemException {
1647        Object[] finderArgs = new Object[] {
1648                String.valueOf(start), String.valueOf(end),
1649                String.valueOf(orderByComparator)
1650            };
1651
1652        List<MBCategory> list = (List<MBCategory>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1653                finderArgs, this);
1654
1655        if (list == null) {
1656            StringBundler query = null;
1657            String sql = null;
1658
1659            if (orderByComparator != null) {
1660                query = new StringBundler(2 +
1661                        (orderByComparator.getOrderByFields().length * 3));
1662
1663                query.append(_SQL_SELECT_MBCATEGORY);
1664
1665                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1666                    orderByComparator);
1667
1668                sql = query.toString();
1669            }
1670            else {
1671                sql = _SQL_SELECT_MBCATEGORY.concat(MBCategoryModelImpl.ORDER_BY_JPQL);
1672            }
1673
1674            Session session = null;
1675
1676            try {
1677                session = openSession();
1678
1679                Query q = session.createQuery(sql);
1680
1681                if (orderByComparator == null) {
1682                    list = (List<MBCategory>)QueryUtil.list(q, getDialect(),
1683                            start, end, false);
1684
1685                    Collections.sort(list);
1686                }
1687                else {
1688                    list = (List<MBCategory>)QueryUtil.list(q, getDialect(),
1689                            start, end);
1690                }
1691            }
1692            catch (Exception e) {
1693                throw processException(e);
1694            }
1695            finally {
1696                if (list == null) {
1697                    list = new ArrayList<MBCategory>();
1698                }
1699
1700                cacheResult(list);
1701
1702                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1703
1704                closeSession(session);
1705            }
1706        }
1707
1708        return list;
1709    }
1710
1711    public void removeByUuid(String uuid) throws SystemException {
1712        for (MBCategory mbCategory : findByUuid(uuid)) {
1713            remove(mbCategory);
1714        }
1715    }
1716
1717    public void removeByUUID_G(String uuid, long groupId)
1718        throws NoSuchCategoryException, SystemException {
1719        MBCategory mbCategory = findByUUID_G(uuid, groupId);
1720
1721        remove(mbCategory);
1722    }
1723
1724    public void removeByGroupId(long groupId) throws SystemException {
1725        for (MBCategory mbCategory : findByGroupId(groupId)) {
1726            remove(mbCategory);
1727        }
1728    }
1729
1730    public void removeByCompanyId(long companyId) throws SystemException {
1731        for (MBCategory mbCategory : findByCompanyId(companyId)) {
1732            remove(mbCategory);
1733        }
1734    }
1735
1736    public void removeByG_P(long groupId, long parentCategoryId)
1737        throws SystemException {
1738        for (MBCategory mbCategory : findByG_P(groupId, parentCategoryId)) {
1739            remove(mbCategory);
1740        }
1741    }
1742
1743    public void removeAll() throws SystemException {
1744        for (MBCategory mbCategory : findAll()) {
1745            remove(mbCategory);
1746        }
1747    }
1748
1749    public int countByUuid(String uuid) throws SystemException {
1750        Object[] finderArgs = new Object[] { uuid };
1751
1752        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID,
1753                finderArgs, this);
1754
1755        if (count == null) {
1756            StringBundler query = new StringBundler(2);
1757
1758            query.append(_SQL_COUNT_MBCATEGORY_WHERE);
1759
1760            if (uuid == null) {
1761                query.append(_FINDER_COLUMN_UUID_UUID_1);
1762            }
1763            else {
1764                if (uuid.equals(StringPool.BLANK)) {
1765                    query.append(_FINDER_COLUMN_UUID_UUID_3);
1766                }
1767                else {
1768                    query.append(_FINDER_COLUMN_UUID_UUID_2);
1769                }
1770            }
1771
1772            String sql = query.toString();
1773
1774            Session session = null;
1775
1776            try {
1777                session = openSession();
1778
1779                Query q = session.createQuery(sql);
1780
1781                QueryPos qPos = QueryPos.getInstance(q);
1782
1783                if (uuid != null) {
1784                    qPos.add(uuid);
1785                }
1786
1787                count = (Long)q.uniqueResult();
1788            }
1789            catch (Exception e) {
1790                throw processException(e);
1791            }
1792            finally {
1793                if (count == null) {
1794                    count = Long.valueOf(0);
1795                }
1796
1797                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID,
1798                    finderArgs, count);
1799
1800                closeSession(session);
1801            }
1802        }
1803
1804        return count.intValue();
1805    }
1806
1807    public int countByUUID_G(String uuid, long groupId)
1808        throws SystemException {
1809        Object[] finderArgs = new Object[] { uuid, groupId };
1810
1811        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_UUID_G,
1812                finderArgs, this);
1813
1814        if (count == null) {
1815            StringBundler query = new StringBundler(3);
1816
1817            query.append(_SQL_COUNT_MBCATEGORY_WHERE);
1818
1819            if (uuid == null) {
1820                query.append(_FINDER_COLUMN_UUID_G_UUID_1);
1821            }
1822            else {
1823                if (uuid.equals(StringPool.BLANK)) {
1824                    query.append(_FINDER_COLUMN_UUID_G_UUID_3);
1825                }
1826                else {
1827                    query.append(_FINDER_COLUMN_UUID_G_UUID_2);
1828                }
1829            }
1830
1831            query.append(_FINDER_COLUMN_UUID_G_GROUPID_2);
1832
1833            String sql = query.toString();
1834
1835            Session session = null;
1836
1837            try {
1838                session = openSession();
1839
1840                Query q = session.createQuery(sql);
1841
1842                QueryPos qPos = QueryPos.getInstance(q);
1843
1844                if (uuid != null) {
1845                    qPos.add(uuid);
1846                }
1847
1848                qPos.add(groupId);
1849
1850                count = (Long)q.uniqueResult();
1851            }
1852            catch (Exception e) {
1853                throw processException(e);
1854            }
1855            finally {
1856                if (count == null) {
1857                    count = Long.valueOf(0);
1858                }
1859
1860                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_UUID_G,
1861                    finderArgs, count);
1862
1863                closeSession(session);
1864            }
1865        }
1866
1867        return count.intValue();
1868    }
1869
1870    public int countByGroupId(long groupId) throws SystemException {
1871        Object[] finderArgs = new Object[] { groupId };
1872
1873        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1874                finderArgs, this);
1875
1876        if (count == null) {
1877            StringBundler query = new StringBundler(2);
1878
1879            query.append(_SQL_COUNT_MBCATEGORY_WHERE);
1880
1881            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1882
1883            String sql = query.toString();
1884
1885            Session session = null;
1886
1887            try {
1888                session = openSession();
1889
1890                Query q = session.createQuery(sql);
1891
1892                QueryPos qPos = QueryPos.getInstance(q);
1893
1894                qPos.add(groupId);
1895
1896                count = (Long)q.uniqueResult();
1897            }
1898            catch (Exception e) {
1899                throw processException(e);
1900            }
1901            finally {
1902                if (count == null) {
1903                    count = Long.valueOf(0);
1904                }
1905
1906                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1907                    finderArgs, count);
1908
1909                closeSession(session);
1910            }
1911        }
1912
1913        return count.intValue();
1914    }
1915
1916    public int countByCompanyId(long companyId) throws SystemException {
1917        Object[] finderArgs = new Object[] { companyId };
1918
1919        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1920                finderArgs, this);
1921
1922        if (count == null) {
1923            StringBundler query = new StringBundler(2);
1924
1925            query.append(_SQL_COUNT_MBCATEGORY_WHERE);
1926
1927            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1928
1929            String sql = query.toString();
1930
1931            Session session = null;
1932
1933            try {
1934                session = openSession();
1935
1936                Query q = session.createQuery(sql);
1937
1938                QueryPos qPos = QueryPos.getInstance(q);
1939
1940                qPos.add(companyId);
1941
1942                count = (Long)q.uniqueResult();
1943            }
1944            catch (Exception e) {
1945                throw processException(e);
1946            }
1947            finally {
1948                if (count == null) {
1949                    count = Long.valueOf(0);
1950                }
1951
1952                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1953                    finderArgs, count);
1954
1955                closeSession(session);
1956            }
1957        }
1958
1959        return count.intValue();
1960    }
1961
1962    public int countByG_P(long groupId, long parentCategoryId)
1963        throws SystemException {
1964        Object[] finderArgs = new Object[] { groupId, parentCategoryId };
1965
1966        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_P,
1967                finderArgs, this);
1968
1969        if (count == null) {
1970            StringBundler query = new StringBundler(3);
1971
1972            query.append(_SQL_COUNT_MBCATEGORY_WHERE);
1973
1974            query.append(_FINDER_COLUMN_G_P_GROUPID_2);
1975
1976            query.append(_FINDER_COLUMN_G_P_PARENTCATEGORYID_2);
1977
1978            String sql = query.toString();
1979
1980            Session session = null;
1981
1982            try {
1983                session = openSession();
1984
1985                Query q = session.createQuery(sql);
1986
1987                QueryPos qPos = QueryPos.getInstance(q);
1988
1989                qPos.add(groupId);
1990
1991                qPos.add(parentCategoryId);
1992
1993                count = (Long)q.uniqueResult();
1994            }
1995            catch (Exception e) {
1996                throw processException(e);
1997            }
1998            finally {
1999                if (count == null) {
2000                    count = Long.valueOf(0);
2001                }
2002
2003                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_P, finderArgs,
2004                    count);
2005
2006                closeSession(session);
2007            }
2008        }
2009
2010        return count.intValue();
2011    }
2012
2013    public int countAll() throws SystemException {
2014        Object[] finderArgs = new Object[0];
2015
2016        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2017                finderArgs, this);
2018
2019        if (count == null) {
2020            Session session = null;
2021
2022            try {
2023                session = openSession();
2024
2025                Query q = session.createQuery(_SQL_COUNT_MBCATEGORY);
2026
2027                count = (Long)q.uniqueResult();
2028            }
2029            catch (Exception e) {
2030                throw processException(e);
2031            }
2032            finally {
2033                if (count == null) {
2034                    count = Long.valueOf(0);
2035                }
2036
2037                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
2038                    count);
2039
2040                closeSession(session);
2041            }
2042        }
2043
2044        return count.intValue();
2045    }
2046
2047    public void afterPropertiesSet() {
2048        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2049                    com.liferay.portal.util.PropsUtil.get(
2050                        "value.object.listener.com.liferay.portlet.messageboards.model.MBCategory")));
2051
2052        if (listenerClassNames.length > 0) {
2053            try {
2054                List<ModelListener<MBCategory>> listenersList = new ArrayList<ModelListener<MBCategory>>();
2055
2056                for (String listenerClassName : listenerClassNames) {
2057                    listenersList.add((ModelListener<MBCategory>)InstanceFactory.newInstance(
2058                            listenerClassName));
2059                }
2060
2061                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2062            }
2063            catch (Exception e) {
2064                _log.error(e);
2065            }
2066        }
2067    }
2068
2069    public void destroy() {
2070        EntityCacheUtil.removeCache(MBCategoryImpl.class.getName());
2071        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2072        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
2073    }
2074
2075    @BeanReference(type = MBBanPersistence.class)
2076    protected MBBanPersistence mbBanPersistence;
2077    @BeanReference(type = MBCategoryPersistence.class)
2078    protected MBCategoryPersistence mbCategoryPersistence;
2079    @BeanReference(type = MBDiscussionPersistence.class)
2080    protected MBDiscussionPersistence mbDiscussionPersistence;
2081    @BeanReference(type = MBMailingListPersistence.class)
2082    protected MBMailingListPersistence mbMailingListPersistence;
2083    @BeanReference(type = MBMessagePersistence.class)
2084    protected MBMessagePersistence mbMessagePersistence;
2085    @BeanReference(type = MBMessageFlagPersistence.class)
2086    protected MBMessageFlagPersistence mbMessageFlagPersistence;
2087    @BeanReference(type = MBStatsUserPersistence.class)
2088    protected MBStatsUserPersistence mbStatsUserPersistence;
2089    @BeanReference(type = MBThreadPersistence.class)
2090    protected MBThreadPersistence mbThreadPersistence;
2091    @BeanReference(type = GroupPersistence.class)
2092    protected GroupPersistence groupPersistence;
2093    @BeanReference(type = ResourcePersistence.class)
2094    protected ResourcePersistence resourcePersistence;
2095    @BeanReference(type = SubscriptionPersistence.class)
2096    protected SubscriptionPersistence subscriptionPersistence;
2097    @BeanReference(type = UserPersistence.class)
2098    protected UserPersistence userPersistence;
2099    @BeanReference(type = TagsEntryPersistence.class)
2100    protected TagsEntryPersistence tagsEntryPersistence;
2101    private static final String _SQL_SELECT_MBCATEGORY = "SELECT mbCategory FROM MBCategory mbCategory";
2102    private static final String _SQL_SELECT_MBCATEGORY_WHERE = "SELECT mbCategory FROM MBCategory mbCategory WHERE ";
2103    private static final String _SQL_COUNT_MBCATEGORY = "SELECT COUNT(mbCategory) FROM MBCategory mbCategory";
2104    private static final String _SQL_COUNT_MBCATEGORY_WHERE = "SELECT COUNT(mbCategory) FROM MBCategory mbCategory WHERE ";
2105    private static final String _FINDER_COLUMN_UUID_UUID_1 = "mbCategory.uuid IS NULL";
2106    private static final String _FINDER_COLUMN_UUID_UUID_2 = "mbCategory.uuid = ?";
2107    private static final String _FINDER_COLUMN_UUID_UUID_3 = "(mbCategory.uuid IS NULL OR mbCategory.uuid = ?)";
2108    private static final String _FINDER_COLUMN_UUID_G_UUID_1 = "mbCategory.uuid IS NULL AND ";
2109    private static final String _FINDER_COLUMN_UUID_G_UUID_2 = "mbCategory.uuid = ? AND ";
2110    private static final String _FINDER_COLUMN_UUID_G_UUID_3 = "(mbCategory.uuid IS NULL OR mbCategory.uuid = ?) AND ";
2111    private static final String _FINDER_COLUMN_UUID_G_GROUPID_2 = "mbCategory.groupId = ?";
2112    private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "mbCategory.groupId = ?";
2113    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "mbCategory.companyId = ?";
2114    private static final String _FINDER_COLUMN_G_P_GROUPID_2 = "mbCategory.groupId = ? AND ";
2115    private static final String _FINDER_COLUMN_G_P_PARENTCATEGORYID_2 = "mbCategory.parentCategoryId = ?";
2116    private static final String _ORDER_BY_ENTITY_ALIAS = "mbCategory.";
2117    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBCategory exists with the primary key ";
2118    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBCategory exists with the key {";
2119    private static Log _log = LogFactoryUtil.getLog(MBCategoryPersistenceImpl.class);
2120}