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