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.QueryPos;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.dao.orm.SQLQuery;
020    import com.liferay.portal.kernel.dao.orm.Session;
021    import com.liferay.portal.kernel.dao.orm.Type;
022    import com.liferay.portal.kernel.exception.SystemException;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class MBThreadFinderImpl
042            extends BasePersistenceImpl<MBThread> implements MBThreadFinder {
043    
044            public static final String COUNT_BY_G_C =
045                    MBThreadFinder.class.getName() + ".countByG_C";
046    
047            public static final String COUNT_BY_G_U_S =
048                    MBThreadFinder.class.getName() + ".countByG_U_S";
049    
050            public static final String COUNT_BY_G_U_A_S =
051                    MBThreadFinder.class.getName() + ".countByG_U_A_S";
052    
053            public static final String COUNT_BY_S_G_U_S =
054                    MBThreadFinder.class.getName() + ".countByS_G_U_S";
055    
056            public static final String COUNT_BY_S_G_U_C_S =
057                    MBThreadFinder.class.getName() + ".countByS_G_U_C_S";
058    
059            public static final String FIND_BY_NO_ASSETS =
060                    MBThreadFinder.class.getName() + ".findByNoAssets";
061    
062            public static final String FIND_BY_G_C =
063                    MBThreadFinder.class.getName() + ".findByG_C";
064    
065            public static final String FIND_BY_G_U_S =
066                    MBThreadFinder.class.getName() + ".findByG_U_S";
067    
068            public static final String FIND_BY_G_U_A_S =
069                    MBThreadFinder.class.getName() + ".findByG_U_A_S";
070    
071            public static final String FIND_BY_S_G_U_S =
072                    MBThreadFinder.class.getName() + ".findByS_G_U_S";
073    
074            public static final String FIND_BY_S_G_U_C_S =
075                    MBThreadFinder.class.getName() + ".findByS_G_U_C_S";
076    
077            public int countByG_U_S(long groupId, long userId, int status)
078                    throws SystemException {
079    
080                    Session session = null;
081    
082                    try {
083                            session = openSession();
084    
085                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_S);
086    
087                            if (status != WorkflowConstants.STATUS_ANY) {
088                                    sql = CustomSQLUtil.appendCriteria(
089                                            sql, "AND (MBMessage.status = ?)");
090                            }
091    
092                            SQLQuery q = session.createSQLQuery(sql);
093    
094                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
095    
096                            QueryPos qPos = QueryPos.getInstance(q);
097    
098                            qPos.add(groupId);
099                            qPos.add(userId);
100    
101                            if (status != WorkflowConstants.STATUS_ANY) {
102                                    qPos.add(status);
103                            }
104    
105                            Iterator<Long> itr = q.iterate();
106    
107                            if (itr.hasNext()) {
108                                    Long count = itr.next();
109    
110                                    if (count != null) {
111                                            return count.intValue();
112                                    }
113                            }
114    
115                            return 0;
116                    }
117                    catch (Exception e) {
118                            throw new SystemException(e);
119                    }
120                    finally {
121                            closeSession(session);
122                    }
123            }
124    
125            public int countByG_C_S(long groupId, long categoryId, int status)
126                    throws SystemException {
127    
128                    return doCountByG_C_S(groupId, categoryId, status, false);
129            }
130    
131            public int countByG_U_A_S(
132                            long groupId, long userId, boolean anonymous, int status)
133                    throws SystemException {
134    
135                    Session session = null;
136    
137                    try {
138                            session = openSession();
139    
140                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_A_S);
141    
142                            if (status != WorkflowConstants.STATUS_ANY) {
143                                    sql = CustomSQLUtil.appendCriteria(
144                                            sql, "AND (MBMessage.status = ?)");
145                            }
146    
147                            SQLQuery q = session.createSQLQuery(sql);
148    
149                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
150    
151                            QueryPos qPos = QueryPos.getInstance(q);
152    
153                            qPos.add(groupId);
154                            qPos.add(userId);
155                            qPos.add(anonymous);
156    
157                            if (status != WorkflowConstants.STATUS_ANY) {
158                                    qPos.add(status);
159                            }
160    
161                            Iterator<Long> itr = q.iterate();
162    
163                            if (itr.hasNext()) {
164                                    Long count = itr.next();
165    
166                                    if (count != null) {
167                                            return count.intValue();
168                                    }
169                            }
170    
171                            return 0;
172                    }
173                    catch (Exception e) {
174                            throw new SystemException(e);
175                    }
176                    finally {
177                            closeSession(session);
178                    }
179            }
180    
181            public int countByS_G_U_S(long groupId, long userId, int status)
182                    throws SystemException {
183    
184                    return doCountByS_G_U_S(groupId, userId, status);
185            }
186    
187            public int countByS_G_U_C_S(
188                            long groupId, long userId, long[] categoryIds, int status)
189                    throws SystemException {
190    
191                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, false);
192            }
193    
194            public int filterCountByG_C(long groupId, long categoryId)
195                    throws SystemException {
196    
197                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
198                            return MBThreadUtil.countByG_C(groupId, categoryId);
199                    }
200    
201                    Session session = null;
202    
203                    try {
204                            session = openSession();
205    
206                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
207    
208                            sql = InlineSQLHelperUtil.replacePermissionCheck(
209                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
210                                    groupId);
211    
212                            SQLQuery q = session.createSQLQuery(sql);
213    
214                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
215    
216                            QueryPos qPos = QueryPos.getInstance(q);
217    
218                            qPos.add(groupId);
219                            qPos.add(categoryId);
220    
221                            Iterator<Long> itr = q.iterate();
222    
223                            if (itr.hasNext()) {
224                                    Long count = itr.next();
225    
226                                    if (count != null) {
227                                            return count.intValue();
228                                    }
229                            }
230    
231                            return 0;
232                    }
233                    catch (Exception e) {
234                            throw processException(e);
235                    }
236                    finally {
237                            closeSession(session);
238                    }
239            }
240    
241            public int filterCountByG_C_S(long groupId, long categoryId, int status)
242                    throws SystemException {
243    
244                    return doCountByG_C_S(groupId, categoryId, status, true);
245            }
246    
247            public int filterCountByS_G_U_C_S(
248                            long groupId, long userId, long[] categoryIds, int status)
249                    throws SystemException {
250    
251                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, true);
252            }
253    
254            public List<MBThread> filterFindByG_C(
255                            long groupId, long categoryId, int start, int end)
256                    throws SystemException {
257    
258                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
259                            return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
260                    }
261    
262                    Session session = null;
263    
264                    try {
265                            session = openSession();
266    
267                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
268    
269                            sql = InlineSQLHelperUtil.replacePermissionCheck(
270                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
271                                    groupId);
272    
273                            SQLQuery q = session.createSQLQuery(sql);
274    
275                            q.addEntity("MBThread", MBThreadImpl.class);
276    
277                            QueryPos qPos = QueryPos.getInstance(q);
278    
279                            qPos.add(groupId);
280                            qPos.add(categoryId);
281    
282                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
283                    }
284                    catch (Exception e) {
285                            throw new SystemException(e);
286                    }
287                    finally {
288                            closeSession(session);
289                    }
290            }
291    
292            public List<MBThread> filterFindByG_C_S(
293                            long groupId, long categoryId, int status, int start, int end)
294                    throws SystemException {
295    
296                    return doFindByG_C_S(groupId, categoryId, status, start, end, true);
297            }
298    
299            public List<MBThread> filterFindByS_G_U_C_S(
300                            long groupId, long userId, long[] categoryIds, int status,
301                            int start, int end)
302                    throws SystemException {
303    
304                    return doFindByS_G_U_C_S(
305                            groupId, userId, categoryIds, status, start, end, true);
306            }
307    
308            public List<MBThread> findByNoAssets() throws SystemException {
309                    Session session = null;
310    
311                    try {
312                            session = openSession();
313    
314                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
315    
316                            SQLQuery q = session.createSQLQuery(sql);
317    
318                            q.addEntity("MBThread", MBThreadImpl.class);
319    
320                            return q.list(true);
321                    }
322                    catch (Exception e) {
323                            throw new SystemException(e);
324                    }
325                    finally {
326                            closeSession(session);
327                    }
328            }
329    
330            public List<MBThread> findByG_U_S(
331                            long groupId, long userId, int status, int start, int end)
332                    throws SystemException {
333    
334                    Session session = null;
335    
336                    try {
337                            session = openSession();
338    
339                            String sql = CustomSQLUtil.get(FIND_BY_G_U_S);
340    
341                            if (status != WorkflowConstants.STATUS_ANY) {
342                                    sql = CustomSQLUtil.appendCriteria(
343                                            sql, "AND (MBMessage.status = ?)");
344                            }
345    
346                            SQLQuery q = session.createSQLQuery(sql);
347    
348                            q.addEntity("MBThread", MBThreadImpl.class);
349    
350                            QueryPos qPos = QueryPos.getInstance(q);
351    
352                            qPos.add(groupId);
353                            qPos.add(userId);
354    
355                            if (status != WorkflowConstants.STATUS_ANY) {
356                                    qPos.add(status);
357                            }
358    
359                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
360                    }
361                    catch (Exception e) {
362                            throw new SystemException(e);
363                    }
364                    finally {
365                            closeSession(session);
366                    }
367            }
368    
369            public List<MBThread> findByG_C_S(
370                            long groupId, long categoryId, int status, int start, int end)
371                    throws SystemException {
372    
373                    return doFindByG_C_S(groupId, categoryId, status, start, end, false);
374            }
375    
376            public List<MBThread> findByG_U_A_S(
377                            long groupId, long userId, boolean anonymous, int status, int start,
378                            int end)
379                    throws SystemException {
380    
381                    Session session = null;
382    
383                    try {
384                            session = openSession();
385    
386                            String sql = CustomSQLUtil.get(FIND_BY_G_U_A_S);
387    
388                            if (status != WorkflowConstants.STATUS_ANY) {
389                                    sql = CustomSQLUtil.appendCriteria(
390                                            sql, "AND (MBMessage.status = ?)");
391                            }
392    
393                            SQLQuery q = session.createSQLQuery(sql);
394    
395                            q.addEntity("MBThread", MBThreadImpl.class);
396    
397                            QueryPos qPos = QueryPos.getInstance(q);
398    
399                            qPos.add(groupId);
400                            qPos.add(userId);
401                            qPos.add(anonymous);
402    
403                            if (status != WorkflowConstants.STATUS_ANY) {
404                                    qPos.add(status);
405                            }
406    
407                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
408                    }
409                    catch (Exception e) {
410                            throw new SystemException(e);
411                    }
412                    finally {
413                            closeSession(session);
414                    }
415            }
416    
417            public List<MBThread> findByS_G_U_S(
418                    long groupId, long userId, int status, int start, int end)
419                    throws SystemException {
420    
421                    Session session = null;
422    
423                    try {
424                            session = openSession();
425    
426                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_S);
427    
428                            if (status != WorkflowConstants.STATUS_ANY) {
429                                    sql = CustomSQLUtil.appendCriteria(
430                                            sql, "AND (MBThread.status = ?)");
431                            }
432    
433                            SQLQuery q = session.createSQLQuery(sql);
434    
435                            q.addEntity("MBThread", MBThreadImpl.class);
436    
437                            QueryPos qPos = QueryPos.getInstance(q);
438    
439                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
440                            qPos.add(groupId);
441                            qPos.add(userId);
442    
443                            if (status != WorkflowConstants.STATUS_ANY) {
444                                    qPos.add(status);
445                            }
446    
447                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
448                    }
449                    catch (Exception e) {
450                            throw new SystemException(e);
451                    }
452                    finally {
453                            closeSession(session);
454                    }
455            }
456    
457            public List<MBThread> findByS_G_U_C_S(
458                            long groupId, long userId, long[] categoryIds, int status,
459                            int start, int end)
460                    throws SystemException {
461    
462                    return doFindByS_G_U_C_S(
463                            groupId, userId, categoryIds, status, start, end, false);
464            }
465    
466            protected int doCountByG_C_S(
467                            long groupId, long categoryId, int status, boolean inlineSQLHelper)
468                    throws SystemException {
469    
470                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
471                            if (status != WorkflowConstants.STATUS_ANY) {
472                                    return MBThreadUtil.countByG_C_S(groupId, categoryId, status);
473                            }
474                            else {
475                                    return MBThreadUtil.countByG_C(groupId, categoryId);
476                            }
477                    }
478    
479                    Session session = null;
480    
481                    try {
482                            session = openSession();
483    
484                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
485    
486                            if (status != WorkflowConstants.STATUS_ANY) {
487                                    sql = CustomSQLUtil.appendCriteria(
488                                            sql, "AND (MBThread.status = ?)");
489                            }
490    
491                            sql = InlineSQLHelperUtil.replacePermissionCheck(
492                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
493                                    groupId);
494    
495                            SQLQuery q = session.createSQLQuery(sql);
496    
497                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
498    
499                            QueryPos qPos = QueryPos.getInstance(q);
500    
501                            qPos.add(groupId);
502                            qPos.add(categoryId);
503    
504                            if (status != WorkflowConstants.STATUS_ANY) {
505                                    qPos.add(status);
506                            }
507    
508                            Iterator<Long> itr = q.iterate();
509    
510                            if (itr.hasNext()) {
511                                    Long count = itr.next();
512    
513                                    if (count != null) {
514                                            return count.intValue();
515                                    }
516                            }
517    
518                            return 0;
519                    }
520                    catch (Exception e) {
521                            throw processException(e);
522                    }
523                    finally {
524                            closeSession(session);
525                    }
526            }
527    
528            protected int doCountByS_G_U_S(long groupId, long userId, int status)
529                    throws SystemException {
530    
531                    Session session = null;
532    
533                    try {
534                            session = openSession();
535    
536                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_S);
537    
538                            if (status != WorkflowConstants.STATUS_ANY) {
539                                    sql = CustomSQLUtil.appendCriteria(
540                                            sql, "AND (MBThread.status = ?)");
541                            }
542    
543                            SQLQuery q = session.createSQLQuery(sql);
544    
545                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
546    
547                            QueryPos qPos = QueryPos.getInstance(q);
548    
549                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
550                            qPos.add(groupId);
551                            qPos.add(userId);
552    
553                            if (status != WorkflowConstants.STATUS_ANY) {
554                                    qPos.add(status);
555                            }
556    
557                            Iterator<Long> itr = q.iterate();
558    
559                            if (itr.hasNext()) {
560                                    Long count = itr.next();
561    
562                                    if (count != null) {
563                                            return count.intValue();
564                                    }
565                            }
566    
567                            return 0;
568                    }
569                    catch (Exception e) {
570                            throw new SystemException(e);
571                    }
572                    finally {
573                            closeSession(session);
574                    }
575            }
576    
577            protected int doCountByS_G_U_C_S(
578                            long groupId, long userId, long[] categoryIds, int status,
579                            boolean inlineSQLHelper)
580                    throws SystemException {
581    
582                    Session session = null;
583    
584                    try {
585                            session = openSession();
586    
587                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_C_S);
588    
589                            if ((categoryIds == null) || (categoryIds.length == 0)) {
590                                    sql = StringUtil.replace(
591                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
592                            }
593                            else {
594                                    sql = StringUtil.replace(
595                                            sql, "MBThread.categoryId = ?",
596                                            "MBThread.categoryId = " +
597                                                    StringUtil.merge(
598                                                            categoryIds, " OR MBThread.categoryId = "));
599                            }
600    
601                            if (status != WorkflowConstants.STATUS_ANY) {
602                                    sql = CustomSQLUtil.appendCriteria(
603                                            sql, "AND (MBThread.status = ?)");
604                            }
605    
606                            if (inlineSQLHelper) {
607                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
608                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
609                                            groupId);
610                            }
611    
612                            SQLQuery q = session.createSQLQuery(sql);
613    
614                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
615    
616                            QueryPos qPos = QueryPos.getInstance(q);
617    
618                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
619                            qPos.add(groupId);
620                            qPos.add(userId);
621    
622                            if (status != WorkflowConstants.STATUS_ANY) {
623                                    qPos.add(status);
624                            }
625    
626                            Iterator<Long> itr = q.iterate();
627    
628                            if (itr.hasNext()) {
629                                    Long count = itr.next();
630    
631                                    if (count != null) {
632                                            return count.intValue();
633                                    }
634                            }
635    
636                            return 0;
637                    }
638                    catch (Exception e) {
639                            throw new SystemException(e);
640                    }
641                    finally {
642                            closeSession(session);
643                    }
644            }
645    
646            protected List<MBThread> doFindByG_C_S(
647                            long groupId, long categoryId, int status, int start, int end,
648                            boolean inlineSQLHelper)
649                    throws SystemException {
650    
651                    if (!inlineSQLHelper || !InlineSQLHelperUtil.isEnabled(groupId)) {
652                            if (status != WorkflowConstants.STATUS_ANY) {
653                                    return MBThreadUtil.findByG_C_S(
654                                            groupId, categoryId, status, start, end);
655                            }
656                            else {
657                                    return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
658                            }
659                    }
660    
661                    Session session = null;
662    
663                    try {
664                            session = openSession();
665    
666                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
667    
668                            if (status != WorkflowConstants.STATUS_ANY) {
669                                    sql = CustomSQLUtil.appendCriteria(
670                                            sql, "AND (MBThread.status = ?)");
671                            }
672    
673                            sql = InlineSQLHelperUtil.replacePermissionCheck(
674                                    sql, MBMessage.class.getName(), "MBThread.rootMessageId",
675                                    groupId);
676    
677                            SQLQuery q = session.createSQLQuery(sql);
678    
679                            q.addEntity("MBThread", MBThreadImpl.class);
680    
681                            QueryPos qPos = QueryPos.getInstance(q);
682    
683                            qPos.add(groupId);
684                            qPos.add(categoryId);
685    
686                            if (status != WorkflowConstants.STATUS_ANY) {
687                                    qPos.add(status);
688                            }
689    
690                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
691                    }
692                    catch (Exception e) {
693                            throw new SystemException(e);
694                    }
695                    finally {
696                            closeSession(session);
697                    }
698            }
699    
700            protected List<MBThread> doFindByS_G_U_C_S(
701                            long groupId, long userId, long[] categoryIds, int status,
702                            int start, int end, boolean inlineSQLHelper)
703                    throws SystemException {
704    
705                    Session session = null;
706    
707                    try {
708                            session = openSession();
709    
710                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_C_S);
711    
712                            if ((categoryIds == null) || (categoryIds.length == 0)) {
713                                    sql = StringUtil.replace(
714                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
715                            }
716                            else {
717                                    sql = StringUtil.replace(
718                                            sql, "MBThread.categoryId = ?",
719                                            "MBThread.categoryId = " +
720                                                    StringUtil.merge(
721                                                            categoryIds, " OR MBThread.categoryId = "));
722                            }
723    
724                            if (status != WorkflowConstants.STATUS_ANY) {
725                                    sql = CustomSQLUtil.appendCriteria(
726                                            sql, "AND (MBThread.status = ?)");
727                            }
728    
729                            if (inlineSQLHelper) {
730                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
731                                            sql, MBMessage.class.getName(), "MBThread.rootMessageId",
732                                            groupId);
733                            }
734    
735                            SQLQuery q = session.createSQLQuery(sql);
736    
737                            q.addEntity("MBThread", MBThreadImpl.class);
738    
739                            QueryPos qPos = QueryPos.getInstance(q);
740    
741                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
742                            qPos.add(groupId);
743                            qPos.add(userId);
744    
745                            if (status != WorkflowConstants.STATUS_ANY) {
746                                    qPos.add(status);
747                            }
748    
749                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
750                    }
751                    catch (Exception e) {
752                            throw new SystemException(e);
753                    }
754                    finally {
755                            closeSession(session);
756                    }
757            }
758    
759    }