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