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 categoryIds) throws SystemException {
77          Session session = null;
78  
79          try {
80              session = HibernateUtil.openSession();
81  
82              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
83  
84              sql = StringUtil.replace(
85                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
86  
87              SQLQuery q = session.createSQLQuery(sql);
88  
89              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
90  
91              QueryPos qPos = QueryPos.getInstance(q);
92  
93              for (int i = 0; i < categoryIds.size(); i++) {
94                  Long categoryId = (Long)categoryIds.get(i);
95  
96                  qPos.add(categoryId);
97              }
98  
99              Iterator itr = q.list().iterator();
100 
101             if (itr.hasNext()) {
102                 Long count = (Long)itr.next();
103 
104                 if (count != null) {
105                     return count.intValue();
106                 }
107             }
108 
109             return 0;
110         }
111         catch (Exception e) {
112             throw new SystemException(e);
113         }
114         finally {
115             HibernateUtil.closeSession(session);
116         }
117     }
118 
119     public int countByGroupId(long groupId) throws SystemException {
120         Session session = null;
121 
122         try {
123             session = HibernateUtil.openSession();
124 
125             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
126 
127             SQLQuery q = session.createSQLQuery(sql);
128 
129             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
130 
131             QueryPos qPos = QueryPos.getInstance(q);
132 
133             qPos.add(groupId);
134 
135             Iterator itr = q.list().iterator();
136 
137             if (itr.hasNext()) {
138                 Long count = (Long)itr.next();
139 
140                 if (count != null) {
141                     return count.intValue();
142                 }
143             }
144 
145             return 0;
146         }
147         catch (Exception e) {
148             throw new SystemException(e);
149         }
150         finally {
151             HibernateUtil.closeSession(session);
152         }
153     }
154 
155     public int countByG_U(long groupId, long userId) throws SystemException {
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 List findByGroupId(long groupId, int begin, int end)
193         throws SystemException {
194 
195         return findByGroupId(groupId, begin, end, null);
196     }
197 
198     public List findByGroupId(
199             long groupId, int begin, int end, OrderByComparator obc)
200         throws SystemException {
201 
202         Session session = null;
203 
204         try {
205             session = HibernateUtil.openSession();
206 
207             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
208 
209             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
210 
211             SQLQuery q = session.createSQLQuery(sql);
212 
213             q.addEntity("MBMessage", MBMessageImpl.class);
214 
215             QueryPos qPos = QueryPos.getInstance(q);
216 
217             qPos.add(groupId);
218 
219             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
220         }
221         catch (Exception e) {
222             throw new SystemException(e);
223         }
224         finally {
225             HibernateUtil.closeSession(session);
226         }
227     }
228 
229     public List findByNoAssets() throws SystemException {
230         Session session = null;
231 
232         try {
233             session = HibernateUtil.openSession();
234 
235             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
236 
237             SQLQuery q = session.createSQLQuery(sql);
238 
239             q.addEntity("MBMessage", MBMessageImpl.class);
240 
241             return q.list();
242         }
243         catch (Exception e) {
244             throw new SystemException(e);
245         }
246         finally {
247             HibernateUtil.closeSession(session);
248         }
249     }
250 
251     public MBMessage findByUuid_G(String uuid, long groupId)
252         throws NoSuchMessageException, SystemException {
253 
254         Session session = null;
255 
256         try {
257             session = HibernateUtil.openSession();
258 
259             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
260 
261             SQLQuery q = session.createSQLQuery(sql);
262 
263             q.addEntity("MBMessage", MBMessageImpl.class);
264 
265             QueryPos qPos = QueryPos.getInstance(q);
266 
267             qPos.add(uuid);
268             qPos.add(groupId);
269 
270             List list = q.list();
271 
272             if (list.size() == 0) {
273                 StringMaker sm = new StringMaker();
274 
275                 sm.append("No MBMessage exists with the key {uuid=");
276                 sm.append(uuid);
277                 sm.append(", groupId=");
278                 sm.append(groupId);
279                 sm.append("}");
280 
281                 throw new NoSuchMessageException(sm.toString());
282             }
283             else {
284                 return (MBMessage)list.get(0);
285             }
286         }
287         catch (NoSuchMessageException nsme) {
288             throw nsme;
289         }
290         catch (Exception e) {
291             throw new SystemException(e);
292         }
293         finally {
294             HibernateUtil.closeSession(session);
295         }
296     }
297 
298     public List findByG_U(long groupId, long userId, int begin, int end)
299         throws SystemException {
300 
301         return findByG_U(groupId, userId, begin, end, null);
302     }
303 
304     public List findByG_U(
305             long groupId, long userId, int begin, int end,
306             OrderByComparator obc)
307         throws SystemException {
308 
309         Session session = null;
310 
311         try {
312             session = HibernateUtil.openSession();
313 
314             String sql = CustomSQLUtil.get(FIND_BY_G_U);
315 
316             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
317 
318             SQLQuery q = session.createSQLQuery(sql);
319 
320             q.addEntity("MBMessage", MBMessageImpl.class);
321 
322             QueryPos qPos = QueryPos.getInstance(q);
323 
324             qPos.add(groupId);
325             qPos.add(userId);
326 
327             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
328         }
329         catch (Exception e) {
330             throw new SystemException(e);
331         }
332         finally {
333             HibernateUtil.closeSession(session);
334         }
335     }
336 
337     public List findByC_C(long classNameId, long classPK)
338         throws SystemException {
339 
340         Session session = null;
341 
342         try {
343             session = HibernateUtil.openSession();
344 
345             String sql = CustomSQLUtil.get(FIND_BY_C_C);
346 
347             SQLQuery q = session.createSQLQuery(sql);
348 
349             q.addEntity("MBMessage", MBMessageImpl.class);
350 
351             QueryPos qPos = QueryPos.getInstance(q);
352 
353             qPos.add(classNameId);
354             qPos.add(classPK);
355 
356             return q.list();
357         }
358         catch (Exception e) {
359             throw new SystemException(e);
360         }
361         finally {
362             HibernateUtil.closeSession(session);
363         }
364     }
365 
366     protected String getCategoryIds(List categoryIds) {
367         StringMaker sm = new StringMaker();
368 
369         for (int i = 0; i < categoryIds.size(); i++) {
370             sm.append("categoryId = ? ");
371 
372             if ((i + 1) != categoryIds.size()) {
373                 sm.append("OR ");
374             }
375         }
376 
377         return sm.toString();
378     }
379 
380 }