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