1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.service.persistence.impl.BasePersistenceImpl;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class MBMessageFinderImpl
46      extends BasePersistenceImpl implements MBMessageFinder {
47  
48      public static String COUNT_BY_G_U =
49          MBMessageFinder.class.getName() + ".countByG_U";
50  
51      public static String COUNT_BY_G_U_A =
52          MBMessageFinder.class.getName() + ".countByG_U_A";
53  
54      public static String FIND_BY_NO_ASSETS =
55          MBMessageFinder.class.getName() + ".findByNoAssets";
56  
57      public static String FIND_BY_G_U =
58          MBMessageFinder.class.getName() + ".findByG_U";
59  
60      public static String FIND_BY_G_U_A =
61          MBMessageFinder.class.getName() + ".findByG_U_A";
62  
63      public int countByG_U(long groupId, long userId) throws SystemException {
64          Session session = null;
65  
66          try {
67              session = openSession();
68  
69              String sql = CustomSQLUtil.get(COUNT_BY_G_U);
70  
71              SQLQuery q = session.createSQLQuery(sql);
72  
73              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
74  
75              QueryPos qPos = QueryPos.getInstance(q);
76  
77              qPos.add(groupId);
78              qPos.add(userId);
79  
80              Iterator<Long> itr = q.list().iterator();
81  
82              if (itr.hasNext()) {
83                  Long count = itr.next();
84  
85                  if (count != null) {
86                      return count.intValue();
87                  }
88              }
89  
90              return 0;
91          }
92          catch (Exception e) {
93              throw new SystemException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public int countByG_U_A(
101             long groupId, long userId, boolean anonymous)
102         throws SystemException {
103 
104         Session session = null;
105 
106         try {
107             session = openSession();
108 
109             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
110 
111             SQLQuery q = session.createSQLQuery(sql);
112 
113             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
114 
115             QueryPos qPos = QueryPos.getInstance(q);
116 
117             qPos.add(groupId);
118             qPos.add(userId);
119             qPos.add(anonymous);
120 
121             Iterator<Long> itr = q.list().iterator();
122 
123             if (itr.hasNext()) {
124                 Long count = itr.next();
125 
126                 if (count != null) {
127                     return count.intValue();
128                 }
129             }
130 
131             return 0;
132         }
133         catch (Exception e) {
134             throw new SystemException(e);
135         }
136         finally {
137             closeSession(session);
138         }
139     }
140 
141     public List<MBMessage> findByNoAssets() throws SystemException {
142         Session session = null;
143 
144         try {
145             session = openSession();
146 
147             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
148 
149             SQLQuery q = session.createSQLQuery(sql);
150 
151             q.addEntity("MBMessage", MBMessageImpl.class);
152 
153             return q.list();
154         }
155         catch (Exception e) {
156             throw new SystemException(e);
157         }
158         finally {
159             closeSession(session);
160         }
161     }
162 
163     public List<Long> findByG_U(
164             long groupId, long userId, int start, int end)
165         throws SystemException {
166 
167         Session session = null;
168 
169         try {
170             session = openSession();
171 
172             String sql = CustomSQLUtil.get(FIND_BY_G_U);
173 
174             SQLQuery q = session.createSQLQuery(sql);
175 
176             q.addScalar("threadId", Type.LONG);
177 
178             QueryPos qPos = QueryPos.getInstance(q);
179 
180             qPos.add(groupId);
181             qPos.add(userId);
182 
183             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
184         }
185         catch (Exception e) {
186             throw new SystemException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List<Long> findByG_U_A(
194             long groupId, long userId, boolean anonymous, int start, int end)
195         throws SystemException {
196 
197         Session session = null;
198 
199         try {
200             session = openSession();
201 
202             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
203 
204             SQLQuery q = session.createSQLQuery(sql);
205 
206             q.addScalar("threadId", Type.LONG);
207 
208             QueryPos qPos = QueryPos.getInstance(q);
209 
210             qPos.add(groupId);
211             qPos.add(userId);
212             qPos.add(anonymous);
213 
214             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
215         }
216         catch (Exception e) {
217             throw new SystemException(e);
218         }
219         finally {
220             closeSession(session);
221         }
222     }
223 
224 }