001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Date;
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     * @author Shuyang Zhou
041     */
042    public class MBThreadFinderImpl
043            extends BasePersistenceImpl<MBThread> implements MBThreadFinder {
044    
045            public static final String COUNT_BY_G_C =
046                    MBThreadFinder.class.getName() + ".countByG_C";
047    
048            public static final String COUNT_BY_G_U_S =
049                    MBThreadFinder.class.getName() + ".countByG_U_S";
050    
051            public static final String COUNT_BY_G_U_MD_S =
052                    MBThreadFinder.class.getName() + ".countByG_U_MD_S";
053    
054            public static final String COUNT_BY_G_U_A_S =
055                    MBThreadFinder.class.getName() + ".countByG_U_A_S";
056    
057            public static final String COUNT_BY_S_G_U_S =
058                    MBThreadFinder.class.getName() + ".countByS_G_U_S";
059    
060            public static final String COUNT_BY_S_G_U_C_S =
061                    MBThreadFinder.class.getName() + ".countByS_G_U_C_S";
062    
063            public static final String FIND_BY_NO_ASSETS =
064                    MBThreadFinder.class.getName() + ".findByNoAssets";
065    
066            public static final String FIND_BY_G_C =
067                    MBThreadFinder.class.getName() + ".findByG_C";
068    
069            public static final String FIND_BY_G_U_S =
070                    MBThreadFinder.class.getName() + ".findByG_U_S";
071    
072            public static final String FIND_BY_G_U_MD_S =
073                    MBThreadFinder.class.getName() + ".findByG_U_MD_S";
074    
075            public static final String FIND_BY_G_U_A_S =
076                    MBThreadFinder.class.getName() + ".findByG_U_A_S";
077    
078            public static final String FIND_BY_S_G_U_S =
079                    MBThreadFinder.class.getName() + ".findByS_G_U_S";
080    
081            public static final String FIND_BY_S_G_U_C_S =
082                    MBThreadFinder.class.getName() + ".findByS_G_U_C_S";
083    
084            @Override
085            public int countByG_U_S(long groupId, long userId, int status)
086                    throws SystemException {
087    
088                    Session session = null;
089    
090                    try {
091                            session = openSession();
092    
093                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_S);
094    
095                            if (status != WorkflowConstants.STATUS_ANY) {
096                                    sql = CustomSQLUtil.appendCriteria(
097                                            sql, "AND (MBMessage.status = ?)");
098                            }
099    
100                            SQLQuery q = session.createSQLQuery(sql);
101    
102                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
103    
104                            QueryPos qPos = QueryPos.getInstance(q);
105    
106                            qPos.add(groupId);
107                            qPos.add(userId);
108    
109                            if (status != WorkflowConstants.STATUS_ANY) {
110                                    qPos.add(status);
111                            }
112    
113                            Iterator<Long> itr = q.iterate();
114    
115                            if (itr.hasNext()) {
116                                    Long count = itr.next();
117    
118                                    if (count != null) {
119                                            return count.intValue();
120                                    }
121                            }
122    
123                            return 0;
124                    }
125                    catch (Exception e) {
126                            throw new SystemException(e);
127                    }
128                    finally {
129                            closeSession(session);
130                    }
131            }
132    
133            @Override
134            public int countByG_C_S(long groupId, long categoryId, int status)
135                    throws SystemException {
136    
137                    return doCountByG_C_S(groupId, categoryId, status, false);
138            }
139    
140            @Override
141            public int countByG_U_MD_S(
142                            long groupId, long userId, Date modifiedDate, int status)
143                    throws SystemException {
144    
145                    Session session = null;
146    
147                    try {
148                            session = openSession();
149    
150                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_MD_S);
151    
152                            if (userId <= 0) {
153                                    sql = StringUtil.replace(
154                                            sql, _INNER_JOIN_SQL, StringPool.BLANK);
155                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
156                            }
157    
158                            if (status != WorkflowConstants.STATUS_ANY) {
159                                    sql = CustomSQLUtil.appendCriteria(
160                                            sql, "AND (MBThread.status = ?)");
161                            }
162    
163                            SQLQuery q = session.createSQLQuery(sql);
164    
165                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
166    
167                            QueryPos qPos = QueryPos.getInstance(q);
168    
169                            qPos.add(groupId);
170                            qPos.add(modifiedDate);
171    
172                            if (userId > 0) {
173                                    qPos.add(userId);
174                            }
175    
176                            if (status != WorkflowConstants.STATUS_ANY) {
177                                    qPos.add(status);
178                            }
179    
180                            Iterator<Long> itr = q.iterate();
181    
182                            if (itr.hasNext()) {
183                                    Long count = itr.next();
184    
185                                    if (count != null) {
186                                            return count.intValue();
187                                    }
188                            }
189    
190                            return 0;
191                    }
192                    catch (Exception e) {
193                            throw new SystemException(e);
194                    }
195                    finally {
196                            closeSession(session);
197                    }
198            }
199    
200            @Override
201            public int countByG_U_A_S(
202                            long groupId, long userId, boolean anonymous, int status)
203                    throws SystemException {
204    
205                    Session session = null;
206    
207                    try {
208                            session = openSession();
209    
210                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_A_S);
211    
212                            if (status != WorkflowConstants.STATUS_ANY) {
213                                    sql = CustomSQLUtil.appendCriteria(
214                                            sql, "AND (MBMessage.status = ?)");
215                            }
216    
217                            SQLQuery q = session.createSQLQuery(sql);
218    
219                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
220    
221                            QueryPos qPos = QueryPos.getInstance(q);
222    
223                            qPos.add(groupId);
224                            qPos.add(userId);
225                            qPos.add(anonymous);
226    
227                            if (status != WorkflowConstants.STATUS_ANY) {
228                                    qPos.add(status);
229                            }
230    
231                            Iterator<Long> itr = q.iterate();
232    
233                            if (itr.hasNext()) {
234                                    Long count = itr.next();
235    
236                                    if (count != null) {
237                                            return count.intValue();
238                                    }
239                            }
240    
241                            return 0;
242                    }
243                    catch (Exception e) {
244                            throw new SystemException(e);
245                    }
246                    finally {
247                            closeSession(session);
248                    }
249            }
250    
251            @Override
252            public int countByS_G_U_S(long groupId, long userId, int status)
253                    throws SystemException {
254    
255                    return doCountByS_G_U_S(groupId, userId, status);
256            }
257    
258            @Override
259            public int countByS_G_U_C_S(
260                            long groupId, long userId, long[] categoryIds, int status)
261                    throws SystemException {
262    
263                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, false);
264            }
265    
266            @Override
267            public int filterCountByG_C(long groupId, long categoryId)
268                    throws SystemException {
269    
270                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
271                            return MBThreadUtil.countByG_C(groupId, categoryId);
272                    }
273    
274                    Session session = null;
275    
276                    try {
277                            session = openSession();
278    
279                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
280    
281                            sql = InlineSQLHelperUtil.replacePermissionCheck(
282                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
283                                    groupId);
284    
285                            SQLQuery q = session.createSQLQuery(sql);
286    
287                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
288    
289                            QueryPos qPos = QueryPos.getInstance(q);
290    
291                            qPos.add(groupId);
292                            qPos.add(categoryId);
293    
294                            Iterator<Long> itr = q.iterate();
295    
296                            if (itr.hasNext()) {
297                                    Long count = itr.next();
298    
299                                    if (count != null) {
300                                            return count.intValue();
301                                    }
302                            }
303    
304                            return 0;
305                    }
306                    catch (Exception e) {
307                            throw processException(e);
308                    }
309                    finally {
310                            closeSession(session);
311                    }
312            }
313    
314            @Override
315            public int filterCountByG_C_S(long groupId, long categoryId, int status)
316                    throws SystemException {
317    
318                    return doCountByG_C_S(groupId, categoryId, status, true);
319            }
320    
321            @Override
322            public int filterCountByS_G_U_C_S(
323                            long groupId, long userId, long[] categoryIds, int status)
324                    throws SystemException {
325    
326                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, true);
327            }
328    
329            @Override
330            public List<MBThread> filterFindByG_C(
331                            long groupId, long categoryId, int start, int end)
332                    throws SystemException {
333    
334                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
335                            return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
336                    }
337    
338                    Session session = null;
339    
340                    try {
341                            session = openSession();
342    
343                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
344    
345                            sql = InlineSQLHelperUtil.replacePermissionCheck(
346                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
347                                    groupId);
348    
349                            SQLQuery q = session.createSQLQuery(sql);
350    
351                            q.addEntity("MBThread", MBThreadImpl.class);
352    
353                            QueryPos qPos = QueryPos.getInstance(q);
354    
355                            qPos.add(groupId);
356                            qPos.add(categoryId);
357    
358                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
359                    }
360                    catch (Exception e) {
361                            throw new SystemException(e);
362                    }
363                    finally {
364                            closeSession(session);
365                    }
366            }
367    
368            @Override
369            public List<MBThread> filterFindByG_C_S(
370                            long groupId, long categoryId, int status, int start, int end)
371                    throws SystemException {
372    
373                    return doFindByG_C_S(groupId, categoryId, status, start, end, true);
374            }
375    
376            @Override
377            public List<MBThread> filterFindByS_G_U_C_S(
378                            long groupId, long userId, long[] categoryIds, int status,
379                            int start, int end)
380                    throws SystemException {
381    
382                    return doFindByS_G_U_C_S(
383                            groupId, userId, categoryIds, status, start, end, true);
384            }
385    
386            @Override
387            public List<MBThread> findByNoAssets() throws SystemException {
388                    Session session = null;
389    
390                    try {
391                            session = openSession();
392    
393                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
394    
395                            SQLQuery q = session.createSQLQuery(sql);
396    
397                            q.addEntity("MBThread", MBThreadImpl.class);
398    
399                            return q.list(true);
400                    }
401                    catch (Exception e) {
402                            throw new SystemException(e);
403                    }
404                    finally {
405                            closeSession(session);
406                    }
407            }
408    
409            @Override
410            public List<MBThread> findByG_U_S(
411                            long groupId, long userId, int status, int start, int end)
412                    throws SystemException {
413    
414                    Session session = null;
415    
416                    try {
417                            session = openSession();
418    
419                            String sql = CustomSQLUtil.get(FIND_BY_G_U_S);
420    
421                            if (status != WorkflowConstants.STATUS_ANY) {
422                                    sql = CustomSQLUtil.appendCriteria(
423                                            sql, "AND (MBMessage.status = ?)");
424                            }
425    
426                            SQLQuery q = session.createSQLQuery(sql);
427    
428                            q.addEntity("MBThread", MBThreadImpl.class);
429    
430                            QueryPos qPos = QueryPos.getInstance(q);
431    
432                            qPos.add(groupId);
433                            qPos.add(userId);
434    
435                            if (status != WorkflowConstants.STATUS_ANY) {
436                                    qPos.add(status);
437                            }
438    
439                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
440                    }
441                    catch (Exception e) {
442                            throw new SystemException(e);
443                    }
444                    finally {
445                            closeSession(session);
446                    }
447            }
448    
449            @Override
450            public List<MBThread> findByG_C_S(
451                            long groupId, long categoryId, int status, int start, int end)
452                    throws SystemException {
453    
454                    return doFindByG_C_S(groupId, categoryId, status, start, end, false);
455            }
456    
457            @Override
458            public List<MBThread> findByG_U_MD_S(
459                            long groupId, long userId, Date modifiedDate, int status, int start,
460                            int end)
461                    throws SystemException {
462    
463                    Session session = null;
464    
465                    try {
466                            session = openSession();
467    
468                            String sql = CustomSQLUtil.get(FIND_BY_G_U_MD_S);
469    
470                            if (userId <= 0) {
471                                    sql = StringUtil.replace(
472                                            sql, _INNER_JOIN_SQL, StringPool.BLANK);
473                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
474                            }
475    
476                            if (status != WorkflowConstants.STATUS_ANY) {
477                                    sql = CustomSQLUtil.appendCriteria(
478                                            sql, "AND (MBThread.status = ?)");
479                            }
480    
481                            SQLQuery q = session.createSQLQuery(sql);
482    
483                            q.addEntity("MBThread", MBThreadImpl.class);
484    
485                            QueryPos qPos = QueryPos.getInstance(q);
486    
487                            qPos.add(groupId);
488                            qPos.add(modifiedDate);
489    
490                            if (userId > 0) {
491                                    qPos.add(userId);
492                            }
493    
494                            if (status != WorkflowConstants.STATUS_ANY) {
495                                    qPos.add(status);
496                            }
497    
498                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
499                    }
500                    catch (Exception e) {
501                            throw new SystemException(e);
502                    }
503                    finally {
504                            closeSession(session);
505                    }
506            }
507    
508            @Override
509            public List<MBThread> findByG_U_A_S(
510                            long groupId, long userId, boolean anonymous, int status, int start,
511                            int end)
512                    throws SystemException {
513    
514                    Session session = null;
515    
516                    try {
517                            session = openSession();
518    
519                            String sql = CustomSQLUtil.get(FIND_BY_G_U_A_S);
520    
521                            if (status != WorkflowConstants.STATUS_ANY) {
522                                    sql = CustomSQLUtil.appendCriteria(
523                                            sql, "AND (MBMessage.status = ?)");
524                            }
525    
526                            SQLQuery q = session.createSQLQuery(sql);
527    
528                            q.addEntity("MBThread", MBThreadImpl.class);
529    
530                            QueryPos qPos = QueryPos.getInstance(q);
531    
532                            qPos.add(groupId);
533                            qPos.add(userId);
534                            qPos.add(anonymous);
535    
536                            if (status != WorkflowConstants.STATUS_ANY) {
537                                    qPos.add(status);
538                            }
539    
540                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
541                    }
542                    catch (Exception e) {
543                            throw new SystemException(e);
544                    }
545                    finally {
546                            closeSession(session);
547                    }
548            }
549    
550            @Override
551            public List<MBThread> findByS_G_U_S(
552                            long groupId, long userId, int status, int start, int end)
553                    throws SystemException {
554    
555                    Session session = null;
556    
557                    try {
558                            session = openSession();
559    
560                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_S);
561    
562                            if (status != WorkflowConstants.STATUS_ANY) {
563                                    sql = CustomSQLUtil.appendCriteria(
564                                            sql, "AND (MBThread.status = ?)");
565                            }
566    
567                            SQLQuery q = session.createSQLQuery(sql);
568    
569                            q.addEntity("MBThread", MBThreadImpl.class);
570    
571                            QueryPos qPos = QueryPos.getInstance(q);
572    
573                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
574                            qPos.add(groupId);
575                            qPos.add(userId);
576    
577                            if (status != WorkflowConstants.STATUS_ANY) {
578                                    qPos.add(status);
579                            }
580    
581                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
582                    }
583                    catch (Exception e) {
584                            throw new SystemException(e);
585                    }
586                    finally {
587                            closeSession(session);
588                    }
589            }
590    
591            @Override
592            public List<MBThread> findByS_G_U_C_S(
593                            long groupId, long userId, long[] categoryIds, int status,
594                            int start, int end)
595                    throws SystemException {
596    
597                    return doFindByS_G_U_C_S(
598                            groupId, userId, categoryIds, status, start, end, false);
599            }
600    
601            protected int doCountByG_C_S(
602                            long groupId, long categoryId, int status, boolean inlineSQLHelper)
603                    throws SystemException {
604    
605                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
606                            if (status != WorkflowConstants.STATUS_ANY) {
607                                    return MBThreadUtil.countByG_C_S(groupId, categoryId, status);
608                            }
609                            else {
610                                    return MBThreadUtil.countByG_C(groupId, categoryId);
611                            }
612                    }
613    
614                    Session session = null;
615    
616                    try {
617                            session = openSession();
618    
619                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
620    
621                            if (status != WorkflowConstants.STATUS_ANY) {
622                                    sql = CustomSQLUtil.appendCriteria(
623                                            sql, "AND (MBThread.status = ?)");
624                            }
625    
626                            sql = InlineSQLHelperUtil.replacePermissionCheck(
627                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
628                                    groupId);
629    
630                            SQLQuery q = session.createSQLQuery(sql);
631    
632                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
633    
634                            QueryPos qPos = QueryPos.getInstance(q);
635    
636                            qPos.add(groupId);
637                            qPos.add(categoryId);
638    
639                            if (status != WorkflowConstants.STATUS_ANY) {
640                                    qPos.add(status);
641                            }
642    
643                            Iterator<Long> itr = q.iterate();
644    
645                            if (itr.hasNext()) {
646                                    Long count = itr.next();
647    
648                                    if (count != null) {
649                                            return count.intValue();
650                                    }
651                            }
652    
653                            return 0;
654                    }
655                    catch (Exception e) {
656                            throw processException(e);
657                    }
658                    finally {
659                            closeSession(session);
660                    }
661            }
662    
663            protected int doCountByS_G_U_S(long groupId, long userId, int status)
664                    throws SystemException {
665    
666                    Session session = null;
667    
668                    try {
669                            session = openSession();
670    
671                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_S);
672    
673                            if (status != WorkflowConstants.STATUS_ANY) {
674                                    sql = CustomSQLUtil.appendCriteria(
675                                            sql, "AND (MBThread.status = ?)");
676                            }
677    
678                            SQLQuery q = session.createSQLQuery(sql);
679    
680                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
681    
682                            QueryPos qPos = QueryPos.getInstance(q);
683    
684                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
685                            qPos.add(groupId);
686                            qPos.add(userId);
687    
688                            if (status != WorkflowConstants.STATUS_ANY) {
689                                    qPos.add(status);
690                            }
691    
692                            Iterator<Long> itr = q.iterate();
693    
694                            if (itr.hasNext()) {
695                                    Long count = itr.next();
696    
697                                    if (count != null) {
698                                            return count.intValue();
699                                    }
700                            }
701    
702                            return 0;
703                    }
704                    catch (Exception e) {
705                            throw new SystemException(e);
706                    }
707                    finally {
708                            closeSession(session);
709                    }
710            }
711    
712            protected int doCountByS_G_U_C_S(
713                            long groupId, long userId, long[] categoryIds, int status,
714                            boolean inlineSQLHelper)
715                    throws SystemException {
716    
717                    Session session = null;
718    
719                    try {
720                            session = openSession();
721    
722                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_C_S);
723    
724                            if ((categoryIds == null) || (categoryIds.length == 0)) {
725                                    sql = StringUtil.replace(
726                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
727                            }
728                            else {
729                                    sql = StringUtil.replace(
730                                            sql, "MBThread.categoryId = ?",
731                                            "MBThread.categoryId = " +
732                                                    StringUtil.merge(
733                                                            categoryIds, " OR MBThread.categoryId = "));
734                            }
735    
736                            if (status != WorkflowConstants.STATUS_ANY) {
737                                    sql = CustomSQLUtil.appendCriteria(
738                                            sql, "AND (MBThread.status = ?)");
739                            }
740    
741                            if (inlineSQLHelper) {
742                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
743                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
744                                            groupId);
745                            }
746    
747                            SQLQuery q = session.createSQLQuery(sql);
748    
749                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
750    
751                            QueryPos qPos = QueryPos.getInstance(q);
752    
753                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
754                            qPos.add(groupId);
755                            qPos.add(userId);
756    
757                            if (status != WorkflowConstants.STATUS_ANY) {
758                                    qPos.add(status);
759                            }
760    
761                            Iterator<Long> itr = q.iterate();
762    
763                            if (itr.hasNext()) {
764                                    Long count = itr.next();
765    
766                                    if (count != null) {
767                                            return count.intValue();
768                                    }
769                            }
770    
771                            return 0;
772                    }
773                    catch (Exception e) {
774                            throw new SystemException(e);
775                    }
776                    finally {
777                            closeSession(session);
778                    }
779            }
780    
781            protected List<MBThread> doFindByG_C_S(
782                            long groupId, long categoryId, int status, int start, int end,
783                            boolean inlineSQLHelper)
784                    throws SystemException {
785    
786                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
787                            if (status != WorkflowConstants.STATUS_ANY) {
788                                    return MBThreadUtil.findByG_C_S(
789                                            groupId, categoryId, status, start, end);
790                            }
791                            else {
792                                    return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
793                            }
794                    }
795    
796                    Session session = null;
797    
798                    try {
799                            session = openSession();
800    
801                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
802    
803                            if (status != WorkflowConstants.STATUS_ANY) {
804                                    sql = CustomSQLUtil.appendCriteria(
805                                            sql, "AND (MBThread.status = ?)");
806                            }
807    
808                            sql = InlineSQLHelperUtil.replacePermissionCheck(
809                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
810                                    groupId);
811    
812                            SQLQuery q = session.createSQLQuery(sql);
813    
814                            q.addEntity("MBThread", MBThreadImpl.class);
815    
816                            QueryPos qPos = QueryPos.getInstance(q);
817    
818                            qPos.add(groupId);
819                            qPos.add(categoryId);
820    
821                            if (status != WorkflowConstants.STATUS_ANY) {
822                                    qPos.add(status);
823                            }
824    
825                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
826                    }
827                    catch (Exception e) {
828                            throw new SystemException(e);
829                    }
830                    finally {
831                            closeSession(session);
832                    }
833            }
834    
835            protected List<MBThread> doFindByS_G_U_C_S(
836                            long groupId, long userId, long[] categoryIds, int status,
837                            int start, int end, boolean inlineSQLHelper)
838                    throws SystemException {
839    
840                    Session session = null;
841    
842                    try {
843                            session = openSession();
844    
845                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_C_S);
846    
847                            if ((categoryIds == null) || (categoryIds.length == 0)) {
848                                    sql = StringUtil.replace(
849                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
850                            }
851                            else {
852                                    sql = StringUtil.replace(
853                                            sql, "MBThread.categoryId = ?",
854                                            "MBThread.categoryId = " +
855                                                    StringUtil.merge(
856                                                            categoryIds, " OR MBThread.categoryId = "));
857                            }
858    
859                            if (status != WorkflowConstants.STATUS_ANY) {
860                                    sql = CustomSQLUtil.appendCriteria(
861                                            sql, "AND (MBThread.status = ?)");
862                            }
863    
864                            if (inlineSQLHelper) {
865                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
866                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
867                                            groupId);
868                            }
869    
870                            SQLQuery q = session.createSQLQuery(sql);
871    
872                            q.addEntity("MBThread", MBThreadImpl.class);
873    
874                            QueryPos qPos = QueryPos.getInstance(q);
875    
876                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
877                            qPos.add(groupId);
878                            qPos.add(userId);
879    
880                            if (status != WorkflowConstants.STATUS_ANY) {
881                                    qPos.add(status);
882                            }
883    
884                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
885                    }
886                    catch (Exception e) {
887                            throw new SystemException(e);
888                    }
889                    finally {
890                            closeSession(session);
891                    }
892            }
893    
894            private static final String _INNER_JOIN_SQL =
895                    "INNER JOIN MBMessage ON (MBThread.threadId = MBMessage.threadId)";
896    
897            private static final String _USER_ID_SQL = "AND (MBMessage.userId = ?)";
898    
899    }