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