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.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   */
47  public class MBThreadFinderImpl
48      extends BasePersistenceImpl implements MBThreadFinder {
49  
50      /**
51       * @deprecated
52       */
53      public static String COUNT_BY_CATEGORY_IDS =
54          MBThreadFinder.class.getName() + ".countByCategoryIds";
55  
56      public static String COUNT_BY_S_G_U =
57          MBThreadFinder.class.getName() + ".countByS_G_U";
58  
59      public static String FIND_BY_S_G_U =
60          MBThreadFinder.class.getName() + ".findByS_G_U";
61  
62      /**
63       * @deprecated
64       */
65      public int countByCategoryIds(List<Long> categoryIds)
66          throws SystemException {
67  
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              String sql = CustomSQLUtil.get(COUNT_BY_CATEGORY_IDS);
74  
75              sql = StringUtil.replace(
76                  sql, "[$CATEGORY_ID$]", getCategoryIds(categoryIds));
77  
78              SQLQuery q = session.createSQLQuery(sql);
79  
80              q.addScalar(COUNT_COLUMN_NAME, Type.INTEGER);
81  
82              QueryPos qPos = QueryPos.getInstance(q);
83  
84              for (int i = 0; i < categoryIds.size(); i++) {
85                  Long categoryId = categoryIds.get(i);
86  
87                  qPos.add(categoryId);
88              }
89  
90              Iterator<Integer> itr = q.list().iterator();
91  
92              if (itr.hasNext()) {
93                  Integer count = itr.next();
94  
95                  if (count != null) {
96                      return count.intValue();
97                  }
98              }
99  
100             return 0;
101         }
102         catch (Exception e) {
103             throw new SystemException(e);
104         }
105         finally {
106             closeSession(session);
107         }
108     }
109 
110     public int countByS_G_U(long groupId, long userId) throws SystemException {
111         Session session = null;
112 
113         try {
114             session = openSession();
115 
116             String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
117 
118             SQLQuery q = session.createSQLQuery(sql);
119 
120             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
121 
122             QueryPos qPos = QueryPos.getInstance(q);
123 
124             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
125             qPos.add(groupId);
126             qPos.add(userId);
127 
128             Iterator<Long> itr = q.list().iterator();
129 
130             if (itr.hasNext()) {
131                 Long count = itr.next();
132 
133                 if (count != null) {
134                     return count.intValue();
135                 }
136             }
137 
138             return 0;
139         }
140         catch (Exception e) {
141             throw new SystemException(e);
142         }
143         finally {
144             closeSession(session);
145         }
146     }
147 
148     public List<MBThread> findByS_G_U(
149             long groupId, long userId, int start, int end)
150         throws SystemException {
151 
152         Session session = null;
153 
154         try {
155             session = openSession();
156 
157             String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
158 
159             SQLQuery q = session.createSQLQuery(sql);
160 
161             q.addEntity("MBThread", MBThreadImpl.class);
162 
163             QueryPos qPos = QueryPos.getInstance(q);
164 
165             qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
166             qPos.add(groupId);
167             qPos.add(userId);
168 
169             return (List<MBThread>)QueryUtil.list(
170                 q, getDialect(), start, end);
171         }
172         catch (Exception e) {
173             throw new SystemException(e);
174         }
175         finally {
176             closeSession(session);
177         }
178     }
179 
180     /**
181      * @deprecated
182      */
183     protected String getCategoryIds(List<Long> categoryIds) {
184         StringBuilder sb = new StringBuilder();
185 
186         for (int i = 0; i < categoryIds.size(); i++) {
187             sb.append("categoryId = ? ");
188 
189             if ((i + 1) != categoryIds.size()) {
190                 sb.append("OR ");
191             }
192         }
193 
194         return sb.toString();
195     }
196 
197 }