1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.workflow.WorkflowConstants;
26  import com.liferay.portal.security.permission.InlineSQLHelperUtil;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.messageboards.model.MBMessage;
30  import com.liferay.portlet.messageboards.model.MBThread;
31  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class MBThreadFinderImpl
43      extends BasePersistenceImpl<MBThread> implements MBThreadFinder {
44  
45      public static String COUNT_BY_G_C =
46          MBThreadFinder.class.getName() + ".countByG_C";
47  
48      public static String COUNT_BY_G_C_S =
49          MBThreadFinder.class.getName() + ".countByG_C_S";
50  
51      public static String COUNT_BY_S_G_U_S =
52          MBThreadFinder.class.getName() + ".countByS_G_U_S";
53  
54      public static String FIND_BY_G_C =
55          MBThreadFinder.class.getName() + ".findByG_C";
56  
57      public static String FIND_BY_G_C_S =
58          MBThreadFinder.class.getName() + ".findByG_C_S";
59  
60      public static String FIND_BY_S_G_U_S =
61          MBThreadFinder.class.getName() + ".findByS_G_U_S";
62  
63      public int countByG_C_S(long groupId, long categoryId, int status)
64          throws SystemException {
65  
66          return doCountByG_C_S(groupId, categoryId, status, false);
67      }
68  
69      public int countByS_G_U_S(long groupId, long userId, int status)
70          throws SystemException {
71  
72          return doCountByS_G_U_S(groupId, userId, status, false);
73      }
74  
75      public int filterCountByG_C(long groupId, long categoryId)
76          throws SystemException {
77  
78          if (!InlineSQLHelperUtil.isEnabled(groupId)) {
79              return MBThreadUtil.countByG_C(groupId, categoryId);
80          }
81  
82          Session session = null;
83  
84          try {
85              session = openSession();
86  
87              String sql = CustomSQLUtil.get(COUNT_BY_G_C);
88  
89              sql = InlineSQLHelperUtil.replacePermissionCheck(
90                  sql, MBMessage.class.getName(), "MBMessage.messageId",
91                  "MBMessage.userId", groupId);
92  
93              SQLQuery q = session.createSQLQuery(sql);
94  
95              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
96  
97              QueryPos qPos = QueryPos.getInstance(q);
98  
99              qPos.add(groupId);
100             qPos.add(categoryId);
101 
102             Iterator<Long> itr = q.list().iterator();
103 
104             if (itr.hasNext()) {
105                 Long count = itr.next();
106 
107                 if (count != null) {
108                     return count.intValue();
109                 }
110             }
111 
112             return 0;
113         }
114         catch (Exception e) {
115             throw processException(e);
116         }
117         finally {
118             closeSession(session);
119         }
120     }
121 
122     public int filterCountByG_C_S(long groupId, long categoryId, int status)
123         throws SystemException {
124 
125         return doCountByG_C_S(groupId, categoryId, status, true);
126     }
127 
128     public int filterCountByS_G_U_S(long groupId, long userId, int status)
129         throws SystemException {
130 
131         return doCountByS_G_U_S(groupId, userId, 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_S(
180             long groupId, long userId, int status, int start, int end)
181         throws SystemException {
182 
183         return doFindByS_G_U_S(groupId, userId, status, start, end, true);
184     }
185 
186     public List<MBThread> findByG_C_S(
187             long groupId, long categoryId, int status, int start, int end)
188         throws SystemException {
189 
190         return doFindByG_C_S(groupId, categoryId, status, start, end, false);
191     }
192 
193     public List<MBThread> findByS_G_U_S(
194             long groupId, long userId, int status, int start, int end)
195         throws SystemException {
196 
197         return doFindByS_G_U_S(groupId, userId, status, start, end, false);
198     }
199 
200     protected int doCountByG_C_S(
201             long groupId, long categoryId, int status, boolean inlineSQLHelper)
202         throws SystemException {
203 
204         Session session = null;
205 
206         try {
207             session = openSession();
208 
209             String sql = CustomSQLUtil.get(COUNT_BY_G_C_S);
210 
211             if (status != WorkflowConstants.STATUS_ANY) {
212                 sql = StringUtil.replace(
213                     sql, "[$STATUS$]", "AND (MBThread.status = ?)");
214             }
215             else {
216                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
217             }
218 
219             if (inlineSQLHelper) {
220                 sql = InlineSQLHelperUtil.replacePermissionCheck(
221                     sql, MBMessage.class.getName(), "MBMessage.messageId",
222                     "MBMessage.userId", groupId);
223             }
224 
225             SQLQuery q = session.createSQLQuery(sql);
226 
227             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
228 
229             QueryPos qPos = QueryPos.getInstance(q);
230 
231             qPos.add(groupId);
232             qPos.add(categoryId);
233 
234             if (status != WorkflowConstants.STATUS_ANY) {
235                 qPos.add(status);
236             }
237 
238             Iterator<Long> itr = q.list().iterator();
239 
240             if (itr.hasNext()) {
241                 Long count = itr.next();
242 
243                 if (count != null) {
244                     return count.intValue();
245                 }
246             }
247 
248             return 0;
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             closeSession(session);
255         }
256     }
257 
258     protected int doCountByS_G_U_S(
259             long groupId, long userId, int status, boolean inlineSQLHelper)
260         throws SystemException {
261 
262         Session session = null;
263 
264         try {
265             session = openSession();
266 
267             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_S);
268 
269             if (status != WorkflowConstants.STATUS_ANY) {
270                 sql = StringUtil.replace(
271                     sql, "[$STATUS$]", "AND (MBThread.status = ?)");
272             }
273             else {
274                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
275             }
276 
277             if (inlineSQLHelper) {
278                 sql = InlineSQLHelperUtil.replacePermissionCheck(
279                     sql, MBMessage.class.getName(), "MBMessage.messageId",
280                     "MBMessage.userId", groupId);
281             }
282 
283             SQLQuery q = session.createSQLQuery(sql);
284 
285             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
286 
287             QueryPos qPos = QueryPos.getInstance(q);
288 
289             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
290             qPos.add(groupId);
291 
292             if (status != WorkflowConstants.STATUS_ANY) {
293                 qPos.add(status);
294             }
295 
296             qPos.add(userId);
297 
298             Iterator<Long> itr = q.list().iterator();
299 
300             if (itr.hasNext()) {
301                 Long count = itr.next();
302 
303                 if (count != null) {
304                     return count.intValue();
305                 }
306             }
307 
308             return 0;
309         }
310         catch (Exception e) {
311             throw new SystemException(e);
312         }
313         finally {
314             closeSession(session);
315         }
316     }
317 
318     protected List<MBThread> doFindByG_C_S(
319             long groupId, long categoryId, int status, int start, int end,
320             boolean inlineSQLHelper)
321         throws SystemException {
322 
323         Session session = null;
324 
325         try {
326             session = openSession();
327 
328             String sql = CustomSQLUtil.get(FIND_BY_G_C_S);
329 
330             if (status != WorkflowConstants.STATUS_ANY) {
331                 sql = StringUtil.replace(
332                     sql, "[$STATUS$]", "AND (MBThread.status = ?)");
333             }
334             else {
335                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
336             }
337 
338             if (inlineSQLHelper) {
339                 sql = InlineSQLHelperUtil.replacePermissionCheck(
340                     sql, MBMessage.class.getName(), "MBMessage.messageId",
341                     "MBMessage.userId", groupId);
342             }
343 
344             SQLQuery q = session.createSQLQuery(sql);
345 
346             q.addEntity("MBThread", MBThreadImpl.class);
347 
348             QueryPos qPos = QueryPos.getInstance(q);
349 
350             qPos.add(groupId);
351             qPos.add(categoryId);
352 
353             if (status != WorkflowConstants.STATUS_ANY) {
354                 qPos.add(status);
355             }
356 
357             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
358         }
359         catch (Exception e) {
360             throw new SystemException(e);
361         }
362         finally {
363             closeSession(session);
364         }
365     }
366 
367     protected List<MBThread> doFindByS_G_U_S(
368             long groupId, long userId, int status, int start, int end,
369             boolean inlineSQLHelper)
370         throws SystemException {
371 
372         Session session = null;
373 
374         try {
375             session = openSession();
376 
377             String sql = CustomSQLUtil.get(FIND_BY_S_G_U_S);
378 
379             if (status != WorkflowConstants.STATUS_ANY) {
380                 sql = StringUtil.replace(
381                     sql, "[$STATUS$]", "AND (MBThread.status = ?)");
382             }
383             else {
384                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
385             }
386 
387             if (inlineSQLHelper) {
388                 sql = InlineSQLHelperUtil.replacePermissionCheck(
389                     sql, MBMessage.class.getName(), "MBMessage.messageId",
390                     "MBMessage.userId", groupId);
391             }
392 
393             SQLQuery q = session.createSQLQuery(sql);
394 
395             q.addEntity("MBThread", MBThreadImpl.class);
396 
397             QueryPos qPos = QueryPos.getInstance(q);
398 
399             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
400             qPos.add(groupId);
401 
402             if (status != WorkflowConstants.STATUS_ANY) {
403                 qPos.add(status);
404             }
405 
406             qPos.add(userId);
407 
408             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
409         }
410         catch (Exception e) {
411             throw new SystemException(e);
412         }
413         finally {
414             closeSession(session);
415         }
416     }
417 
418 }