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