001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryDefinition;
018    import com.liferay.portal.kernel.dao.orm.QueryPos;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.messageboards.model.MBMessage;
031    import com.liferay.portlet.messageboards.model.MBThread;
032    import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
033    import com.liferay.portlet.messageboards.service.persistence.MBThreadFinder;
034    import com.liferay.portlet.messageboards.service.persistence.MBThreadUtil;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.Date;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Shuyang Zhou
044     */
045    public class MBThreadFinderImpl
046            extends MBThreadFinderBaseImpl implements MBThreadFinder {
047    
048            public static final String COUNT_BY_G_U =
049                    MBThreadFinder.class.getName() + ".countByG_U";
050    
051            public static final String COUNT_BY_G_C =
052                    MBThreadFinder.class.getName() + ".countByG_C";
053    
054            public static final String COUNT_BY_G_U_C =
055                    MBThreadFinder.class.getName() + ".countByG_U_C";
056    
057            public static final String COUNT_BY_G_U_LPD =
058                    MBThreadFinder.class.getName() + ".countByG_U_LPD";
059    
060            public static final String COUNT_BY_G_U_A =
061                    MBThreadFinder.class.getName() + ".countByG_U_A";
062    
063            public static final String COUNT_BY_S_G_U =
064                    MBThreadFinder.class.getName() + ".countByS_G_U";
065    
066            public static final String COUNT_BY_G_U_C_A =
067                    MBThreadFinder.class.getName() + ".countByG_U_C_A";
068    
069            public static final String COUNT_BY_S_G_U_C =
070                    MBThreadFinder.class.getName() + ".countByS_G_U_C";
071    
072            public static final String FIND_BY_NO_ASSETS =
073                    MBThreadFinder.class.getName() + ".findByNoAssets";
074    
075            public static final String FIND_BY_G_U =
076                    MBThreadFinder.class.getName() + ".findByG_U";
077    
078            public static final String FIND_BY_G_C =
079                    MBThreadFinder.class.getName() + ".findByG_C";
080    
081            public static final String FIND_BY_G_U_C =
082                    MBThreadFinder.class.getName() + ".findByG_U_C";
083    
084            public static final String FIND_BY_G_U_LPD =
085                    MBThreadFinder.class.getName() + ".findByG_U_LPD";
086    
087            public static final String FIND_BY_G_U_A =
088                    MBThreadFinder.class.getName() + ".findByG_U_A";
089    
090            public static final String FIND_BY_S_G_U =
091                    MBThreadFinder.class.getName() + ".findByS_G_U";
092    
093            public static final String FIND_BY_G_U_C_A =
094                    MBThreadFinder.class.getName() + ".findByG_U_C_A";
095    
096            public static final String FIND_BY_S_G_U_C =
097                    MBThreadFinder.class.getName() + ".findByS_G_U_C";
098    
099            @Override
100            public int countByG_U(
101                    long groupId, long userId, QueryDefinition<MBThread> queryDefinition) {
102    
103                    Session session = null;
104    
105                    try {
106                            session = openSession();
107    
108                            String sql = CustomSQLUtil.get(COUNT_BY_G_U);
109    
110                            sql = updateSQL(sql, queryDefinition);
111    
112                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
113    
114                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
115    
116                            QueryPos qPos = QueryPos.getInstance(q);
117    
118                            qPos.add(groupId);
119                            qPos.add(userId);
120    
121                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
122                                    qPos.add(queryDefinition.getStatus());
123                            }
124    
125                            Iterator<Long> itr = q.iterate();
126    
127                            if (itr.hasNext()) {
128                                    Long count = itr.next();
129    
130                                    if (count != null) {
131                                            return count.intValue();
132                                    }
133                            }
134    
135                            return 0;
136                    }
137                    catch (Exception e) {
138                            throw new SystemException(e);
139                    }
140                    finally {
141                            closeSession(session);
142                    }
143            }
144    
145            @Override
146            public int countByG_C(
147                    long groupId, long categoryId,
148                    QueryDefinition<MBThread> queryDefinition) {
149    
150                    return doCountByG_C(groupId, categoryId, queryDefinition, false);
151            }
152    
153            @Override
154            public int countByG_U_C(
155                    long groupId, long userId, long[] categoryIds,
156                    QueryDefinition<MBThread> queryDefinition) {
157    
158                    Session session = null;
159    
160                    try {
161                            session = openSession();
162    
163                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C);
164    
165                            if (ArrayUtil.isEmpty(categoryIds)) {
166                                    sql = StringUtil.replace(
167                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
168                            }
169                            else {
170                                    sql = StringUtil.replace(
171                                            sql, "MBThread.categoryId = ?",
172                                            "MBThread.categoryId = " +
173                                                    StringUtil.merge(
174                                                            categoryIds, " OR MBThread.categoryId = "));
175                            }
176    
177                            sql = updateSQL(sql, queryDefinition);
178    
179                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
180    
181                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
182    
183                            QueryPos qPos = QueryPos.getInstance(q);
184    
185                            qPos.add(groupId);
186                            qPos.add(userId);
187    
188                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
189                                    qPos.add(queryDefinition.getStatus());
190                            }
191    
192                            Iterator<Long> itr = q.iterate();
193    
194                            if (itr.hasNext()) {
195                                    Long count = itr.next();
196    
197                                    if (count != null) {
198                                            return count.intValue();
199                                    }
200                            }
201    
202                            return 0;
203                    }
204                    catch (Exception e) {
205                            throw new SystemException(e);
206                    }
207                    finally {
208                            closeSession(session);
209                    }
210            }
211    
212            @Override
213            public int countByG_U_LPD(
214                    long groupId, long userId, Date lastPostDate,
215                    QueryDefinition<MBThread> queryDefinition) {
216    
217                    Session session = null;
218    
219                    try {
220                            session = openSession();
221    
222                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_LPD);
223    
224                            if (userId <= 0) {
225                                    sql = StringUtil.replace(
226                                            sql, _INNER_JOIN_SQL, StringPool.BLANK);
227                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
228                            }
229    
230                            sql = updateSQL(sql, queryDefinition);
231    
232                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
233    
234                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
235    
236                            QueryPos qPos = QueryPos.getInstance(q);
237    
238                            qPos.add(groupId);
239                            qPos.add(lastPostDate);
240    
241                            if (userId > 0) {
242                                    qPos.add(userId);
243                            }
244    
245                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
246                                    qPos.add(queryDefinition.getStatus());
247                            }
248    
249                            Iterator<Long> itr = q.iterate();
250    
251                            if (itr.hasNext()) {
252                                    Long count = itr.next();
253    
254                                    if (count != null) {
255                                            return count.intValue();
256                                    }
257                            }
258    
259                            return 0;
260                    }
261                    catch (Exception e) {
262                            throw new SystemException(e);
263                    }
264                    finally {
265                            closeSession(session);
266                    }
267            }
268    
269            @Override
270            public int countByG_U_A(
271                    long groupId, long userId, boolean anonymous,
272                    QueryDefinition<MBThread> queryDefinition) {
273    
274                    Session session = null;
275    
276                    try {
277                            session = openSession();
278    
279                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
280    
281                            sql = updateSQL(sql, queryDefinition);
282    
283                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
284    
285                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
286    
287                            QueryPos qPos = QueryPos.getInstance(q);
288    
289                            qPos.add(groupId);
290                            qPos.add(userId);
291                            qPos.add(anonymous);
292    
293                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
294                                    qPos.add(queryDefinition.getStatus());
295                            }
296    
297                            Iterator<Long> itr = q.iterate();
298    
299                            if (itr.hasNext()) {
300                                    Long count = itr.next();
301    
302                                    if (count != null) {
303                                            return count.intValue();
304                                    }
305                            }
306    
307                            return 0;
308                    }
309                    catch (Exception e) {
310                            throw new SystemException(e);
311                    }
312                    finally {
313                            closeSession(session);
314                    }
315            }
316    
317            @Override
318            public int countByS_G_U(
319                    long groupId, long userId, QueryDefinition<MBThread> queryDefinition) {
320    
321                    return doCountByS_G_U(groupId, userId, queryDefinition);
322            }
323    
324            @Override
325            public int countByG_U_C_A(
326                    long groupId, long userId, long[] categoryIds, boolean anonymous,
327                    QueryDefinition<MBThread> queryDefinition) {
328    
329                    Session session = null;
330    
331                    try {
332                            session = openSession();
333    
334                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C);
335    
336                            if (ArrayUtil.isEmpty(categoryIds)) {
337                                    sql = StringUtil.replace(
338                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
339                            }
340                            else {
341                                    sql = StringUtil.replace(
342                                            sql, "MBThread.categoryId = ?",
343                                            "MBThread.categoryId = " +
344                                                    StringUtil.merge(
345                                                            categoryIds, " OR MBThread.categoryId = "));
346                            }
347    
348                            sql = updateSQL(sql, queryDefinition);
349    
350                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
351    
352                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
353    
354                            QueryPos qPos = QueryPos.getInstance(q);
355    
356                            qPos.add(groupId);
357                            qPos.add(userId);
358                            qPos.add(anonymous);
359    
360                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
361                                    qPos.add(queryDefinition.getStatus());
362                            }
363    
364                            Iterator<Long> itr = q.iterate();
365    
366                            if (itr.hasNext()) {
367                                    Long count = itr.next();
368    
369                                    if (count != null) {
370                                            return count.intValue();
371                                    }
372                            }
373    
374                            return 0;
375                    }
376                    catch (Exception e) {
377                            throw new SystemException(e);
378                    }
379                    finally {
380                            closeSession(session);
381                    }
382            }
383    
384            @Override
385            public int countByS_G_U_C(
386                    long groupId, long userId, long[] categoryIds,
387                    QueryDefinition<MBThread> queryDefinition) {
388    
389                    return doCountByS_G_U_C(
390                            groupId, userId, categoryIds, queryDefinition, false);
391            }
392    
393            @Override
394            public int filterCountByG_C(long groupId, long categoryId) {
395                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
396                            return MBThreadUtil.countByG_C(groupId, categoryId);
397                    }
398    
399                    Session session = null;
400    
401                    try {
402                            session = openSession();
403    
404                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
405    
406                            sql = InlineSQLHelperUtil.replacePermissionCheck(
407                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
408                                    groupId);
409    
410                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
411    
412                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
413    
414                            QueryPos qPos = QueryPos.getInstance(q);
415    
416                            qPos.add(groupId);
417                            qPos.add(categoryId);
418    
419                            Iterator<Long> itr = q.iterate();
420    
421                            if (itr.hasNext()) {
422                                    Long count = itr.next();
423    
424                                    if (count != null) {
425                                            return count.intValue();
426                                    }
427                            }
428    
429                            return 0;
430                    }
431                    catch (Exception e) {
432                            throw processException(e);
433                    }
434                    finally {
435                            closeSession(session);
436                    }
437            }
438    
439            @Override
440            public int filterCountByG_C(
441                    long groupId, long categoryId,
442                    QueryDefinition<MBThread> queryDefinition) {
443    
444                    return doCountByG_C(groupId, categoryId, queryDefinition, true);
445            }
446    
447            @Override
448            public int filterCountByS_G_U_C(
449                    long groupId, long userId, long[] categoryIds,
450                    QueryDefinition<MBThread> queryDefinition) {
451    
452                    return doCountByS_G_U_C(
453                            groupId, userId, categoryIds, queryDefinition, true);
454            }
455    
456            @Override
457            public List<MBThread> filterFindByG_C(
458                    long groupId, long categoryId, int start, int end) {
459    
460                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
461                            return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
462                    }
463    
464                    Session session = null;
465    
466                    try {
467                            session = openSession();
468    
469                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
470    
471                            sql = InlineSQLHelperUtil.replacePermissionCheck(
472                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
473                                    groupId);
474    
475                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
476    
477                            q.addEntity("MBThread", MBThreadImpl.class);
478    
479                            QueryPos qPos = QueryPos.getInstance(q);
480    
481                            qPos.add(groupId);
482                            qPos.add(categoryId);
483    
484                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
485                    }
486                    catch (Exception e) {
487                            throw new SystemException(e);
488                    }
489                    finally {
490                            closeSession(session);
491                    }
492            }
493    
494            @Override
495            public List<MBThread> filterFindByG_C(
496                    long groupId, long categoryId,
497                    QueryDefinition<MBThread> queryDefinition) {
498    
499                    return doFindByG_C(groupId, categoryId, queryDefinition, true);
500            }
501    
502            @Override
503            public List<MBThread> filterFindByS_G_U_C(
504                    long groupId, long userId, long[] categoryIds,
505                    QueryDefinition<MBThread> queryDefinition) {
506    
507                    return doFindByS_G_U_C(
508                            groupId, userId, categoryIds, queryDefinition, true);
509            }
510    
511            @Override
512            public List<MBThread> findByNoAssets() {
513                    Session session = null;
514    
515                    try {
516                            session = openSession();
517    
518                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
519    
520                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
521    
522                            q.addEntity("MBThread", MBThreadImpl.class);
523    
524                            return q.list(true);
525                    }
526                    catch (Exception e) {
527                            throw new SystemException(e);
528                    }
529                    finally {
530                            closeSession(session);
531                    }
532            }
533    
534            @Override
535            public List<MBThread> findByG_U(
536                    long groupId, long userId, QueryDefinition<MBThread> queryDefinition) {
537    
538                    Session session = null;
539    
540                    try {
541                            session = openSession();
542    
543                            String sql = CustomSQLUtil.get(FIND_BY_G_U);
544    
545                            sql = updateSQL(sql, queryDefinition);
546    
547                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
548    
549                            q.addEntity("MBThread", MBThreadImpl.class);
550    
551                            QueryPos qPos = QueryPos.getInstance(q);
552    
553                            qPos.add(groupId);
554                            qPos.add(userId);
555    
556                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
557                                    qPos.add(queryDefinition.getStatus());
558                            }
559    
560                            return (List<MBThread>)QueryUtil.list(
561                                    q, getDialect(), queryDefinition.getStart(),
562                                    queryDefinition.getEnd());
563                    }
564                    catch (Exception e) {
565                            throw new SystemException(e);
566                    }
567                    finally {
568                            closeSession(session);
569                    }
570            }
571    
572            @Override
573            public List<MBThread> findByG_C(
574                    long groupId, long categoryId,
575                    QueryDefinition<MBThread> queryDefinition) {
576    
577                    return doFindByG_C(groupId, categoryId, queryDefinition, false);
578            }
579    
580            @Override
581            public List<MBThread> findByG_U_C(
582                    long groupId, long userId, long[] categoryIds,
583                    QueryDefinition<MBThread> queryDefinition) {
584    
585                    Session session = null;
586    
587                    try {
588                            session = openSession();
589    
590                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C);
591    
592                            if (ArrayUtil.isEmpty(categoryIds)) {
593                                    sql = StringUtil.replace(
594                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
595                            }
596                            else {
597                                    sql = StringUtil.replace(
598                                            sql, "MBThread.categoryId = ?",
599                                            "MBThread.categoryId = " +
600                                                    StringUtil.merge(
601                                                            categoryIds, " OR MBThread.categoryId = "));
602                            }
603    
604                            sql = updateSQL(sql, queryDefinition);
605    
606                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
607    
608                            q.addEntity("MBThread", MBThreadImpl.class);
609    
610                            QueryPos qPos = QueryPos.getInstance(q);
611    
612                            qPos.add(groupId);
613                            qPos.add(userId);
614    
615                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
616                                    qPos.add(queryDefinition.getStatus());
617                            }
618    
619                            return (List<MBThread>)QueryUtil.list(
620                                    q, getDialect(), queryDefinition.getStart(),
621                                    queryDefinition.getEnd());
622                    }
623                    catch (Exception e) {
624                            throw new SystemException(e);
625                    }
626                    finally {
627                            closeSession(session);
628                    }
629            }
630    
631            @Override
632            public List<MBThread> findByG_U_LPD(
633                    long groupId, long userId, Date lastPostDate,
634                    QueryDefinition<MBThread> queryDefinition) {
635    
636                    Session session = null;
637    
638                    try {
639                            session = openSession();
640    
641                            String sql = CustomSQLUtil.get(FIND_BY_G_U_LPD);
642    
643                            if (userId <= 0) {
644                                    sql = StringUtil.replace(
645                                            sql, _INNER_JOIN_SQL, StringPool.BLANK);
646                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
647                            }
648    
649                            sql = updateSQL(sql, queryDefinition);
650    
651                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
652    
653                            q.addEntity("MBThread", MBThreadImpl.class);
654    
655                            QueryPos qPos = QueryPos.getInstance(q);
656    
657                            qPos.add(groupId);
658                            qPos.add(lastPostDate);
659    
660                            if (userId > 0) {
661                                    qPos.add(userId);
662                            }
663    
664                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
665                                    qPos.add(queryDefinition.getStatus());
666                            }
667    
668                            return (List<MBThread>)QueryUtil.list(
669                                    q, getDialect(), queryDefinition.getStart(),
670                                    queryDefinition.getEnd());
671                    }
672                    catch (Exception e) {
673                            throw new SystemException(e);
674                    }
675                    finally {
676                            closeSession(session);
677                    }
678            }
679    
680            @Override
681            public List<MBThread> findByG_U_A(
682                    long groupId, long userId, boolean anonymous,
683                    QueryDefinition<MBThread> queryDefinition) {
684    
685                    Session session = null;
686    
687                    try {
688                            session = openSession();
689    
690                            String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
691    
692                            sql = updateSQL(sql, queryDefinition);
693    
694                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
695    
696                            q.addEntity("MBThread", MBThreadImpl.class);
697    
698                            QueryPos qPos = QueryPos.getInstance(q);
699    
700                            qPos.add(groupId);
701                            qPos.add(userId);
702                            qPos.add(anonymous);
703    
704                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
705                                    qPos.add(queryDefinition.getStatus());
706                            }
707    
708                            return (List<MBThread>)QueryUtil.list(
709                                    q, getDialect(), queryDefinition.getStart(),
710                                    queryDefinition.getEnd());
711                    }
712                    catch (Exception e) {
713                            throw new SystemException(e);
714                    }
715                    finally {
716                            closeSession(session);
717                    }
718            }
719    
720            @Override
721            public List<MBThread> findByS_G_U(
722                    long groupId, long userId, QueryDefinition<MBThread> queryDefinition) {
723    
724                    Session session = null;
725    
726                    try {
727                            session = openSession();
728    
729                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
730    
731                            sql = updateSQL(sql, queryDefinition);
732    
733                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
734    
735                            q.addEntity("MBThread", MBThreadImpl.class);
736    
737                            QueryPos qPos = QueryPos.getInstance(q);
738    
739                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
740                            qPos.add(groupId);
741                            qPos.add(userId);
742    
743                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
744                                    qPos.add(queryDefinition.getStatus());
745                            }
746    
747                            return (List<MBThread>)QueryUtil.list(
748                                    q, getDialect(), queryDefinition.getStart(),
749                                    queryDefinition.getEnd());
750                    }
751                    catch (Exception e) {
752                            throw new SystemException(e);
753                    }
754                    finally {
755                            closeSession(session);
756                    }
757            }
758    
759            @Override
760            public List<MBThread> findByG_U_C_A(
761                    long groupId, long userId, long[] categoryIds, boolean anonymous,
762                    QueryDefinition<MBThread> queryDefinition) {
763    
764                    Session session = null;
765    
766                    try {
767                            session = openSession();
768    
769                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_A);
770    
771                            if (ArrayUtil.isEmpty(categoryIds)) {
772                                    sql = StringUtil.replace(
773                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
774                            }
775                            else {
776                                    sql = StringUtil.replace(
777                                            sql, "MBThread.categoryId = ?",
778                                            "MBThread.categoryId = " +
779                                                    StringUtil.merge(
780                                                            categoryIds, " OR MBThread.categoryId = "));
781                            }
782    
783                            sql = updateSQL(sql, queryDefinition);
784    
785                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
786    
787                            q.addEntity("MBThread", MBThreadImpl.class);
788    
789                            QueryPos qPos = QueryPos.getInstance(q);
790    
791                            qPos.add(groupId);
792                            qPos.add(userId);
793                            qPos.add(anonymous);
794    
795                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
796                                    qPos.add(queryDefinition.getStatus());
797                            }
798    
799                            return (List<MBThread>)QueryUtil.list(
800                                    q, getDialect(), queryDefinition.getStart(),
801                                    queryDefinition.getEnd());
802                    }
803                    catch (Exception e) {
804                            throw new SystemException(e);
805                    }
806                    finally {
807                            closeSession(session);
808                    }
809            }
810    
811            @Override
812            public List<MBThread> findByS_G_U_C(
813                    long groupId, long userId, long[] categoryIds,
814                    QueryDefinition<MBThread> queryDefinition) {
815    
816                    return doFindByS_G_U_C(
817                            groupId, userId, categoryIds, queryDefinition, false);
818            }
819    
820            protected int doCountByG_C(
821                    long groupId, long categoryId,
822                    QueryDefinition<MBThread> queryDefinition, boolean inlineSQLHelper) {
823    
824                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
825                            if (queryDefinition.isExcludeStatus()) {
826                                    return MBThreadUtil.countByG_C_NotS(
827                                            groupId, categoryId, queryDefinition.getStatus());
828                            }
829                            else {
830                                    if (queryDefinition.getStatus() !=
831                                                    WorkflowConstants.STATUS_ANY) {
832    
833                                            return MBThreadUtil.countByG_C_S(
834                                                    groupId, categoryId, queryDefinition.getStatus());
835                                    }
836                                    else {
837                                            return MBThreadUtil.countByG_C(groupId, categoryId);
838                                    }
839                            }
840                    }
841    
842                    Session session = null;
843    
844                    try {
845                            session = openSession();
846    
847                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
848    
849                            sql = updateSQL(sql, queryDefinition);
850    
851                            sql = InlineSQLHelperUtil.replacePermissionCheck(
852                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
853                                    groupId);
854    
855                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
856    
857                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
858    
859                            QueryPos qPos = QueryPos.getInstance(q);
860    
861                            qPos.add(groupId);
862                            qPos.add(categoryId);
863    
864                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
865                                    qPos.add(queryDefinition.getStatus());
866                            }
867    
868                            Iterator<Long> itr = q.iterate();
869    
870                            if (itr.hasNext()) {
871                                    Long count = itr.next();
872    
873                                    if (count != null) {
874                                            return count.intValue();
875                                    }
876                            }
877    
878                            return 0;
879                    }
880                    catch (Exception e) {
881                            throw processException(e);
882                    }
883                    finally {
884                            closeSession(session);
885                    }
886            }
887    
888            protected int doCountByS_G_U(
889                    long groupId, long userId, QueryDefinition<MBThread> queryDefinition) {
890    
891                    Session session = null;
892    
893                    try {
894                            session = openSession();
895    
896                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
897    
898                            sql = updateSQL(sql, queryDefinition);
899    
900                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
901    
902                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
903    
904                            QueryPos qPos = QueryPos.getInstance(q);
905    
906                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
907                            qPos.add(groupId);
908                            qPos.add(userId);
909    
910                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
911                                    qPos.add(queryDefinition.getStatus());
912                            }
913    
914                            Iterator<Long> itr = q.iterate();
915    
916                            if (itr.hasNext()) {
917                                    Long count = itr.next();
918    
919                                    if (count != null) {
920                                            return count.intValue();
921                                    }
922                            }
923    
924                            return 0;
925                    }
926                    catch (Exception e) {
927                            throw new SystemException(e);
928                    }
929                    finally {
930                            closeSession(session);
931                    }
932            }
933    
934            protected int doCountByS_G_U_C(
935                    long groupId, long userId, long[] categoryIds,
936                    QueryDefinition<MBThread> queryDefinition, boolean inlineSQLHelper) {
937    
938                    Session session = null;
939    
940                    try {
941                            session = openSession();
942    
943                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_C);
944    
945                            if (ArrayUtil.isEmpty(categoryIds)) {
946                                    sql = StringUtil.replace(
947                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
948                            }
949                            else {
950                                    sql = StringUtil.replace(
951                                            sql, "MBThread.categoryId = ?",
952                                            "MBThread.categoryId = " +
953                                                    StringUtil.merge(
954                                                            categoryIds, " OR MBThread.categoryId = "));
955                            }
956    
957                            sql = updateSQL(sql, queryDefinition);
958    
959                            if (inlineSQLHelper) {
960                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
961                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
962                                            groupId);
963                            }
964    
965                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
966    
967                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
968    
969                            QueryPos qPos = QueryPos.getInstance(q);
970    
971                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
972                            qPos.add(groupId);
973                            qPos.add(userId);
974    
975                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
976                                    qPos.add(queryDefinition.getStatus());
977                            }
978    
979                            Iterator<Long> itr = q.iterate();
980    
981                            if (itr.hasNext()) {
982                                    Long count = itr.next();
983    
984                                    if (count != null) {
985                                            return count.intValue();
986                                    }
987                            }
988    
989                            return 0;
990                    }
991                    catch (Exception e) {
992                            throw new SystemException(e);
993                    }
994                    finally {
995                            closeSession(session);
996                    }
997            }
998    
999            protected List<MBThread> doFindByG_C(
1000                    long groupId, long categoryId,
1001                    QueryDefinition<MBThread> queryDefinition, boolean inlineSQLHelper) {
1002    
1003                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
1004                            if (queryDefinition.isExcludeStatus()) {
1005                                    return MBThreadUtil.findByG_C_NotS(
1006                                            groupId, categoryId, queryDefinition.getStatus(),
1007                                            queryDefinition.getStart(), queryDefinition.getEnd());
1008                            }
1009                            else {
1010                                    if (queryDefinition.getStatus() !=
1011                                                    WorkflowConstants.STATUS_ANY) {
1012    
1013                                            return MBThreadUtil.findByG_C_S(
1014                                                    groupId, categoryId, queryDefinition.getStatus(),
1015                                                    queryDefinition.getStart(), queryDefinition.getEnd());
1016                                    }
1017                                    else {
1018                                            return MBThreadUtil.findByG_C(
1019                                                    groupId, categoryId, queryDefinition.getStart(),
1020                                                    queryDefinition.getEnd());
1021                                    }
1022                            }
1023                    }
1024    
1025                    Session session = null;
1026    
1027                    try {
1028                            session = openSession();
1029    
1030                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
1031    
1032                            sql = updateSQL(sql, queryDefinition);
1033    
1034                            sql = InlineSQLHelperUtil.replacePermissionCheck(
1035                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
1036                                    groupId);
1037    
1038                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
1039    
1040                            q.addEntity("MBThread", MBThreadImpl.class);
1041    
1042                            QueryPos qPos = QueryPos.getInstance(q);
1043    
1044                            qPos.add(groupId);
1045                            qPos.add(categoryId);
1046    
1047                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
1048                                    qPos.add(queryDefinition.getStatus());
1049                            }
1050    
1051                            return (List<MBThread>)QueryUtil.list(
1052                                    q, getDialect(), queryDefinition.getStart(),
1053                                    queryDefinition.getEnd());
1054                    }
1055                    catch (Exception e) {
1056                            throw new SystemException(e);
1057                    }
1058                    finally {
1059                            closeSession(session);
1060                    }
1061            }
1062    
1063            protected List<MBThread> doFindByS_G_U_C(
1064                    long groupId, long userId, long[] categoryIds,
1065                    QueryDefinition<MBThread> queryDefinition, boolean inlineSQLHelper) {
1066    
1067                    Session session = null;
1068    
1069                    try {
1070                            session = openSession();
1071    
1072                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_C);
1073    
1074                            if (ArrayUtil.isEmpty(categoryIds)) {
1075                                    sql = StringUtil.replace(
1076                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
1077                            }
1078                            else {
1079                                    sql = StringUtil.replace(
1080                                            sql, "MBThread.categoryId = ?",
1081                                            "MBThread.categoryId = " +
1082                                                    StringUtil.merge(
1083                                                            categoryIds, " OR MBThread.categoryId = "));
1084                            }
1085    
1086                            sql = updateSQL(sql, queryDefinition);
1087    
1088                            if (inlineSQLHelper) {
1089                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
1090                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
1091                                            groupId);
1092                            }
1093    
1094                            SQLQuery q = session.createSynchronizedSQLQuery(sql);
1095    
1096                            q.addEntity("MBThread", MBThreadImpl.class);
1097    
1098                            QueryPos qPos = QueryPos.getInstance(q);
1099    
1100                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
1101                            qPos.add(groupId);
1102                            qPos.add(userId);
1103    
1104                            if (queryDefinition.getStatus() != WorkflowConstants.STATUS_ANY) {
1105                                    qPos.add(queryDefinition.getStatus());
1106                            }
1107    
1108                            return (List<MBThread>)QueryUtil.list(
1109                                    q, getDialect(), queryDefinition.getStart(),
1110                                    queryDefinition.getEnd());
1111                    }
1112                    catch (Exception e) {
1113                            throw new SystemException(e);
1114                    }
1115                    finally {
1116                            closeSession(session);
1117                    }
1118            }
1119    
1120            protected String updateSQL(
1121                    String sql, QueryDefinition<MBThread> queryDefinition) {
1122    
1123                    if (queryDefinition.getStatus() == WorkflowConstants.STATUS_ANY) {
1124                            return sql;
1125                    }
1126    
1127                    if (queryDefinition.isExcludeStatus()) {
1128                            return CustomSQLUtil.appendCriteria(
1129                                    sql, "AND (MBThread.status != ?)");
1130                    }
1131    
1132                    return CustomSQLUtil.appendCriteria(sql, "AND (MBThread.status = ?)");
1133            }
1134    
1135            private static final String _INNER_JOIN_SQL =
1136                    "INNER JOIN MBMessage ON (MBThread.threadId = MBMessage.threadId)";
1137    
1138            private static final String _USER_ID_SQL = "AND (MBMessage.userId = ?)";
1139    
1140    }