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.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.model.impl.MBMessageImpl;
32  import com.liferay.util.dao.hibernate.QueryPos;
33  import com.liferay.util.dao.hibernate.QueryUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import org.hibernate.Hibernate;
39  import org.hibernate.SQLQuery;
40  import org.hibernate.Session;
41  
42  /**
43   * <a href="MBMessageFinder.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class MBMessageFinder {
49  
50      public static String COUNT_BY_CATEGORY_IDS =
51          MBMessageFinder.class.getName() + ".countByCategoryIds";
52  
53      public static String COUNT_BY_GROUP_ID =
54          MBMessageFinder.class.getName() + ".countByGroupId";
55  
56      public static String FIND_BY_GROUP_ID =
57          MBMessageFinder.class.getName() + ".findByGroupId";
58  
59      public static int countByCategoryIds(List categoryIds)
60          throws SystemException {
61  
62          Session session = null;
63  
64          try {
65              session = HibernateUtil.openSession();
66  
67              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
68  
69              sql = StringUtil.replace(
70                  sql, "[$CATEGORY_ID$]", _getCategoryIds(categoryIds));
71  
72              SQLQuery q = session.createSQLQuery(sql);
73  
74              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
75  
76              QueryPos qPos = QueryPos.getInstance(q);
77  
78              for (int i = 0; i < categoryIds.size(); i++) {
79                  Long categoryId = (Long)categoryIds.get(i);
80  
81                  qPos.add(categoryId);
82              }
83  
84              Iterator itr = q.list().iterator();
85  
86              if (itr.hasNext()) {
87                  Long count = (Long)itr.next();
88  
89                  if (count != null) {
90                      return count.intValue();
91                  }
92              }
93  
94              return 0;
95          }
96          catch (Exception e) {
97              throw new SystemException(e);
98          }
99          finally {
100             HibernateUtil.closeSession(session);
101         }
102     }
103 
104     public static int countByGroupId(long groupId) throws SystemException {
105         Session session = null;
106 
107         try {
108             session = HibernateUtil.openSession();
109 
110             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
111 
112             SQLQuery q = session.createSQLQuery(sql);
113 
114             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
115 
116             QueryPos qPos = QueryPos.getInstance(q);
117 
118             qPos.add(groupId);
119 
120             Iterator itr = q.list().iterator();
121 
122             if (itr.hasNext()) {
123                 Long count = (Long)itr.next();
124 
125                 if (count != null) {
126                     return count.intValue();
127                 }
128             }
129 
130             return 0;
131         }
132         catch (Exception e) {
133             throw new SystemException(e);
134         }
135         finally {
136             HibernateUtil.closeSession(session);
137         }
138     }
139 
140     public static List findByGroupId(long groupId, int begin, int end)
141         throws SystemException {
142 
143         return findByGroupId(groupId, begin, end, null);
144     }
145 
146     public static List findByGroupId(
147             long groupId, int begin, int end, OrderByComparator obc)
148         throws SystemException {
149 
150         Session session = null;
151 
152         try {
153             session = HibernateUtil.openSession();
154 
155             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
156 
157             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
158 
159             SQLQuery q = session.createSQLQuery(sql);
160 
161             q.addEntity("MBMessage", MBMessageImpl.class);
162 
163             QueryPos qPos = QueryPos.getInstance(q);
164 
165             qPos.add(groupId);
166 
167             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
168         }
169         catch (Exception e) {
170             throw new SystemException(e);
171         }
172         finally {
173             HibernateUtil.closeSession(session);
174         }
175     }
176 
177     private static String _getCategoryIds(List categoryIds) {
178         StringMaker sm = new StringMaker();
179 
180         for (int i = 0; i < categoryIds.size(); i++) {
181             sm.append("categoryId = ? ");
182 
183             if ((i + 1) != categoryIds.size()) {
184                 sm.append("OR ");
185             }
186         }
187 
188         return sm.toString();
189     }
190 
191 }