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.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
027    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.messageboards.model.MBMessage;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
032    import com.liferay.util.dao.orm.CustomSQLUtil;
033    
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class MBThreadFinderImpl
041            extends BasePersistenceImpl<MBThread> implements MBThreadFinder {
042    
043            public static String COUNT_BY_G_C =
044                    MBThreadFinder.class.getName() + ".countByG_C";
045    
046            public static String COUNT_BY_G_C_S =
047                    MBThreadFinder.class.getName() + ".countByG_C_S";
048    
049            public static String COUNT_BY_S_G_U_C_S =
050                    MBThreadFinder.class.getName() + ".countByS_G_U_C_S";
051    
052            public static String FIND_BY_G_C =
053                    MBThreadFinder.class.getName() + ".findByG_C";
054    
055            public static String FIND_BY_G_C_S =
056                    MBThreadFinder.class.getName() + ".findByG_C_S";
057    
058            public static String FIND_BY_S_G_U_C_S =
059                    MBThreadFinder.class.getName() + ".findByS_G_U_C_S";
060    
061            public int countByG_C_S(long groupId, long categoryId, int status)
062                    throws SystemException {
063    
064                    return doCountByG_C_S(groupId, categoryId, status, false);
065            }
066    
067            public int countByS_G_U_C_S(
068                            long groupId, long userId, long[] categoryIds, int status)
069                    throws SystemException {
070    
071                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, false);
072            }
073    
074            public int filterCountByG_C(long groupId, long categoryId)
075                    throws SystemException {
076    
077                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
078                            return MBThreadUtil.countByG_C(groupId, categoryId);
079                    }
080    
081                    Session session = null;
082    
083                    try {
084                            session = openSession();
085    
086                            String sql = CustomSQLUtil.get(COUNT_BY_G_C);
087    
088                            sql = InlineSQLHelperUtil.replacePermissionCheck(
089                                    sql, MBMessage.class.getName(), "MBMessage.messageId",
090                                    "MBMessage.userId", groupId);
091    
092                            SQLQuery q = session.createSQLQuery(sql);
093    
094                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
095    
096                            QueryPos qPos = QueryPos.getInstance(q);
097    
098                            qPos.add(groupId);
099                            qPos.add(categoryId);
100    
101                            Iterator<Long> itr = q.list().iterator();
102    
103                            if (itr.hasNext()) {
104                                    Long count = itr.next();
105    
106                                    if (count != null) {
107                                            return count.intValue();
108                                    }
109                            }
110    
111                            return 0;
112                    }
113                    catch (Exception e) {
114                            throw processException(e);
115                    }
116                    finally {
117                            closeSession(session);
118                    }
119            }
120    
121            public int filterCountByG_C_S(long groupId, long categoryId, int status)
122                    throws SystemException {
123    
124                    return doCountByG_C_S(groupId, categoryId, status, true);
125            }
126    
127            public int filterCountByS_G_U_C_S(
128                            long groupId, long userId, long[] categoryIds, int status)
129                    throws SystemException {
130    
131                    return doCountByS_G_U_C_S(groupId, userId, categoryIds, status, true);
132            }
133    
134            public List<MBThread> filterFindByG_C(
135                            long groupId, long categoryId, int start, int end)
136                    throws SystemException {
137    
138                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
139                            return MBThreadUtil.findByG_C(groupId, categoryId, start, end);
140                    }
141    
142                    Session session = null;
143    
144                    try {
145                            session = openSession();
146    
147                            String sql = CustomSQLUtil.get(FIND_BY_G_C);
148    
149                            sql = InlineSQLHelperUtil.replacePermissionCheck(
150                                    sql, MBMessage.class.getName(), "MBMessage.messageId",
151                                    "MBMessage.userId", groupId);
152    
153                            SQLQuery q = session.createSQLQuery(sql);
154    
155                            q.addEntity("MBThread", MBThreadImpl.class);
156    
157                            QueryPos qPos = QueryPos.getInstance(q);
158    
159                            qPos.add(groupId);
160                            qPos.add(categoryId);
161    
162                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
163                    }
164                    catch (Exception e) {
165                            throw new SystemException(e);
166                    }
167                    finally {
168                            closeSession(session);
169                    }
170            }
171    
172            public List<MBThread> filterFindByG_C_S(
173                            long groupId, long categoryId, int status, int start, int end)
174                    throws SystemException {
175    
176                    return doFindByG_C_S(groupId, categoryId, status, start, end, true);
177            }
178    
179            public List<MBThread> filterFindByS_G_U_C_S(
180                            long groupId, long userId, long[] categoryIds, int status,
181                            int start, int end)
182                    throws SystemException {
183    
184                    return doFindByS_G_U_C_S(
185                            groupId, userId, categoryIds, status, start, end, true);
186            }
187    
188            public List<MBThread> findByG_C_S(
189                            long groupId, long categoryId, int status, int start, int end)
190                    throws SystemException {
191    
192                    return doFindByG_C_S(groupId, categoryId, status, start, end, false);
193            }
194    
195            public List<MBThread> findByS_G_U_C_S(
196                            long groupId, long userId, long[] categoryIds, int status,
197                            int start, int end)
198                    throws SystemException {
199    
200                    return doFindByS_G_U_C_S(
201                            groupId, userId, categoryIds, status, start, end, false);
202            }
203    
204            protected int doCountByG_C_S(
205                            long groupId, long categoryId, int status, boolean inlineSQLHelper)
206                    throws SystemException {
207    
208                    Session session = null;
209    
210                    try {
211                            session = openSession();
212    
213                            String sql = CustomSQLUtil.get(COUNT_BY_G_C_S);
214    
215                            if (status != WorkflowConstants.STATUS_ANY) {
216                                    sql = CustomSQLUtil.appendCriteria(
217                                            sql, "AND (MBThread.status = ?)");
218                            }
219    
220                            if (inlineSQLHelper) {
221                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
222                                            sql, MBMessage.class.getName(), "MBMessage.messageId",
223                                            "MBMessage.userId", groupId);
224                            }
225    
226                            SQLQuery q = session.createSQLQuery(sql);
227    
228                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
229    
230                            QueryPos qPos = QueryPos.getInstance(q);
231    
232                            qPos.add(groupId);
233                            qPos.add(categoryId);
234    
235                            if (status != WorkflowConstants.STATUS_ANY) {
236                                    qPos.add(status);
237                            }
238    
239                            Iterator<Long> itr = q.list().iterator();
240    
241                            if (itr.hasNext()) {
242                                    Long count = itr.next();
243    
244                                    if (count != null) {
245                                            return count.intValue();
246                                    }
247                            }
248    
249                            return 0;
250                    }
251                    catch (Exception e) {
252                            throw processException(e);
253                    }
254                    finally {
255                            closeSession(session);
256                    }
257            }
258    
259            protected int doCountByS_G_U_C_S(
260                            long groupId, long userId, long[] categoryIds, int status,
261                            boolean inlineSQLHelper)
262                    throws SystemException {
263    
264                    Session session = null;
265    
266                    try {
267                            session = openSession();
268    
269                            String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_C_S);
270    
271                            if ((categoryIds == null) || (categoryIds.length == 0)) {
272                                    sql = StringUtil.replace(
273                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
274                            }
275                            else {
276                                    sql = StringUtil.replace(
277                                            sql, "MBThread.categoryId = ?",
278                                            "MBThread.categoryId = " +
279                                                    StringUtil.merge(
280                                                            categoryIds, " OR MBThread.categoryId = "));
281                            }
282    
283                            if (status != WorkflowConstants.STATUS_ANY) {
284                                    sql = CustomSQLUtil.appendCriteria(
285                                            sql, "AND (MBThread.status = ?)");
286                            }
287    
288                            if (inlineSQLHelper) {
289                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
290                                            sql, MBMessage.class.getName(), "MBMessage.messageId",
291                                            "MBMessage.userId", groupId);
292                            }
293    
294                            SQLQuery q = session.createSQLQuery(sql);
295    
296                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
297    
298                            QueryPos qPos = QueryPos.getInstance(q);
299    
300                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
301                            qPos.add(groupId);
302                            qPos.add(userId);
303    
304                            if (status != WorkflowConstants.STATUS_ANY) {
305                                    qPos.add(status);
306                            }
307    
308                            Iterator<Long> itr = q.list().iterator();
309    
310                            if (itr.hasNext()) {
311                                    Long count = itr.next();
312    
313                                    if (count != null) {
314                                            return count.intValue();
315                                    }
316                            }
317    
318                            return 0;
319                    }
320                    catch (Exception e) {
321                            throw new SystemException(e);
322                    }
323                    finally {
324                            closeSession(session);
325                    }
326            }
327    
328            protected List<MBThread> doFindByG_C_S(
329                            long groupId, long categoryId, int status, int start, int end,
330                            boolean inlineSQLHelper)
331                    throws SystemException {
332    
333                    Session session = null;
334    
335                    try {
336                            session = openSession();
337    
338                            String sql = CustomSQLUtil.get(FIND_BY_G_C_S);
339    
340                            if (status != WorkflowConstants.STATUS_ANY) {
341                                    sql = CustomSQLUtil.appendCriteria(
342                                            sql, "AND (MBThread.status = ?)");
343                            }
344    
345                            if (inlineSQLHelper) {
346                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
347                                            sql, MBMessage.class.getName(), "MBMessage.messageId",
348                                            "MBMessage.userId", groupId);
349                            }
350    
351                            SQLQuery q = session.createSQLQuery(sql);
352    
353                            q.addEntity("MBThread", MBThreadImpl.class);
354    
355                            QueryPos qPos = QueryPos.getInstance(q);
356    
357                            qPos.add(groupId);
358                            qPos.add(categoryId);
359    
360                            if (status != WorkflowConstants.STATUS_ANY) {
361                                    qPos.add(status);
362                            }
363    
364                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
365                    }
366                    catch (Exception e) {
367                            throw new SystemException(e);
368                    }
369                    finally {
370                            closeSession(session);
371                    }
372            }
373    
374            protected List<MBThread> doFindByS_G_U_C_S(
375                            long groupId, long userId, long[] categoryIds, int status,
376                            int start, int end, boolean inlineSQLHelper)
377                    throws SystemException {
378    
379                    Session session = null;
380    
381                    try {
382                            session = openSession();
383    
384                            String sql = CustomSQLUtil.get(FIND_BY_S_G_U_C_S);
385    
386                            if ((categoryIds == null) || (categoryIds.length == 0)) {
387                                    sql = StringUtil.replace(
388                                            sql, "(MBThread.categoryId = ?) AND", StringPool.BLANK);
389                            }
390                            else {
391                                    sql = StringUtil.replace(
392                                            sql, "MBThread.categoryId = ?",
393                                            "MBThread.categoryId = " +
394                                                    StringUtil.merge(
395                                                            categoryIds, " OR MBThread.categoryId = "));
396                            }
397    
398                            if (status != WorkflowConstants.STATUS_ANY) {
399                                    sql = CustomSQLUtil.appendCriteria(
400                                            sql, "AND (MBThread.status = ?)");
401                            }
402    
403                            if (inlineSQLHelper) {
404                                    sql = InlineSQLHelperUtil.replacePermissionCheck(
405                                            sql, MBMessage.class.getName(), "MBMessage.messageId",
406                                            "MBMessage.userId", groupId);
407                            }
408    
409                            SQLQuery q = session.createSQLQuery(sql);
410    
411                            q.addEntity("MBThread", MBThreadImpl.class);
412    
413                            QueryPos qPos = QueryPos.getInstance(q);
414    
415                            qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
416                            qPos.add(groupId);
417                            qPos.add(userId);
418    
419                            if (status != WorkflowConstants.STATUS_ANY) {
420                                    qPos.add(status);
421                            }
422    
423                            return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
424                    }
425                    catch (Exception e) {
426                            throw new SystemException(e);
427                    }
428                    finally {
429                            closeSession(session);
430                    }
431            }
432    
433    }