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.StringUtil;
32  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.messageboards.model.MBThread;
35  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
36  import com.liferay.util.dao.orm.CustomSQLUtil;
37  
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="MBThreadFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class MBThreadFinderImpl
47      extends BasePersistenceImpl implements MBThreadFinder {
48  
49      /**
50       * @deprecated
51       */
52      public static String COUNT_BY_CATEGORY_IDS =
53          MBThreadFinder.class.getName() + ".countByCategoryIds";
54  
55      public static String COUNT_BY_S_G_U =
56          MBThreadFinder.class.getName() + ".countByS_G_U";
57  
58      public static String FIND_BY_S_G_U =
59          MBThreadFinder.class.getName() + ".findByS_G_U";
60  
61      /**
62       * @deprecated
63       */
64      public int countByCategoryIds(List<Long> categoryIds)
65          throws SystemException {
66  
67          Session session = null;
68  
69          try {
70              session = openSession();
71  
72              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
73  
74              sql = StringUtil.replace(
75                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
76  
77              SQLQuery q = session.createSQLQuery(sql);
78  
79              q.addScalar(COUNT_COLUMN_NAME, Type.INTEGER);
80  
81              QueryPos qPos = QueryPos.getInstance(q);
82  
83              for (int i = 0; i < categoryIds.size(); i++) {
84                  Long categoryId = categoryIds.get(i);
85  
86                  qPos.add(categoryId);
87              }
88  
89              Iterator<Integer> itr = q.list().iterator();
90  
91              if (itr.hasNext()) {
92                  Integer count = itr.next();
93  
94                  if (count != null) {
95                      return count.intValue();
96                  }
97              }
98  
99              return 0;
100         }
101         catch (Exception e) {
102             throw new SystemException(e);
103         }
104         finally {
105             closeSession(session);
106         }
107     }
108 
109     public int countByS_G_U(long groupId, long userId) throws SystemException {
110         Session session = null;
111 
112         try {
113             session = openSession();
114 
115             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
116 
117             SQLQuery q = session.createSQLQuery(sql);
118 
119             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
120 
121             QueryPos qPos = QueryPos.getInstance(q);
122 
123             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
124             qPos.add(groupId);
125             qPos.add(userId);
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 List<MBThread> findByS_G_U(
148             long groupId, long userId, int start, int end)
149         throws SystemException {
150 
151         Session session = null;
152 
153         try {
154             session = openSession();
155 
156             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
157 
158             SQLQuery q = session.createSQLQuery(sql);
159 
160             q.addEntity("MBThread", MBThreadImpl.class);
161 
162             QueryPos qPos = QueryPos.getInstance(q);
163 
164             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
165             qPos.add(groupId);
166             qPos.add(userId);
167 
168             return (List<MBThread>)QueryUtil.list(
169                 q, getDialect(), start, end);
170         }
171         catch (Exception e) {
172             throw new SystemException(e);
173         }
174         finally {
175             closeSession(session);
176         }
177     }
178 
179     /**
180      * @deprecated
181      */
182     protected String getCategoryIds(List<Long> categoryIds) {
183         StringBuilder sb = new StringBuilder();
184 
185         for (int i = 0; i < categoryIds.size(); i++) {
186             sb.append("categoryId = ? ");
187 
188             if ((i + 1) != categoryIds.size()) {
189                 sb.append("OR ");
190             }
191         }
192 
193         return sb.toString();
194     }
195 
196 }