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