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