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