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.kernel.util.StringUtil;
29  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
30  import com.liferay.portlet.messageboards.model.MBMessage;
31  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
32  import com.liferay.util.dao.orm.CustomSQLUtil;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class MBMessageFinderImpl
44      extends BasePersistenceImpl implements MBMessageFinder {
45  
46      /**
47       * @deprecated
48       */
49      public static String COUNT_BY_CATEGORY_IDS =
50          MBMessageFinder.class.getName() + ".countByCategoryIds";
51  
52      public static String COUNT_BY_G_U =
53          MBMessageFinder.class.getName() + ".countByG_U";
54  
55      public static String COUNT_BY_G_U_A =
56          MBMessageFinder.class.getName() + ".countByG_U_A";
57  
58      public static String FIND_BY_NO_ASSETS =
59          MBMessageFinder.class.getName() + ".findByNoAssets";
60  
61      public static String FIND_BY_G_U =
62          MBMessageFinder.class.getName() + ".findByG_U";
63  
64      public static String FIND_BY_G_U_A =
65          MBMessageFinder.class.getName() + ".findByG_U_A";
66  
67      /**
68       * @deprecated
69       */
70      public int countByCategoryIds(List<Long> categoryIds)
71          throws SystemException {
72  
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
79  
80              sql = StringUtil.replace(
81                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
82  
83              SQLQuery q = session.createSQLQuery(sql);
84  
85              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
86  
87              QueryPos qPos = QueryPos.getInstance(q);
88  
89              for (int i = 0; i < categoryIds.size(); i++) {
90                  Long categoryId = categoryIds.get(i);
91  
92                  qPos.add(categoryId);
93              }
94  
95              Iterator<Long> itr = q.list().iterator();
96  
97              if (itr.hasNext()) {
98                  Long count = itr.next();
99  
100                 if (count != null) {
101                     return count.intValue();
102                 }
103             }
104 
105             return 0;
106         }
107         catch (Exception e) {
108             throw new SystemException(e);
109         }
110         finally {
111             closeSession(session);
112         }
113     }
114 
115     public int countByG_U(long groupId, long userId) throws SystemException {
116         Session session = null;
117 
118         try {
119             session = openSession();
120 
121             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
122 
123             SQLQuery q = session.createSQLQuery(sql);
124 
125             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
126 
127             QueryPos qPos = QueryPos.getInstance(q);
128 
129             qPos.add(groupId);
130             qPos.add(userId);
131 
132             Iterator<Long> itr = q.list().iterator();
133 
134             if (itr.hasNext()) {
135                 Long count = itr.next();
136 
137                 if (count != null) {
138                     return count.intValue();
139                 }
140             }
141 
142             return 0;
143         }
144         catch (Exception e) {
145             throw new SystemException(e);
146         }
147         finally {
148             closeSession(session);
149         }
150     }
151 
152     public int countByG_U_A(
153             long groupId, long userId, boolean anonymous)
154         throws SystemException {
155 
156         Session session = null;
157 
158         try {
159             session = openSession();
160 
161             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
162 
163             SQLQuery q = session.createSQLQuery(sql);
164 
165             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
166 
167             QueryPos qPos = QueryPos.getInstance(q);
168 
169             qPos.add(groupId);
170             qPos.add(userId);
171             qPos.add(anonymous);
172 
173             Iterator<Long> itr = q.list().iterator();
174 
175             if (itr.hasNext()) {
176                 Long count = itr.next();
177 
178                 if (count != null) {
179                     return count.intValue();
180                 }
181             }
182 
183             return 0;
184         }
185         catch (Exception e) {
186             throw new SystemException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List<MBMessage> findByNoAssets() throws SystemException {
194         Session session = null;
195 
196         try {
197             session = openSession();
198 
199             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
200 
201             SQLQuery q = session.createSQLQuery(sql);
202 
203             q.addEntity("MBMessage", MBMessageImpl.class);
204 
205             return q.list();
206         }
207         catch (Exception e) {
208             throw new SystemException(e);
209         }
210         finally {
211             closeSession(session);
212         }
213     }
214 
215     public List<Long> findByG_U(
216             long groupId, long userId, int start, int end)
217         throws SystemException {
218 
219         Session session = null;
220 
221         try {
222             session = openSession();
223 
224             String sql = CustomSQLUtil.get(FIND_BY_G_U);
225 
226             SQLQuery q = session.createSQLQuery(sql);
227 
228             q.addScalar("threadId", Type.LONG);
229 
230             QueryPos qPos = QueryPos.getInstance(q);
231 
232             qPos.add(groupId);
233             qPos.add(userId);
234 
235             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
236         }
237         catch (Exception e) {
238             throw new SystemException(e);
239         }
240         finally {
241             closeSession(session);
242         }
243     }
244 
245     public List<Long> findByG_U_A(
246             long groupId, long userId, boolean anonymous, int start, int end)
247         throws SystemException {
248 
249         Session session = null;
250 
251         try {
252             session = openSession();
253 
254             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
255 
256             SQLQuery q = session.createSQLQuery(sql);
257 
258             q.addScalar("threadId", Type.LONG);
259 
260             QueryPos qPos = QueryPos.getInstance(q);
261 
262             qPos.add(groupId);
263             qPos.add(userId);
264             qPos.add(anonymous);
265 
266             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
267         }
268         catch (Exception e) {
269             throw new SystemException(e);
270         }
271         finally {
272             closeSession(session);
273         }
274     }
275 
276     /**
277      * @deprecated
278      */
279     protected String getCategoryIds(List<Long> categoryIds) {
280         StringBuilder sb = new StringBuilder();
281 
282         for (int i = 0; i < categoryIds.size(); i++) {
283             sb.append("categoryId = ? ");
284 
285             if ((i + 1) != categoryIds.size()) {
286                 sb.append("OR ");
287             }
288         }
289 
290         return sb.toString();
291     }
292 
293 }