1   /**
2    * Copyright (c) 2000-2008 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_S_G_U =
60          MBThreadFinder.class.getName() + ".countByS_G_U";
61  
62      public static String FIND_BY_GROUP_ID =
63          MBThreadFinder.class.getName() + ".findByGroupId";
64  
65      public static String FIND_BY_G_U =
66          MBThreadFinder.class.getName() + ".findByG_U";
67  
68      public static String FIND_BY_S_G_U =
69          MBThreadFinder.class.getName() + ".findByS_G_U";
70  
71      public int countByCategoryIds(List<Long> categoryIds)
72          throws SystemException {
73  
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
80  
81              sql = StringUtil.replace(
82                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
83  
84              SQLQuery q = session.createSQLQuery(sql);
85  
86              q.addScalar(COUNT_COLUMN_NAME, Type.INTEGER);
87  
88              QueryPos qPos = QueryPos.getInstance(q);
89  
90              for (int i = 0; i < categoryIds.size(); i++) {
91                  Long categoryId = categoryIds.get(i);
92  
93                  qPos.add(categoryId);
94              }
95  
96              Iterator<Integer> itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Integer count = itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             closeSession(session);
113         }
114     }
115 
116     public int countByGroupId(long groupId) throws SystemException {
117         Session session = null;
118 
119         try {
120             session = openSession();
121 
122             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
123 
124             SQLQuery q = session.createSQLQuery(sql);
125 
126             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
127 
128             QueryPos qPos = QueryPos.getInstance(q);
129 
130             qPos.add(groupId);
131 
132             Iterator<Long> itr = q.list().iterator();
133 
134             if (itr.hasNext()) {
135                 Long count = itr.next();
136 
137                 if (count != null) {
138                     return count.intValue();
139                 }
140             }
141 
142             return 0;
143         }
144         catch (Exception e) {
145             throw new SystemException(e);
146         }
147         finally {
148             closeSession(session);
149         }
150     }
151 
152     public int countByG_U(long groupId, long userId) throws SystemException {
153         Session session = null;
154 
155         try {
156             session = openSession();
157 
158             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
159 
160             SQLQuery q = session.createSQLQuery(sql);
161 
162             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
163 
164             QueryPos qPos = QueryPos.getInstance(q);
165 
166             qPos.add(groupId);
167             qPos.add(userId);
168 
169             Iterator<Long> itr = q.list().iterator();
170 
171             if (itr.hasNext()) {
172                 Long count = itr.next();
173 
174                 if (count != null) {
175                     return count.intValue();
176                 }
177             }
178 
179             return 0;
180         }
181         catch (Exception e) {
182             throw new SystemException(e);
183         }
184         finally {
185             closeSession(session);
186         }
187     }
188 
189     public int countByS_G_U(long groupId, long userId) throws SystemException {
190         Session session = null;
191 
192         try {
193             session = openSession();
194 
195             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
196 
197             SQLQuery q = session.createSQLQuery(sql);
198 
199             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
200 
201             QueryPos qPos = QueryPos.getInstance(q);
202 
203             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
204             qPos.add(groupId);
205             qPos.add(userId);
206 
207             Iterator<Long> itr = q.list().iterator();
208 
209             if (itr.hasNext()) {
210                 Long count = itr.next();
211 
212                 if (count != null) {
213                     return count.intValue();
214                 }
215             }
216 
217             return 0;
218         }
219         catch (Exception e) {
220             throw new SystemException(e);
221         }
222         finally {
223             closeSession(session);
224         }
225     }
226 
227     public List<MBThread> findByGroupId(long groupId, int start, int end)
228         throws SystemException {
229 
230         Session session = null;
231 
232         try {
233             session = openSession();
234 
235             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
236 
237             SQLQuery q = session.createSQLQuery(sql);
238 
239             q.addEntity("MBThread", MBThreadImpl.class);
240 
241             QueryPos qPos = QueryPos.getInstance(q);
242 
243             qPos.add(groupId);
244 
245             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
246         }
247         catch (Exception e) {
248             throw new SystemException(e);
249         }
250         finally {
251             closeSession(session);
252         }
253     }
254 
255     public List<MBThread> findByG_U(
256             long groupId, long userId, int start, int end)
257         throws SystemException {
258 
259         Session session = null;
260 
261         try {
262             session = openSession();
263 
264             String sql = CustomSQLUtil.get(FIND_BY_G_U);
265 
266             SQLQuery q = session.createSQLQuery(sql);
267 
268             q.addEntity("MBThread", MBThreadImpl.class);
269 
270             QueryPos qPos = QueryPos.getInstance(q);
271 
272             qPos.add(groupId);
273             qPos.add(userId);
274 
275             return (List<MBThread>)QueryUtil.list(
276                 q, getDialect(), start, end);
277         }
278         catch (Exception e) {
279             throw new SystemException(e);
280         }
281         finally {
282             closeSession(session);
283         }
284     }
285 
286     public List<MBThread> findByS_G_U(
287             long groupId, long userId, int start, int end)
288         throws SystemException {
289 
290         Session session = null;
291 
292         try {
293             session = openSession();
294 
295             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
296 
297             SQLQuery q = session.createSQLQuery(sql);
298 
299             q.addEntity("MBThread", MBThreadImpl.class);
300 
301             QueryPos qPos = QueryPos.getInstance(q);
302 
303             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
304             qPos.add(groupId);
305             qPos.add(userId);
306 
307             return (List<MBThread>)QueryUtil.list(
308                 q, getDialect(), start, end);
309         }
310         catch (Exception e) {
311             throw new SystemException(e);
312         }
313         finally {
314             closeSession(session);
315         }
316     }
317 
318     protected String getCategoryIds(List<Long> categoryIds) {
319         StringBuilder sb = new StringBuilder();
320 
321         for (int i = 0; i < categoryIds.size(); i++) {
322             sb.append("categoryId = ? ");
323 
324             if ((i + 1) != categoryIds.size()) {
325                 sb.append("OR ");
326             }
327         }
328 
329         return sb.toString();
330     }
331 
332 }