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