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.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
31  import com.liferay.portlet.messageboards.NoSuchMessageException;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
34  import com.liferay.util.dao.orm.CustomSQLUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="MBMessageFinderImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class MBMessageFinderImpl
46      extends BasePersistenceImpl implements MBMessageFinder {
47  
48      public static String COUNT_BY_CATEGORY_IDS =
49          MBMessageFinder.class.getName() + ".countByCategoryIds";
50  
51      public static String COUNT_BY_GROUP_ID =
52          MBMessageFinder.class.getName() + ".countByGroupId";
53  
54      public static String COUNT_BY_G_U =
55          MBMessageFinder.class.getName() + ".countByG_U";
56  
57      public static String FIND_BY_GROUP_ID =
58          MBMessageFinder.class.getName() + ".findByGroupId";
59  
60      public static String FIND_BY_NO_ASSETS =
61          MBMessageFinder.class.getName() + ".findByNoAssets";
62  
63      public static String FIND_BY_UUID_G =
64          MBMessageFinder.class.getName() + ".findByUuid_G";
65  
66      public static String FIND_BY_G_U =
67          MBMessageFinder.class.getName() + ".findByG_U";
68  
69      public static String FIND_BY_C_C =
70          MBMessageFinder.class.getName() + ".findByC_C";
71  
72      public int countByCategoryIds(List<Long> categoryIds)
73          throws SystemException {
74  
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
81  
82              sql = StringUtil.replace(
83                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
84  
85              SQLQuery q = session.createSQLQuery(sql);
86  
87              q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
88  
89              QueryPos qPos = QueryPos.getInstance(q);
90  
91              for (int i = 0; i < categoryIds.size(); i++) {
92                  Long categoryId = categoryIds.get(i);
93  
94                  qPos.add(categoryId);
95              }
96  
97              Iterator<Long> itr = q.list().iterator();
98  
99              if (itr.hasNext()) {
100                 Long count = itr.next();
101 
102                 if (count != null) {
103                     return count.intValue();
104                 }
105             }
106 
107             return 0;
108         }
109         catch (Exception e) {
110             throw new SystemException(e);
111         }
112         finally {
113             closeSession(session);
114         }
115     }
116 
117     public int countByGroupId(long groupId) throws SystemException {
118         Session session = null;
119 
120         try {
121             session = openSession();
122 
123             String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
124 
125             SQLQuery q = session.createSQLQuery(sql);
126 
127             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
128 
129             QueryPos qPos = QueryPos.getInstance(q);
130 
131             qPos.add(groupId);
132 
133             Iterator<Long> itr = q.list().iterator();
134 
135             if (itr.hasNext()) {
136                 Long count = itr.next();
137 
138                 if (count != null) {
139                     return count.intValue();
140                 }
141             }
142 
143             return 0;
144         }
145         catch (Exception e) {
146             throw new SystemException(e);
147         }
148         finally {
149             closeSession(session);
150         }
151     }
152 
153     public int countByG_U(long groupId, long userId) throws SystemException {
154         Session session = null;
155 
156         try {
157             session = openSession();
158 
159             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
160 
161             SQLQuery q = session.createSQLQuery(sql);
162 
163             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
164 
165             QueryPos qPos = QueryPos.getInstance(q);
166 
167             qPos.add(groupId);
168             qPos.add(userId);
169 
170             Iterator<Long> itr = q.list().iterator();
171 
172             if (itr.hasNext()) {
173                 Long count = itr.next();
174 
175                 if (count != null) {
176                     return count.intValue();
177                 }
178             }
179 
180             return 0;
181         }
182         catch (Exception e) {
183             throw new SystemException(e);
184         }
185         finally {
186             closeSession(session);
187         }
188     }
189 
190     public List<MBMessage> findByGroupId(long groupId, int start, int end)
191         throws SystemException {
192 
193         return findByGroupId(groupId, start, end, null);
194     }
195 
196     public List<MBMessage> findByGroupId(
197             long groupId, int start, int end, OrderByComparator obc)
198         throws SystemException {
199 
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
206 
207             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
208 
209             SQLQuery q = session.createSQLQuery(sql);
210 
211             q.addEntity("MBMessage", MBMessageImpl.class);
212 
213             QueryPos qPos = QueryPos.getInstance(q);
214 
215             qPos.add(groupId);
216 
217             return (List<MBMessage>)QueryUtil.list(q, getDialect(), start, end);
218         }
219         catch (Exception e) {
220             throw new SystemException(e);
221         }
222         finally {
223             closeSession(session);
224         }
225     }
226 
227     public List<MBMessage> findByNoAssets() throws SystemException {
228         Session session = null;
229 
230         try {
231             session = openSession();
232 
233             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
234 
235             SQLQuery q = session.createSQLQuery(sql);
236 
237             q.addEntity("MBMessage", MBMessageImpl.class);
238 
239             return q.list();
240         }
241         catch (Exception e) {
242             throw new SystemException(e);
243         }
244         finally {
245             closeSession(session);
246         }
247     }
248 
249     public MBMessage findByUuid_G(String uuid, long groupId)
250         throws NoSuchMessageException, SystemException {
251 
252         Session session = null;
253 
254         try {
255             session = openSession();
256 
257             String sql = CustomSQLUtil.get(FIND_BY_UUID_G);
258 
259             SQLQuery q = session.createSQLQuery(sql);
260 
261             q.addEntity("MBMessage", MBMessageImpl.class);
262 
263             QueryPos qPos = QueryPos.getInstance(q);
264 
265             qPos.add(uuid);
266             qPos.add(groupId);
267 
268             List<MBMessage> list = q.list();
269 
270             if (list.size() == 0) {
271                 StringBuilder sb = new StringBuilder();
272 
273                 sb.append("No MBMessage exists with the key {uuid=");
274                 sb.append(uuid);
275                 sb.append(", groupId=");
276                 sb.append(groupId);
277                 sb.append("}");
278 
279                 throw new NoSuchMessageException(sb.toString());
280             }
281             else {
282                 return list.get(0);
283             }
284         }
285         catch (NoSuchMessageException nsme) {
286             throw nsme;
287         }
288         catch (Exception e) {
289             throw new SystemException(e);
290         }
291         finally {
292             closeSession(session);
293         }
294     }
295 
296     public List<MBMessage> findByG_U(
297             long groupId, long userId, int start, int end)
298         throws SystemException {
299 
300         return findByG_U(groupId, userId, start, end, null);
301     }
302 
303     public List<MBMessage> findByG_U(
304             long groupId, long userId, int start, int end,
305             OrderByComparator obc)
306         throws SystemException {
307 
308         Session session = null;
309 
310         try {
311             session = openSession();
312 
313             String sql = CustomSQLUtil.get(FIND_BY_G_U);
314 
315             sql = CustomSQLUtil.replaceOrderBy(sql, obc);
316 
317             SQLQuery q = session.createSQLQuery(sql);
318 
319             q.addEntity("MBMessage", MBMessageImpl.class);
320 
321             QueryPos qPos = QueryPos.getInstance(q);
322 
323             qPos.add(groupId);
324             qPos.add(userId);
325 
326             return (List<MBMessage>)QueryUtil.list(q, getDialect(), start, end);
327         }
328         catch (Exception e) {
329             throw new SystemException(e);
330         }
331         finally {
332             closeSession(session);
333         }
334     }
335 
336     public List<MBMessage> findByC_C(long classNameId, long classPK)
337         throws SystemException {
338 
339         Session session = null;
340 
341         try {
342             session = openSession();
343 
344             String sql = CustomSQLUtil.get(FIND_BY_C_C);
345 
346             SQLQuery q = session.createSQLQuery(sql);
347 
348             q.addEntity("MBMessage", MBMessageImpl.class);
349 
350             QueryPos qPos = QueryPos.getInstance(q);
351 
352             qPos.add(classNameId);
353             qPos.add(classPK);
354 
355             return q.list();
356         }
357         catch (Exception e) {
358             throw new SystemException(e);
359         }
360         finally {
361             closeSession(session);
362         }
363     }
364 
365     protected String getCategoryIds(List<Long> categoryIds) {
366         StringBuilder sb = new StringBuilder();
367 
368         for (int i = 0; i < categoryIds.size(); i++) {
369             sb.append("categoryId = ? ");
370 
371             if ((i + 1) != categoryIds.size()) {
372                 sb.append("OR ");
373             }
374         }
375 
376         return sb.toString();
377     }
378 
379 }