001    /**
002     * Copyright (c) 2000-2010 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 String COUNT_BY_C_T =
046                    MBMessageFinder.class.getName() + ".countByC_T";
047    
048            public static String COUNT_BY_G_U_C_S =
049                    MBMessageFinder.class.getName() + ".countByG_U_C_S";
050    
051            public static String COUNT_BY_G_U_C_A_S =
052                    MBMessageFinder.class.getName() + ".countByG_U_C_A_S";
053    
054            public static String FIND_BY_NO_ASSETS =
055                    MBMessageFinder.class.getName() + ".findByNoAssets";
056    
057            public static String FIND_BY_G_U_C_S =
058                    MBMessageFinder.class.getName() + ".findByG_U_C_S";
059    
060            public static 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.list().iterator();
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();
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) ||
207                                    (categoryIds.length == 0)) {
208    
209                                    sql = StringUtil.replace(
210                                            sql, "(currentMessage.categoryId = ?) AND",
211                                            StringPool.BLANK);
212                            }
213                            else {
214                                    sql = StringUtil.replace(
215                                            sql, "currentMessage.categoryId = ?",
216                                            "currentMessage.categoryId = " +
217                                                    StringUtil.merge(
218                                                            categoryIds, " OR currentMessage.categoryId = "));
219                            }
220    
221                            if (status != WorkflowConstants.STATUS_ANY) {
222                                    sql = CustomSQLUtil.appendCriteria(
223                                            sql, "AND (currentMessage.status = ?)");
224                            }
225    
226                            if (inlineSQLHelper) {
227                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
228                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
229                                            "rootMessage.userId", groupId);
230                            }
231    
232                            SQLQuery q = session.createSQLQuery(sql);
233    
234                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
235    
236                            QueryPos qPos = QueryPos.getInstance(q);
237    
238                            qPos.add(groupId);
239                            qPos.add(userId);
240    
241                            if (status != WorkflowConstants.STATUS_ANY) {
242                                    qPos.add(status);
243                            }
244    
245                            Iterator<Long> itr = q.list().iterator();
246    
247                            if (itr.hasNext()) {
248                                    Long count = itr.next();
249    
250                                    if (count != null) {
251                                            return count.intValue();
252                                    }
253                            }
254    
255                            return 0;
256                    }
257                    catch (Exception e) {
258                            throw new SystemException(e);
259                    }
260                    finally {
261                            closeSession(session);
262                    }
263            }
264    
265            protected int doCountByG_U_C_A_S(
266                            long groupId, long userId, long[] categoryIds, boolean anonymous,
267                            int status, boolean inlineSQLHelper)
268                    throws SystemException {
269    
270                    Session session = null;
271    
272                    try {
273                            session = openSession();
274    
275                            String sql = CustomSQLUtil.get(COUNT_BY_G_U_C_A_S);
276    
277                            if ((categoryIds == null) ||
278                                    (categoryIds.length == 0)) {
279    
280                                    sql = StringUtil.replace(
281                                            sql, "(currentMessage.categoryId = ?) AND",
282                                            StringPool.BLANK);
283                            }
284                            else {
285                                    sql = StringUtil.replace(
286                                            sql, "currentMessage.categoryId = ?",
287                                            "currentMessage.categoryId = " +
288                                                    StringUtil.merge(
289                                                            categoryIds, " OR currentMessage.categoryId = "));
290                            }
291    
292                            if (status != WorkflowConstants.STATUS_ANY) {
293                                    sql = CustomSQLUtil.appendCriteria(
294                                            sql, "AND (currentMessage.status = ?)");
295                            }
296    
297                            if (inlineSQLHelper) {
298                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
299                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
300                                            "rootMessage.userId", groupId);
301                            }
302    
303                            SQLQuery q = session.createSQLQuery(sql);
304    
305                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
306    
307                            QueryPos qPos = QueryPos.getInstance(q);
308    
309                            qPos.add(groupId);
310                            qPos.add(userId);
311                            qPos.add(anonymous);
312    
313                            if (status != WorkflowConstants.STATUS_ANY) {
314                                    qPos.add(status);
315                            }
316    
317                            Iterator<Long> itr = q.list().iterator();
318    
319                            if (itr.hasNext()) {
320                                    Long count = itr.next();
321    
322                                    if (count != null) {
323                                            return count.intValue();
324                                    }
325                            }
326    
327                            return 0;
328                    }
329                    catch (Exception e) {
330                            throw new SystemException(e);
331                    }
332                    finally {
333                            closeSession(session);
334                    }
335            }
336    
337            protected List<Long> doFindByG_U_C_S(
338                            long groupId, long userId, long[] categoryIds, int status,
339                            int start, int end, boolean inlineSQLHelper)
340                    throws SystemException {
341    
342                    Session session = null;
343    
344                    try {
345                            session = openSession();
346    
347                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_S);
348    
349                            if ((categoryIds == null) ||
350                                    (categoryIds.length == 0)) {
351    
352                                    sql = StringUtil.replace(
353                                            sql, "(currentMessage.categoryId = ?) AND",
354                                            StringPool.BLANK);
355                            }
356                            else {
357                                    sql = StringUtil.replace(
358                                            sql, "currentMessage.categoryId = ?",
359                                            "currentMessage.categoryId = " +
360                                                    StringUtil.merge(
361                                                            categoryIds, " OR currentMessage.categoryId = "));
362                            }
363    
364                            if (status != WorkflowConstants.STATUS_ANY) {
365                                    sql = CustomSQLUtil.appendCriteria(
366                                            sql, "AND (currentMessage.status = ?)");
367                            }
368    
369                            if (inlineSQLHelper) {
370                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
371                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
372                                            "rootMessage.userId", groupId);
373                            }
374    
375                            SQLQuery q = session.createSQLQuery(sql);
376    
377                            q.addScalar("threadId", Type.LONG);
378    
379                            QueryPos qPos = QueryPos.getInstance(q);
380    
381                            qPos.add(groupId);
382                            qPos.add(userId);
383    
384                            if (status != WorkflowConstants.STATUS_ANY) {
385                                    qPos.add(status);
386                            }
387    
388                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
389                    }
390                    catch (Exception e) {
391                            throw new SystemException(e);
392                    }
393                    finally {
394                            closeSession(session);
395                    }
396            }
397    
398            protected List<Long> doFindByG_U_C_A_S(
399                            long groupId, long userId, long[] categoryIds, boolean anonymous,
400                            int status, int start, int end, boolean inlineSQLHelper)
401                    throws SystemException {
402    
403                    Session session = null;
404    
405                    try {
406                            session = openSession();
407    
408                            String sql = CustomSQLUtil.get(FIND_BY_G_U_C_A_S);
409    
410                            if ((categoryIds == null) ||
411                                    (categoryIds.length == 0)) {
412    
413                                    sql = StringUtil.replace(
414                                            sql, "(currentMessage.categoryId = ?) AND",
415                                            StringPool.BLANK);
416                            }
417                            else {
418                                    sql = StringUtil.replace(
419                                            sql, "currentMessage.categoryId = ?",
420                                            "currentMessage.categoryId = " +
421                                                    StringUtil.merge(
422                                                            categoryIds, " OR currentMessage.categoryId = "));
423                            }
424    
425                            if (status != WorkflowConstants.STATUS_ANY) {
426                                    sql = CustomSQLUtil.appendCriteria(
427                                            sql, "AND (currentMessage.status = ?)");
428                            }
429    
430                            if (inlineSQLHelper) {
431                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
432                                            sql, MBMessage.class.getName(), "rootMessage.messageId",
433                                            "rootMessage.userId", groupId);
434                            }
435    
436                            SQLQuery q = session.createSQLQuery(sql);
437    
438                            q.addScalar("threadId", Type.LONG);
439    
440                            QueryPos qPos = QueryPos.getInstance(q);
441    
442                            qPos.add(groupId);
443                            qPos.add(userId);
444                            qPos.add(anonymous);
445    
446                            if (status != WorkflowConstants.STATUS_ANY) {
447                                    qPos.add(status);
448                            }
449    
450                            return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
451                    }
452                    catch (Exception e) {
453                            throw new SystemException(e);
454                    }
455                    finally {
456                            closeSession(session);
457                    }
458            }
459    
460    }