001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.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.CalendarUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
028    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
031    import com.liferay.util.dao.orm.CustomSQLUtil;
032    
033    import java.sql.Timestamp;
034    
035    import java.util.Date;
036    import java.util.Iterator;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class MBMessageFinderImpl
043            extends BasePersistenceImpl<MBMessage> implements MBMessageFinder {
044    
045            public static final String COUNT_BY_C_T =
046                    MBMessageFinder.class.getName() + ".countByC_T";
047    
048            public static final String COUNT_BY_G_U_C_S =
049                    MBMessageFinder.class.getName() + ".countByG_U_C_S";
050    
051            public static final String COUNT_BY_G_U_MD_C_S =
052            MBMessageFinder.class.getName() + ".countByG_U_MD_C_S";
053    
054            public static final String COUNT_BY_G_U_C_A_S =
055                    MBMessageFinder.class.getName() + ".countByG_U_C_A_S";
056    
057            public static final String FIND_BY_NO_ASSETS =
058                    MBMessageFinder.class.getName() + ".findByNoAssets";
059    
060            public static final String FIND_BY_G_U_C_S =
061                    MBMessageFinder.class.getName() + ".findByG_U_C_S";
062    
063            public static final String FIND_BY_G_U_MD_C_S =
064            MBMessageFinder.class.getName() + ".findByG_U_MD_C_S";
065    
066            public static final String FIND_BY_G_U_C_A_S =
067                    MBMessageFinder.class.getName() + ".findByG_U_C_A_S";
068    
069            @Override
070            public int countByC_T(Date createDate, long threadId)
071                    throws SystemException {
072    
073                    Timestamp createDate_TS = CalendarUtil.getTimestamp(createDate);
074    
075                    Session session = null;
076    
077                    try {
078                            session = openSession();
079    
080                            String sql = CustomSQLUtil.get(COUNT_BY_C_T);
081    
082                            SQLQuery q = session.createSQLQuery(sql);
083    
084                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
085    
086                            QueryPos qPos = QueryPos.getInstance(q);
087    
088                            qPos.add(createDate_TS);
089                            qPos.add(threadId);
090    
091                            Iterator<Long> itr = q.iterate();
092    
093                            if (itr.hasNext()) {
094                                    Long count = itr.next();
095    
096                                    if (count != null) {
097                                            return count.intValue();
098                                    }
099                            }
100    
101                            return 0;
102                    }
103                    catch (Exception e) {
104                            throw new SystemException(e);
105                    }
106                    finally {
107                            closeSession(session);
108                    }
109            }
110    
111            @Override
112            public int countByG_U_C_S(
113                            long groupId, long userId, long[] categoryIds, int status)
114                    throws SystemException {
115    
116                    return doCountByG_U_C_S(groupId, userId, categoryIds, status, false);
117            }
118    
119            @Override
120            public int countByG_U_C_A_S(
121                            long groupId, long userId, long[] categoryIds, boolean anonymous,
122                            int status)
123                    throws SystemException {
124    
125                    return doCountByG_U_C_A_S(
126                            groupId, userId, categoryIds, anonymous, status, false);
127            }
128    
129            @Override
130            public int filterCountByG_U_C_S(
131                            long groupId, long userId, long[] categoryIds, int status)
132                    throws SystemException {
133    
134                    return doCountByG_U_C_S(groupId, userId, categoryIds, status, true);
135            }
136    
137            @Override
138            public int filterCountByG_U_MD_C_S(
139                            long groupId, long userId, Date modifiedDate, long[] categoryIds,
140                            int status)
141                    throws SystemException {
142    
143                    return doCountByG_U_MD_C_S(
144                            groupId, userId, modifiedDate, categoryIds, status, true);
145            }
146    
147            @Override
148            public int filterCountByG_U_C_A_S(
149                            long groupId, long userId, long[] categoryIds, boolean anonymous,
150                            int status)
151                    throws SystemException {
152    
153                    return doCountByG_U_C_A_S(
154                            groupId, userId, categoryIds, anonymous, status, true);
155            }
156    
157            @Override
158            public List<Long> filterFindByG_U_C_S(
159                            long groupId, long userId, long[] categoryIds, int status,
160                            int start, int end)
161                    throws SystemException {
162    
163                    return doFindByG_U_C_S(
164                            groupId, userId, categoryIds, status, start, end, true);
165            }
166    
167            @Override
168            public List<Long> filterFindByG_U_MD_C_S(
169                            long groupId, long userId, Date modifiedDate, long[] categoryIds,
170                            int status, int start, int end)
171                    throws SystemException {
172    
173                    return doFindByG_U_MD_C_S(
174                            groupId, userId, modifiedDate, categoryIds, status, start, end,
175                            true);
176            }
177    
178            @Override
179            public List<Long> filterFindByG_U_C_A_S(
180                            long groupId, long userId, long[] categoryIds, boolean anonymous,
181                            int status, int start, int end)
182                    throws SystemException {
183    
184                    return doFindByG_U_C_A_S(
185                            groupId, userId, categoryIds, anonymous, status, start, end, true);
186            }
187    
188            @Override
189            public List<MBMessage> findByNoAssets() throws SystemException {
190                    Session session = null;
191    
192                    try {
193                            session = openSession();
194    
195                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
196    
197                            SQLQuery q = session.createSQLQuery(sql);
198    
199                            q.addEntity("MBMessage", MBMessageImpl.class);
200    
201                            return q.list(true);
202                    }
203                    catch (Exception e) {
204                            throw new SystemException(e);
205                    }
206                    finally {
207                            closeSession(session);
208                    }
209            }
210    
211            @Override
212            public List<Long> findByG_U_C_S(
213                            long groupId, long userId, long[] categoryIds, int status,
214                            int start, int end)
215                    throws SystemException {
216    
217                    return doFindByG_U_C_S(
218                            groupId, userId, categoryIds, status, start, end, false);
219            }
220    
221            @Override
222            public List<Long> findByG_U_C_A_S(
223                            long groupId, long userId, long[] categoryIds, boolean anonymous,
224                            int status, int start, int end)
225                    throws SystemException {
226    
227                    return doFindByG_U_C_A_S(
228                            groupId, userId, categoryIds, anonymous, status, start, end, false);
229            }
230    
231            protected int doCountByG_U_C_S(
232                            long groupId, long userId, long[] categoryIds, int status,
233                            boolean inlineSQLHelper)
234                    throws SystemException {
235    
236                    Session session = null;
237    
238                    try {
239                            session = openSession();
240    
241                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C_S);
242    
243                            if (userId <= 0) {
244                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
245                            }
246    
247                            if ((categoryIds == null) || (categoryIds.length == 0)) {
248                                    sql = StringUtil.replace(
249                                            sql, "(currentMessage.categoryId = ?) AND",
250                                            StringPool.BLANK);
251                            }
252                            else {
253                                    sql = StringUtil.replace(
254                                            sql, "currentMessage.categoryId = ?",
255                                            "currentMessage.categoryId = " +
256                                                    StringUtil.merge(
257                                                            categoryIds, " OR currentMessage.categoryId = "));
258                            }
259    
260                            if (status != WorkflowConstants.STATUS_ANY) {
261                                    sql = CustomSQLUtil.appendCriteria(
262                                            sql, "AND (currentMessage.status = ?)");
263                            }
264    
265                            if (inlineSQLHelper) {
266                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
267                                            sql, MBMessage.class.getName(),
268                                            "currentMessage.rootMessageId", groupId);
269                            }
270    
271                            SQLQuery q = session.createSQLQuery(sql);
272    
273                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
274    
275                            QueryPos qPos = QueryPos.getInstance(q);
276    
277                            qPos.add(groupId);
278    
279                            if (userId > 0) {
280                                    qPos.add(userId);
281                            }
282    
283                            if (status != WorkflowConstants.STATUS_ANY) {
284                                    qPos.add(status);
285                            }
286    
287                            Iterator<Long> itr = q.iterate();
288    
289                            if (itr.hasNext()) {
290                                    Long count = itr.next();
291    
292                                    if (count != null) {
293                                            return count.intValue();
294                                    }
295                            }
296    
297                            return 0;
298                    }
299                    catch (Exception e) {
300                            throw new SystemException(e);
301                    }
302                    finally {
303                            closeSession(session);
304                    }
305            }
306    
307            protected int doCountByG_U_MD_C_S(
308                            long groupId, long userId, Date modifiedDate, long[] categoryIds,
309                            int status, boolean inlineSQLHelper)
310                    throws SystemException {
311    
312                    Session session = null;
313    
314                    try {
315                            session = openSession();
316    
317                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_MD_C_S);
318    
319                            if (userId <= 0) {
320                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
321                            }
322    
323                            if ((categoryIds == null) || (categoryIds.length == 0)) {
324                                    sql = StringUtil.replace(
325                                            sql, "(currentMessage.categoryId = ?) AND",
326                                            StringPool.BLANK);
327                            }
328                            else {
329                                    sql = StringUtil.replace(
330                                            sql, "currentMessage.categoryId = ?",
331                                            "currentMessage.categoryId = " +
332                                                    StringUtil.merge(
333                                                            categoryIds, " OR currentMessage.categoryId = "));
334                            }
335    
336                            if (status != WorkflowConstants.STATUS_ANY) {
337                                    sql = CustomSQLUtil.appendCriteria(
338                                            sql, "AND (currentMessage.status = ?)");
339                            }
340    
341                            if (inlineSQLHelper) {
342                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
343                                            sql, MBMessage.class.getName(),
344                                            "currentMessage.rootMessageId", groupId);
345                            }
346    
347                            SQLQuery q = session.createSQLQuery(sql);
348    
349                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
350    
351                            QueryPos qPos = QueryPos.getInstance(q);
352    
353                            qPos.add(groupId);
354    
355                            if (userId > 0) {
356                                    qPos.add(userId);
357                            }
358    
359                            qPos.add(modifiedDate);
360    
361                            if (status != WorkflowConstants.STATUS_ANY) {
362                                    qPos.add(status);
363                            }
364    
365                            Iterator<Long> itr = q.iterate();
366    
367                            if (itr.hasNext()) {
368                                    Long count = itr.next();
369    
370                                    if (count != null) {
371                                            return count.intValue();
372                                    }
373                            }
374    
375                            return 0;
376                    }
377                    catch (Exception e) {
378                            throw new SystemException(e);
379                    }
380                    finally {
381                            closeSession(session);
382                    }
383            }
384    
385            protected int doCountByG_U_C_A_S(
386                            long groupId, long userId, long[] categoryIds, boolean anonymous,
387                            int status, boolean inlineSQLHelper)
388                    throws SystemException {
389    
390                    Session session = null;
391    
392                    try {
393                            session = openSession();
394    
395                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C_A_S);
396    
397                            if ((categoryIds == null) || (categoryIds.length == 0)) {
398                                    sql = StringUtil.replace(
399                                            sql, "(currentMessage.categoryId = ?) AND",
400                                            StringPool.BLANK);
401                            }
402                            else {
403                                    sql = StringUtil.replace(
404                                            sql, "currentMessage.categoryId = ?",
405                                            "currentMessage.categoryId = " +
406                                                    StringUtil.merge(
407                                                            categoryIds, " OR currentMessage.categoryId = "));
408                            }
409    
410                            if (status != WorkflowConstants.STATUS_ANY) {
411                                    sql = CustomSQLUtil.appendCriteria(
412                                            sql, "AND (currentMessage.status = ?)");
413                            }
414    
415                            if (inlineSQLHelper) {
416                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
417                                            sql, MBMessage.class.getName(),
418                                            "currentMessage.rootMessageId", groupId);
419                            }
420    
421                            SQLQuery q = session.createSQLQuery(sql);
422    
423                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
424    
425                            QueryPos qPos = QueryPos.getInstance(q);
426    
427                            qPos.add(groupId);
428                            qPos.add(userId);
429                            qPos.add(anonymous);
430    
431                            if (status != WorkflowConstants.STATUS_ANY) {
432                                    qPos.add(status);
433                            }
434    
435                            Iterator<Long> itr = q.iterate();
436    
437                            if (itr.hasNext()) {
438                                    Long count = itr.next();
439    
440                                    if (count != null) {
441                                            return count.intValue();
442                                    }
443                            }
444    
445                            return 0;
446                    }
447                    catch (Exception e) {
448                            throw new SystemException(e);
449                    }
450                    finally {
451                            closeSession(session);
452                    }
453            }
454    
455            protected List<Long> doFindByG_U_C_S(
456                            long groupId, long userId, long[] categoryIds, int status,
457                            int start, int end, boolean inlineSQLHelper)
458                    throws SystemException {
459    
460                    Session session = null;
461    
462                    try {
463                            session = openSession();
464    
465                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_S);
466    
467                            if (userId <= 0) {
468                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
469                            }
470    
471                            if ((categoryIds == null) || (categoryIds.length == 0)) {
472                                    sql = StringUtil.replace(
473                                            sql, "(currentMessage.categoryId = ?) AND",
474                                            StringPool.BLANK);
475                            }
476                            else {
477                                    sql = StringUtil.replace(
478                                            sql, "currentMessage.categoryId = ?",
479                                            "currentMessage.categoryId = " +
480                                                    StringUtil.merge(
481                                                            categoryIds, " OR currentMessage.categoryId = "));
482                            }
483    
484                            if (status != WorkflowConstants.STATUS_ANY) {
485                                    sql = CustomSQLUtil.appendCriteria(
486                                            sql, "AND (currentMessage.status = ?)");
487                            }
488    
489                            if (inlineSQLHelper) {
490                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
491                                            sql, MBMessage.class.getName(),
492                                            "currentMessage.rootMessageId", groupId);
493                            }
494    
495                            SQLQuery q = session.createSQLQuery(sql);
496    
497                            q.addScalar("threadId", Type.LONG);
498    
499                            QueryPos qPos = QueryPos.getInstance(q);
500    
501                            qPos.add(groupId);
502    
503                            if (userId > 0) {
504                                    qPos.add(userId);
505                            }
506    
507                            if (status != WorkflowConstants.STATUS_ANY) {
508                                    qPos.add(status);
509                            }
510    
511                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
512                    }
513                    catch (Exception e) {
514                            throw new SystemException(e);
515                    }
516                    finally {
517                            closeSession(session);
518                    }
519            }
520    
521            protected List<Long> doFindByG_U_MD_C_S(
522                            long groupId, long userId, Date modifiedDate, long[] categoryIds,
523                            int status, int start, int end, boolean inlineSQLHelper)
524                    throws SystemException {
525    
526                    Session session = null;
527    
528                    try {
529                            session = openSession();
530    
531                            String sql = CustomSQLUtil.get(FIND_BY_G_U_MD_C_S);
532    
533                            if (userId <= 0) {
534                                    sql = StringUtil.replace(sql, _USER_ID_SQL, StringPool.BLANK);
535                            }
536    
537                            if ((categoryIds == null) || (categoryIds.length == 0)) {
538                                    sql = StringUtil.replace(
539                                            sql, "(currentMessage.categoryId = ?) AND",
540                                            StringPool.BLANK);
541                            }
542                            else {
543                                    sql = StringUtil.replace(
544                                            sql, "currentMessage.categoryId = ?",
545                                            "currentMessage.categoryId = " +
546                                                    StringUtil.merge(
547                                                            categoryIds, " OR currentMessage.categoryId = "));
548                            }
549    
550                            if (status != WorkflowConstants.STATUS_ANY) {
551                                    sql = CustomSQLUtil.appendCriteria(
552                                            sql, "AND (currentMessage.status = ?)");
553                            }
554    
555                            if (inlineSQLHelper) {
556                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
557                                            sql, MBMessage.class.getName(),
558                                            "currentMessage.rootMessageId", groupId);
559                            }
560    
561                            SQLQuery q = session.createSQLQuery(sql);
562    
563                            q.addScalar("threadId", Type.LONG);
564    
565                            QueryPos qPos = QueryPos.getInstance(q);
566    
567                            qPos.add(groupId);
568    
569                            if (userId > 0) {
570                                    qPos.add(userId);
571                            }
572    
573                            qPos.add(modifiedDate);
574    
575                            if (status != WorkflowConstants.STATUS_ANY) {
576                                    qPos.add(status);
577                            }
578    
579                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
580                    }
581                    catch (Exception e) {
582                            throw new SystemException(e);
583                    }
584                    finally {
585                            closeSession(session);
586                    }
587            }
588    
589            protected List<Long> doFindByG_U_C_A_S(
590                            long groupId, long userId, long[] categoryIds, boolean anonymous,
591                            int status, int start, int end, boolean inlineSQLHelper)
592                    throws SystemException {
593    
594                    Session session = null;
595    
596                    try {
597                            session = openSession();
598    
599                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_A_S);
600    
601                            if ((categoryIds == null) || (categoryIds.length == 0)) {
602                                    sql = StringUtil.replace(
603                                            sql, "(currentMessage.categoryId = ?) AND",
604                                            StringPool.BLANK);
605                            }
606                            else {
607                                    sql = StringUtil.replace(
608                                            sql, "currentMessage.categoryId = ?",
609                                            "currentMessage.categoryId = " +
610                                                    StringUtil.merge(
611                                                            categoryIds, " OR currentMessage.categoryId = "));
612                            }
613    
614                            if (status != WorkflowConstants.STATUS_ANY) {
615                                    sql = CustomSQLUtil.appendCriteria(
616                                            sql, "AND (currentMessage.status = ?)");
617                            }
618    
619                            if (inlineSQLHelper) {
620                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
621                                            sql, MBMessage.class.getName(),
622                                            "currentMessage.rootMessageId", groupId);
623                            }
624    
625                            SQLQuery q = session.createSQLQuery(sql);
626    
627                            q.addScalar("threadId", Type.LONG);
628    
629                            QueryPos qPos = QueryPos.getInstance(q);
630    
631                            qPos.add(groupId);
632                            qPos.add(userId);
633                            qPos.add(anonymous);
634    
635                            if (status != WorkflowConstants.STATUS_ANY) {
636                                    qPos.add(status);
637                            }
638    
639                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
640                    }
641                    catch (Exception e) {
642                            throw new SystemException(e);
643                    }
644                    finally {
645                            closeSession(session);
646                    }
647            }
648    
649            private static final String _USER_ID_SQL =
650                    "AND (currentMessage.userId = ?)";
651    
652    }