1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryPos;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.dao.orm.SQLQuery;
20  import com.liferay.portal.kernel.dao.orm.Session;
21  import com.liferay.portal.kernel.dao.orm.Type;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.workflow.StatusConstants;
26  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portlet.messageboards.model.MBThread;
29  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
30  import com.liferay.util.dao.orm.CustomSQLUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  /**
36   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class MBThreadFinderImpl
41      extends BasePersistenceImpl<MBThread> implements MBThreadFinder {
42  
43      public static String COUNT_BY_S_G_U_S =
44          MBThreadFinder.class.getName() + ".countByS_G_U_S";
45  
46      public static String FIND_BY_S_G_U_S =
47          MBThreadFinder.class.getName() + ".findByS_G_U_S";
48  
49      public int countByS_G_U_S(long groupId, long userId, int status)
50          throws SystemException {
51  
52          Session session = null;
53  
54          try {
55              session = openSession();
56  
57              String sql = CustomSQLUtil.get(COUNT_BY_S_G_U_S);
58  
59              if (status != StatusConstants.ANY) {
60                  sql = StringUtil.replace(
61                      sql, "[$STATUS$]", "AND (MBThread.status = ?)");
62              }
63              else {
64                  sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
65              }
66  
67              SQLQuery q = session.createSQLQuery(sql);
68  
69              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
70  
71              QueryPos qPos = QueryPos.getInstance(q);
72  
73              qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
74              qPos.add(groupId);
75  
76              if (status != StatusConstants.ANY) {
77                  qPos.add(status);
78              }
79  
80              qPos.add(userId);
81  
82              Iterator<Long> itr = q.list().iterator();
83  
84              if (itr.hasNext()) {
85                  Long count = itr.next();
86  
87                  if (count != null) {
88                      return count.intValue();
89                  }
90              }
91  
92              return 0;
93          }
94          catch (Exception e) {
95              throw new SystemException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public List<MBThread> findByS_G_U_S(
103             long groupId, long userId, int status, int start, int end)
104         throws SystemException {
105 
106         Session session = null;
107 
108         try {
109             session = openSession();
110 
111             String sql = CustomSQLUtil.get(FIND_BY_S_G_U_S);
112 
113             if (status != StatusConstants.ANY) {
114                 sql = StringUtil.replace(
115                     sql, "[$STATUS$]", "AND (MBThread.status = ?)");
116             }
117             else {
118                 sql = StringUtil.replace(sql, "[$STATUS$]", StringPool.BLANK);
119             }
120 
121             SQLQuery q = session.createSQLQuery(sql);
122 
123             q.addEntity("MBThread", MBThreadImpl.class);
124 
125             QueryPos qPos = QueryPos.getInstance(q);
126 
127             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
128             qPos.add(groupId);
129 
130             if (status != StatusConstants.ANY) {
131                 qPos.add(status);
132             }
133 
134             qPos.add(userId);
135 
136             return (List<MBThread>)QueryUtil.list(q, getDialect(), start, end);
137         }
138         catch (Exception e) {
139             throw new SystemException(e);
140         }
141         finally {
142             closeSession(session);
143         }
144     }
145 
146 }