1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.QueryPos;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.kernel.dao.orm.SQLQuery;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.dao.orm.Type;
28  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.messageboards.model.MBThread;
31  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class MBThreadFinderImpl
44      extends BasePersistenceImpl implements MBThreadFinder {
45  
46      public static String COUNT_BY_S_G_U =
47          MBThreadFinder.class.getName() + ".countByS_G_U";
48  
49      public static String FIND_BY_S_G_U =
50          MBThreadFinder.class.getName() + ".findByS_G_U";
51  
52      public int countByS_G_U(long groupId, long userId) throws SystemException {
53          Session session = null;
54  
55          try {
56              session = openSession();
57  
58              String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
59  
60              SQLQuery q = session.createSQLQuery(sql);
61  
62              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
63  
64              QueryPos qPos = QueryPos.getInstance(q);
65  
66              qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
67              qPos.add(groupId);
68              qPos.add(userId);
69  
70              Iterator<Long> itr = q.list().iterator();
71  
72              if (itr.hasNext()) {
73                  Long count = itr.next();
74  
75                  if (count != null) {
76                      return count.intValue();
77                  }
78              }
79  
80              return 0;
81          }
82          catch (Exception e) {
83              throw new SystemException(e);
84          }
85          finally {
86              closeSession(session);
87          }
88      }
89  
90      public List<MBThread> findByS_G_U(
91              long groupId, long userId, int start, int end)
92          throws SystemException {
93  
94          Session session = null;
95  
96          try {
97              session = openSession();
98  
99              String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
100 
101             SQLQuery q = session.createSQLQuery(sql);
102 
103             q.addEntity("MBThread", MBThreadImpl.class);
104 
105             QueryPos qPos = QueryPos.getInstance(q);
106 
107             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
108             qPos.add(groupId);
109             qPos.add(userId);
110 
111             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
112         }
113         catch (Exception e) {
114             throw new SystemException(e);
115         }
116         finally {
117             closeSession(session);
118         }
119     }
120 
121 }