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.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  public class MBMessageFinderImpl
45      extends BasePersistenceImpl implements MBMessageFinder {
46  
47      public static String COUNT_BY_G_U =
48          MBMessageFinder.class.getName() + ".countByG_U";
49  
50      public static String COUNT_BY_G_U_A =
51          MBMessageFinder.class.getName() + ".countByG_U_A";
52  
53      public static String FIND_BY_NO_ASSETS =
54          MBMessageFinder.class.getName() + ".findByNoAssets";
55  
56      public static String FIND_BY_G_U =
57          MBMessageFinder.class.getName() + ".findByG_U";
58  
59      public static String FIND_BY_G_U_A =
60          MBMessageFinder.class.getName() + ".findByG_U_A";
61  
62      public int countByG_U(long groupId, long userId) throws SystemException {
63          Session session = null;
64  
65          try {
66              session = openSession();
67  
68              String sql = CustomSQLUtil.get(COUNT_BY_G_U);
69  
70              SQLQuery q = session.createSQLQuery(sql);
71  
72              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
73  
74              QueryPos qPos = QueryPos.getInstance(q);
75  
76              qPos.add(groupId);
77              qPos.add(userId);
78  
79              Iterator<Long> itr = q.list().iterator();
80  
81              if (itr.hasNext()) {
82                  Long count = itr.next();
83  
84                  if (count != null) {
85                      return count.intValue();
86                  }
87              }
88  
89              return 0;
90          }
91          catch (Exception e) {
92              throw new SystemException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public int countByG_U_A(
100             long groupId, long userId, boolean anonymous)
101         throws SystemException {
102 
103         Session session = null;
104 
105         try {
106             session = openSession();
107 
108             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
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(groupId);
117             qPos.add(userId);
118             qPos.add(anonymous);
119 
120             Iterator<Long> itr = q.list().iterator();
121 
122             if (itr.hasNext()) {
123                 Long count = itr.next();
124 
125                 if (count != null) {
126                     return count.intValue();
127                 }
128             }
129 
130             return 0;
131         }
132         catch (Exception e) {
133             throw new SystemException(e);
134         }
135         finally {
136             closeSession(session);
137         }
138     }
139 
140     public List<MBMessage> findByNoAssets() throws SystemException {
141         Session session = null;
142 
143         try {
144             session = openSession();
145 
146             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
147 
148             SQLQuery q = session.createSQLQuery(sql);
149 
150             q.addEntity("MBMessage", MBMessageImpl.class);
151 
152             return q.list();
153         }
154         catch (Exception e) {
155             throw new SystemException(e);
156         }
157         finally {
158             closeSession(session);
159         }
160     }
161 
162     public List<Long> findByG_U(
163             long groupId, long userId, int start, int end)
164         throws SystemException {
165 
166         Session session = null;
167 
168         try {
169             session = openSession();
170 
171             String sql = CustomSQLUtil.get(FIND_BY_G_U);
172 
173             SQLQuery q = session.createSQLQuery(sql);
174 
175             q.addScalar("threadId", Type.LONG);
176 
177             QueryPos qPos = QueryPos.getInstance(q);
178 
179             qPos.add(groupId);
180             qPos.add(userId);
181 
182             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
183         }
184         catch (Exception e) {
185             throw new SystemException(e);
186         }
187         finally {
188             closeSession(session);
189         }
190     }
191 
192     public List<Long> findByG_U_A(
193             long groupId, long userId, boolean anonymous, int start, int end)
194         throws SystemException {
195 
196         Session session = null;
197 
198         try {
199             session = openSession();
200 
201             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
202 
203             SQLQuery q = session.createSQLQuery(sql);
204 
205             q.addScalar("threadId", Type.LONG);
206 
207             QueryPos qPos = QueryPos.getInstance(q);
208 
209             qPos.add(groupId);
210             qPos.add(userId);
211             qPos.add(anonymous);
212 
213             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
214         }
215         catch (Exception e) {
216             throw new SystemException(e);
217         }
218         finally {
219             closeSession(session);
220         }
221     }
222 
223 }