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.service.persistence.impl.BasePersistenceImpl;
25  import com.liferay.portlet.messageboards.model.MBMessage;
26  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
27  import com.liferay.util.dao.orm.CustomSQLUtil;
28  
29  import java.sql.Timestamp;
30  
31  import java.util.Date;
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class MBMessageFinderImpl
41      extends BasePersistenceImpl<MBMessage> implements MBMessageFinder {
42  
43      public static String COUNT_BY_G_U =
44          MBMessageFinder.class.getName() + ".countByG_U";
45  
46      public static String COUNT_BY_C_T =
47          MBMessageFinder.class.getName() + ".countByC_T";
48  
49      public static String COUNT_BY_G_U_A =
50          MBMessageFinder.class.getName() + ".countByG_U_A";
51  
52      public static String FIND_BY_NO_ASSETS =
53          MBMessageFinder.class.getName() + ".findByNoAssets";
54  
55      public static String FIND_BY_G_U =
56          MBMessageFinder.class.getName() + ".findByG_U";
57  
58      public static String FIND_BY_G_U_A =
59          MBMessageFinder.class.getName() + ".findByG_U_A";
60  
61      public int countByG_U(long groupId, long userId) throws SystemException {
62          Session session = null;
63  
64          try {
65              session = openSession();
66  
67              String sql = CustomSQLUtil.get(COUNT_BY_G_U);
68  
69              SQLQuery q = session.createSQLQuery(sql);
70  
71              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
72  
73              QueryPos qPos = QueryPos.getInstance(q);
74  
75              qPos.add(groupId);
76              qPos.add(userId);
77  
78              Iterator<Long> itr = q.list().iterator();
79  
80              if (itr.hasNext()) {
81                  Long count = itr.next();
82  
83                  if (count != null) {
84                      return count.intValue();
85                  }
86              }
87  
88              return 0;
89          }
90          catch (Exception e) {
91              throw new SystemException(e);
92          }
93          finally {
94              closeSession(session);
95          }
96      }
97  
98      public int countByC_T(Date createDate, long threadId)
99          throws SystemException {
100 
101         Timestamp createDate_TS = CalendarUtil.getTimestamp(createDate);
102 
103         Session session = null;
104 
105         try {
106             session = openSession();
107 
108             String sql = CustomSQLUtil.get(COUNT_BY_C_T);
109 
110             SQLQuery q = session.createSQLQuery(sql);
111 
112             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
113 
114             QueryPos qPos = QueryPos.getInstance(q);
115 
116             qPos.add(createDate_TS);
117             qPos.add(threadId);
118 
119             Iterator<Long> itr = q.list().iterator();
120 
121             if (itr.hasNext()) {
122                 Long count = itr.next();
123 
124                 if (count != null) {
125                     return count.intValue();
126                 }
127             }
128 
129             return 0;
130         }
131         catch (Exception e) {
132             throw new SystemException(e);
133         }
134         finally {
135             closeSession(session);
136         }
137     }
138 
139     public int countByG_U_A(
140             long groupId, long userId, boolean anonymous)
141         throws SystemException {
142 
143         Session session = null;
144 
145         try {
146             session = openSession();
147 
148             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
149 
150             SQLQuery q = session.createSQLQuery(sql);
151 
152             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
153 
154             QueryPos qPos = QueryPos.getInstance(q);
155 
156             qPos.add(groupId);
157             qPos.add(userId);
158             qPos.add(anonymous);
159 
160             Iterator<Long> itr = q.list().iterator();
161 
162             if (itr.hasNext()) {
163                 Long count = itr.next();
164 
165                 if (count != null) {
166                     return count.intValue();
167                 }
168             }
169 
170             return 0;
171         }
172         catch (Exception e) {
173             throw new SystemException(e);
174         }
175         finally {
176             closeSession(session);
177         }
178     }
179 
180     public List<MBMessage> findByNoAssets() throws SystemException {
181         Session session = null;
182 
183         try {
184             session = openSession();
185 
186             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
187 
188             SQLQuery q = session.createSQLQuery(sql);
189 
190             q.addEntity("MBMessage", MBMessageImpl.class);
191 
192             return q.list();
193         }
194         catch (Exception e) {
195             throw new SystemException(e);
196         }
197         finally {
198             closeSession(session);
199         }
200     }
201 
202     public List<Long> findByG_U(
203             long groupId, long userId, int start, int end)
204         throws SystemException {
205 
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             String sql = CustomSQLUtil.get(FIND_BY_G_U);
212 
213             SQLQuery q = session.createSQLQuery(sql);
214 
215             q.addScalar("threadId", Type.LONG);
216 
217             QueryPos qPos = QueryPos.getInstance(q);
218 
219             qPos.add(groupId);
220             qPos.add(userId);
221 
222             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
223         }
224         catch (Exception e) {
225             throw new SystemException(e);
226         }
227         finally {
228             closeSession(session);
229         }
230     }
231 
232     public List<Long> findByG_U_A(
233             long groupId, long userId, boolean anonymous, int start, int end)
234         throws SystemException {
235 
236         Session session = null;
237 
238         try {
239             session = openSession();
240 
241             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
242 
243             SQLQuery q = session.createSQLQuery(sql);
244 
245             q.addScalar("threadId", Type.LONG);
246 
247             QueryPos qPos = QueryPos.getInstance(q);
248 
249             qPos.add(groupId);
250             qPos.add(userId);
251             qPos.add(anonymous);
252 
253             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
254         }
255         catch (Exception e) {
256             throw new SystemException(e);
257         }
258         finally {
259             closeSession(session);
260         }
261     }
262 
263 }