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