1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.messageboards.model.MBThread;
35  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
36  import com.liferay.util.dao.orm.CustomSQLUtil;
37  
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class MBThreadFinderImpl
48      extends BasePersistenceImpl implements MBThreadFinder {
49  
50      public static String COUNT_BY_CATEGORY_IDS =
51          MBThreadFinder.class.getName() + ".countByCategoryIds";
52  
53      public static String COUNT_BY_GROUP_ID =
54          MBThreadFinder.class.getName() + ".countByGroupId";
55  
56      public static String COUNT_BY_G_U =
57          MBThreadFinder.class.getName() + ".countByG_U";
58  
59      public static String COUNT_BY_G_U_A =
60          MBThreadFinder.class.getName() + ".countByG_U_A";
61  
62      public static String COUNT_BY_S_G_U =
63          MBThreadFinder.class.getName() + ".countByS_G_U";
64  
65      public static String FIND_BY_GROUP_ID =
66          MBThreadFinder.class.getName() + ".findByGroupId";
67  
68      public static String FIND_BY_G_U =
69          MBThreadFinder.class.getName() + ".findByG_U";
70  
71      public static String FIND_BY_G_U_A =
72          MBThreadFinder.class.getName() + ".findByG_U_A";
73  
74      public static String FIND_BY_S_G_U =
75          MBThreadFinder.class.getName() + ".findByS_G_U";
76  
77      public int countByCategoryIds(List<Long> categoryIds)
78          throws SystemException {
79  
80          Session session = null;
81  
82          try {
83              session = openSession();
84  
85              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
86  
87              sql = StringUtil.replace(
88                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
89  
90              SQLQuery q = session.createSQLQuery(sql);
91  
92              q.addScalar(COUNT_COLUMN_NAME, Type.INTEGER);
93  
94              QueryPos qPos = QueryPos.getInstance(q);
95  
96              for (int i = 0; i < categoryIds.size(); i++) {
97                  Long categoryId = categoryIds.get(i);
98  
99                  qPos.add(categoryId);
100             }
101 
102             Iterator<Integer> itr = q.list().iterator();
103 
104             if (itr.hasNext()) {
105                 Integer 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 new SystemException(e);
116         }
117         finally {
118             closeSession(session);
119         }
120     }
121 
122     public int countByGroupId(long groupId) throws SystemException {
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
129 
130             SQLQuery q = session.createSQLQuery(sql);
131 
132             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
133 
134             QueryPos qPos = QueryPos.getInstance(q);
135 
136             qPos.add(groupId);
137 
138             Iterator<Long> itr = q.list().iterator();
139 
140             if (itr.hasNext()) {
141                 Long count = itr.next();
142 
143                 if (count != null) {
144                     return count.intValue();
145                 }
146             }
147 
148             return 0;
149         }
150         catch (Exception e) {
151             throw new SystemException(e);
152         }
153         finally {
154             closeSession(session);
155         }
156     }
157 
158     public int countByG_U(long groupId, long userId) throws SystemException {
159         Session session = null;
160 
161         try {
162             session = openSession();
163 
164             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
165 
166             SQLQuery q = session.createSQLQuery(sql);
167 
168             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
169 
170             QueryPos qPos = QueryPos.getInstance(q);
171 
172             qPos.add(groupId);
173             qPos.add(userId);
174 
175             Iterator<Long> itr = q.list().iterator();
176 
177             if (itr.hasNext()) {
178                 Long count = itr.next();
179 
180                 if (count != null) {
181                     return count.intValue();
182                 }
183             }
184 
185             return 0;
186         }
187         catch (Exception e) {
188             throw new SystemException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193     }
194 
195     public int countByG_U_A(
196             long groupId, long userId, boolean anonymous)
197         throws SystemException {
198 
199         Session session = null;
200 
201         try {
202             session = openSession();
203 
204             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
205 
206             SQLQuery q = session.createSQLQuery(sql);
207 
208             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
209 
210             QueryPos qPos = QueryPos.getInstance(q);
211 
212             qPos.add(groupId);
213             qPos.add(userId);
214             qPos.add(anonymous);
215 
216             Iterator<Long> itr = q.list().iterator();
217 
218             if (itr.hasNext()) {
219                 Long count = itr.next();
220 
221                 if (count != null) {
222                     return count.intValue();
223                 }
224             }
225 
226             return 0;
227         }
228         catch (Exception e) {
229             throw new SystemException(e);
230         }
231         finally {
232             closeSession(session);
233         }
234     }
235 
236     public int countByS_G_U(long groupId, long userId) throws SystemException {
237         Session session = null;
238 
239         try {
240             session = openSession();
241 
242             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
243 
244             SQLQuery q = session.createSQLQuery(sql);
245 
246             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
247 
248             QueryPos qPos = QueryPos.getInstance(q);
249 
250             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
251             qPos.add(groupId);
252             qPos.add(userId);
253 
254             Iterator<Long> itr = q.list().iterator();
255 
256             if (itr.hasNext()) {
257                 Long count = itr.next();
258 
259                 if (count != null) {
260                     return count.intValue();
261                 }
262             }
263 
264             return 0;
265         }
266         catch (Exception e) {
267             throw new SystemException(e);
268         }
269         finally {
270             closeSession(session);
271         }
272     }
273 
274     public List<MBThread> findByGroupId(long groupId, int start, int end)
275         throws SystemException {
276 
277         Session session = null;
278 
279         try {
280             session = openSession();
281 
282             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
283 
284             SQLQuery q = session.createSQLQuery(sql);
285 
286             q.addEntity("MBThread", MBThreadImpl.class);
287 
288             QueryPos qPos = QueryPos.getInstance(q);
289 
290             qPos.add(groupId);
291 
292             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
293         }
294         catch (Exception e) {
295             throw new SystemException(e);
296         }
297         finally {
298             closeSession(session);
299         }
300     }
301 
302     public List<MBThread> findByG_U(
303             long groupId, long userId, int start, int end)
304         throws SystemException {
305 
306         Session session = null;
307 
308         try {
309             session = openSession();
310 
311             String sql = CustomSQLUtil.get(FIND_BY_G_U);
312 
313             SQLQuery q = session.createSQLQuery(sql);
314 
315             q.addEntity("MBThread", MBThreadImpl.class);
316 
317             QueryPos qPos = QueryPos.getInstance(q);
318 
319             qPos.add(groupId);
320             qPos.add(userId);
321 
322             return (List<MBThread>)QueryUtil.list(
323                 q, getDialect(), start, end);
324         }
325         catch (Exception e) {
326             throw new SystemException(e);
327         }
328         finally {
329             closeSession(session);
330         }
331     }
332 
333     public List<MBThread> findByG_U_A(
334             long groupId, long userId, boolean anonymous, int start, int end)
335         throws SystemException {
336 
337         Session session = null;
338 
339         try {
340             session = openSession();
341 
342             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
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(userId);
352             qPos.add(anonymous);
353 
354             return (List<MBThread>)QueryUtil.list(
355                 q, getDialect(), start, end);
356         }
357         catch (Exception e) {
358             throw new SystemException(e);
359         }
360         finally {
361             closeSession(session);
362         }
363     }
364 
365     public List<MBThread> findByS_G_U(
366             long groupId, long userId, int start, int end)
367         throws SystemException {
368 
369         Session session = null;
370 
371         try {
372             session = openSession();
373 
374             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
375 
376             SQLQuery q = session.createSQLQuery(sql);
377 
378             q.addEntity("MBThread", MBThreadImpl.class);
379 
380             QueryPos qPos = QueryPos.getInstance(q);
381 
382             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
383             qPos.add(groupId);
384             qPos.add(userId);
385 
386             return (List<MBThread>)QueryUtil.list(
387                 q, getDialect(), start, end);
388         }
389         catch (Exception e) {
390             throw new SystemException(e);
391         }
392         finally {
393             closeSession(session);
394         }
395     }
396 
397     protected String getCategoryIds(List<Long> categoryIds) {
398         StringBuilder sb = new StringBuilder();
399 
400         for (int i = 0; i < categoryIds.size(); i++) {
401             sb.append("categoryId = ? ");
402 
403             if ((i + 1) != categoryIds.size()) {
404                 sb.append("OR ");
405             }
406         }
407 
408         return sb.toString();
409     }
410 
411 }