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