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