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