1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
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.orm.CustomSQLUtil;
34  
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class MBThreadFinderImpl
45      extends BasePersistenceImpl implements MBThreadFinder {
46  
47      public static String COUNT_BY_CATEGORY_IDS =
48          MBThreadFinder.class.getName() + ".countByCategoryIds";
49  
50      public static String COUNT_BY_GROUP_ID =
51          MBThreadFinder.class.getName() + ".countByGroupId";
52  
53      public static String COUNT_BY_G_U =
54          MBThreadFinder.class.getName() + ".countByG_U";
55  
56      public static String COUNT_BY_S_G_U =
57          MBThreadFinder.class.getName() + ".countByS_G_U";
58  
59      public static String FIND_BY_GROUP_ID =
60          MBThreadFinder.class.getName() + ".findByGroupId";
61  
62      public static String FIND_BY_G_U =
63          MBThreadFinder.class.getName() + ".findByG_U";
64  
65      public static String FIND_BY_S_G_U =
66          MBThreadFinder.class.getName() + ".findByS_G_U";
67  
68      public int countByCategoryIds(List<Long> categoryIds)
69          throws SystemException {
70  
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
77  
78              sql = StringUtil.replace(
79                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
80  
81              SQLQuery q = session.createSQLQuery(sql);
82  
83              q.addScalar(COUNT_COLUMN_NAME, Type.INTEGER);
84  
85              QueryPos qPos = QueryPos.getInstance(q);
86  
87              for (int i = 0; i < categoryIds.size(); i++) {
88                  Long categoryId = categoryIds.get(i);
89  
90                  qPos.add(categoryId);
91              }
92  
93              Iterator<Integer> itr = q.list().iterator();
94  
95              if (itr.hasNext()) {
96                  Integer count = itr.next();
97  
98                  if (count != null) {
99                      return count.intValue();
100                 }
101             }
102 
103             return 0;
104         }
105         catch (Exception e) {
106             throw new SystemException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     public int countByGroupId(long groupId) throws SystemException {
114         Session session = null;
115 
116         try {
117             session = openSession();
118 
119             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
124 
125             QueryPos qPos = QueryPos.getInstance(q);
126 
127             qPos.add(groupId);
128 
129             Iterator<Long> itr = q.list().iterator();
130 
131             if (itr.hasNext()) {
132                 Long count = itr.next();
133 
134                 if (count != null) {
135                     return count.intValue();
136                 }
137             }
138 
139             return 0;
140         }
141         catch (Exception e) {
142             throw new SystemException(e);
143         }
144         finally {
145             closeSession(session);
146         }
147     }
148 
149     public int countByG_U(long groupId, long userId) throws SystemException {
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
156 
157             SQLQuery q = session.createSQLQuery(sql);
158 
159             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
160 
161             QueryPos qPos = QueryPos.getInstance(q);
162 
163             qPos.add(groupId);
164             qPos.add(userId);
165 
166             Iterator<Long> itr = q.list().iterator();
167 
168             if (itr.hasNext()) {
169                 Long count = itr.next();
170 
171                 if (count != null) {
172                     return count.intValue();
173                 }
174             }
175 
176             return 0;
177         }
178         catch (Exception e) {
179             throw new SystemException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public int countByS_G_U(long groupId, long userId) throws SystemException {
187         Session session = null;
188 
189         try {
190             session = openSession();
191 
192             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
193 
194             SQLQuery q = session.createSQLQuery(sql);
195 
196             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
197 
198             QueryPos qPos = QueryPos.getInstance(q);
199 
200             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
201             qPos.add(groupId);
202             qPos.add(userId);
203 
204             Iterator<Long> itr = q.list().iterator();
205 
206             if (itr.hasNext()) {
207                 Long count = itr.next();
208 
209                 if (count != null) {
210                     return count.intValue();
211                 }
212             }
213 
214             return 0;
215         }
216         catch (Exception e) {
217             throw new SystemException(e);
218         }
219         finally {
220             closeSession(session);
221         }
222     }
223 
224     public List<MBThread> findByGroupId(long groupId, int start, int end)
225         throws SystemException {
226 
227         Session session = null;
228 
229         try {
230             session = openSession();
231 
232             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
233 
234             SQLQuery q = session.createSQLQuery(sql);
235 
236             q.addEntity("MBThread", MBThreadImpl.class);
237 
238             QueryPos qPos = QueryPos.getInstance(q);
239 
240             qPos.add(groupId);
241 
242             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
243         }
244         catch (Exception e) {
245             throw new SystemException(e);
246         }
247         finally {
248             closeSession(session);
249         }
250     }
251 
252     public List<MBThread> findByG_U(
253             long groupId, long userId, int start, int end)
254         throws SystemException {
255 
256         Session session = null;
257 
258         try {
259             session = openSession();
260 
261             String sql = CustomSQLUtil.get(FIND_BY_G_U);
262 
263             SQLQuery q = session.createSQLQuery(sql);
264 
265             q.addEntity("MBThread", MBThreadImpl.class);
266 
267             QueryPos qPos = QueryPos.getInstance(q);
268 
269             qPos.add(groupId);
270             qPos.add(userId);
271 
272             return (List<MBThread>)QueryUtil.list(
273                 q, getDialect(), start, end);
274         }
275         catch (Exception e) {
276             throw new SystemException(e);
277         }
278         finally {
279             closeSession(session);
280         }
281     }
282 
283     public List<MBThread> findByS_G_U(
284             long groupId, long userId, int start, int end)
285         throws SystemException {
286 
287         Session session = null;
288 
289         try {
290             session = openSession();
291 
292             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
293 
294             SQLQuery q = session.createSQLQuery(sql);
295 
296             q.addEntity("MBThread", MBThreadImpl.class);
297 
298             QueryPos qPos = QueryPos.getInstance(q);
299 
300             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
301             qPos.add(groupId);
302             qPos.add(userId);
303 
304             return (List<MBThread>)QueryUtil.list(
305                 q, getDialect(), start, end);
306         }
307         catch (Exception e) {
308             throw new SystemException(e);
309         }
310         finally {
311             closeSession(session);
312         }
313     }
314 
315     protected String getCategoryIds(List<Long> categoryIds) {
316         StringBuilder sb = new StringBuilder();
317 
318         for (int i = 0; i < categoryIds.size(); i++) {
319             sb.append("categoryId = ? ");
320 
321             if ((i + 1) != categoryIds.size()) {
322                 sb.append("OR ");
323             }
324         }
325 
326         return sb.toString();
327     }
328 
329 }