1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class MBMessageFinderImpl
46      extends BasePersistenceImpl implements MBMessageFinder {
47  
48      /**
49       * @deprecated
50       */
51      public static String COUNT_BY_CATEGORY_IDS =
52          MBMessageFinder.class.getName() + ".countByCategoryIds";
53  
54      public static String COUNT_BY_G_U =
55          MBMessageFinder.class.getName() + ".countByG_U";
56  
57      public static String COUNT_BY_G_U_A =
58          MBMessageFinder.class.getName() + ".countByG_U_A";
59  
60      public static String FIND_BY_NO_ASSETS =
61          MBMessageFinder.class.getName() + ".findByNoAssets";
62  
63      public static String FIND_BY_G_U =
64          MBMessageFinder.class.getName() + ".findByG_U";
65  
66      public static String FIND_BY_G_U_A =
67          MBMessageFinder.class.getName() + ".findByG_U_A";
68  
69      /**
70       * @deprecated
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 countByG_U(long groupId, long userId) throws SystemException {
118         Session session = null;
119 
120         try {
121             session = openSession();
122 
123             String sql = CustomSQLUtil.get(COUNT_BY_G_U);
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             qPos.add(userId);
133 
134             Iterator<Long> itr = q.list().iterator();
135 
136             if (itr.hasNext()) {
137                 Long count = itr.next();
138 
139                 if (count != null) {
140                     return count.intValue();
141                 }
142             }
143 
144             return 0;
145         }
146         catch (Exception e) {
147             throw new SystemException(e);
148         }
149         finally {
150             closeSession(session);
151         }
152     }
153 
154     public int countByG_U_A(
155             long groupId, long userId, boolean anonymous)
156         throws SystemException {
157 
158         Session session = null;
159 
160         try {
161             session = openSession();
162 
163             String sql = CustomSQLUtil.get(COUNT_BY_G_U_A);
164 
165             SQLQuery q = session.createSQLQuery(sql);
166 
167             q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
168 
169             QueryPos qPos = QueryPos.getInstance(q);
170 
171             qPos.add(groupId);
172             qPos.add(userId);
173             qPos.add(anonymous);
174 
175             Iterator<Long> itr = q.list().iterator();
176 
177             if (itr.hasNext()) {
178                 Long count = itr.next();
179 
180                 if (count != null) {
181                     return count.intValue();
182                 }
183             }
184 
185             return 0;
186         }
187         catch (Exception e) {
188             throw new SystemException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193     }
194 
195     public List<MBMessage> findByNoAssets() throws SystemException {
196         Session session = null;
197 
198         try {
199             session = openSession();
200 
201             String sql = CustomSQLUtil.get(FIND_BY_NO_ASSETS);
202 
203             SQLQuery q = session.createSQLQuery(sql);
204 
205             q.addEntity("MBMessage", MBMessageImpl.class);
206 
207             return q.list();
208         }
209         catch (Exception e) {
210             throw new SystemException(e);
211         }
212         finally {
213             closeSession(session);
214         }
215     }
216 
217     public List<Long> findByG_U(
218             long groupId, long userId, int start, int end)
219         throws SystemException {
220 
221         Session session = null;
222 
223         try {
224             session = openSession();
225 
226             String sql = CustomSQLUtil.get(FIND_BY_G_U);
227 
228             SQLQuery q = session.createSQLQuery(sql);
229 
230             q.addScalar("threadId", Type.LONG);
231 
232             QueryPos qPos = QueryPos.getInstance(q);
233 
234             qPos.add(groupId);
235             qPos.add(userId);
236 
237             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
238         }
239         catch (Exception e) {
240             throw new SystemException(e);
241         }
242         finally {
243             closeSession(session);
244         }
245     }
246 
247     public List<Long> findByG_U_A(
248             long groupId, long userId, boolean anonymous, int start, int end)
249         throws SystemException {
250 
251         Session session = null;
252 
253         try {
254             session = openSession();
255 
256             String sql = CustomSQLUtil.get(FIND_BY_G_U_A);
257 
258             SQLQuery q = session.createSQLQuery(sql);
259 
260             q.addScalar("threadId", Type.LONG);
261 
262             QueryPos qPos = QueryPos.getInstance(q);
263 
264             qPos.add(groupId);
265             qPos.add(userId);
266             qPos.add(anonymous);
267 
268             return (List<Long>)QueryUtil.list(q, getDialect(), start, end);
269         }
270         catch (Exception e) {
271             throw new SystemException(e);
272         }
273         finally {
274             closeSession(session);
275         }
276     }
277 
278     /**
279      * @deprecated
280      */
281     protected String getCategoryIds(List<Long> categoryIds) {
282         StringBuilder sb = new StringBuilder();
283 
284         for (int i = 0; i < categoryIds.size(); i++) {
285             sb.append("categoryId = ? ");
286 
287             if ((i + 1) != categoryIds.size()) {
288                 sb.append("OR ");
289             }
290         }
291 
292         return sb.toString();
293     }
294 
295 }