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.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_C_A_S =
052                    MBMessageFinder.class.getName() + ".countByG_U_C_A_S";
053    
054            public static final String FIND_BY_NO_ASSETS =
055                    MBMessageFinder.class.getName() + ".findByNoAssets";
056    
057            public static final String FIND_BY_G_U_C_S =
058                    MBMessageFinder.class.getName() + ".findByG_U_C_S";
059    
060            public static final String FIND_BY_G_U_C_A_S =
061                    MBMessageFinder.class.getName() + ".findByG_U_C_A_S";
062    
063            public int countByC_T(Date createDate, long threadId)
064                    throws SystemException {
065    
066                    Timestamp createDate_TS = CalendarUtil.getTimestamp(createDate);
067    
068                    Session session = null;
069    
070                    try {
071                            session = openSession();
072    
073                            String sql = CustomSQLUtil.get(COUNT_BY_C_T);
074    
075                            SQLQuery q = session.createSQLQuery(sql);
076    
077                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
078    
079                            QueryPos qPos = QueryPos.getInstance(q);
080    
081                            qPos.add(createDate_TS);
082                            qPos.add(threadId);
083    
084                            Iterator<Long> itr = q.iterate();
085    
086                            if (itr.hasNext()) {
087                                    Long count = itr.next();
088    
089                                    if (count != null) {
090                                            return count.intValue();
091                                    }
092                            }
093    
094                            return 0;
095                    }
096                    catch (Exception e) {
097                            throw new SystemException(e);
098                    }
099                    finally {
100                            closeSession(session);
101                    }
102            }
103    
104            public int countByG_U_C_S(
105                            long groupId, long userId, long[] categoryIds, int status)
106                    throws SystemException {
107    
108                    return doCountByG_U_C_S(groupId, userId, categoryIds, status, false);
109            }
110    
111            public int countByG_U_C_A_S(
112                            long groupId, long userId, long[] categoryIds, boolean anonymous,
113                            int status)
114                    throws SystemException {
115    
116                    return doCountByG_U_C_A_S(
117                            groupId, userId, categoryIds, anonymous, status, false);
118            }
119    
120            public int filterCountByG_U_C_S(
121                            long groupId, long userId, long[] categoryIds, int status)
122                    throws SystemException {
123    
124                    return doCountByG_U_C_S(groupId, userId, categoryIds, status, true);
125            }
126    
127            public int filterCountByG_U_C_A_S(
128                            long groupId, long userId, long[] categoryIds, boolean anonymous,
129                            int status)
130                    throws SystemException {
131    
132                    return doCountByG_U_C_A_S(
133                            groupId, userId, categoryIds, anonymous, status, true);
134            }
135    
136            public List<Long> filterFindByG_U_C_S(
137                            long groupId, long userId, long[] categoryIds, int status,
138                            int start, int end)
139                    throws SystemException {
140    
141                    return doFindByG_U_C_S(
142                            groupId, userId, categoryIds, status, start, end, true);
143            }
144    
145            public List<Long> filterFindByG_U_C_A_S(
146                            long groupId, long userId, long[] categoryIds, boolean anonymous,
147                            int status, int start, int end)
148                    throws SystemException {
149    
150                    return doFindByG_U_C_A_S(
151                            groupId, userId, categoryIds, anonymous, status, start, end, true);
152            }
153    
154            public List<MBMessage> findByNoAssets() throws SystemException {
155                    Session session = null;
156    
157                    try {
158                            session = openSession();
159    
160                            String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
161    
162                            SQLQuery q = session.createSQLQuery(sql);
163    
164                            q.addEntity("MBMessage", MBMessageImpl.class);
165    
166                            return q.list(true);
167                    }
168                    catch (Exception e) {
169                            throw new SystemException(e);
170                    }
171                    finally {
172                            closeSession(session);
173                    }
174            }
175    
176            public List<Long> findByG_U_C_S(
177                            long groupId, long userId, long[] categoryIds, int status,
178                            int start, int end)
179                    throws SystemException {
180    
181                    return doFindByG_U_C_S(
182                            groupId, userId, categoryIds, status, start, end, false);
183            }
184    
185            public List<Long> findByG_U_C_A_S(
186                            long groupId, long userId, long[] categoryIds, boolean anonymous,
187                            int status, int start, int end)
188                    throws SystemException {
189    
190                    return doFindByG_U_C_A_S(
191                            groupId, userId, categoryIds, anonymous, status, start, end, false);
192            }
193    
194            protected int doCountByG_U_C_S(
195                            long groupId, long userId, long[] categoryIds, int status,
196                            boolean inlineSQLHelper)
197                    throws SystemException {
198    
199                    Session session = null;
200    
201                    try {
202                            session = openSession();
203    
204                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C_S);
205    
206                            if ((categoryIds == null) || (categoryIds.length == 0)) {
207                                    sql = StringUtil.replace(
208                                            sql, "(currentMessage.categoryId = ?) AND",
209                                            StringPool.BLANK);
210                            }
211                            else {
212                                    sql = StringUtil.replace(
213                                            sql, "currentMessage.categoryId = ?",
214                                            "currentMessage.categoryId = " +
215                                                    StringUtil.merge(
216                                                            categoryIds, " OR currentMessage.categoryId = "));
217                            }
218    
219                            if (status != WorkflowConstants.STATUS_ANY) {
220                                    sql = CustomSQLUtil.appendCriteria(
221                                            sql, "AND (currentMessage.status = ?)");
222                            }
223    
224                            if (inlineSQLHelper) {
225                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
226                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
227                                            groupId);
228                            }
229    
230                            SQLQuery q = session.createSQLQuery(sql);
231    
232                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
233    
234                            QueryPos qPos = QueryPos.getInstance(q);
235    
236                            qPos.add(groupId);
237                            qPos.add(userId);
238    
239                            if (status != WorkflowConstants.STATUS_ANY) {
240                                    qPos.add(status);
241                            }
242    
243                            Iterator<Long> itr = q.iterate();
244    
245                            if (itr.hasNext()) {
246                                    Long count = itr.next();
247    
248                                    if (count != null) {
249                                            return count.intValue();
250                                    }
251                            }
252    
253                            return 0;
254                    }
255                    catch (Exception e) {
256                            throw new SystemException(e);
257                    }
258                    finally {
259                            closeSession(session);
260                    }
261            }
262    
263            protected int doCountByG_U_C_A_S(
264                            long groupId, long userId, long[] categoryIds, boolean anonymous,
265                            int status, boolean inlineSQLHelper)
266                    throws SystemException {
267    
268                    Session session = null;
269    
270                    try {
271                            session = openSession();
272    
273                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C_A_S);
274    
275                            if ((categoryIds == null) || (categoryIds.length == 0)) {
276                                    sql = StringUtil.replace(
277                                            sql, "(currentMessage.categoryId = ?) AND",
278                                            StringPool.BLANK);
279                            }
280                            else {
281                                    sql = StringUtil.replace(
282                                            sql, "currentMessage.categoryId = ?",
283                                            "currentMessage.categoryId = " +
284                                                    StringUtil.merge(
285                                                            categoryIds, " OR currentMessage.categoryId = "));
286                            }
287    
288                            if (status != WorkflowConstants.STATUS_ANY) {
289                                    sql = CustomSQLUtil.appendCriteria(
290                                            sql, "AND (currentMessage.status = ?)");
291                            }
292    
293                            if (inlineSQLHelper) {
294                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
295                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
296                                            groupId);
297                            }
298    
299                            SQLQuery q = session.createSQLQuery(sql);
300    
301                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
302    
303                            QueryPos qPos = QueryPos.getInstance(q);
304    
305                            qPos.add(groupId);
306                            qPos.add(userId);
307                            qPos.add(anonymous);
308    
309                            if (status != WorkflowConstants.STATUS_ANY) {
310                                    qPos.add(status);
311                            }
312    
313                            Iterator<Long> itr = q.iterate();
314    
315                            if (itr.hasNext()) {
316                                    Long count = itr.next();
317    
318                                    if (count != null) {
319                                            return count.intValue();
320                                    }
321                            }
322    
323                            return 0;
324                    }
325                    catch (Exception e) {
326                            throw new SystemException(e);
327                    }
328                    finally {
329                            closeSession(session);
330                    }
331            }
332    
333            protected List<Long> doFindByG_U_C_S(
334                            long groupId, long userId, long[] categoryIds, int status,
335                            int start, int end, boolean inlineSQLHelper)
336                    throws SystemException {
337    
338                    Session session = null;
339    
340                    try {
341                            session = openSession();
342    
343                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_S);
344    
345                            if ((categoryIds == null) || (categoryIds.length == 0)) {
346                                    sql = StringUtil.replace(
347                                            sql, "(currentMessage.categoryId = ?) AND",
348                                            StringPool.BLANK);
349                            }
350                            else {
351                                    sql = StringUtil.replace(
352                                            sql, "currentMessage.categoryId = ?",
353                                            "currentMessage.categoryId = " +
354                                                    StringUtil.merge(
355                                                            categoryIds, " OR currentMessage.categoryId = "));
356                            }
357    
358                            if (status != WorkflowConstants.STATUS_ANY) {
359                                    sql = CustomSQLUtil.appendCriteria(
360                                            sql, "AND (currentMessage.status = ?)");
361                            }
362    
363                            if (inlineSQLHelper) {
364                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
365                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
366                                            groupId);
367                            }
368    
369                            SQLQuery q = session.createSQLQuery(sql);
370    
371                            q.addScalar("threadId", Type.LONG);
372    
373                            QueryPos qPos = QueryPos.getInstance(q);
374    
375                            qPos.add(groupId);
376                            qPos.add(userId);
377    
378                            if (status != WorkflowConstants.STATUS_ANY) {
379                                    qPos.add(status);
380                            }
381    
382                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
383                    }
384                    catch (Exception e) {
385                            throw new SystemException(e);
386                    }
387                    finally {
388                            closeSession(session);
389                    }
390            }
391    
392            protected List<Long> doFindByG_U_C_A_S(
393                            long groupId, long userId, long[] categoryIds, boolean anonymous,
394                            int status, int start, int end, boolean inlineSQLHelper)
395                    throws SystemException {
396    
397                    Session session = null;
398    
399                    try {
400                            session = openSession();
401    
402                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_A_S);
403    
404                            if ((categoryIds == null) || (categoryIds.length == 0)) {
405                                    sql = StringUtil.replace(
406                                            sql, "(currentMessage.categoryId = ?) AND",
407                                            StringPool.BLANK);
408                            }
409                            else {
410                                    sql = StringUtil.replace(
411                                            sql, "currentMessage.categoryId = ?",
412                                            "currentMessage.categoryId = " +
413                                                    StringUtil.merge(
414                                                            categoryIds, " OR currentMessage.categoryId = "));
415                            }
416    
417                            if (status != WorkflowConstants.STATUS_ANY) {
418                                    sql = CustomSQLUtil.appendCriteria(
419                                            sql, "AND (currentMessage.status = ?)");
420                            }
421    
422                            if (inlineSQLHelper) {
423                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
424                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
425                                            groupId);
426                            }
427    
428                            SQLQuery q = session.createSQLQuery(sql);
429    
430                            q.addScalar("threadId", Type.LONG);
431    
432                            QueryPos qPos = QueryPos.getInstance(q);
433    
434                            qPos.add(groupId);
435                            qPos.add(userId);
436                            qPos.add(anonymous);
437    
438                            if (status != WorkflowConstants.STATUS_ANY) {
439                                    qPos.add(status);
440                            }
441    
442                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
443                    }
444                    catch (Exception e) {
445                            throw new SystemException(e);
446                    }
447                    finally {
448                            closeSession(session);
449                    }
450            }
451    
452    }