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.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="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class MBThreadFinderImpl implements 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 int countByCategoryIds(List<Long> 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 = categoryIds.get(i);
93  
94                  qPos.add(categoryId);
95              }
96  
97              Iterator<Integer> itr = q.list().iterator();
98  
99              if (itr.hasNext()) {
100                 Integer count = 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 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<Long> itr = q.list().iterator();
134 
135             if (itr.hasNext()) {
136                 Long count = 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 int countByG_U(long groupId, long userId) throws SystemException {
154         Session session = null;
155 
156         try {
157             session = HibernateUtil.openSession();
158 
159             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
160 
161             SQLQuery q = session.createSQLQuery(sql);
162 
163             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
164 
165             QueryPos qPos = QueryPos.getInstance(q);
166 
167             qPos.add(groupId);
168             qPos.add(userId);
169 
170             Iterator<Long> itr = q.list().iterator();
171 
172             if (itr.hasNext()) {
173                 Long count = itr.next();
174 
175                 if (count != null) {
176                     return count.intValue();
177                 }
178             }
179 
180             return 0;
181         }
182         catch (Exception e) {
183             throw new SystemException(e);
184         }
185         finally {
186             HibernateUtil.closeSession(session);
187         }
188     }
189 
190     public int countByS_G_U(long groupId, long userId) throws SystemException {
191         Session session = null;
192 
193         try {
194             session = HibernateUtil.openSession();
195 
196             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
197 
198             SQLQuery q = session.createSQLQuery(sql);
199 
200             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
201 
202             QueryPos qPos = QueryPos.getInstance(q);
203 
204             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
205             qPos.add(groupId);
206             qPos.add(userId);
207 
208             Iterator<Long> itr = q.list().iterator();
209 
210             if (itr.hasNext()) {
211                 Long count = itr.next();
212 
213                 if (count != null) {
214                     return count.intValue();
215                 }
216             }
217 
218             return 0;
219         }
220         catch (Exception e) {
221             throw new SystemException(e);
222         }
223         finally {
224             HibernateUtil.closeSession(session);
225         }
226     }
227 
228     public List<MBThread> findByGroupId(long groupId, int begin, int end)
229         throws SystemException {
230 
231         Session session = null;
232 
233         try {
234             session = HibernateUtil.openSession();
235 
236             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
237 
238             SQLQuery q = session.createSQLQuery(sql);
239 
240             q.addEntity("MBThread", MBThreadImpl.class);
241 
242             QueryPos qPos = QueryPos.getInstance(q);
243 
244             qPos.add(groupId);
245 
246             return (List<MBThread>)QueryUtil.list(
247                 q, HibernateUtil.getDialect(), begin, end);
248         }
249         catch (Exception e) {
250             throw new SystemException(e);
251         }
252         finally {
253             HibernateUtil.closeSession(session);
254         }
255     }
256 
257     public List<MBThread> findByG_U(
258             long groupId, long userId, int begin, int end)
259         throws SystemException {
260 
261         Session session = null;
262 
263         try {
264             session = HibernateUtil.openSession();
265 
266             String sql = CustomSQLUtil.get(FIND_BY_G_U);
267 
268             SQLQuery q = session.createSQLQuery(sql);
269 
270             q.addEntity("MBThread", MBThreadImpl.class);
271 
272             QueryPos qPos = QueryPos.getInstance(q);
273 
274             qPos.add(groupId);
275             qPos.add(userId);
276 
277             return (List<MBThread>)QueryUtil.list(
278                 q, HibernateUtil.getDialect(), begin, end);
279         }
280         catch (Exception e) {
281             throw new SystemException(e);
282         }
283         finally {
284             HibernateUtil.closeSession(session);
285         }
286     }
287 
288     public List<MBThread> findByS_G_U(
289             long groupId, long userId, int begin, int end)
290         throws SystemException {
291 
292         Session session = null;
293 
294         try {
295             session = HibernateUtil.openSession();
296 
297             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
298 
299             SQLQuery q = session.createSQLQuery(sql);
300 
301             q.addEntity("MBThread", MBThreadImpl.class);
302 
303             QueryPos qPos = QueryPos.getInstance(q);
304 
305             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
306             qPos.add(groupId);
307             qPos.add(userId);
308 
309             return (List<MBThread>)QueryUtil.list(
310                 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     protected String getCategoryIds(List<Long> 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 }