1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryPos;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.dao.orm.SQLQuery;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.Type;
23  import com.liferay.portal.kernel.util.CalendarUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
26  import com.liferay.portlet.messageboards.model.MBMessage;
27  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
28  import com.liferay.util.dao.orm.CustomSQLUtil;
29  
30  import java.sql.Timestamp;
31  
32  import java.util.Date;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class MBMessageFinderImpl
42      extends BasePersistenceImpl<MBMessage> implements MBMessageFinder {
43  
44      /**
45       * @deprecated
46       */
47      public static String COUNT_BY_CATEGORY_IDS =
48          MBMessageFinder.class.getName() + ".countByCategoryIds";
49  
50      public static String COUNT_BY_G_U =
51          MBMessageFinder.class.getName() + ".countByG_U";
52  
53      public static String COUNT_BY_C_T =
54          MBMessageFinder.class.getName() + ".countByC_T";
55  
56      public static String COUNT_BY_G_U_A =
57          MBMessageFinder.class.getName() + ".countByG_U_A";
58  
59      public static String FIND_BY_NO_ASSETS =
60          MBMessageFinder.class.getName() + ".findByNoAssets";
61  
62      public static String FIND_BY_G_U =
63          MBMessageFinder.class.getName() + ".findByG_U";
64  
65      public static String FIND_BY_G_U_A =
66          MBMessageFinder.class.getName() + ".findByG_U_A";
67  
68      /**
69       * @deprecated
70       */
71      public int countByCategoryIds(List<Long> categoryIds)
72          throws SystemException {
73  
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
80  
81              sql = StringUtil.replace(
82                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
83  
84              SQLQuery q = session.createSQLQuery(sql);
85  
86              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
87  
88              QueryPos qPos = QueryPos.getInstance(q);
89  
90              for (int i = 0; i < categoryIds.size(); i++) {
91                  Long categoryId = categoryIds.get(i);
92  
93                  qPos.add(categoryId);
94              }
95  
96              Iterator<Long> itr = q.list().iterator();
97  
98              if (itr.hasNext()) {
99                  Long count = itr.next();
100 
101                 if (count != null) {
102                     return count.intValue();
103                 }
104             }
105 
106             return 0;
107         }
108         catch (Exception e) {
109             throw new SystemException(e);
110         }
111         finally {
112             closeSession(session);
113         }
114     }
115 
116     public int countByG_U(long groupId, long userId) throws SystemException {
117         Session session = null;
118 
119         try {
120             session = openSession();
121 
122             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
123 
124             SQLQuery q = session.createSQLQuery(sql);
125 
126             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
127 
128             QueryPos qPos = QueryPos.getInstance(q);
129 
130             qPos.add(groupId);
131             qPos.add(userId);
132 
133             Iterator<Long> itr = q.list().iterator();
134 
135             if (itr.hasNext()) {
136                 Long count = itr.next();
137 
138                 if (count != null) {
139                     return count.intValue();
140                 }
141             }
142 
143             return 0;
144         }
145         catch (Exception e) {
146             throw new SystemException(e);
147         }
148         finally {
149             closeSession(session);
150         }
151     }
152 
153     public int countByC_T(Date createDate, long threadId)
154         throws SystemException {
155 
156         Timestamp createDate_TS = CalendarUtil.getTimestamp(createDate);
157 
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             String sql = CustomSQLUtil.get(COUNT_BY_C_T);
164 
165             SQLQuery q = session.createSQLQuery(sql);
166 
167             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
168 
169             QueryPos qPos = QueryPos.getInstance(q);
170 
171             qPos.add(createDate_TS);
172             qPos.add(threadId);
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             closeSession(session);
191         }
192     }
193 
194     public int countByG_U_A(
195             long groupId, long userId, boolean anonymous)
196         throws SystemException {
197 
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
204 
205             SQLQuery q = session.createSQLQuery(sql);
206 
207             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
208 
209             QueryPos qPos = QueryPos.getInstance(q);
210 
211             qPos.add(groupId);
212             qPos.add(userId);
213             qPos.add(anonymous);
214 
215             Iterator<Long> itr = q.list().iterator();
216 
217             if (itr.hasNext()) {
218                 Long count = itr.next();
219 
220                 if (count != null) {
221                     return count.intValue();
222                 }
223             }
224 
225             return 0;
226         }
227         catch (Exception e) {
228             throw new SystemException(e);
229         }
230         finally {
231             closeSession(session);
232         }
233     }
234 
235     public List<MBMessage> findByNoAssets() throws SystemException {
236         Session session = null;
237 
238         try {
239             session = openSession();
240 
241             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
242 
243             SQLQuery q = session.createSQLQuery(sql);
244 
245             q.addEntity("MBMessage", MBMessageImpl.class);
246 
247             return q.list();
248         }
249         catch (Exception e) {
250             throw new SystemException(e);
251         }
252         finally {
253             closeSession(session);
254         }
255     }
256 
257     public List<Long> findByG_U(
258             long groupId, long userId, int start, int end)
259         throws SystemException {
260 
261         Session session = null;
262 
263         try {
264             session = openSession();
265 
266             String sql = CustomSQLUtil.get(FIND_BY_G_U);
267 
268             SQLQuery q = session.createSQLQuery(sql);
269 
270             q.addScalar("threadId", Type.LONG);
271 
272             QueryPos qPos = QueryPos.getInstance(q);
273 
274             qPos.add(groupId);
275             qPos.add(userId);
276 
277             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
278         }
279         catch (Exception e) {
280             throw new SystemException(e);
281         }
282         finally {
283             closeSession(session);
284         }
285     }
286 
287     public List<Long> findByG_U_A(
288             long groupId, long userId, boolean anonymous, int start, int end)
289         throws SystemException {
290 
291         Session session = null;
292 
293         try {
294             session = openSession();
295 
296             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
297 
298             SQLQuery q = session.createSQLQuery(sql);
299 
300             q.addScalar("threadId", Type.LONG);
301 
302             QueryPos qPos = QueryPos.getInstance(q);
303 
304             qPos.add(groupId);
305             qPos.add(userId);
306             qPos.add(anonymous);
307 
308             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
309         }
310         catch (Exception e) {
311             throw new SystemException(e);
312         }
313         finally {
314             closeSession(session);
315         }
316     }
317 
318     /**
319      * @deprecated
320      */
321     protected String getCategoryIds(List<Long> categoryIds) {
322         StringBuilder sb = new StringBuilder();
323 
324         for (int i = 0; i < categoryIds.size(); i++) {
325             sb.append("categoryId = ? ");
326 
327             if ((i + 1) != categoryIds.size()) {
328                 sb.append("OR ");
329             }
330         }
331 
332         return sb.toString();
333     }
334 
335 }