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