1
22
23 package com.liferay.portlet.messageboards.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.util.StringMaker;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29 import com.liferay.portal.spring.hibernate.HibernateUtil;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portlet.messageboards.model.MBThread;
32 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
33 import com.liferay.util.dao.hibernate.QueryPos;
34 import com.liferay.util.dao.hibernate.QueryUtil;
35
36 import java.util.Iterator;
37 import java.util.List;
38
39 import org.hibernate.Hibernate;
40 import org.hibernate.SQLQuery;
41 import org.hibernate.Session;
42
43
49 public class MBThreadFinderImpl implements MBThreadFinder {
50
51 public static String COUNT_BY_CATEGORY_IDS =
52 MBThreadFinder.class.getName() + ".countByCategoryIds";
53
54 public static String COUNT_BY_GROUP_ID =
55 MBThreadFinder.class.getName() + ".countByGroupId";
56
57 public static String COUNT_BY_G_U =
58 MBThreadFinder.class.getName() + ".countByG_U";
59
60 public static String COUNT_BY_S_G_U =
61 MBThreadFinder.class.getName() + ".countByS_G_U";
62
63 public static String FIND_BY_GROUP_ID =
64 MBThreadFinder.class.getName() + ".findByGroupId";
65
66 public static String FIND_BY_G_U =
67 MBThreadFinder.class.getName() + ".findByG_U";
68
69 public static String FIND_BY_S_G_U =
70 MBThreadFinder.class.getName() + ".findByS_G_U";
71
72 public int countByCategoryIds(List<Long> categoryIds)
73 throws SystemException {
74
75 Session session = null;
76
77 try {
78 session = HibernateUtil.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(HibernateUtil.getCountColumnName(), Hibernate.INTEGER);
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<Integer> itr = q.list().iterator();
98
99 if (itr.hasNext()) {
100 Integer 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 HibernateUtil.closeSession(session);
114 }
115 }
116
117 public int countByGroupId(long groupId) throws SystemException {
118 Session session = null;
119
120 try {
121 session = HibernateUtil.openSession();
122
123 String sql = CustomSQLUtil.get(COUNT_BY_GROUP_ID);
124
125 SQLQuery q = session.createSQLQuery(sql);
126
127 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.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 HibernateUtil.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 = HibernateUtil.openSession();
158
159 String sql = CustomSQLUtil.get(COUNT_BY_G_U);
160
161 SQLQuery q = session.createSQLQuery(sql);
162
163 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.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 HibernateUtil.closeSession(session);
187 }
188 }
189
190 public int countByS_G_U(long groupId, long userId) throws SystemException {
191 Session session = null;
192
193 try {
194 session = HibernateUtil.openSession();
195
196 String sql = CustomSQLUtil.get(COUNT_BY_S_G_U);
197
198 SQLQuery q = session.createSQLQuery(sql);
199
200 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
201
202 QueryPos qPos = QueryPos.getInstance(q);
203
204 qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
205 qPos.add(groupId);
206 qPos.add(userId);
207
208 Iterator<Long> itr = q.list().iterator();
209
210 if (itr.hasNext()) {
211 Long count = itr.next();
212
213 if (count != null) {
214 return count.intValue();
215 }
216 }
217
218 return 0;
219 }
220 catch (Exception e) {
221 throw new SystemException(e);
222 }
223 finally {
224 HibernateUtil.closeSession(session);
225 }
226 }
227
228 public List<MBThread> findByGroupId(long groupId, int begin, int end)
229 throws SystemException {
230
231 Session session = null;
232
233 try {
234 session = HibernateUtil.openSession();
235
236 String sql = CustomSQLUtil.get(FIND_BY_GROUP_ID);
237
238 SQLQuery q = session.createSQLQuery(sql);
239
240 q.addEntity("MBThread", MBThreadImpl.class);
241
242 QueryPos qPos = QueryPos.getInstance(q);
243
244 qPos.add(groupId);
245
246 return (List<MBThread>)QueryUtil.list(
247 q, HibernateUtil.getDialect(), begin, end);
248 }
249 catch (Exception e) {
250 throw new SystemException(e);
251 }
252 finally {
253 HibernateUtil.closeSession(session);
254 }
255 }
256
257 public List<MBThread> findByG_U(
258 long groupId, long userId, int begin, int end)
259 throws SystemException {
260
261 Session session = null;
262
263 try {
264 session = HibernateUtil.openSession();
265
266 String sql = CustomSQLUtil.get(FIND_BY_G_U);
267
268 SQLQuery q = session.createSQLQuery(sql);
269
270 q.addEntity("MBThread", MBThreadImpl.class);
271
272 QueryPos qPos = QueryPos.getInstance(q);
273
274 qPos.add(groupId);
275 qPos.add(userId);
276
277 return (List<MBThread>)QueryUtil.list(
278 q, HibernateUtil.getDialect(), begin, end);
279 }
280 catch (Exception e) {
281 throw new SystemException(e);
282 }
283 finally {
284 HibernateUtil.closeSession(session);
285 }
286 }
287
288 public List<MBThread> findByS_G_U(
289 long groupId, long userId, int begin, int end)
290 throws SystemException {
291
292 Session session = null;
293
294 try {
295 session = HibernateUtil.openSession();
296
297 String sql = CustomSQLUtil.get(FIND_BY_S_G_U);
298
299 SQLQuery q = session.createSQLQuery(sql);
300
301 q.addEntity("MBThread", MBThreadImpl.class);
302
303 QueryPos qPos = QueryPos.getInstance(q);
304
305 qPos.add(PortalUtil.getClassNameId(MBThread.class.getName()));
306 qPos.add(groupId);
307 qPos.add(userId);
308
309 return (List<MBThread>)QueryUtil.list(
310 q, HibernateUtil.getDialect(), begin, end);
311 }
312 catch (Exception e) {
313 throw new SystemException(e);
314 }
315 finally {
316 HibernateUtil.closeSession(session);
317 }
318 }
319
320 protected String getCategoryIds(List<Long> categoryIds) {
321 StringMaker sm = new StringMaker();
322
323 for (int i = 0; i < categoryIds.size(); i++) {
324 sm.append("categoryId = ? ");
325
326 if ((i + 1) != categoryIds.size()) {
327 sm.append("OR ");
328 }
329 }
330
331 return sm.toString();
332 }
333
334 }