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