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