1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.CalendarUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.workflow.StatusConstants;
27  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
28  import com.liferay.portlet.messageboards.model.MBMessage;
29  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.sql.Timestamp;
33  
34  import java.util.Date;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class MBMessageFinderImpl
44      extends BasePersistenceImpl<MBMessage> implements MBMessageFinder {
45  
46      public static String COUNT_BY_C_T =
47          MBMessageFinder.class.getName() + ".countByC_T";
48  
49      public static String COUNT_BY_G_U_S =
50          MBMessageFinder.class.getName() + ".countByG_U_S";
51  
52      public static String COUNT_BY_G_U_A_S =
53          MBMessageFinder.class.getName() + ".countByG_U_A_S";
54  
55      public static String FIND_BY_NO_ASSETS =
56          MBMessageFinder.class.getName() + ".findByNoAssets";
57  
58      public static String FIND_BY_G_U_S =
59          MBMessageFinder.class.getName() + ".findByG_U_S";
60  
61      public static String FIND_BY_G_U_A_S =
62          MBMessageFinder.class.getName() + ".findByG_U_A_S";
63  
64      public int countByC_T(Date createDate, long threadId)
65          throws SystemException {
66  
67          Timestamp createDate_TS = CalendarUtil.getTimestamp(createDate);
68  
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              String sql = CustomSQLUtil.get(COUNT_BY_C_T);
75  
76              SQLQuery q = session.createSQLQuery(sql);
77  
78              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
79  
80              QueryPos qPos = QueryPos.getInstance(q);
81  
82              qPos.add(createDate_TS);
83              qPos.add(threadId);
84  
85              Iterator<Long> itr = q.list().iterator();
86  
87              if (itr.hasNext()) {
88                  Long count = itr.next();
89  
90                  if (count != null) {
91                      return count.intValue();
92                  }
93              }
94  
95              return 0;
96          }
97          catch (Exception e) {
98              throw new SystemException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public int countByG_U_S(long groupId, long userId, int status)
106         throws SystemException {
107 
108         Session session = null;
109 
110         try {
111             session = openSession();
112 
113             String sql = CustomSQLUtil.get(COUNT_BY_G_U_S);
114 
115             if (status != StatusConstants.ANY) {
116                 sql = StringUtil.replace(sql, "[$STATUS$]", "AND (status = ?)");
117             }
118             else {
119                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
120             }
121 
122             SQLQuery q = session.createSQLQuery(sql);
123 
124             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
125 
126             QueryPos qPos = QueryPos.getInstance(q);
127 
128             qPos.add(groupId);
129             qPos.add(userId);
130 
131             if (status != StatusConstants.ANY) {
132                 qPos.add(status);
133             }
134 
135             Iterator<Long> itr = q.list().iterator();
136 
137             if (itr.hasNext()) {
138                 Long count = 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             closeSession(session);
152         }
153     }
154 
155     public int countByG_U_A_S(
156             long groupId, long userId, boolean anonymous, int status)
157         throws SystemException {
158 
159         Session session = null;
160 
161         try {
162             session = openSession();
163 
164             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A_S);
165 
166             if (status != StatusConstants.ANY) {
167                 sql = StringUtil.replace(sql, "[$STATUS$]", "AND (status = ?)");
168             }
169             else {
170                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
171             }
172 
173             SQLQuery q = session.createSQLQuery(sql);
174 
175             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
176 
177             QueryPos qPos = QueryPos.getInstance(q);
178 
179             qPos.add(groupId);
180             qPos.add(userId);
181             qPos.add(anonymous);
182 
183             if (status != StatusConstants.ANY) {
184                 qPos.add(status);
185             }
186 
187             Iterator<Long> itr = q.list().iterator();
188 
189             if (itr.hasNext()) {
190                 Long count = itr.next();
191 
192                 if (count != null) {
193                     return count.intValue();
194                 }
195             }
196 
197             return 0;
198         }
199         catch (Exception e) {
200             throw new SystemException(e);
201         }
202         finally {
203             closeSession(session);
204         }
205     }
206 
207     public List<MBMessage> findByNoAssets() throws SystemException {
208         Session session = null;
209 
210         try {
211             session = openSession();
212 
213             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
214 
215             SQLQuery q = session.createSQLQuery(sql);
216 
217             q.addEntity("MBMessage", MBMessageImpl.class);
218 
219             return q.list();
220         }
221         catch (Exception e) {
222             throw new SystemException(e);
223         }
224         finally {
225             closeSession(session);
226         }
227     }
228 
229     public List<Long> findByG_U_S(
230             long groupId, long userId, int status, int start, int end)
231         throws SystemException {
232 
233         Session session = null;
234 
235         try {
236             session = openSession();
237 
238             String sql = CustomSQLUtil.get(FIND_BY_G_U_S);
239 
240             if (status != StatusConstants.ANY) {
241                 sql = StringUtil.replace(sql, "[$STATUS$]", "AND (status = ?)");
242             }
243             else {
244                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
245             }
246 
247             SQLQuery q = session.createSQLQuery(sql);
248 
249             q.addScalar("threadId", Type.LONG);
250 
251             QueryPos qPos = QueryPos.getInstance(q);
252 
253             qPos.add(groupId);
254             qPos.add(userId);
255 
256             if (status != StatusConstants.ANY) {
257                 qPos.add(status);
258             }
259 
260             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
261         }
262         catch (Exception e) {
263             throw new SystemException(e);
264         }
265         finally {
266             closeSession(session);
267         }
268     }
269 
270     public List<Long> findByG_U_A_S(
271             long groupId, long userId, boolean anonymous, int status,
272             int start, int end)
273         throws SystemException {
274 
275         Session session = null;
276 
277         try {
278             session = openSession();
279 
280             String sql = CustomSQLUtil.get(FIND_BY_G_U_A_S);
281 
282             if (status != StatusConstants.ANY) {
283                 sql = StringUtil.replace(sql, "[$STATUS$]", "AND (status = ?)");
284             }
285             else {
286                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
287             }
288 
289             SQLQuery q = session.createSQLQuery(sql);
290 
291             q.addScalar("threadId", Type.LONG);
292 
293             QueryPos qPos = QueryPos.getInstance(q);
294 
295             qPos.add(groupId);
296             qPos.add(userId);
297             qPos.add(anonymous);
298 
299             if (status != StatusConstants.ANY) {
300                 qPos.add(status);
301             }
302 
303             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
304         }
305         catch (Exception e) {
306             throw new SystemException(e);
307         }
308         finally {
309             closeSession(session);
310         }
311     }
312 
313 }